vendor/ibexa/page-builder/src/lib/Event/Subscriber/ScheduleBlockSubscriber.php line 171

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\PageBuilder\Event\Subscriber;
  8. use DateTime;
  9. use DateTimeImmutable;
  10. use Ibexa\Contracts\AdminUi\Resolver\IconPathResolverInterface;
  11. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  12. use Ibexa\Core\Helper\TranslationHelper;
  13. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
  14. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  15. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  16. use Ibexa\FieldTypePage\ScheduleBlock\Item\UnavailableLocation;
  17. use Ibexa\FieldTypePage\ScheduleBlock\Scheduler;
  18. use Ibexa\FieldTypePage\ScheduleBlock\ScheduleService;
  19. use Ibexa\FieldTypePage\ScheduleBlock\ScheduleSnapshotService;
  20. use Ibexa\PageBuilder\Block\Context\ContentCreateBlockContext;
  21. use Ibexa\PageBuilder\Block\Context\ContentEditBlockContext;
  22. use Ibexa\PageBuilder\Block\Context\ContentTranslateBlockContext;
  23. use Ibexa\PageBuilder\Block\Mapper\BlockConfigurationMapper;
  24. use Ibexa\PageBuilder\Block\ScheduleBlock\ConfigurationDataGenerator;
  25. use Ibexa\PageBuilder\Event\BlockConfigurationViewEvent;
  26. use Ibexa\PageBuilder\Event\BlockConfigurationViewEvents;
  27. use Ibexa\PageBuilder\Event\BlockPreviewEvents;
  28. use Ibexa\PageBuilder\Event\BlockPreviewResponseEvent;
  29. use Ibexa\PageBuilder\PageBuilder\Timeline\BasicEvent;
  30. use Ibexa\PageBuilder\PageBuilder\Timeline\Context\PageContextInterface;
  31. use Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent;
  32. use Ibexa\PageBuilder\PageBuilder\Timeline\Event\TimelineEvents;
  33. use JMS\Serializer\SerializerInterface;
  34. use JMS\TranslationBundle\Annotation\Desc;
  35. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  36. use Symfony\Component\HttpFoundation\RequestStack;
  37. use Symfony\Contracts\Translation\TranslatorInterface;
  38. use Twig\Environment;
  39. class ScheduleBlockSubscriber implements EventSubscriberInterface
  40. {
  41.     /** @var \Ibexa\PageBuilder\Block\Mapper\BlockConfigurationMapper */
  42.     private $blockConfigurationMapper;
  43.     /** @var \JMS\Serializer\SerializerInterface */
  44.     private $serializer;
  45.     /** @var \Ibexa\FieldTypePage\ScheduleBlock\Scheduler */
  46.     private $scheduler;
  47.     /** @var \Ibexa\FieldTypePage\ScheduleBlock\ScheduleService */
  48.     private $scheduleService;
  49.     /** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
  50.     private $contentTypeService;
  51.     /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface */
  52.     private $blockDefinitionFactory;
  53.     /** @var \Twig\Environment */
  54.     private $templating;
  55.     /** @var \Symfony\Contracts\Translation\TranslatorInterface */
  56.     private $translator;
  57.     /** @var \Symfony\Component\HttpFoundation\RequestStack */
  58.     private $requestStack;
  59.     /** @var \Ibexa\Core\Helper\TranslationHelper */
  60.     private $translationHelper;
  61.     /** @var \Ibexa\Contracts\AdminUi\Resolver\IconPathResolverInterface */
  62.     private $iconPathResolver;
  63.     /** @var \Ibexa\FieldTypePage\ScheduleBlock\ScheduleSnapshotService */
  64.     private $scheduleSnapshotService;
  65.     public function __construct(
  66.         BlockConfigurationMapper $blockConfigurationMapper,
  67.         SerializerInterface $serializer,
  68.         Scheduler $scheduler,
  69.         ScheduleService $scheduleService,
  70.         ContentTypeService $contentTypeService,
  71.         BlockDefinitionFactoryInterface $blockDefinitionFactory,
  72.         Environment $templating,
  73.         TranslatorInterface $translator,
  74.         RequestStack $requestStack,
  75.         TranslationHelper $translationHelper,
  76.         IconPathResolverInterface $iconPathResolver,
  77.         ScheduleSnapshotService $scheduleSnapshotService
  78.     ) {
  79.         $this->blockConfigurationMapper $blockConfigurationMapper;
  80.         $this->serializer $serializer;
  81.         $this->scheduler $scheduler;
  82.         $this->scheduleService $scheduleService;
  83.         $this->contentTypeService $contentTypeService;
  84.         $this->blockDefinitionFactory $blockDefinitionFactory;
  85.         $this->templating $templating;
  86.         $this->translator $translator;
  87.         $this->requestStack $requestStack;
  88.         $this->translationHelper $translationHelper;
  89.         $this->iconPathResolver $iconPathResolver;
  90.         $this->scheduleSnapshotService $scheduleSnapshotService;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public static function getSubscribedEvents(): array
  96.     {
  97.         return [
  98.             BlockConfigurationViewEvents::getBlockConfigurationViewEventName('schedule') => 'onBlockConfiguration',
  99.             BlockPreviewEvents::getBlockPreviewResponseEventName('schedule') => ['onBlockPreviewResponse'0],
  100.             TimelineEvents::COLLECT_EVENTS => 'onTimelineEventsCollect',
  101.             BlockRenderEvents::getBlockPreRenderEventName('schedule') => ['scheduleContent'100],
  102.         ];
  103.     }
  104.     /**
  105.      * @param \Ibexa\PageBuilder\Event\BlockConfigurationViewEvent $event
  106.      *
  107.      * @throws \Exception
  108.      */
  109.     public function onBlockConfiguration(BlockConfigurationViewEvent $event): void
  110.     {
  111.         $configurationDataGenerator = new ConfigurationDataGenerator();
  112.         $now = new DateTime();
  113.         $view $event->getBlockConfigurationView();
  114.         $blockConfiguration $event->getBlockConfiguration();
  115.         $blockValue $this->blockConfigurationMapper->mapToBlockValue($blockConfiguration);
  116.         $this->scheduleService->initializeScheduleData($blockValue);
  117.         $this->scheduleSnapshotService->restoreFromSnapshot($blockValue$now);
  118.         $this->scheduler->scheduleToDate($blockValue$now);
  119.         $data $configurationDataGenerator->generate($blockValue);
  120.         $view->addParameters([
  121.             'serialized_data' => $this->serializer->serialize($data'json'),
  122.         ]);
  123.     }
  124.     /**
  125.      * @param \Ibexa\PageBuilder\Event\BlockPreviewResponseEvent $event
  126.      *
  127.      * @throws \Exception
  128.      */
  129.     public function onBlockPreviewResponse(BlockPreviewResponseEvent $event): void
  130.     {
  131.         $pagePreviewParameters $event->getPagePreviewParameters();
  132.         $date = isset($pagePreviewParameters['referenceTimestamp'])
  133.             ? DateTimeImmutable::createFromFormat('U'$pagePreviewParameters['referenceTimestamp'])
  134.             : new DateTimeImmutable();
  135.         $blockValue $event->getBlockValue();
  136.         $this->scheduleService->initializeScheduleData($blockValue);
  137.         $this->scheduleSnapshotService->restoreFromSnapshot($blockValue$date);
  138.         $this->scheduler->scheduleToDate($blockValue$date);
  139.     }
  140.     /**
  141.      * @param \Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent $event
  142.      *
  143.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  144.      */
  145.     public function onTimelineEventsCollect(ContentTimelineEvent $event): void
  146.     {
  147.         $dataGenerator = new ConfigurationDataGenerator();
  148.         $timelineEvents $event->getTimelineEvents();
  149.         $context $event->getContext();
  150.         if (!$context instanceof PageContextInterface) {
  151.             return;
  152.         }
  153.         $page $context->getPage();
  154.         foreach ($page->getZones() as $zone) {
  155.             foreach ($zone->getBlocks() as $block) {
  156.                 if ($block->getType() !== 'schedule') {
  157.                     continue;
  158.                 }
  159.                 // @todo refactor to not use DataGenerator
  160.                 $data $dataGenerator->generate($block);
  161.                 /** @var \Ibexa\FieldTypePage\ScheduleBlock\Item\Item $item */
  162.                 foreach ($data['lists']['queue'] as $item) {
  163.                     $location $item->getLocation();
  164.                     if ($location instanceof UnavailableLocation) {
  165.                         continue;
  166.                     }
  167.                     $itemContentType $this->contentTypeService->loadContentType($location->contentInfo->contentTypeId);
  168.                     $eventName $this->translator->trans(
  169.                         /** @Desc("%contentTypeName% '%contentName%' added to block %blockName%") */
  170.                         'event.schedule_block.item_added.title',
  171.                         [
  172.                             '%contentTypeName%' => $itemContentType->getName(),
  173.                             '%blockName%' => $block->getName(),
  174.                             '%contentName%' => $this->translationHelper->getTranslatedContentName($location->getContent()),
  175.                         ],
  176.                         'ibexa_page_builder_timeline_events'
  177.                     );
  178.                     $blockDefinition $this->blockDefinitionFactory->getBlockDefinition($block->getType());
  179.                     $timelineEvents[] = new BasicEvent(
  180.                         $eventName,
  181.                         $this->templating->render(
  182.                             '@IbexaPageBuilder/page_builder/timeline/events/schedule_block/item_added_event_description.twig',
  183.                             [
  184.                                 'name' => $eventName,
  185.                                 'date' => $item->getAdditionDate(),
  186.                                 'block_value' => $block,
  187.                                 'block_definition' => $blockDefinition,
  188.                             ]
  189.                         ),
  190.                         $item->getAdditionDate(),
  191.                         $this->iconPathResolver->resolve('block-visible')
  192.                     );
  193.                 }
  194.             }
  195.         }
  196.         $event->setTimelineEvents($timelineEvents);
  197.     }
  198.     /**
  199.      * @param \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent $event
  200.      *
  201.      * @throws \Exception
  202.      */
  203.     public function scheduleContent(PreRenderEvent $event): void
  204.     {
  205.         $context $event->getBlockContext();
  206.         // schedule when in PageBuilder only
  207.         if (
  208.             !$context instanceof ContentEditBlockContext
  209.             && !$context instanceof ContentCreateBlockContext
  210.             && !$context instanceof ContentTranslateBlockContext
  211.         ) {
  212.             return;
  213.         }
  214.         $request $this->requestStack->getCurrentRequest();
  215.         if (null === $request) {
  216.             return;
  217.         }
  218.         $previewRequestParameters $request->get('parameters', []);
  219.         if (empty($previewRequestParameters['pagePreview']['referenceTimestamp'])) {
  220.             return;
  221.         }
  222.         $date DateTimeImmutable::createFromFormat('U'$previewRequestParameters['pagePreview']['referenceTimestamp']);
  223.         $blockValue $event->getBlockValue();
  224.         $this->scheduleService->initializeScheduleData($blockValue);
  225.         $this->scheduleSnapshotService->restoreFromSnapshot($blockValue$date);
  226.         $this->scheduler->scheduleToDate($blockValue$date);
  227.     }
  228. }
  229. class_alias(ScheduleBlockSubscriber::class, 'EzSystems\EzPlatformPageBuilder\Event\Subscriber\ScheduleBlockSubscriber');