src/EventListener/DataObject/ExerciseEventListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\DataObject;
  3. use Pimcore\Event\DataObjectEvents;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\Element\ValidationException;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ExerciseEventListener implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @inheritDoc
  12.      */
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.         DataObjectEvents::PRE_ADD => 'onPreAdd',
  17.         DataObjectEvents::PRE_UPDATE => 'onPreUpdate',
  18.     ];
  19.     }
  20.     public function onPreAdd(DataObjectEvent $event)
  21.     {
  22.         $exercise $event->getObject();
  23.         if (!$exercise instanceof DataObject\Exercise) {
  24.             return;
  25.         }
  26.         $this->validate($exercise);
  27.     }
  28.     public function onPreUpdate(DataObjectEvent $event)
  29.     {
  30.         $exercise $event->getObject();
  31.         if (!$exercise instanceof DataObject\Exercise) {
  32.             return;
  33.         }
  34.         $this->validate($exercise);
  35.     }
  36.     private function validateDuplication(DataObject\Exercise $exercise)
  37.     {
  38.         $existingExerciseByExerciseName DataObject\Exercise::getByName($exercise->getName(), ['unpublished' => true'limit' => 1]);
  39.         $messageForName 'Duplicate entry for Exercise Name. Please change its value and then try again.';
  40.         if ($exercise->getId()) {
  41.             if ($existingExerciseByExerciseName instanceof DataObject\Exercise && $exercise->getId() != $existingExerciseByExerciseName->getId()) {
  42.                 throw new ValidationException($messageForName);
  43.             }
  44.         } else {
  45.             if ($existingExerciseByExerciseName instanceof DataObject\Exercise) {
  46.                 throw new ValidationException($messageForName);
  47.             }
  48.         }
  49.     }
  50.     private function validate(DataObject\Exercise $exercise)
  51.     {
  52.         $this->validateDuplication($exercise);
  53.         if (!$exercise->getPublished()) {
  54.             return;
  55.         }
  56.         $primaryMuscleGroup $exercise->getPrimaryMuscleGroup();
  57.         if ($primaryMuscleGroup instanceof DataObject\MuscleGroup) {
  58.             if (!$primaryMuscleGroup->getPublished()) {
  59.                 throw new ValidationException("The selected 'Primary Muscle Group' is not published.");
  60.             }
  61.         }
  62.         $secondaryMuscleGroup $exercise->getSecondaryMuscleGroup();
  63.         if ($secondaryMuscleGroup instanceof DataObject\MuscleGroup) {
  64.             if (!$secondaryMuscleGroup->getPublished()) {
  65.                 throw new ValidationException("The selected 'Secondary Muscle Group' is not published.");
  66.             }
  67.         }
  68.     }
  69. }