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

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