core/lib/Thelia/Action/Config.php line 62

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