core/lib/Thelia/Action/ModuleHook.php line 195

  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\EventSubscriberInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. use Thelia\Core\Event\Cache\CacheEvent;
  15. use Thelia\Core\Event\Hook\HookToggleActivationEvent;
  16. use Thelia\Core\Event\Hook\HookUpdateEvent;
  17. use Thelia\Core\Event\Hook\ModuleHookCreateEvent;
  18. use Thelia\Core\Event\Hook\ModuleHookDeleteEvent;
  19. use Thelia\Core\Event\Hook\ModuleHookToggleActivationEvent;
  20. use Thelia\Core\Event\Hook\ModuleHookUpdateEvent;
  21. use Thelia\Core\Event\Module\ModuleDeleteEvent;
  22. use Thelia\Core\Event\Module\ModuleToggleActivationEvent;
  23. use Thelia\Core\Event\TheliaEvents;
  24. use Thelia\Core\Event\UpdatePositionEvent;
  25. use Thelia\Core\Translation\Translator;
  26. use Thelia\Model\Base\IgnoredModuleHookQuery;
  27. use Thelia\Model\HookQuery;
  28. use Thelia\Model\IgnoredModuleHook;
  29. use Thelia\Model\ModuleHook as ModuleHookModel;
  30. use Thelia\Model\ModuleHookQuery;
  31. use Thelia\Model\ModuleQuery;
  32. use Thelia\Module\BaseModule;
  33. /**
  34.  * Class ModuleHook.
  35.  *
  36.  * @author  Julien Chanséaume <jchanseaume@openstudio.fr>
  37.  */
  38. class ModuleHook extends BaseAction implements EventSubscriberInterface
  39. {
  40.     /** @var string */
  41.     protected $cacheDir;
  42.     /** @var EventDispatcherInterface */
  43.     protected $dispatcher;
  44.     public function __construct($kernelCacheDirEventDispatcherInterface $dispatcher)
  45.     {
  46.         $this->cacheDir $kernelCacheDir;
  47.         $this->dispatcher $dispatcher;
  48.     }
  49.     public function toggleModuleActivation(ModuleToggleActivationEvent $event)
  50.     {
  51.         if (null !== $module ModuleQuery::create()->findPk($event->getModuleId())) {
  52.             ModuleHookQuery::create()
  53.                 ->filterByModuleId($module->getId())
  54.                 ->update(['ModuleActive' => ($module->getActivate() == BaseModule::IS_ACTIVATED)]);
  55.         }
  56.         return $event;
  57.     }
  58.     public function deleteModule(ModuleDeleteEvent $event)
  59.     {
  60.         if ($event->getModuleId()) {
  61.             ModuleHookQuery::create()
  62.                 ->filterByModuleId($event->getModuleId())
  63.                 ->delete();
  64.         }
  65.         return $event;
  66.     }
  67.     protected function isModuleActive($module_id)
  68.     {
  69.         if (null !== $module ModuleQuery::create()->findPk($module_id)) {
  70.             return $module->getActivate();
  71.         }
  72.         return false;
  73.     }
  74.     protected function isHookActive($hook_id)
  75.     {
  76.         if (null !== $hook HookQuery::create()->findPk($hook_id)) {
  77.             return $hook->getActivate();
  78.         }
  79.         return false;
  80.     }
  81.     protected function getLastPositionInHook($hook_id)
  82.     {
  83.         $result ModuleHookQuery::create()
  84.             ->filterByHookId($hook_id)
  85.             ->withColumn('MAX(ModuleHook.position)''maxPos')
  86.             ->groupBy('ModuleHook.hook_id')
  87.             ->select(['maxPos'])
  88.             ->findOne();
  89.         return (int) $result 1;
  90.     }
  91.     public function createModuleHook(ModuleHookCreateEvent $event): void
  92.     {
  93.         $moduleHook = new ModuleHookModel();
  94.         // todo: test if classname and method exists
  95.         $moduleHook
  96.             ->setModuleId($event->getModuleId())
  97.             ->setHookId($event->getHookId())
  98.             ->setActive(false)
  99.             ->setClassname($event->getClassname())
  100.             ->setMethod($event->getMethod())
  101.             ->setModuleActive($this->isModuleActive($event->getModuleId()))
  102.             ->setHookActive($this->isHookActive($event->getHookId()))
  103.             ->setPosition($this->getLastPositionInHook($event->getHookId()))
  104.             ->setTemplates($event->getTemplates())
  105.             ->save();
  106.         // Be sure to delete this module hook from the ignored module hook table
  107.         IgnoredModuleHookQuery::create()
  108.             ->filterByHookId($event->getHookId())
  109.             ->filterByModuleId($event->getModuleId())
  110.             ->delete();
  111.         $event->setModuleHook($moduleHook);
  112.     }
  113.     public function updateModuleHook(ModuleHookUpdateEvent $event): void
  114.     {
  115.         if (null !== $moduleHook ModuleHookQuery::create()->findPk($event->getModuleHookId())) {
  116.             // todo: test if classname and method exists
  117.             $moduleHook
  118.                 ->setHookId($event->getHookId())
  119.                 ->setModuleId($event->getModuleId())
  120.                 ->setClassname($event->getClassname())
  121.                 ->setMethod($event->getMethod())
  122.                 ->setActive($event->getActive())
  123.                 ->setHookActive($this->isHookActive($event->getHookId()))
  124.                 ->setTemplates($event->getTemplates())
  125.                 ->save();
  126.             $event->setModuleHook($moduleHook);
  127.             $this->cacheClear();
  128.         }
  129.     }
  130.     public function deleteModuleHook(ModuleHookDeleteEvent $event): void
  131.     {
  132.         if (null !== $moduleHook ModuleHookQuery::create()->findPk($event->getModuleHookId())) {
  133.             $moduleHook->delete();
  134.             $event->setModuleHook($moduleHook);
  135.             // Prevent hook recreation by RegisterListenersPass::registerHook()
  136.             // We store the method here to be able to retreive it when
  137.             // we need to get all hook declared by a module
  138.             $imh = new IgnoredModuleHook();
  139.             $imh
  140.                 ->setModuleId($moduleHook->getModuleId())
  141.                 ->setHookId($moduleHook->getHookId())
  142.                 ->setMethod($moduleHook->getMethod())
  143.                 ->setClassname($moduleHook->getClassname())
  144.                 ->save();
  145.             $this->cacheClear();
  146.         }
  147.     }
  148.     public function toggleModuleHookActivation(ModuleHookToggleActivationEvent $event)
  149.     {
  150.         if (null !== $moduleHook $event->getModuleHook()) {
  151.             if ($moduleHook->getModuleActive()) {
  152.                 $moduleHook->setActive(!$moduleHook->getActive());
  153.                 $moduleHook->save();
  154.             } else {
  155.                 throw new \LogicException(Translator::getInstance()->trans('The module has to be activated.'));
  156.             }
  157.         }
  158.         $this->cacheClear();
  159.         return $event;
  160.     }
  161.     /**
  162.      * Changes position, selecting absolute ou relative change.
  163.      *
  164.      * @return UpdatePositionEvent $event
  165.      */
  166.     public function updateModuleHookPosition(UpdatePositionEvent $event)
  167.     {
  168.         $this->genericUpdatePosition(ModuleHookQuery::create(), $event);
  169.         $this->cacheClear();
  170.         return $event;
  171.     }
  172.     public function updateHook(HookUpdateEvent $event): void
  173.     {
  174.         if ($event->hasHook()) {
  175.             $hook $event->getHook();
  176.             ModuleHookQuery::create()
  177.                 ->filterByHookId($hook->getId())
  178.                 ->update(['HookActive' => $hook->getActivate()]);
  179.             $this->cacheClear();
  180.         }
  181.     }
  182.     public function toggleHookActivation(HookToggleActivationEvent $event): void
  183.     {
  184.         if ($event->hasHook()) {
  185.             $hook $event->getHook();
  186.             ModuleHookQuery::create()
  187.                 ->filterByHookId($hook->getId())
  188.                 ->update(['HookActive' => $hook->getActivate()]);
  189.             $this->cacheClear();
  190.         }
  191.     }
  192.     protected function cacheClear(): void
  193.     {
  194.         $cacheEvent = new CacheEvent($this->cacheDir);
  195.         $this->dispatcher->dispatch($cacheEventTheliaEvents::CACHE_CLEAR);
  196.     }
  197.     /**
  198.      * {@inheritdoc}
  199.      */
  200.     public static function getSubscribedEvents()
  201.     {
  202.         return [
  203.             TheliaEvents::MODULE_HOOK_CREATE => ['createModuleHook'128],
  204.             TheliaEvents::MODULE_HOOK_UPDATE => ['updateModuleHook'128],
  205.             TheliaEvents::MODULE_HOOK_DELETE => ['deleteModuleHook'128],
  206.             TheliaEvents::MODULE_HOOK_UPDATE_POSITION => ['updateModuleHookPosition'128],
  207.             TheliaEvents::MODULE_HOOK_TOGGLE_ACTIVATION => ['toggleModuleHookActivation'128],
  208.             TheliaEvents::MODULE_TOGGLE_ACTIVATION => ['toggleModuleActivation'64],
  209.             TheliaEvents::MODULE_DELETE => ['deleteModule'64],
  210.             TheliaEvents::HOOK_TOGGLE_ACTIVATION => ['toggleHookActivation'64],
  211.             TheliaEvents::HOOK_UPDATE => ['updateHook'64],
  212.         ];
  213.     }
  214. }