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