core/lib/Thelia/Action/Feature.php line 77

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\Feature\FeatureCreateEvent;
  15. use Thelia\Core\Event\Feature\FeatureDeleteEvent;
  16. use Thelia\Core\Event\Feature\FeatureEvent;
  17. use Thelia\Core\Event\Feature\FeatureUpdateEvent;
  18. use Thelia\Core\Event\TheliaEvents;
  19. use Thelia\Core\Event\UpdatePositionEvent;
  20. use Thelia\Model\Feature as FeatureModel;
  21. use Thelia\Model\FeatureQuery;
  22. use Thelia\Model\FeatureTemplate;
  23. use Thelia\Model\FeatureTemplateQuery;
  24. use Thelia\Model\TemplateQuery;
  25. class Feature extends BaseAction implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * Create a new feature entry.
  29.      */
  30.     public function create(FeatureCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  31.     {
  32.         $feature = new FeatureModel();
  33.         $feature
  34.             ->setLocale($event->getLocale())
  35.             ->setTitle($event->getTitle())
  36.             ->save()
  37.         ;
  38.         $event->setFeature($feature);
  39.         // Add atribute to all product templates if required
  40.         if ($event->getAddToAllTemplates() != 0) {
  41.             $this->doAddToAllTemplates($feature);
  42.         }
  43.     }
  44.     /**
  45.      * Change a product feature.
  46.      */
  47.     public function update(FeatureUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  48.     {
  49.         if (null !== $feature FeatureQuery::create()->findPk($event->getFeatureId())) {
  50.             $feature
  51.                 ->setLocale($event->getLocale())
  52.                 ->setTitle($event->getTitle())
  53.                 ->setDescription($event->getDescription())
  54.                 ->setChapo($event->getChapo())
  55.                 ->setPostscriptum($event->getPostscriptum())
  56.                 ->save();
  57.             $event->setFeature($feature);
  58.         }
  59.     }
  60.     /**
  61.      * Delete a product feature entry.
  62.      */
  63.     public function delete(FeatureDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  64.     {
  65.         if (null !== ($feature FeatureQuery::create()->findPk($event->getFeatureId()))) {
  66.             $feature
  67.                 ->delete()
  68.             ;
  69.             $event->setFeature($feature);
  70.         }
  71.     }
  72.     /**
  73.      * Changes position, selecting absolute ou relative change.
  74.      */
  75.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  76.     {
  77.         $this->genericUpdatePosition(FeatureQuery::create(), $event$dispatcher);
  78.     }
  79.     protected function doAddToAllTemplates(FeatureModel $feature): void
  80.     {
  81.         $templates TemplateQuery::create()->find();
  82.         foreach ($templates as $template) {
  83.             $feature_template = new FeatureTemplate();
  84.             if (null === FeatureTemplateQuery::create()->filterByFeature($feature)->filterByTemplate($template)->findOne()) {
  85.                 $feature_template
  86.                     ->setFeature($feature)
  87.                     ->setTemplate($template)
  88.                     ->save()
  89.                 ;
  90.             }
  91.         }
  92.     }
  93.     public function addToAllTemplates(FeatureEvent $event): void
  94.     {
  95.         $this->doAddToAllTemplates($event->getFeature());
  96.     }
  97.     public function removeFromAllTemplates(FeatureEvent $event): void
  98.     {
  99.         // Delete this feature from all product templates
  100.         FeatureTemplateQuery::create()->filterByFeature($event->getFeature())->delete();
  101.     }
  102.     /**
  103.      * {@inheritDoc}
  104.      */
  105.     public static function getSubscribedEvents()
  106.     {
  107.         return [
  108.             TheliaEvents::FEATURE_CREATE => ['create'128],
  109.             TheliaEvents::FEATURE_UPDATE => ['update'128],
  110.             TheliaEvents::FEATURE_DELETE => ['delete'128],
  111.             TheliaEvents::FEATURE_UPDATE_POSITION => ['updatePosition'128],
  112.             TheliaEvents::FEATURE_REMOVE_FROM_ALL_TEMPLATES => ['removeFromAllTemplates'128],
  113.             TheliaEvents::FEATURE_ADD_TO_ALL_TEMPLATES => ['addToAllTemplates'128],
  114.         ];
  115.     }
  116. }