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

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. public static function getSubscribedEvents(): array
  93. {
  94. return [
  95. BlockConfigurationViewEvents::getBlockConfigurationViewEventName('schedule') => 'onBlockConfiguration',
  96. BlockPreviewEvents::getBlockPreviewResponseEventName('schedule') => ['onBlockPreviewResponse', 0],
  97. TimelineEvents::COLLECT_EVENTS => 'onTimelineEventsCollect',
  98. BlockRenderEvents::getBlockPreRenderEventName('schedule') => ['scheduleContent', 100],
  99. ];
  100. }
  101. /**
  102. * @param \Ibexa\PageBuilder\Event\BlockConfigurationViewEvent $event
  103. *
  104. * @throws \Exception
  105. */
  106. public function onBlockConfiguration(BlockConfigurationViewEvent $event): void
  107. {
  108. $configurationDataGenerator = new ConfigurationDataGenerator();
  109. $now = new DateTime();
  110. $view = $event->getBlockConfigurationView();
  111. $blockConfiguration = $event->getBlockConfiguration();
  112. $blockValue = $this->blockConfigurationMapper->mapToBlockValue($blockConfiguration);
  113. $this->scheduleService->initializeScheduleData($blockValue);
  114. $this->scheduleSnapshotService->restoreFromSnapshot($blockValue, $now);
  115. $this->scheduler->scheduleToDate($blockValue, $now);
  116. $data = $configurationDataGenerator->generate($blockValue);
  117. $view->addParameters([
  118. 'serialized_data' => $this->serializer->serialize($data, 'json'),
  119. ]);
  120. }
  121. /**
  122. * @param \Ibexa\PageBuilder\Event\BlockPreviewResponseEvent $event
  123. *
  124. * @throws \Exception
  125. */
  126. public function onBlockPreviewResponse(BlockPreviewResponseEvent $event): void
  127. {
  128. $pagePreviewParameters = $event->getPagePreviewParameters();
  129. $date = isset($pagePreviewParameters['referenceTimestamp'])
  130. ? DateTimeImmutable::createFromFormat('U', $pagePreviewParameters['referenceTimestamp'])
  131. : new DateTimeImmutable();
  132. $blockValue = $event->getBlockValue();
  133. $this->scheduleService->initializeScheduleData($blockValue);
  134. $this->scheduleSnapshotService->restoreFromSnapshot($blockValue, $date);
  135. $this->scheduler->scheduleToDate($blockValue, $date);
  136. }
  137. /**
  138. * @param \Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent $event
  139. *
  140. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  141. */
  142. public function onTimelineEventsCollect(ContentTimelineEvent $event): void
  143. {
  144. $dataGenerator = new ConfigurationDataGenerator();
  145. $timelineEvents = $event->getTimelineEvents();
  146. $context = $event->getContext();
  147. if (!$context instanceof PageContextInterface) {
  148. return;
  149. }
  150. $page = $context->getPage();
  151. foreach ($page->getZones() as $zone) {
  152. foreach ($zone->getBlocks() as $block) {
  153. if ($block->getType() !== 'schedule') {
  154. continue;
  155. }
  156. // @todo refactor to not use DataGenerator
  157. $data = $dataGenerator->generate($block);
  158. /** @var \Ibexa\FieldTypePage\ScheduleBlock\Item\Item $item */
  159. foreach ($data['lists']['queue'] as $item) {
  160. $location = $item->getLocation();
  161. if ($location instanceof UnavailableLocation) {
  162. continue;
  163. }
  164. $itemContentType = $this->contentTypeService->loadContentType($location->contentInfo->contentTypeId);
  165. $eventName = $this->translator->trans(
  166. /** @Desc("%contentTypeName% '%contentName%' added to block %blockName%") */
  167. 'event.schedule_block.item_added.title',
  168. [
  169. '%contentTypeName%' => $itemContentType->getName(),
  170. '%blockName%' => $block->getName(),
  171. '%contentName%' => $this->translationHelper->getTranslatedContentName($location->getContent()),
  172. ],
  173. 'ibexa_page_builder_timeline_events'
  174. );
  175. $blockDefinition = $this->blockDefinitionFactory->getBlockDefinition($block->getType());
  176. $timelineEvents[] = new BasicEvent(
  177. $eventName,
  178. $this->templating->render(
  179. '@IbexaPageBuilder/page_builder/timeline/events/schedule_block/item_added_event_description.twig',
  180. [
  181. 'name' => $eventName,
  182. 'date' => $item->getAdditionDate(),
  183. 'block_value' => $block,
  184. 'block_definition' => $blockDefinition,
  185. ]
  186. ),
  187. $item->getAdditionDate(),
  188. $this->iconPathResolver->resolve('block-visible')
  189. );
  190. }
  191. }
  192. }
  193. $event->setTimelineEvents($timelineEvents);
  194. }
  195. /**
  196. * @param \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent $event
  197. *
  198. * @throws \Exception
  199. */
  200. public function scheduleContent(PreRenderEvent $event): void
  201. {
  202. $context = $event->getBlockContext();
  203. // schedule when in PageBuilder only
  204. if (
  205. !$context instanceof ContentEditBlockContext
  206. && !$context instanceof ContentCreateBlockContext
  207. && !$context instanceof ContentTranslateBlockContext
  208. ) {
  209. return;
  210. }
  211. $request = $this->requestStack->getCurrentRequest();
  212. if (null === $request) {
  213. return;
  214. }
  215. $previewRequestParameters = $request->get('parameters', []);
  216. if (empty($previewRequestParameters['pagePreview']['referenceTimestamp'])) {
  217. return;
  218. }
  219. $date = DateTimeImmutable::createFromFormat('U', $previewRequestParameters['pagePreview']['referenceTimestamp']);
  220. $blockValue = $event->getBlockValue();
  221. $this->scheduleService->initializeScheduleData($blockValue);
  222. $this->scheduleSnapshotService->restoreFromSnapshot($blockValue, $date);
  223. $this->scheduler->scheduleToDate($blockValue, $date);
  224. }
  225. }
  226. class_alias(ScheduleBlockSubscriber::class, 'EzSystems\EzPlatformPageBuilder\Event\Subscriber\ScheduleBlockSubscriber');