vendor/pimcore/pimcore/lib/Workflow/EventSubscriber/NotesSubscriber.php line 99

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\Workflow\EventSubscriber;
  15. use Pimcore\Event\Workflow\GlobalActionEvent;
  16. use Pimcore\Event\WorkflowEvents;
  17. use Pimcore\Model\Element\AbstractElement;
  18. use Pimcore\Model\Element\ValidationException;
  19. use Pimcore\Workflow;
  20. use Pimcore\Workflow\Transition;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Translation\TranslatorInterface;
  23. use Symfony\Component\Workflow\Event\Event;
  24. class NotesSubscriber implements EventSubscriberInterface
  25. {
  26.     const ADDITIONAL_DATA_NOTES_COMMENT 'notes';
  27.     const ADDITIONAL_DATA_NOTES_ADDITIONAL_FIELDS 'additional';
  28.     /**
  29.      * @var TranslatorInterface
  30.      */
  31.     private $translator;
  32.     /**
  33.      * @var bool
  34.      */
  35.     private $enabled true;
  36.     /**
  37.      * @var array
  38.      */
  39.     private $additionalData = [];
  40.     public function __construct(TranslatorInterface $translator)
  41.     {
  42.         $this->translator $translator;
  43.     }
  44.     /**
  45.      * @param Event $event
  46.      *
  47.      * @throws ValidationException
  48.      */
  49.     public function onWorkflowEnter(Event $event)
  50.     {
  51.         if (!$this->checkEvent($event)) {
  52.             return;
  53.         }
  54.         /**
  55.          * @var AbstractElement $subject
  56.          * @var Transition $transition
  57.          */
  58.         $subject $event->getSubject();
  59.         $transition $event->getTransition();
  60.         $this->handleNotesPreWorkflow($transition$subject);
  61.     }
  62.     /**
  63.      * @param Event $event
  64.      *
  65.      * @throws ValidationException
  66.      */
  67.     public function onWorkflowCompleted(Event $event)
  68.     {
  69.         if (!$this->checkEvent($event)) {
  70.             return;
  71.         }
  72.         /**
  73.          * @var AbstractElement $subject
  74.          * @var Transition $transition
  75.          */
  76.         $subject $event->getSubject();
  77.         $transition $event->getTransition();
  78.         $this->handleNotesPostWorkflow($transition$subject);
  79.     }
  80.     /**
  81.      * @param GlobalActionEvent $event
  82.      *
  83.      * @throws ValidationException
  84.      */
  85.     public function onPreGlobalAction(GlobalActionEvent $event)
  86.     {
  87.         if (!$this->checkGlobalActionEvent($event)) {
  88.             return;
  89.         }
  90.         $subject $event->getSubject();
  91.         $globalAction $event->getGlobalAction();
  92.         $this->handleNotesPreWorkflow($globalAction$subject);
  93.     }
  94.     /**
  95.      * @param GlobalActionEvent $event
  96.      *
  97.      * @throws ValidationException
  98.      */
  99.     public function onPostGlobalAction(GlobalActionEvent $event)
  100.     {
  101.         if (!$this->checkGlobalActionEvent($event)) {
  102.             return;
  103.         }
  104.         $subject $event->getSubject();
  105.         $globalAction $event->getGlobalAction();
  106.         $this->handleNotesPostWorkflow($globalAction$subject);
  107.     }
  108.     /**
  109.      * @param Workflow\Notes\NotesAwareInterface $notesAware
  110.      * @param AbstractElement $subject
  111.      *
  112.      * @throws ValidationException
  113.      */
  114.     private function handleNotesPreWorkflow(Workflow\Notes\NotesAwareInterface $notesAwareAbstractElement $subject)
  115.     {
  116.         if (($setterFn $notesAware->getNotesCommentSetterFn()) && ($notes $this->getNotesComment())) {
  117.             $subject->$setterFn($notes);
  118.         }
  119.         foreach ($notesAware->getNotesAdditionalFields() as $additionalFieldConfig) {
  120.             $data $this->getAdditionalDataForField($additionalFieldConfig);
  121.             //check required
  122.             if ($additionalFieldConfig['required'] && (empty($data) || !$data)) {
  123.                 $label = isset($additionalFieldConfig['title']) && strlen($additionalFieldConfig['title']) > 0
  124.                     $additionalFieldConfig['title']
  125.                     : $additionalFieldConfig['name'];
  126.                 throw new ValidationException(
  127.                     $this->translator->trans('workflow_notes_requred_field_message', [$label], 'admin')
  128.                 );
  129.             }
  130.             //work out whether or not to set the value directly to the object or to add it to the note data
  131.             if (!empty($additionalFieldConfig['setterFn'])) {
  132.                 $setterFn $additionalFieldConfig['setterFn'];
  133.                 $subject->$setterFn($this->getAdditionalDataForField($additionalFieldConfig));
  134.             }
  135.         }
  136.     }
  137.     /**
  138.      * @param Workflow\Notes\NotesAwareInterface $notesAware
  139.      * @param AbstractElement $subject
  140.      *
  141.      * @throws ValidationException
  142.      */
  143.     private function handleNotesPostWorkflow(Workflow\Notes\NotesAwareInterface $notesAwareAbstractElement $subject)
  144.     {
  145.         $additionalFieldsData = [];
  146.         foreach ($notesAware->getNotesAdditionalFields() as $additionalFieldConfig) {
  147.             /**
  148.              * Additional Field example
  149.              * [
  150.             'name' => 'dateLastContacted',
  151.             'fieldType' => 'date',
  152.             'label' => 'Date of Conversation',
  153.             'required' => true,
  154.             'setterFn' => ''
  155.             ]
  156.              */
  157.             //work out whether or not to set the value directly to the object or to add it to the note data
  158.             if (empty($additionalFieldConfig['setterFn'])) {
  159.                 $additionalFieldsData[] = Workflow\Service::createNoteData($additionalFieldConfig$this->getAdditionalDataForField($additionalFieldConfig));
  160.             }
  161.         }
  162.         Workflow\Service::createActionNote(
  163.             $subject,
  164.             $notesAware->getNotesType(),
  165.             $notesAware->getNotesTitle(),
  166.             $this->getNotesComment(),
  167.             $additionalFieldsData
  168.         );
  169.     }
  170.     /**
  171.      * check's if the event subscriber should be executed
  172.      *
  173.      * @param Event $event
  174.      *
  175.      * @return bool
  176.      */
  177.     private function checkEvent(Event $event): bool
  178.     {
  179.         return $this->isEnabled()
  180.                && $event->getTransition() instanceof Transition
  181.                && $event->getSubject() instanceof AbstractElement;
  182.     }
  183.     private function checkGlobalActionEvent(GlobalActionEvent $event): bool
  184.     {
  185.         return $this->isEnabled()
  186.                && $event->getSubject() instanceof AbstractElement;
  187.     }
  188.     /**
  189.      * @return bool
  190.      */
  191.     public function isEnabled(): bool
  192.     {
  193.         return $this->enabled;
  194.     }
  195.     /**
  196.      * @param bool $enabled
  197.      */
  198.     public function setEnabled(bool $enabled): void
  199.     {
  200.         $this->enabled $enabled;
  201.     }
  202.     /**
  203.      * @return array|null
  204.      */
  205.     public function getAdditionalData(): ?array
  206.     {
  207.         return $this->additionalData;
  208.     }
  209.     /**
  210.      * @param array|null $additionalData
  211.      */
  212.     public function setAdditionalData(array $additionalData = []): void
  213.     {
  214.         $this->additionalData $additionalData;
  215.     }
  216.     private function getAdditionalDataForField(array $fieldConfig)
  217.     {
  218.         $additional $this->getAdditionalFields();
  219.         $data $additional[$fieldConfig['name']];
  220.         if ($fieldConfig['fieldType'] === 'checkbox') {
  221.             return $data === 'true';
  222.         }
  223.         return $data;
  224.     }
  225.     private function getNotesComment(): string
  226.     {
  227.         return $this->additionalData[self::ADDITIONAL_DATA_NOTES_COMMENT] ?? '';
  228.     }
  229.     private function getAdditionalFields(): array
  230.     {
  231.         return $this->additionalData[self::ADDITIONAL_DATA_NOTES_ADDITIONAL_FIELDS] ?? [];
  232.     }
  233.     public static function getSubscribedEvents()
  234.     {
  235.         return [
  236.             'workflow.completed' => ['onWorkflowCompleted'1],
  237.             'workflow.enter' => 'onWorkflowEnter',
  238.             WorkflowEvents::PRE_GLOBAL_ACTION => 'onPreGlobalAction',
  239.             WorkflowEvents::POST_GLOBAL_ACTION => 'onPostGlobalAction'
  240.         ];
  241.     }
  242. }