vendor/ibexa/workflow/src/lib/Event/Subscriber/UiComponentsSubscriber.php line 79

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\Workflow\Event\Subscriber;
  8. use Ibexa\AdminUi\Form\Data\Version\VersionRemoveData;
  9. use Ibexa\AdminUi\Form\Factory\FormFactory;
  10. use Ibexa\AdminUi\Tab\Event\TabEvents;
  11. use Ibexa\AdminUi\Tab\Event\TabViewRenderEvent;
  12. use Ibexa\Contracts\Core\Repository\ContentService;
  13. use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
  14. use Ibexa\Contracts\Workflow\Registry\WorkflowDefinitionMetadataRegistryInterface;
  15. use Ibexa\Contracts\Workflow\Registry\WorkflowRegistryInterface;
  16. use Ibexa\Contracts\Workflow\Service\WorkflowServiceInterface;
  17. use Ibexa\Workflow\Exception\NotFoundException;
  18. use Ibexa\Workflow\Value\WorkflowDefinitionMetadata;
  19. use Pagerfanta\Adapter\ArrayAdapter;
  20. use Pagerfanta\Pagerfanta;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Form\FormInterface;
  23. class UiComponentsSubscriber implements EventSubscriberInterface
  24. {
  25.     public const PERMISSION_LAYER_TRANSITION_BLOCKER_CODE '23373c81-5524-49a0-bf5a-b462748aa9e2';
  26.     /** @var \Symfony\Component\Workflow\Registry */
  27.     private $workflowRegistry;
  28.     /** @var \Ibexa\Contracts\Core\Repository\ContentService */
  29.     private $contentService;
  30.     /** @var \Ibexa\AdminUi\Form\Factory\FormFactory */
  31.     private $formFactory;
  32.     /** @var \Ibexa\Contracts\Workflow\Service\WorkflowServiceInterface */
  33.     private $workflowService;
  34.     /** @var \Ibexa\Workflow\Factory\Registry\WorkflowDefinitionMetadataRegistryFactory */
  35.     private $workflowMetadataRegistry;
  36.     /**
  37.      * @param \Ibexa\Contracts\Workflow\Registry\WorkflowRegistryInterface $workflowRegistry
  38.      * @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
  39.      * @param \Ibexa\AdminUi\Form\Factory\FormFactory $formFactory
  40.      * @param \Ibexa\Contracts\Workflow\Service\WorkflowServiceInterface $workflowService
  41.      * @param \Ibexa\Contracts\Workflow\Registry\WorkflowDefinitionMetadataRegistryInterface $workflowMetadataRegistry
  42.      */
  43.     public function __construct(
  44.         WorkflowRegistryInterface $workflowRegistry,
  45.         ContentService $contentService,
  46.         FormFactory $formFactory,
  47.         WorkflowServiceInterface $workflowService,
  48.         WorkflowDefinitionMetadataRegistryInterface $workflowMetadataRegistry
  49.     ) {
  50.         $this->workflowRegistry $workflowRegistry;
  51.         $this->contentService $contentService;
  52.         $this->formFactory $formFactory;
  53.         $this->workflowService $workflowService;
  54.         $this->workflowMetadataRegistry $workflowMetadataRegistry;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             TabEvents::TAB_RENDER => ['onTabRender'0],
  60.         ];
  61.     }
  62.     /**
  63.      * @param \Ibexa\AdminUi\Tab\Event\TabViewRenderEvent $event
  64.      *
  65.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  66.      */
  67.     public function onTabRender(TabViewRenderEvent $event): void
  68.     {
  69.         if ($event->getTabIdentifier() !== 'versions') {
  70.             return;
  71.         }
  72.         $parameters $event->getParameters();
  73.         if (!isset($parameters['draft_pager'])) {
  74.             return;
  75.         }
  76.         /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
  77.         $content $parameters['content'];
  78.         /** @var \Ibexa\AdminUi\UI\Dataset\VersionsDataset $dataset */
  79.         $dataset $parameters['versions_dataset'];
  80.         // Create workflows map
  81.         $draftVersions $dataset->getDraftVersions();
  82.         $workflowDefinitionMetadata = [];
  83.         $stages = [];
  84.         foreach ($draftVersions as $versionInfo) {
  85.             $contentVersion $this->contentService->loadContentByVersionInfo($versionInfo);
  86.             $workflows $this->workflowRegistry->getSupportedWorkflows($contentVersion);
  87.             if (!array_key_exists($versionInfo->versionNo$stages)) {
  88.                 $stages[$versionInfo->versionNo] = [];
  89.             }
  90.             foreach ($workflows as $workflow) {
  91.                 try {
  92.                     $workflowMetadata $this->workflowService->loadWorkflowMetadataForContent($contentVersion);
  93.                 } catch (NotFoundException $e) {
  94.                     continue;
  95.                 }
  96.                 $workflowName $workflow->getName();
  97.                 if (!array_key_exists($workflowName$stages)) {
  98.                     $stages[$versionInfo->versionNo][$workflowName] = [];
  99.                 }
  100.                 if (!array_key_exists($workflowName$workflowDefinitionMetadata)) {
  101.                     $workflowDefinitionMetadata[$workflowName] = $this->workflowMetadataRegistry->getWorkflowMetadata($workflowName);
  102.                 }
  103.                 $stages[$versionInfo->versionNo][$workflowName] = [];
  104.                 $places array_keys($workflow->getMarking($contentVersion)->getPlaces());
  105.                 foreach ($places as $place) {
  106.                     $stages[$versionInfo->versionNo][$workflowName][] = $this->createStagePropertiesArray(
  107.                         $workflowDefinitionMetadata[$workflowName],
  108.                         $place
  109.                     );
  110.                 }
  111.             }
  112.         }
  113.         // ignore changes to the table if there are no workflows
  114.         if (empty(array_filter($stages))) {
  115.             return;
  116.         }
  117.         // Assign parameters
  118.         $parameters['workflow_definition_metadata'] = $workflowDefinitionMetadata;
  119.         $parameters['workflow_stages_map'] = $stages;
  120.         $draftPagerfanta = new Pagerfanta(new ArrayAdapter($draftVersions));
  121.         $draftPaginationParams $parameters['draft_pagination_params'];
  122.         $draftPagerfanta->setMaxPerPage($draftPaginationParams['limit']);
  123.         $draftPagerfanta->setCurrentPage(min($draftPaginationParams['page'], $draftPagerfanta->getNbPages()));
  124.         $parameters['draft_pager'] = $draftPagerfanta;
  125.         // Inject to original tab
  126.         $event->setParameters($parameters);
  127.         $event->setTemplate('@ibexadesign/ibexa_workflow/admin/content_view/tab/versions/tab.html.twig');
  128.     }
  129.     /**
  130.      * @param \Ibexa\Workflow\Value\WorkflowDefinitionMetadata $workflowDefinitionMetadata
  131.      * @param string $stage
  132.      *
  133.      * @return array
  134.      */
  135.     private function createStagePropertiesArray(
  136.         WorkflowDefinitionMetadata $workflowDefinitionMetadata,
  137.         string $stage
  138.     ): array {
  139.         $stageMetadata $workflowDefinitionMetadata->getStagesMetadata()[$stage];
  140.         return [
  141.             'identifier' => $stage,
  142.             'label' => $stageMetadata->getLabel(),
  143.             'color' => $stageMetadata->getColor(),
  144.         ];
  145.     }
  146.     /**
  147.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
  148.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo[] $versions
  149.      *
  150.      * @return \Symfony\Component\Form\FormInterface
  151.      */
  152.     private function createVersionRemoveForm(ContentInfo $contentInfo, array $versions): FormInterface
  153.     {
  154.         $versionNumbers array_column($versions'versionNo');
  155.         $versionsData array_combine($versionNumbersarray_fill_keys($versionNumbersfalse));
  156.         return $this->formFactory->removeVersion(new VersionRemoveData($contentInfo$versionsData));
  157.     }
  158. }
  159. class_alias(UiComponentsSubscriber::class, 'EzSystems\EzPlatformWorkflow\Event\Subscriber\UiComponentsSubscriber');