vendor/thelia/core/lib/Thelia/Action/Config.php line 52

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\Config\ConfigCreateEvent;
  15. use Thelia\Core\Event\Config\ConfigDeleteEvent;
  16. use Thelia\Core\Event\Config\ConfigUpdateEvent;
  17. use Thelia\Core\Event\TheliaEvents;
  18. use Thelia\Model\Config as ConfigModel;
  19. use Thelia\Model\ConfigQuery;
  20. class Config extends BaseAction implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * Create a new configuration entry.
  24.      *
  25.      * @param $eventName
  26.      */
  27.     public function create(ConfigCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  28.     {
  29.         $config = new ConfigModel();
  30.         $config
  31.             ->setName($event->getEventName())
  32.             ->setValue($event->getValue())
  33.             ->setLocale($event->getLocale())
  34.             ->setTitle($event->getTitle())
  35.             ->setHidden($event->getHidden())
  36.             ->setSecured($event->getSecured())
  37.         ->save();
  38.         $event->setConfig($config);
  39.     }
  40.     /**
  41.      * Change a configuration entry value.
  42.      *
  43.      * @param $eventName
  44.      */
  45.     public function setValue(ConfigUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  46.     {
  47.         if (null !== $config ConfigQuery::create()->findPk($event->getConfigId())) {
  48.             if ($event->getValue() !== $config->getValue()) {
  49.                 $config->setValue($event->getValue())->save();
  50.                 $event->setConfig($config);
  51.             }
  52.         }
  53.     }
  54.     /**
  55.      * Change a configuration entry.
  56.      *
  57.      * @param $eventName
  58.      */
  59.     public function modify(ConfigUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  60.     {
  61.         if (null !== $config ConfigQuery::create()->findPk($event->getConfigId())) {
  62.             $config
  63.                 ->setName($event->getEventName())
  64.                 ->setValue($event->getValue())
  65.                 ->setHidden($event->getHidden())
  66.                 ->setSecured($event->getSecured())
  67.                 ->setLocale($event->getLocale())
  68.                 ->setTitle($event->getTitle())
  69.                 ->setDescription($event->getDescription())
  70.                 ->setChapo($event->getChapo())
  71.                 ->setPostscriptum($event->getPostscriptum())
  72.             ->save();
  73.         }
  74.     }
  75.     /**
  76.      * Delete a configuration entry.
  77.      *
  78.      * @param $eventName
  79.      */
  80.     public function delete(ConfigDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  81.     {
  82.         if (null !== ($config ConfigQuery::create()->findPk($event->getConfigId()))) {
  83.             if (!$config->getSecured()) {
  84.                 $config->delete();
  85.                 $event->setConfig($config);
  86.             }
  87.         }
  88.     }
  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     public static function getSubscribedEvents()
  93.     {
  94.         return [
  95.                 TheliaEvents::CONFIG_CREATE => [
  96.                     'create'128,
  97.                 ], TheliaEvents::CONFIG_SETVALUE => [
  98.                     'setValue'128,
  99.                 ], TheliaEvents::CONFIG_UPDATE => [
  100.                     'modify'128,
  101.                 ], TheliaEvents::CONFIG_DELETE => [
  102.                     'delete'128,
  103.                 ],
  104.         ];
  105.     }
  106. }