core/lib/Thelia/Action/Attribute.php line 34

  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.     public function create(AttributeCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  31.     {
  32.         $attribute = new AttributeModel();
  33.         $attribute
  34.             ->setLocale($event->getLocale())
  35.             ->setTitle($event->getTitle())
  36.             ->save()
  37.         ;
  38.         $event->setAttribute($attribute);
  39.         // Add atribute to all product templates if required
  40.         if ($event->getAddToAllTemplates() != 0) {
  41.             $this->doAddToAllTemplates($attribute);
  42.         }
  43.     }
  44.     /**
  45.      * Change a product attribute.
  46.      */
  47.     public function update(AttributeUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  48.     {
  49.         if (null !== $attribute AttributeQuery::create()->findPk($event->getAttributeId())) {
  50.             $attribute
  51.                 ->setLocale($event->getLocale())
  52.                 ->setTitle($event->getTitle())
  53.                 ->setDescription($event->getDescription())
  54.                 ->setChapo($event->getChapo())
  55.                 ->setPostscriptum($event->getPostscriptum())
  56.                 ->save();
  57.             $event->setAttribute($attribute);
  58.         }
  59.     }
  60.     /**
  61.      * Delete a product attribute entry.
  62.      */
  63.     public function delete(AttributeDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  64.     {
  65.         if (null !== ($attribute AttributeQuery::create()->findPk($event->getAttributeId()))) {
  66.             $attribute
  67.                 ->delete()
  68.             ;
  69.             $event->setAttribute($attribute);
  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(AttributeQuery::create(), $event$dispatcher);
  78.     }
  79.     protected function doAddToAllTemplates(AttributeModel $attribute): void
  80.     {
  81.         $templates TemplateQuery::create()->find();
  82.         foreach ($templates as $template) {
  83.             $attribute_template = new AttributeTemplate();
  84.             if (null === AttributeTemplateQuery::create()->filterByAttribute($attribute)->filterByTemplate($template)->findOne()) {
  85.                 $attribute_template
  86.                     ->setAttribute($attribute)
  87.                     ->setTemplate($template)
  88.                     ->save()
  89.                 ;
  90.             }
  91.         }
  92.     }
  93.     public function addToAllTemplates(AttributeEvent $event): void
  94.     {
  95.         $this->doAddToAllTemplates($event->getAttribute());
  96.     }
  97.     public function removeFromAllTemplates(AttributeEvent $event): void
  98.     {
  99.         // Delete this attribute from all product templates
  100.         AttributeTemplateQuery::create()->filterByAttribute($event->getAttribute())->delete();
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public static function getSubscribedEvents()
  106.     {
  107.         return [
  108.             TheliaEvents::ATTRIBUTE_CREATE => ['create'128],
  109.             TheliaEvents::ATTRIBUTE_UPDATE => ['update'128],
  110.             TheliaEvents::ATTRIBUTE_DELETE => ['delete'128],
  111.             TheliaEvents::ATTRIBUTE_UPDATE_POSITION => ['updatePosition'128],
  112.             TheliaEvents::ATTRIBUTE_REMOVE_FROM_ALL_TEMPLATES => ['removeFromAllTemplates'128],
  113.             TheliaEvents::ATTRIBUTE_ADD_TO_ALL_TEMPLATES => ['addToAllTemplates'128],
  114.         ];
  115.     }
  116. }