core/lib/Thelia/Action/Template.php line 44

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