vendor/thelia/core/lib/Thelia/Action/Attribute.php line 36

Open in your IDE?
  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\Attribute\AttributeCreateEvent;
  15. use Thelia\Core\Event\Attribute\AttributeDeleteEvent;
  16. use Thelia\Core\Event\Attribute\AttributeEvent;
  17. use Thelia\Core\Event\Attribute\AttributeUpdateEvent;
  18. use Thelia\Core\Event\TheliaEvents;
  19. use Thelia\Core\Event\UpdatePositionEvent;
  20. use Thelia\Model\Attribute as AttributeModel;
  21. use Thelia\Model\AttributeQuery;
  22. use Thelia\Model\AttributeTemplate;
  23. use Thelia\Model\AttributeTemplateQuery;
  24. use Thelia\Model\TemplateQuery;
  25. class Attribute extends BaseAction implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * Create a new attribute entry.
  29.      *
  30.      * @param $eventName
  31.      */
  32.     public function create(AttributeCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  33.     {
  34.         $attribute = new AttributeModel();
  35.         $attribute
  36.             ->setLocale($event->getLocale())
  37.             ->setTitle($event->getTitle())
  38.             ->save()
  39.         ;
  40.         $event->setAttribute($attribute);
  41.         // Add atribute to all product templates if required
  42.         if ($event->getAddToAllTemplates() != 0) {
  43.             $this->doAddToAllTemplates($attribute);
  44.         }
  45.     }
  46.     /**
  47.      * Change a product attribute.
  48.      *
  49.      * @param $eventName
  50.      */
  51.     public function update(AttributeUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  52.     {
  53.         if (null !== $attribute AttributeQuery::create()->findPk($event->getAttributeId())) {
  54.             $attribute
  55.                 ->setLocale($event->getLocale())
  56.                 ->setTitle($event->getTitle())
  57.                 ->setDescription($event->getDescription())
  58.                 ->setChapo($event->getChapo())
  59.                 ->setPostscriptum($event->getPostscriptum())
  60.                 ->save();
  61.             $event->setAttribute($attribute);
  62.         }
  63.     }
  64.     /**
  65.      * Delete a product attribute entry.
  66.      *
  67.      * @param $eventName
  68.      */
  69.     public function delete(AttributeDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  70.     {
  71.         if (null !== ($attribute AttributeQuery::create()->findPk($event->getAttributeId()))) {
  72.             $attribute
  73.                 ->delete()
  74.             ;
  75.             $event->setAttribute($attribute);
  76.         }
  77.     }
  78.     /**
  79.      * Changes position, selecting absolute ou relative change.
  80.      *
  81.      * @param $eventName
  82.      */
  83.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  84.     {
  85.         $this->genericUpdatePosition(AttributeQuery::create(), $event$dispatcher);
  86.     }
  87.     protected function doAddToAllTemplates(AttributeModel $attribute): void
  88.     {
  89.         $templates TemplateQuery::create()->find();
  90.         foreach ($templates as $template) {
  91.             $attribute_template = new AttributeTemplate();
  92.             if (null === AttributeTemplateQuery::create()->filterByAttribute($attribute)->filterByTemplate($template)->findOne()) {
  93.                 $attribute_template
  94.                     ->setAttribute($attribute)
  95.                     ->setTemplate($template)
  96.                     ->save()
  97.                 ;
  98.             }
  99.         }
  100.     }
  101.     public function addToAllTemplates(AttributeEvent $event): void
  102.     {
  103.         $this->doAddToAllTemplates($event->getAttribute());
  104.     }
  105.     public function removeFromAllTemplates(AttributeEvent $event): void
  106.     {
  107.         // Delete this attribute from all product templates
  108.         AttributeTemplateQuery::create()->filterByAttribute($event->getAttribute())->delete();
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     public static function getSubscribedEvents()
  114.     {
  115.         return [
  116.             TheliaEvents::ATTRIBUTE_CREATE => ['create'128],
  117.             TheliaEvents::ATTRIBUTE_UPDATE => ['update'128],
  118.             TheliaEvents::ATTRIBUTE_DELETE => ['delete'128],
  119.             TheliaEvents::ATTRIBUTE_UPDATE_POSITION => ['updatePosition'128],
  120.             TheliaEvents::ATTRIBUTE_REMOVE_FROM_ALL_TEMPLATES => ['removeFromAllTemplates'128],
  121.             TheliaEvents::ATTRIBUTE_ADD_TO_ALL_TEMPLATES => ['addToAllTemplates'128],
  122.         ];
  123.     }
  124. }