Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Printable] further improvements to new printable feature #2724

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ services:
CoreShop\Component\Pimcore\Print\PrintablePdfRendererInterface: '@CoreShop\Component\Pimcore\Print\PimcorePrintablePdfRenderer'
CoreShop\Component\Pimcore\Print\PimcorePrintablePdfRenderer:
arguments:
- '@fragment.renderer.inline'
- '@fragment.renderer.inline'

CoreShop\Component\Pimcore\Print\PersistedPrintablePdfRenderer:
decorates: 'CoreShop\Component\Pimcore\Print\PrintablePdfRendererInterface'
arguments:
- '@CoreShop\Component\Pimcore\Print\PersistedPrintablePdfRenderer.inner'
- '%kernel.debug%'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Component\Pimcore\Print;

use Pimcore\Model\Asset;

interface PersistedPrintableInterface
{
public function getRenderedPrintable(array $params = []): ?Asset;

public function getPersistedName(array $params = []): string;

public function getPersistedPath(array $params = []): string;

public function setRenderedPrintable(Asset $asset, array $params = []): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Component\Pimcore\Print;

use Pimcore\Model\Asset;

class PersistedPrintablePdfRenderer implements PrintablePdfRendererInterface
{
public function __construct(
private PrintablePdfRendererInterface $inner,
private bool $debug,
) {
}

public function renderPrintable(PrintableInterface $printable, array $params = []): string
{
if (!$printable instanceof PersistedPrintableInterface) {
return $this->inner->renderPrintable($printable, $params);
}

/**
* @var PrintableInterface&PersistedPrintableInterface $printable
*/

// if in debug, do not store document for implementation purposes.
if ($this->debug) {
return $this->inner->renderPrintable($printable, $params);
}

$renderedAsset = $printable->getRenderedPrintable($params);

if ($renderedAsset instanceof Asset) {
// check if asset is outdated.
if (method_exists($printable, 'getModificationDate')) {
if ($renderedAsset->getCreationDate() >= $printable->getModificationDate()) {
$data = $renderedAsset->getData();

if ($data !== false) {
return $data;
}
}
}

$data = $renderedAsset->getData();

if ($data !== false) {
return $data;
}
}

$pdfContent = $this->inner->renderPrintable($printable, $params);

$assetPath = $printable->getPersistedPath();
$assetName = sprintf('%s.pdf', $printable->getPersistedName($params));

$document = Asset\Document::getByPath($assetPath.'/'.$assetName);

if ($document instanceof Asset\Document) {
$document->delete();
}

$document = new Asset\Document();
$document->setFilename($assetName);
$document->setParent(Asset\Service::createFolderByPath($assetPath));
$document->setData($pdfContent);
$document->save();

$printable->setRenderedPrintable($document);

if (method_exists($printable, 'save')) {
$printable->save();
}

return $pdfContent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public function renderPrintable(PrintableInterface $printable, array $params = [
$request->setLocale($params['locale']);
}

$referenceFooter = new ControllerReference($printable->getPrintBodyController($params), $params);
$params['printable'] = $printable;

$referenceContent = new ControllerReference($printable->getPrintBodyController($params), $params);
$referenceHeader = new ControllerReference($printable->getPrintHeaderController($params), $params);
$referenceContent = new ControllerReference($printable->getPrintFooterController($params), $params);
$referenceFooter = new ControllerReference($printable->getPrintFooterController($params), $params);

$contentHeader = $this->fragmentRenderer->render($referenceHeader, $request)->getContent();
$contentFooter = $this->fragmentRenderer->render($referenceFooter, $request)->getContent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/

namespace CoreShop\Component\Pimcore\Print;

use Pimcore\Model\Asset;
use Pimcore\Model\Element\ElementInterface;
use Webmozart\Assert\Assert;

trait PropertyPersistedPrintableTrait
{
public function getRenderedPrintable(array $params = []): ?Asset
{
/**
* @var ElementInterface $this
*/
Assert::isInstanceOf($this, ElementInterface::class);

return $this->getProperty('rendered_asset');
}

public function setRenderedPrintable(Asset $asset, array $params = []): void
{
/**
* @var ElementInterface $this
*/
Assert::isInstanceOf($this, ElementInterface::class);

$this->setProperty('rendered_asset', 'asset', $asset);
}
}
Loading