vendor/thelia/core/lib/Thelia/Action/Lang.php line 146

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 Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Thelia\Core\Event\Lang\LangCreateEvent;
  17. use Thelia\Core\Event\Lang\LangDefaultBehaviorEvent;
  18. use Thelia\Core\Event\Lang\LangDeleteEvent;
  19. use Thelia\Core\Event\Lang\LangToggleActiveEvent;
  20. use Thelia\Core\Event\Lang\LangToggleDefaultEvent;
  21. use Thelia\Core\Event\Lang\LangToggleVisibleEvent;
  22. use Thelia\Core\Event\Lang\LangUpdateEvent;
  23. use Thelia\Core\Event\TheliaEvents;
  24. use Thelia\Core\HttpFoundation\Session\Session;
  25. use Thelia\Core\Template\Exception\TemplateException;
  26. use Thelia\Core\Template\TemplateHelperInterface;
  27. use Thelia\Core\Translation\Translator;
  28. use Thelia\Form\Lang\LangUrlEvent;
  29. use Thelia\Model\ConfigQuery;
  30. use Thelia\Model\Event\LangEvent;
  31. use Thelia\Model\Lang as LangModel;
  32. use Thelia\Model\LangQuery;
  33. /**
  34.  * Class Lang.
  35.  *
  36.  * @author Manuel Raynaud <manu@raynaud.io>
  37.  */
  38. class Lang extends BaseAction implements EventSubscriberInterface
  39. {
  40.     /** @var TemplateHelperInterface */
  41.     protected $templateHelper;
  42.     /** @var RequestStack */
  43.     protected $requestStack;
  44.     public function __construct(TemplateHelperInterface $templateHelperRequestStack $requestStack)
  45.     {
  46.         $this->templateHelper $templateHelper;
  47.         $this->requestStack $requestStack;
  48.     }
  49.     /**
  50.      * @param $eventName
  51.      *
  52.      * @throws \Propel\Runtime\Exception\PropelException
  53.      */
  54.     public function update(LangUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  55.     {
  56.         if (null !== $lang LangQuery::create()->findPk($event->getId())) {
  57.             $lang->setTitle($event->getTitle())
  58.                 ->setLocale($event->getLocale())
  59.                 ->setCode($event->getCode())
  60.                 ->setDateTimeFormat($event->getDateTimeFormat())
  61.                 ->setDateFormat($event->getDateFormat())
  62.                 ->setTimeFormat($event->getTimeFormat())
  63.                 ->setDecimalSeparator($event->getDecimalSeparator())
  64.                 ->setThousandsSeparator($event->getThousandsSeparator())
  65.                 ->setDecimals($event->getDecimals())
  66.                 ->save();
  67.             $event->setLang($lang);
  68.         }
  69.     }
  70.     /**
  71.      * @param $eventName
  72.      *
  73.      * @throws \Propel\Runtime\Exception\PropelException
  74.      */
  75.     public function toggleDefault(LangToggleDefaultEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  76.     {
  77.         if (null !== $lang LangQuery::create()->findPk($event->getLangId())) {
  78.             $lang->toggleDefault();
  79.             $event->setLang($lang);
  80.         }
  81.     }
  82.     /**
  83.      * @throws \Propel\Runtime\Exception\PropelException
  84.      */
  85.     public function toggleActive(LangToggleActiveEvent $event): void
  86.     {
  87.         if (null !== $lang LangQuery::create()->findPk($event->getLangId())) {
  88.             if ($lang->getByDefault()) {
  89.                 throw new \RuntimeException(
  90.                     Translator::getInstance()->trans('Cannot disable the default language')
  91.                 );
  92.             }
  93.             $lang->setActive($lang->getActive() ? 1);
  94.             if (!$lang->getActive()) {
  95.                 $lang->setVisible(0);
  96.             }
  97.             $lang->save();
  98.             $event->setLang($lang);
  99.         }
  100.     }
  101.     /**
  102.      * @throws \Propel\Runtime\Exception\PropelException
  103.      */
  104.     public function toggleVisible(LangToggleVisibleEvent $event): void
  105.     {
  106.         if (null !== $lang LangQuery::create()->findPk($event->getLangId())) {
  107.             if ($lang->getByDefault()) {
  108.                 throw new \RuntimeException(
  109.                     Translator::getInstance()->trans('Cannot hide the default language')
  110.                 );
  111.             }
  112.             $lang->setVisible($lang->getVisible() ? 1);
  113.             if (!$lang->getActive() && $lang->getVisible()) {
  114.                 $lang->setActive(1);
  115.             }
  116.             $lang->save();
  117.             $event->setLang($lang);
  118.         }
  119.     }
  120.     /**
  121.      * @param $eventName
  122.      *
  123.      * @throws \Propel\Runtime\Exception\PropelException
  124.      */
  125.     public function create(LangCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  126.     {
  127.         $lang = new LangModel();
  128.         $lang
  129.             ->setTitle($event->getTitle())
  130.             ->setCode($event->getCode())
  131.             ->setLocale($event->getLocale())
  132.             ->setDateTimeFormat($event->getDateTimeFormat())
  133.             ->setDateFormat($event->getDateFormat())
  134.             ->setTimeFormat($event->getTimeFormat())
  135.             ->setDecimalSeparator($event->getDecimalSeparator())
  136.             ->setThousandsSeparator($event->getThousandsSeparator())
  137.             ->setDecimals($event->getDecimals())
  138.             ->save();
  139.         $event->setLang($lang);
  140.     }
  141.     /**
  142.      * @param $eventName
  143.      *
  144.      * @throws \Propel\Runtime\Exception\PropelException
  145.      */
  146.     public function delete(LangDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  147.     {
  148.         if (null !== $lang LangQuery::create()->findPk($event->getLangId())) {
  149.             if ($lang->getByDefault()) {
  150.                 throw new \RuntimeException(
  151.                     Translator::getInstance()->trans('It is not allowed to delete the default language')
  152.                 );
  153.             }
  154.             $lang
  155.                 ->delete();
  156.             /** @var Session $session */
  157.             $session $this->requestStack->getCurrentRequest()->getSession();
  158.             // If we've just deleted the current admin edition language, set it to the default one.
  159.             if ($lang->getId() == $session->getAdminEditionLang()->getId()) {
  160.                 $session->setAdminEditionLang(LangModel::getDefaultLanguage());
  161.             }
  162.             // If we've just deleted the current admin language, set it to the default one.
  163.             if ($lang->getId() == $session->getLang()->getId()) {
  164.                 $session->setLang(LangModel::getDefaultLanguage());
  165.             }
  166.             $event->setLang($lang);
  167.         }
  168.     }
  169.     /**
  170.      * @throws \Exception
  171.      * @throws \Propel\Runtime\Exception\PropelException
  172.      */
  173.     public function defaultBehavior(LangDefaultBehaviorEvent $event): void
  174.     {
  175.         ConfigQuery::create()
  176.             ->filterByName('default_lang_without_translation')
  177.             ->update(['Value' => $event->getDefaultBehavior()]);
  178.     }
  179.     /**
  180.      * @throws \Exception
  181.      * @throws \Propel\Runtime\Exception\PropelException
  182.      */
  183.     public function langUrl(LangUrlEvent $event): void
  184.     {
  185.         foreach ($event->getUrl() as $id => $url) {
  186.             LangQuery::create()
  187.                 ->filterById($id)
  188.                 ->update(['Url' => $url]);
  189.         }
  190.     }
  191.     public function fixMissingFlag(LangEvent $event): void
  192.     {
  193.         // Be sure that a lang have a flag, otherwise copy the
  194.         // "unknown" flag
  195.         $adminTemplate $this->templateHelper->getActiveAdminTemplate();
  196.         $unknownFlag ConfigQuery::getUnknownFlagPath();
  197.         try {
  198.             $unknownFlagPath $adminTemplate->getTemplateFilePath($unknownFlag);
  199.             // Check if the country flag exists
  200.             $countryFlag rtrim(\dirname($unknownFlagPath), DS).DS.$event->getModel()->getCode().'.png';
  201.             if (!file_exists($countryFlag)) {
  202.                 $fs = new Filesystem();
  203.                 $fs->copy($unknownFlagPath$countryFlag);
  204.             }
  205.         } catch (TemplateException $ex) {
  206.             throw new \RuntimeException(
  207.                 Translator::getInstance()->trans(
  208.                     'The image which replaces an undefined country flag (%file) was not found. Please check unknown-flag-path configuration variable, and check that the image exists.',
  209.                     ['%file' => $unknownFlag]
  210.                 ),
  211.                 0,
  212.                 $ex
  213.             );
  214.         }
  215.     }
  216.     /**
  217.      * {@inheritdoc}
  218.      */
  219.     public static function getSubscribedEvents()
  220.     {
  221.         return [
  222.             TheliaEvents::LANG_UPDATE => ['update'128],
  223.             TheliaEvents::LANG_TOGGLEDEFAULT => ['toggleDefault'128],
  224.             TheliaEvents::LANG_TOGGLEACTIVE => ['toggleActive'128],
  225.             TheliaEvents::LANG_TOGGLEVISIBLE => ['toggleVisible'128],
  226.             TheliaEvents::LANG_CREATE => ['create'128],
  227.             TheliaEvents::LANG_DELETE => ['delete'128],
  228.             TheliaEvents::LANG_DEFAULTBEHAVIOR => ['defaultBehavior'128],
  229.             TheliaEvents::LANG_URL => ['langUrl'128],
  230.             LangEvent::POST_INSERT => ['fixMissingFlag'128],
  231.             LangEvent::POST_UPDATE => ['fixMissingFlag'128],
  232.         ];
  233.     }
  234. }