vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/WorkflowManagementListener.php line 123

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener;
  15. use Pimcore\Event\AdminEvents;
  16. use Pimcore\Event\AssetEvents;
  17. use Pimcore\Event\DataObjectEvents;
  18. use Pimcore\Event\DocumentEvents;
  19. use Pimcore\Event\Model\ElementEventInterface;
  20. use Pimcore\Model\Asset;
  21. use Pimcore\Model\DataObject;
  22. use Pimcore\Model\DataObject\ClassDefinition;
  23. use Pimcore\Model\DataObject\Concrete as ConcreteObject;
  24. use Pimcore\Model\Document;
  25. use Pimcore\Model\Element\AbstractElement;
  26. use Pimcore\Model\Element\Service;
  27. use Pimcore\Model\Element\WorkflowState;
  28. use Pimcore\Workflow\ActionsButtonService;
  29. use Pimcore\Workflow\Manager;
  30. use Pimcore\Workflow\Place;
  31. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  32. use Symfony\Component\EventDispatcher\GenericEvent;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Symfony\Component\Workflow\Registry;
  35. class WorkflowManagementListener implements EventSubscriberInterface
  36. {
  37.     /**
  38.      * @var bool
  39.      */
  40.     protected $enabled true;
  41.     /**
  42.      * @var Manager
  43.      */
  44.     private $workflowManager;
  45.     /**
  46.      * @var Registry
  47.      */
  48.     private $workflowRegistry;
  49.     /**
  50.      * @var Place\StatusInfo
  51.      */
  52.     private $placeStatusInfo;
  53.     /**
  54.      * @var RequestStack
  55.      */
  56.     private $requestStack;
  57.     /**
  58.      * @var ActionsButtonService
  59.      */
  60.     private $actionsButtonService;
  61.     public function __construct(Manager $workflowManagerRegistry $workflowRegistryPlace\StatusInfo $placeStatusInfoRequestStack $requestStackActionsButtonService $actionsButtonService)
  62.     {
  63.         $this->workflowManager $workflowManager;
  64.         $this->workflowRegistry $workflowRegistry;
  65.         $this->placeStatusInfo $placeStatusInfo;
  66.         $this->requestStack $requestStack;
  67.         $this->actionsButtonService $actionsButtonService;
  68.     }
  69.     /**
  70.      * @inheritDoc
  71.      */
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             DataObjectEvents::POST_DELETE => 'onElementPostDelete',
  76.             DocumentEvents::POST_DELETE => 'onElementPostDelete',
  77.             AssetEvents::POST_DELETE => 'onElementPostDelete',
  78.             AdminEvents::OBJECT_GET_PRE_SEND_DATA => 'onAdminElementGetPreSendData',
  79.             AdminEvents::ASSET_GET_PRE_SEND_DATA => 'onAdminElementGetPreSendData',
  80.             AdminEvents::DOCUMENT_GET_PRE_SEND_DATA => 'onAdminElementGetPreSendData',
  81.         ];
  82.     }
  83.     /**
  84.      * Cleanup status information on element delete
  85.      *
  86.      * @param ElementEventInterface $e
  87.      */
  88.     public function onElementPostDelete(ElementEventInterface $e)
  89.     {
  90.         /**
  91.          * @var Asset|Document|ConcreteObject $element
  92.          */
  93.         $element $e->getElement();
  94.         $list = new WorkflowState\Listing;
  95.         $list->setCondition('cid = ? and ctype = ?', [$element->getId(), Service::getType($element)]);
  96.         foreach ($list->load() as $item) {
  97.             $item->delete();
  98.         }
  99.     }
  100.     /**
  101.      * Fired before information is sent back to the admin UI about an element
  102.      *
  103.      * @param GenericEvent $e
  104.      *
  105.      * @throws \Exception
  106.      */
  107.     public function onAdminElementGetPreSendData(GenericEvent $e)
  108.     {
  109.         $element self::extractElementFromEvent($e);
  110.         $data $e->getArgument('data');
  111.         //create a new namespace for WorkflowManagement
  112.         //set some defaults
  113.         $data['workflowManagement'] = [
  114.             'hasWorkflowManagement' => false,
  115.         ];
  116.         foreach ($this->workflowManager->getAllWorkflows() as $workflowName) {
  117.             $workflow $this->workflowManager->getWorkflowIfExists($element$workflowName);
  118.             $workflowConfig $this->workflowManager->getWorkflowConfig($workflowName);
  119.             if (empty($workflow)) {
  120.                 continue;
  121.             }
  122.             $data['workflowManagement']['hasWorkflowManagement'] = true;
  123.             $data['workflowManagement']['workflows'] = $data['workflowManagement']['workflows'] ?? [];
  124.             $allowedTransitions $this->actionsButtonService->getAllowedTransitions($workflow$element);
  125.             $globalActions $this->actionsButtonService->getGlobalActions($workflow$element);
  126.             $data['workflowManagement']['workflows'][] = [
  127.                 'name' => $workflow->getName(),
  128.                 'label' => $workflowConfig->getLabel(),
  129.                 'allowedTransitions' => $allowedTransitions,
  130.                 'globalActions' => $globalActions
  131.             ];
  132.             $marking $workflow->getMarking($element);
  133.             if (!sizeof($marking->getPlaces())) {
  134.                 continue;
  135.             }
  136.             $permissionsRespected false;
  137.             foreach ($this->workflowManager->getOrderedPlaceConfigs($workflow$marking) as $placeConfig) {
  138.                 if (!$permissionsRespected && !empty($placeConfig->getPermissions($workflow$element))) {
  139.                     $data['userPermissions'] = array_merge((array)$data['userPermissions'], $placeConfig->getUserPermissions($workflow$element));
  140.                     if ($element instanceof ConcreteObject) {
  141.                         $workflowLayoutId $placeConfig->getObjectLayout($workflow$element);
  142.                         $hasSelectedCustomLayout $this->requestStack->getMasterRequest() && $this->requestStack->getMasterRequest()->query->has('layoutId') && $this->requestStack->getMasterRequest()->query->get('layoutId') !== '';
  143.                         if (!is_null($workflowLayoutId) && !$hasSelectedCustomLayout) {
  144.                             //load the new layout into the object container
  145.                             $validLayouts DataObject\Service::getValidLayouts($element);
  146.                             //check that the layout id is valid before trying to load
  147.                             if (!empty($validLayouts)) {
  148.                                 // check user permissions again
  149.                                 if ($validLayouts && $validLayouts[$workflowLayoutId]) {
  150.                                     $customLayout ClassDefinition\CustomLayout::getById($workflowLayoutId);
  151.                                     $customLayoutDefinition $customLayout->getLayoutDefinitions();
  152.                                     DataObject\Service::enrichLayoutDefinition($customLayoutDefinition$e->getArgument('object'));
  153.                                     $data['layout'] = $customLayoutDefinition;
  154.                                     $data['currentLayoutId'] = $workflowLayoutId;
  155.                                 }
  156.                             }
  157.                         }
  158.                     }
  159.                     $permissionsRespected true;
  160.                 }
  161.             }
  162.         }
  163.         if ($data['workflowManagement']['hasWorkflowManagement']) {
  164.             $data['workflowManagement']['statusInfo'] = $this->placeStatusInfo->getToolbarHtml($element);
  165.         }
  166.         $e->setArgument('data'$data);
  167.     }
  168.     /**
  169.      * @param DataObject\AbstractObject $object
  170.      * @param array $notes
  171.      *
  172.      * @return array
  173.      */
  174.     private function enrichNotes(DataObject\AbstractObject $object, array $notes)
  175.     {
  176.         if (!empty($notes['commentGetterFn'])) {
  177.             $commentGetterFn $notes['commentGetterFn'];
  178.             $notes['commentPrefill'] = $object->$commentGetterFn();
  179.         } elseif (!empty($notes)) {
  180.             $notes['commentPrefill'] = '';
  181.         }
  182.         return $notes;
  183.     }
  184.     /**
  185.      * @param GenericEvent $e
  186.      *
  187.      * @return AbstractElement
  188.      *
  189.      * @throws \Exception
  190.      */
  191.     private static function extractElementFromEvent(GenericEvent $e)
  192.     {
  193.         $element null;
  194.         foreach (['object''asset''document'] as $type) {
  195.             if ($e->hasArgument($type)) {
  196.                 $element $e->getArgument($type);
  197.             }
  198.         }
  199.         if (empty($element)) {
  200.             throw new \Exception('No element found in event');
  201.         }
  202.         return $element;
  203.     }
  204.     public function enable()
  205.     {
  206.         $this->enabled true;
  207.     }
  208.     public function disable()
  209.     {
  210.         $this->enabled false;
  211.     }
  212.     /**
  213.      * @return bool
  214.      */
  215.     public function isEnabled()
  216.     {
  217.         return $this->enabled;
  218.     }
  219. }