vendor/thelia/core/lib/Thelia/Action/Template.php line 46

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 Propel\Runtime\Propel;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Thelia\Core\Event\Template\TemplateAddAttributeEvent;
  16. use Thelia\Core\Event\Template\TemplateAddFeatureEvent;
  17. use Thelia\Core\Event\Template\TemplateCreateEvent;
  18. use Thelia\Core\Event\Template\TemplateDeleteAttributeEvent;
  19. use Thelia\Core\Event\Template\TemplateDeleteEvent;
  20. use Thelia\Core\Event\Template\TemplateDeleteFeatureEvent;
  21. use Thelia\Core\Event\Template\TemplateDuplicateEvent;
  22. use Thelia\Core\Event\Template\TemplateUpdateEvent;
  23. use Thelia\Core\Event\TheliaEvents;
  24. use Thelia\Core\Event\UpdatePositionEvent;
  25. use Thelia\Core\Translation\Translator;
  26. use Thelia\Model\AttributeTemplate;
  27. use Thelia\Model\AttributeTemplateQuery;
  28. use Thelia\Model\CategoryQuery;
  29. use Thelia\Model\FeatureTemplate;
  30. use Thelia\Model\FeatureTemplateQuery;
  31. use Thelia\Model\Map\TemplateTableMap;
  32. use Thelia\Model\ProductQuery;
  33. use Thelia\Model\Template as TemplateModel;
  34. use Thelia\Model\TemplateQuery;
  35. class Template extends BaseAction implements EventSubscriberInterface
  36. {
  37.     /**
  38.      * Create a new template entry.
  39.      *
  40.      * @param $eventName
  41.      */
  42.     public function create(TemplateCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  43.     {
  44.         $template = new TemplateModel();
  45.         $template
  46.             ->setLocale($event->getLocale())
  47.             ->setName($event->getTemplateName())
  48.             ->save()
  49.         ;
  50.         $event->setTemplate($template);
  51.     }
  52.     /**
  53.      * Dupliucate an existing template entry.
  54.      *
  55.      * @param \Thelia\Core\Event\Template\TemplateCreateEvent $event
  56.      * @param $eventName
  57.      */
  58.     public function duplicate(TemplateDuplicateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  59.     {
  60.         if (null !== $source TemplateQuery::create()->findPk($event->getSourceTemplateId())) {
  61.             $source->setLocale($event->getLocale());
  62.             $createEvent = new TemplateCreateEvent();
  63.             $createEvent
  64.                 ->setLocale($event->getLocale())
  65.                 ->setTemplateName(
  66.                     Translator::getInstance()->trans('Copy of %tpl', ['%tpl' => $source->getName()])
  67.                 );
  68.             $dispatcher->dispatch($createEventTheliaEvents::TEMPLATE_CREATE);
  69.             $clone $createEvent->getTemplate();
  70.             $attrList AttributeTemplateQuery::create()->findByTemplateId($source->getId());
  71.             /** @var $feat AttributeTemplate */
  72.             foreach ($attrList as $feat) {
  73.                 $dispatcher->dispatch(
  74.                     new TemplateAddAttributeEvent($clone$feat->getAttributeId()),
  75.                     TheliaEvents::TEMPLATE_ADD_ATTRIBUTE
  76.                 );
  77.             }
  78.             $featList FeatureTemplateQuery::create()->findByTemplateId($source->getId());
  79.             /** @var $feat FeatureTemplate */
  80.             foreach ($featList as $feat) {
  81.                 $dispatcher->dispatch(
  82.                     new TemplateAddFeatureEvent($clone$feat->getFeatureId()),
  83.                     TheliaEvents::TEMPLATE_ADD_FEATURE
  84.                 );
  85.             }
  86.             $event->setTemplate($clone);
  87.         }
  88.     }
  89.     /**
  90.      * Change a product template.
  91.      *
  92.      * @param $eventName
  93.      */
  94.     public function update(TemplateUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  95.     {
  96.         if (null !== $template TemplateQuery::create()->findPk($event->getTemplateId())) {
  97.             $template
  98.                 ->setLocale($event->getLocale())
  99.                 ->setName($event->getTemplateName())
  100.                 ->save();
  101.             $event->setTemplate($template);
  102.         }
  103.     }
  104.     /**
  105.      * Delete a product template entry.
  106.      *
  107.      * @param $eventName
  108.      *
  109.      * @throws \Exception
  110.      */
  111.     public function delete(TemplateDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  112.     {
  113.         if (null !== ($template TemplateQuery::create()->findPk($event->getTemplateId()))) {
  114.             // Check if template is used by a product
  115.             $productCount ProductQuery::create()->findByTemplateId($template->getId())->count();
  116.             if ($productCount <= 0) {
  117.                 $con Propel::getWriteConnection(TemplateTableMap::DATABASE_NAME);
  118.                 $con->beginTransaction();
  119.                 try {
  120.                     $template
  121.                         ->delete($con);
  122.                     // We have to also delete any reference of this template in category tables
  123.                     // We can't use a FK here, as the DefaultTemplateId column may be NULL
  124.                     // so let's take care of this.
  125.                     CategoryQuery::create()
  126.                         ->filterByDefaultTemplateId($event->getTemplateId())
  127.                         ->update(['DefaultTemplateId' => null], $con);
  128.                     $con->commit();
  129.                 } catch (\Exception $ex) {
  130.                     $con->rollback();
  131.                     throw $ex;
  132.                 }
  133.             }
  134.             $event->setTemplate($template);
  135.             $event->setProductCount($productCount);
  136.         }
  137.     }
  138.     public function addAttribute(TemplateAddAttributeEvent $event): void
  139.     {
  140.         if (null === AttributeTemplateQuery::create()
  141.                 ->filterByAttributeId($event->getAttributeId())
  142.                 ->filterByTemplate($event->getTemplate())
  143.                 ->findOne()) {
  144.             $attributeTemplate = new AttributeTemplate();
  145.             $attributeTemplate
  146.                 ->setAttributeId($event->getAttributeId())
  147.                 ->setTemplate($event->getTemplate())
  148.                 ->save()
  149.             ;
  150.         }
  151.     }
  152.     /**
  153.      * Changes position, selecting absolute ou relative change.
  154.      *
  155.      * @param $eventName
  156.      */
  157.     public function updateAttributePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  158.     {
  159.         $this->genericUpdatePosition(AttributeTemplateQuery::create(), $event$dispatcher);
  160.     }
  161.     /**
  162.      * Changes position, selecting absolute ou relative change.
  163.      *
  164.      * @param $eventName
  165.      */
  166.     public function updateFeaturePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  167.     {
  168.         $this->genericUpdatePosition(FeatureTemplateQuery::create(), $event$dispatcher);
  169.     }
  170.     public function deleteAttribute(TemplateDeleteAttributeEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  171.     {
  172.         $attributeTemplate AttributeTemplateQuery::create()
  173.             ->filterByAttributeId($event->getAttributeId())
  174.             ->filterByTemplate($event->getTemplate())->findOne()
  175.         ;
  176.         if ($attributeTemplate !== null) {
  177.             $attributeTemplate
  178.                 ->delete();
  179.         } else {
  180.             // Prevent event propagation
  181.             $event->stopPropagation();
  182.         }
  183.     }
  184.     public function addFeature(TemplateAddFeatureEvent $event): void
  185.     {
  186.         if (null === FeatureTemplateQuery::create()
  187.                 ->filterByFeatureId($event->getFeatureId())
  188.                 ->filterByTemplate($event->getTemplate())
  189.                 ->findOne()
  190.         ) {
  191.             $featureTemplate = new FeatureTemplate();
  192.             $featureTemplate
  193.                 ->setFeatureId($event->getFeatureId())
  194.                 ->setTemplate($event->getTemplate())
  195.                 ->save()
  196.             ;
  197.         }
  198.     }
  199.     public function deleteFeature(TemplateDeleteFeatureEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  200.     {
  201.         $featureTemplate FeatureTemplateQuery::create()
  202.             ->filterByFeatureId($event->getFeatureId())
  203.             ->filterByTemplate($event->getTemplate())->findOne()
  204.         ;
  205.         if ($featureTemplate !== null) {
  206.             $featureTemplate
  207.                 ->delete();
  208.         } else {
  209.             // Prevent event propagation
  210.             $event->stopPropagation();
  211.         }
  212.     }
  213.     /**
  214.      * {@inheritDoc}
  215.      */
  216.     public static function getSubscribedEvents()
  217.     {
  218.         return [
  219.             TheliaEvents::TEMPLATE_CREATE => ['create'128],
  220.             TheliaEvents::TEMPLATE_UPDATE => ['update'128],
  221.             TheliaEvents::TEMPLATE_DELETE => ['delete'128],
  222.             TheliaEvents::TEMPLATE_DUPLICATE => ['duplicate'128],
  223.             TheliaEvents::TEMPLATE_ADD_ATTRIBUTE => ['addAttribute'128],
  224.             TheliaEvents::TEMPLATE_DELETE_ATTRIBUTE => ['deleteAttribute'128],
  225.             TheliaEvents::TEMPLATE_ADD_FEATURE => ['addFeature'128],
  226.             TheliaEvents::TEMPLATE_DELETE_FEATURE => ['deleteFeature'128],
  227.             TheliaEvents::TEMPLATE_CHANGE_ATTRIBUTE_POSITION => ['updateAttributePosition'128],
  228.             TheliaEvents::TEMPLATE_CHANGE_FEATURE_POSITION => ['updateFeaturePosition'128],
  229.         ];
  230.     }
  231. }