vendor/ezsystems/repository-forms/lib/Form/Processor/PolicyFormProcessor.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the eZ RepositoryForms package.
  4.  *
  5.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  7.  */
  8. namespace EzSystems\RepositoryForms\Form\Processor;
  9. use eZ\Publish\API\Repository\RoleService;
  10. use eZ\Publish\API\Repository\Values\User\RoleDraft;
  11. use EzSystems\RepositoryForms\Event\FormActionEvent;
  12. use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class PolicyFormProcessor implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var RoleService
  18.      */
  19.     private $roleService;
  20.     public function __construct(RoleService $roleService)
  21.     {
  22.         $this->roleService $roleService;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             RepositoryFormEvents::POLICY_UPDATE => 'processUpdatePolicy',
  28.             RepositoryFormEvents::POLICY_SAVE => 'processSavePolicy',
  29.             RepositoryFormEvents::POLICY_REMOVE_DRAFT => 'processRemoveDraft',
  30.         ];
  31.     }
  32.     public function processUpdatePolicy(FormActionEvent $event)
  33.     {
  34.         // Don't update anything if we just want to cancel the draft.
  35.         if ($event->getClickedButton() === 'removeDraft') {
  36.             return;
  37.         }
  38.         /** @var \EzSystems\RepositoryForms\Data\Role\PolicyCreateData|\EzSystems\RepositoryForms\Data\Role\PolicyUpdateData $data */
  39.         $data $event->getData();
  40.         if ($data->isNew() && $data->moduleFunction) {
  41.             list($module$function) = explode('|'$data->moduleFunction);
  42.             $data->module $module;
  43.             $data->function $function;
  44.             $initialRoleDraft $data->roleDraft;
  45.             $updatedRoleDraft $this->roleService->addPolicyByRoleDraft($initialRoleDraft$data);
  46.             $initialPoliciesById $this->getPoliciesById($initialRoleDraft);
  47.             $updatedPoliciesById $this->getPoliciesById($updatedRoleDraft);
  48.             foreach ($updatedPoliciesById as $policyId => $policyDraft) {
  49.                 if (!isset($initialPoliciesById[$policyId])) {
  50.                     $data->setPolicyDraft($policyDraft);
  51.                     break;
  52.                 }
  53.             }
  54.         } else {
  55.             // Only save limitations on update.
  56.             // It is not possible by design to update policy module/function.
  57.             foreach ($data->limitationsData as $limitation) {
  58.                 // Add posted limitations as valid ones, recognized by RoleService.
  59.                 if (!empty($limitation->limitationValues)) {
  60.                     $data->addLimitation($limitation);
  61.                 }
  62.             }
  63.             $this->roleService->updatePolicyByRoleDraft($data->roleDraft$data->policyDraft$data);
  64.         }
  65.     }
  66.     /**
  67.      * Returns policies for passed RoleDraft object, indexed by their IDs.
  68.      *
  69.      * @param RoleDraft $roleDraft
  70.      * @return array
  71.      */
  72.     private function getPoliciesById(RoleDraft $roleDraft)
  73.     {
  74.         $policies = [];
  75.         foreach ($roleDraft->getPolicies() as $policy) {
  76.             $policies[$policy->id] = $policy;
  77.         }
  78.         return $policies;
  79.     }
  80.     public function processSavePolicy(FormActionEvent $event)
  81.     {
  82.         /** @var \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft */
  83.         $roleDraft $event->getData()->roleDraft;
  84.         $this->roleService->publishRoleDraft($roleDraft);
  85.     }
  86.     public function processRemoveDraft(FormActionEvent $event)
  87.     {
  88.         /** @var \EzSystems\RepositoryForms\Data\Role\PolicyCreateData|\EzSystems\RepositoryForms\Data\Role\PolicyUpdateData $data */
  89.         $data $event->getData();
  90.         if (!$data->isNew()) {
  91.             $this->roleService->removePolicyByRoleDraft($data->roleDraft$data->policyDraft);
  92.         }
  93.         $this->roleService->deleteRoleDraft($data->roleDraft);
  94.     }
  95. }