src/EventListener/UserDelegationEventListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Services\UserDelegationService;
  4. use App\Traits\Log;
  5. use Carbon\Carbon;
  6. use Exception;
  7. use Pimcore\Event\Model\DataObjectEvent;
  8. use Pimcore\Event\Model\ElementEventInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class UserDelegationEventListener
  11. {
  12.     use Log;
  13.     private $service;
  14.     public function __construct()
  15.     {
  16.         $this->service = new UserDelegationService;
  17.     }
  18.     public function onPreUpdate(ElementEventInterface $e)
  19.     {
  20.         if (
  21.             $e instanceof DataObjectEvent &&
  22.             method_exists($e->getObject(), 'getClassName') &&
  23.             $e->getObject() instanceof \Pimcore\Model\DataObject\User
  24.         ) {
  25.             /**
  26.              * @var \Pimcore\Model\DataObject\User $user
  27.              */
  28.             $user $e->getObject();
  29.             $errors $this->service->validation($user);
  30.             if (!empty($errors)) {
  31.                 throw new Exception(implode("<br>"$errors));
  32.             }
  33.             $versions $user->getVersions();
  34.             //if ($user->getUserDelegation() != null && !$this->service->isRecipientAvailable($user))
  35.               //  throw new Exception("User Delegation is already a PLT. Please choose other user.");
  36.         }
  37.     }
  38.     public function onPostUpdate(ElementEventInterface $e)
  39.     {
  40.         if (
  41.             $e instanceof DataObjectEvent &&
  42.             method_exists($e->getObject(), 'getClassName') &&
  43.             $e->getObject() instanceof \Pimcore\Model\DataObject\User
  44.         ) {
  45.             /**
  46.              * @var \Pimcore\Model\DataObject\User $delegator
  47.              */
  48.             $delegator $e->getObject();
  49.             if ($this->service->isEligible($delegator)) {
  50.                 try {
  51.                     $recipient \Pimcore\Model\DataObject\User::getByPimcoreUserId$delegator->getUserDelegation(), );
  52.                     $this->service->updatePLT(
  53.                         $recipient,
  54.                         "Set as PLT by {$delegator->getNip()}",
  55.                         true,
  56.                         $delegator->getDivisionId(),
  57.                         $delegator->getJobPositionId(),
  58.                         $delegator->getDepartmentId()
  59.                     );
  60.                     $delegatorRole \Pimcore\Model\User::getById$delegator->getPimcoreUserId() );
  61.                     $recipientRole \Pimcore\Model\User::getById$recipient->getPimcoreUserId() );
  62.                     $recipientPreviouseRoles $recipientRole->getRoles();
  63.                     $recipientRole->setRolesarray_merge($recipientPreviouseRoles$delegatorRole->getRoles()) );
  64.                     $recipientRole->save();
  65.                     // user update
  66.                     if ($delegator->getDelegation() != null && $delegator->getDelegation()->getRecipient() != $recipient) {
  67.                         $oldDelegation $delegator->getDelegation();
  68.                         $oldDelegation->setIsActive(false);
  69.                         $oldDelegation->setPublished(false);
  70.                         $oldDelegation->save();
  71.                     }
  72.                     $delegationHistoryObject $this->service->saveDelegationHistory($delegator$recipient$recipientPreviouseRoles);
  73.                     $delegator->setDelegation$delegationHistoryObject );
  74.                     $delegator->save();
  75.                     $this->service->delegationLog($delegator$recipient);
  76.                 } catch (\Exception $e) {
  77.                     $this->simpleLog('DELEGATION'$e);
  78.                     throw new Exception("Delegation is failed! Error: " $e->getMessage());
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }