vendor/ibexa/page-builder/src/lib/Event/Subscriber/TimelineEventsSubscriber.php line 70

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 DateTimeImmutable;
  9. use Ibexa\Contracts\AdminUi\Resolver\IconPathResolverInterface;
  10. use Ibexa\Contracts\FieldTypePage\FieldType\LandingPage\Model\BlockValue;
  11. use Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinition;
  12. use Ibexa\Contracts\PageBuilder\Timeline\EventInterface;
  13. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
  14. use Ibexa\PageBuilder\PageBuilder\Timeline\BasicEvent;
  15. use Ibexa\PageBuilder\PageBuilder\Timeline\Context\PageContextInterface;
  16. use Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent;
  17. use Ibexa\PageBuilder\PageBuilder\Timeline\Event\TimelineEvents;
  18. use JMS\TranslationBundle\Annotation\Desc;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Twig\Environment;
  22. class TimelineEventsSubscriber implements EventSubscriberInterface
  23. {
  24. /** @var \Symfony\Contracts\Translation\TranslatorInterface */
  25. private $translator;
  26. /** @var \Twig\Environment */
  27. private $templating;
  28. /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface */
  29. private $blockDefinitionFactory;
  30. /** @var \Ibexa\Contracts\AdminUi\Resolver\IconPathResolverInterface */
  31. private $iconPathResolver;
  32. public function __construct(
  33. TranslatorInterface $translator,
  34. Environment $templating,
  35. BlockDefinitionFactoryInterface $blockDefinitionFactory,
  36. IconPathResolverInterface $iconPathResolver
  37. ) {
  38. $this->translator = $translator;
  39. $this->templating = $templating;
  40. $this->blockDefinitionFactory = $blockDefinitionFactory;
  41. $this->iconPathResolver = $iconPathResolver;
  42. }
  43. /**
  44. * Returns an array of event names this subscriber wants to listen to.
  45. *
  46. * @return array The event names to listen to
  47. */
  48. public static function getSubscribedEvents(): array
  49. {
  50. return [
  51. TimelineEvents::COLLECT_EVENTS => [
  52. ['addBlockVisibilityEvents', 0],
  53. ['filterOutPastEvents', -128],
  54. ],
  55. ];
  56. }
  57. /**
  58. * @param \Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent $event
  59. */
  60. public function addBlockVisibilityEvents(ContentTimelineEvent $event): void
  61. {
  62. $context = $event->getContext();
  63. if (!$context instanceof PageContextInterface) {
  64. return;
  65. }
  66. $events = [$event->getTimelineEvents()];
  67. $page = $context->getPage();
  68. foreach ($page->getZones() as $zone) {
  69. foreach ($zone->getBlocks() as $block) {
  70. $events[] = $this->getBlockEvents($block);
  71. }
  72. }
  73. $event->setTimelineEvents(array_merge(...$events));
  74. }
  75. /**
  76. * @param \Ibexa\Contracts\FieldTypePage\FieldType\LandingPage\Model\BlockValue $blockValue
  77. *
  78. * @return \Ibexa\Contracts\PageBuilder\Timeline\EventInterface[]
  79. */
  80. private function getBlockEvents(BlockValue $blockValue): array
  81. {
  82. $blockEvents = [];
  83. $revealDate = $blockValue->getSince();
  84. $hideDate = $blockValue->getTill();
  85. $blockDefinition = $this->blockDefinitionFactory->getBlockDefinition($blockValue->getType());
  86. if (null !== $revealDate) {
  87. $blockEvents[] = $this->createRevealBlockEvent($blockValue, $blockDefinition, $revealDate);
  88. }
  89. if (null !== $hideDate) {
  90. $blockEvents[] = $this->createHideBlockEvent($blockValue, $blockDefinition, $hideDate);
  91. }
  92. return $blockEvents;
  93. }
  94. /**
  95. * @param \Ibexa\Contracts\FieldTypePage\FieldType\LandingPage\Model\BlockValue $blockValue
  96. * @param \Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinition $blockDefinition
  97. * @param \DateTimeInterface $date
  98. *
  99. * @return \Ibexa\Contracts\PageBuilder\Timeline\EventInterface
  100. */
  101. private function createRevealBlockEvent(
  102. BlockValue $blockValue,
  103. BlockDefinition $blockDefinition,
  104. \DateTimeInterface $date
  105. ): EventInterface {
  106. $eventName = /** @Desc("Reveal block") */
  107. $this->translator->trans('event.reveal_block.title', [], 'ibexa_page_builder_timeline_events');
  108. return new BasicEvent(
  109. $eventName,
  110. $this->templating->render(
  111. '@IbexaPageBuilder/page_builder/timeline/events/block_visibility_event_description.twig',
  112. [
  113. 'name' => $eventName,
  114. 'date' => $date,
  115. 'block_value' => $blockValue,
  116. 'block_definition' => $blockDefinition,
  117. ]
  118. ),
  119. $date,
  120. $this->iconPathResolver->resolve('block-visible')
  121. );
  122. }
  123. /**
  124. * @param \Ibexa\Contracts\FieldTypePage\FieldType\LandingPage\Model\BlockValue $blockValue
  125. * @param \Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinition $blockDefinition
  126. * @param \DateTimeInterface $date
  127. *
  128. * @return \Ibexa\Contracts\PageBuilder\Timeline\EventInterface
  129. */
  130. private function createHideBlockEvent(
  131. BlockValue $blockValue,
  132. BlockDefinition $blockDefinition,
  133. \DateTimeInterface $date
  134. ): EventInterface {
  135. $eventName = /** @Desc("Hide block") */
  136. $this->translator->trans('event.hide_block.title', [], 'ibexa_page_builder_timeline_events');
  137. return new BasicEvent(
  138. $eventName,
  139. $this->templating->render(
  140. '@IbexaPageBuilder/page_builder/timeline/events/block_visibility_event_description.twig',
  141. [
  142. 'name' => $eventName,
  143. 'date' => $date,
  144. 'block_value' => $blockValue,
  145. 'block_definition' => $blockDefinition,
  146. ]
  147. ),
  148. $date,
  149. $this->iconPathResolver->resolve('block-invisible')
  150. );
  151. }
  152. /**
  153. * @param \Ibexa\PageBuilder\PageBuilder\Timeline\Event\ContentTimelineEvent $event
  154. *
  155. * @throws \Exception
  156. */
  157. public function filterOutPastEvents(ContentTimelineEvent $event): void
  158. {
  159. $now = new DateTimeImmutable();
  160. $events = array_filter($event->getTimelineEvents(), static function (EventInterface $event) use ($now) {
  161. return $event->getDate() >= $now;
  162. });
  163. $event->setTimelineEvents($events);
  164. }
  165. }
  166. class_alias(TimelineEventsSubscriber::class, 'EzSystems\EzPlatformPageBuilder\Event\Subscriber\TimelineEventsSubscriber');