Skip to content

Commit

Permalink
Live render PDF in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshyo committed Jan 26, 2022
1 parent 5d17c9c commit 7a60bf1
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 6 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true,
"symfony/thanks": true
}
},
"extra": {
"branch-alias": {
Expand Down
51 changes: 51 additions & 0 deletions src/Controller/Action/AdminGeneratePdfAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use Setono\SyliusGiftCardPlugin\Factory\GiftCardFactoryInterface;
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardConfigurationType;
use Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGeneratorInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class AdminGeneratePdfAction
{
private GiftCardFactoryInterface $giftCardFactory;

private RepositoryInterface $giftCardConfigurationRepository;

private GiftCardPdfGeneratorInterface $pdfGenerator;

private FormFactoryInterface $formFactory;

public function __construct(
GiftCardFactoryInterface $giftCardFactory,
RepositoryInterface $giftCardConfigurationRepository,
GiftCardPdfGeneratorInterface $pdfGenerator,
FormFactoryInterface $formFactory
) {
$this->giftCardFactory = $giftCardFactory;
$this->giftCardConfigurationRepository = $giftCardConfigurationRepository;
$this->pdfGenerator = $pdfGenerator;
$this->formFactory = $formFactory;
}

public function __invoke(Request $request, int $id): Response
{
$giftCard = $this->giftCardFactory->createDummy();
/** @var GiftCardConfigurationInterface|null $giftCardConfiguration */
$giftCardConfiguration = $this->giftCardConfigurationRepository->find($id);

$form = $this->formFactory->create(GiftCardConfigurationType::class, $giftCardConfiguration);
$form->handleRequest($request);

$this->pdfGenerator->generateAndSavePdf($giftCard, $giftCardConfiguration);

return new Response(null, Response::HTTP_NO_CONTENT);
}
}
44 changes: 44 additions & 0 deletions src/Controller/Action/AdminRenderPdfAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\GeneratorInterface;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;

final class AdminRenderPdfAction
{
private GeneratorInterface $snappy;

private string $publicDir;

public function __construct(
GeneratorInterface $snappy,
string $publicDir
) {
$this->snappy = $snappy;
$this->publicDir = $publicDir;
}

public function __invoke(Request $request, int $id): PdfResponse
{
$filePath = \sprintf(
'%s/gift_card_configuration_pdf_%d.pdf',
$this->publicDir,
$id
);
$response = new PdfResponse(\file_get_contents($filePath), 'gift_card.pdf');

$response->headers->add([
'Content-Disposition' => $response->headers->makeDisposition(HeaderUtils::DISPOSITION_INLINE, 'gift_card.pdf')
]);
$response->setPublic();
$response->setMaxAge(0);
$response->headers->addCacheControlDirective('must-revalidate', true);

return $response;
}
}
16 changes: 15 additions & 1 deletion src/Factory/GiftCardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Currency\Context\CurrencyContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Webmozart\Assert\Assert;

Expand All @@ -27,16 +28,20 @@ final class GiftCardFactory implements GiftCardFactoryInterface

private DateTimeProvider $dateTimeProvider;

private CurrencyContextInterface $currencyContext;

public function __construct(
FactoryInterface $decoratedFactory,
GiftCardCodeGeneratorInterface $giftCardCodeGenerator,
GiftCardChannelConfigurationProviderInterface $giftCardChannelConfigurationProvider,
DateTimeProvider $dateTimeProvider
DateTimeProvider $dateTimeProvider,
CurrencyContextInterface $currencyContext
) {
$this->decoratedFactory = $decoratedFactory;
$this->giftCardCodeGenerator = $giftCardCodeGenerator;
$this->giftCardChannelConfigurationProvider = $giftCardChannelConfigurationProvider;
$this->dateTimeProvider = $dateTimeProvider;
$this->currencyContext = $currencyContext;
}

public function createNew(): GiftCardInterface
Expand Down Expand Up @@ -114,4 +119,13 @@ public function createFromOrderItemUnitAndCart(

return $giftCard;
}

public function createDummy(): GiftCardInterface
{
$giftCard = $this->createNew();
$giftCard->setAmount(1500);
$giftCard->setCurrencyCode($this->currencyContext->getCurrencyCode());

return $giftCard;
}
}
2 changes: 2 additions & 0 deletions src/Factory/GiftCardFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public function createFromOrderItemUnitAndCart(
OrderItemUnitInterface $orderItemUnit,
OrderInterface $cart
): GiftCardInterface;

public function createDummy(): GiftCardInterface;
}
27 changes: 25 additions & 2 deletions src/Generator/GiftCardPdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class GiftCardPdfGenerator implements GiftCardPdfGeneratorInterface

private GeneratorInterface $snappy;

public function __construct(Environment $twig, GeneratorInterface $snappy)
{
private string $publicDir;

public function __construct(
Environment $twig,
GeneratorInterface $snappy,
string $publicDir
) {
$this->twig = $twig;
$this->snappy = $snappy;
$this->publicDir = $publicDir;
}

public function generatePdfResponse(
Expand All @@ -33,4 +39,21 @@ public function generatePdfResponse(

return new PdfResponse($this->snappy->getOutputFromHtml($html), 'gift_card.pdf');
}

public function generateAndSavePdf(
GiftCardInterface $giftCard,
GiftCardConfigurationInterface $giftCardChannelConfiguration
): void {
$html = $this->twig->render('@SetonoSyliusGiftCardPlugin/Shop/GiftCard/pdf.html.twig', [
'giftCard' => $giftCard,
'configuration' => $giftCardChannelConfiguration,
]);

$filePath = \sprintf(
'%s/gift_card_configuration_pdf_%d.pdf',
$this->publicDir,
$giftCardChannelConfiguration->getId()
);
$this->snappy->generateFromHtml($html, $filePath, [], true);
}
}
5 changes: 5 additions & 0 deletions src/Generator/GiftCardPdfGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function generatePdfResponse(
GiftCardInterface $giftCard,
GiftCardConfigurationInterface $giftCardChannelConfiguration
): PdfResponse;

public function generateAndSavePdf(
GiftCardInterface $giftCard,
GiftCardConfigurationInterface $giftCardChannelConfiguration
): void;
}
6 changes: 6 additions & 0 deletions src/Resources/config/app/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ liip_imagine:
setono_sylius_gift_card_background:
filters:
thumbnail: { size: [1200], mode: inset }

sylius_ui:
events:
setono_sylius_gift_card.admin.gift_card_configuration.update.javascripts:
blocks:
live_pdf_rendering_js: '@SetonoSyliusGiftCardPlugin/Admin/GiftCardConfiguration/Update/_javascripts.html.twig'
4 changes: 4 additions & 0 deletions src/Resources/config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ setono_sylius_gift_card_admin:
setono_sylius_gift_card_admin_ajax:
resource: "@SetonoSyliusGiftCardPlugin/Resources/config/routes/admin_ajax.yaml"
prefix: /admin/ajax

setono_sylius_gift_card_admin_partial:
resource: "@SetonoSyliusGiftCardPlugin/Resources/config/routes/admin_partial.yaml"
prefix: /admin/partial

setono_sylius_gift_card_admin_api:
resource: "@SetonoSyliusGiftCardPlugin/Resources/config/routes/admin_api.yaml"
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/routes/admin_ajax.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ setono_sylius_gift_card_admin_ajax_customer_by_email:
repository:
method: findBy
arguments: [email: $email]

setono_sylius_gift_card_admin_ajax_generate_pdf:
path: /gift-card-configurations/{id}/generate-pdf
methods: [ GET, POST, PUT ]
controller: setono_sylius_gift_card.controller.action.generate_pdf
requirements:
id: \d+
6 changes: 6 additions & 0 deletions src/Resources/config/routes/admin_partial.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
setono_sylius_gift_card_admin_partial_render_pdf:
path: /gift-card-configurations/{id}/render-pdf
controller: setono_sylius_gift_card.controller.action.admin_render
methods: [GET, POST, PUT]
requirements:
id: \d+
14 changes: 14 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@
<argument type="service" id="router"/>
</service>

<service id="setono_sylius_gift_card.controller.action.admin_render"
class="Setono\SyliusGiftCardPlugin\Controller\Action\AdminRenderPdfAction">
<argument type="service" id="knp_snappy.pdf"/>
<argument>%kernel.project_dir%/public</argument>
</service>

<service id="setono_sylius_gift_card.controller.action.generate_pdf"
class="Setono\SyliusGiftCardPlugin\Controller\Action\AdminGeneratePdfAction">
<argument type="service" id="setono_sylius_gift_card.factory.gift_card"/>
<argument type="service" id="setono_sylius_gift_card.repository.gift_card_configuration"/>
<argument type="service" id="setono_sylius_gift_card.generator.gift_card_pdf"/>
<argument type="service" id="form.factory"/>
</service>

</services>

</container>
1 change: 1 addition & 0 deletions src/Resources/config/services/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument type="service" id="setono_sylius_gift_card.generator.gift_card_code"/>
<argument type="service" id="setono_sylius_gift_card.provider.gift_card_configuration"/>
<argument type="service" id="sylius.calendar"/>
<argument type="service" id="sylius.context.currency"/>
</service>

<service id="setono_sylius_gift_card.custom_factory.gift_card_configuration"
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/misc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class="Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGenerator">
<argument type="service" id="twig"/>
<argument type="service" id="knp_snappy.pdf"/>
<argument>%kernel.project_dir%/public</argument>
</service>
<service id="Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGeneratorInterface"
alias="setono_sylius_gift_card.generator.gift_card_pdf" />
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/public/setono-sylius-gift-card-live-pdf-rendering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function ($) {
'use strict';

$.fn.extend({
applyPdfChanges: function () {
const $element = $(this);
const url = $element.data('url');

$element.on('click', function (event) {
event.preventDefault();

$.ajax(url, {
method: 'POST',
data: $('form[name="setono_sylius_gift_card_gift_card_configuration"]').serialize(),
success() {
const src = document.getElementById('js-ssgc-live-render-box').src;
$('.js-ssgc-live-render-container').html(`<embed id="js-ssgc-live-render-box" src="${src}" style="width: 100%;height: 100%;"/>`);
},
});
});
},
});
})(jQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script src="{{ asset('bundles/setonosyliusgiftcardplugin/setono-sylius-gift-card-live-pdf-rendering.js') }}"></script>
<script>
$().ready(() => {
$('.js-ssgc-apply-pdf-changes').applyPdfChanges();
});
</script>
13 changes: 11 additions & 2 deletions src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@
</ul>
</div>
{{ form_row(form.pdfRenderingCss) }}

<button class="ui button primary labeled icon js-ssgc-apply-pdf-changes"
data-url="{{ path('setono_sylius_gift_card_admin_ajax_generate_pdf', {'id': resource.id}) }}">
<i class="icon check"></i>
{{ 'setono_sylius_gift_card.ui.apply'|trans }}
</button>
</div>
<div class="column">
{# TODO: live rendering #}
<div class="column js-ssgc-live-render-container" style="height: 900px;">
<embed id="js-ssgc-live-render-box"
src="{{ url('setono_sylius_gift_card_admin_partial_render_pdf', {id: resource.id}) }}"
style="width: 100%;height: 100%;"
/>
</div>
</div>
</div>

0 comments on commit 7a60bf1

Please sign in to comment.