src/EventListener/DataObject/WorkoutEventListener.php line 40

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 WorkoutEventListener 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.         $workout $event->getObject();
  23.         if (!$workout instanceof DataObject\Workout) {
  24.             return;
  25.         }
  26.         $workout->setName($this->filterName($workout));
  27.         if (!isset($_REQUEST['key'])) {
  28.             //do not validate if user creates object from Pimcore UI.
  29.             $this->validate($workout);
  30.         }
  31.     }
  32.     public function onPreUpdate(DataObjectEvent $event)
  33.     {
  34.         $workout $event->getObject();
  35.         if (!$workout instanceof DataObject\Workout) {
  36.             return;
  37.         }
  38.         $workout->setName($this->filterName($workout));
  39.         $this->validate($workout);
  40.     }
  41.     private function validate(DataObject\Workout $workout)
  42.     {
  43.         if (!in_array($workout->getMonth(), range(1100))) {
  44.             throw new ValidationException("Invalid month. It should be 1-100. Given is '{$workout->getMonth()}'.");
  45.         }
  46.         $this->validateDuplication($workout);
  47.         if (!$workout->getPublished()) {
  48.             return;
  49.         }
  50.         if (empty($exerciseBlock $workout->getExerciseBlock())) {
  51.             throw new ValidationException('Please select some exercises.');
  52.         }
  53.         foreach ($exerciseBlock as $exerciseBlockItem) {
  54.             $exercise $exerciseBlockItem['exercise']->getData();
  55.             if (!$exercise instanceof DataObject\Exercise) {
  56.                 throw new ValidationException('All items should have exercise. It seems you forgot to select exercise for some items.');
  57.             }
  58.             if (!$exercise->getPublished()) {
  59.                 throw new ValidationException("The selected exercise '{$exercise->getName()}' should be published. All the selected exercises should be published.");
  60.             }
  61.         }
  62.     }
  63.     private function validateDuplication(DataObject\Workout $workout)
  64.     {
  65.         $existingWorkoutByName DataObject\Workout::getByName($workout->getName(), ['unpublished' => true'limit' => 1]);
  66.         if ($workout->getId()) {
  67.             if ($existingWorkoutByName instanceof DataObject\Workout && $workout->getId() != $existingWorkoutByName->getId()) {
  68.                 throw new ValidationException("Update failed. Duplicate workout name = '{$existingWorkoutByName->getName()}'");
  69.             }
  70.         } else {
  71.             if ($existingWorkoutByName instanceof DataObject\Workout) {
  72.                 throw new ValidationException("Create failed. Duplicate workout name = '{$existingWorkoutByName->getName()}'");
  73.             }
  74.         }
  75.     }
  76.     private function filterName(DataObject\Workout $workout)
  77.     {
  78.         $name $workout->getName();
  79.         if ($name && strpos(strtolower($name), 'rest') === false) {
  80.             $parts explode('-'$name);
  81.             $type = isset($parts[2]) ? $parts[2] : 'home';
  82.             $newName $parts[0] . '-' $workout->getMonth().'-'.$type;
  83.             return strtolower($newName);
  84.         }
  85.         return $name;
  86.     }
  87. }