classes/XLite/Model/Language.php line 29

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Includes\Utils\Module\Module;
  9. use XCart\Domain\ModuleManagerDomain;
  10. /**
  11.  * Language
  12.  *
  13.  * @ORM\Entity
  14.  * @ORM\Table (name="languages",
  15.  *      uniqueConstraints={
  16.  *          @ORM\UniqueConstraint (name="code3", columns={"code3"}),
  17.  *          @ORM\UniqueConstraint (name="code2", columns={"code"})
  18.  *      },
  19.  *      indexes={
  20.  *          @ORM\Index (name="added", columns={"added"}),
  21.  *          @ORM\Index (name="enabled", columns={"enabled"})
  22.  *      }
  23.  * )
  24.  */
  25. class Language extends \XLite\Model\Base\I18n
  26. {
  27.     /**
  28.      * Unique id
  29.      *
  30.      * @var integer
  31.      *
  32.      * @ORM\Id
  33.      * @ORM\GeneratedValue (strategy="AUTO")
  34.      * @ORM\Column (type="integer", unique=true)
  35.      */
  36.     protected $lng_id;
  37.     /**
  38.      * Language alpha-2 code (ISO 639-2)
  39.      *
  40.      * @var string
  41.      *
  42.      * @ORM\Column (type="string", options={ "fixed": true }, length=2, unique=true)
  43.      */
  44.     protected $code;
  45.     /**
  46.      * Language alpha-3 code (ISO 639-3)
  47.      *
  48.      * @var string
  49.      *
  50.      * @ORM\Column (type="string", options={ "fixed": true }, length=3, unique=true)
  51.      */
  52.     protected $code3 '';
  53.     /**
  54.      * Right-to-left flag
  55.      *
  56.      * @var boolean
  57.      *
  58.      * @ORM\Column (type="boolean")
  59.      */
  60.     protected $r2l false;
  61.     /**
  62.      * Language status (added/not added)
  63.      *
  64.      * @var integer
  65.      *
  66.      * @ORM\Column (type="boolean")
  67.      */
  68.     protected $added false;
  69.     /**
  70.      * Language state (enabled/disabled)
  71.      *
  72.      * @var integer
  73.      *
  74.      * @ORM\Column (type="boolean")
  75.      */
  76.     protected $enabled false;
  77.     /**
  78.      * Related module (Author\Name)
  79.      *
  80.      * @var string
  81.      *
  82.      * @ORM\Column (type="string", nullable=true)
  83.      */
  84.     protected $module;
  85.     /**
  86.      * Countries
  87.      *
  88.      * @var \XLite\Model\Country[]
  89.      *
  90.      * @ORM\OneToMany (targetEntity="XLite\Model\Country", mappedBy="language")
  91.      */
  92.     protected $countries;
  93.     /**
  94.      * @var \Doctrine\Common\Collections\Collection
  95.      *
  96.      * @ORM\OneToMany (targetEntity="XLite\Model\LanguageTranslation", mappedBy="owner", cascade={"all"})
  97.      */
  98.     protected $translations;
  99.     /**
  100.      * Get module
  101.      *
  102.      * @return string
  103.      */
  104.     public function getValidModule()
  105.     {
  106.         /** @var ModuleManagerDomain $moduleManagerDomain */
  107.         $moduleManagerDomain \XCart\Container::getContainer()?->get(ModuleManagerDomain::class);
  108.         if ($module $this->getModule()) {
  109.             [$author$name] = Module::explodeModuleId($module);
  110.             return $moduleManagerDomain->isEnabled("{$author}-{$name}") ? $module '';
  111.         }
  112.         return '';
  113.     }
  114.     /**
  115.      * Return true if current language is set as a default for customer interface
  116.      *
  117.      * @return boolean
  118.      */
  119.     public function getDefaultCustomer()
  120.     {
  121.         return $this->getCode() == \XLite\Core\Config::getInstance()->General->default_language;
  122.     }
  123.     /**
  124.      * Return true if current language is set as a default for admin interface
  125.      *
  126.      * @return boolean
  127.      */
  128.     public function getDefaultAdmin()
  129.     {
  130.         return $this->getCode() == \XLite\Core\Config::getInstance()->General->default_admin_language;
  131.     }
  132.     /**
  133.      * Return true if current language is set as a default for customer interface
  134.      *
  135.      * @return boolean
  136.      */
  137.     public function getDefaultAuth()
  138.     {
  139.         return (!\XLite\Core\Auth::getInstance()->isAdmin() && $this->getDefaultCustomer())
  140.         || (\XLite\Core\Auth::getInstance()->isAdmin() && $this->getDefaultAdmin());
  141.     }
  142.     /**
  143.      * Get flag URL
  144.      *
  145.      * @return string
  146.      */
  147.     public function getFlagURL()
  148.     {
  149.         return \XLite\Core\Layout::getInstance()->getResourceWebPath(
  150.             $this->getFlagFile(),
  151.             \XLite\Core\Layout::WEB_PATH_OUTPUT_URL,
  152.             \XLite::INTERFACE_WEB,
  153.             \XLite::ZONE_CUSTOMER
  154.         );
  155.     }
  156.     /**
  157.      * Get flag URL
  158.      *
  159.      * @return string
  160.      */
  161.     public function getFlagFile()
  162.     {
  163.         $code $this->getLanguageToFlagCodeMapping()[$this->getCode()] ?? $this->getCode();
  164.         $file "images/flags_svg/{$code}.svg";
  165.         $path \XLite\Core\Layout::getInstance()->getResourceFullPath(
  166.             $file,
  167.             \XLite::INTERFACE_WEB,
  168.             \XLite::ZONE_COMMON
  169.         );
  170.         return $path
  171.             $file
  172.             'images/flags_svg/__.svg';
  173.     }
  174.     /**
  175.      * @return array
  176.      */
  177.     protected function getLanguageToFlagCodeMapping()
  178.     {
  179.         return [
  180.             'be' => 'by',
  181.             'ja' => 'jp',
  182.             'ko' => 'kr',
  183.             'hy' => 'am',
  184.             'ar' => 'sa',
  185.             'el' => 'gr',
  186.             'he' => 'il',
  187.             'kr' => 'ng',
  188.             'sv' => 'se',
  189.             'sl' => 'si',
  190.             'si' => 'lk',
  191.             'et' => 'ee',
  192.             'ee' => 'gh',
  193.             'aa' => 'et',
  194.             'km' => 'kh',
  195.             'ms' => 'my',
  196.             'my' => 'mm',
  197.         ];
  198.     }
  199.     /**
  200.      * Remove all label translations to the language
  201.      * Return true on success
  202.      *
  203.      * @return boolean
  204.      */
  205.     public function removeTranslations()
  206.     {
  207.         return \XLite\Core\Database::getRepo('XLite\Model\LanguageLabel')->deleteTranslations($this->getCode());
  208.     }
  209.     /**
  210.      * Get default language code
  211.      *
  212.      * @return string
  213.      */
  214.     protected function getSessionLanguageCode()
  215.     {
  216.         return $this->getCode();
  217.     }
  218.     /**
  219.      * Get lng_id
  220.      *
  221.      * @return integer
  222.      */
  223.     public function getLngId()
  224.     {
  225.         return $this->lng_id;
  226.     }
  227.     /**
  228.      * Set code
  229.      *
  230.      * @param string $code
  231.      * @return Language
  232.      */
  233.     public function setCode($code)
  234.     {
  235.         $this->code $code;
  236.         return $this;
  237.     }
  238.     /**
  239.      * Get code
  240.      *
  241.      * @return string
  242.      */
  243.     public function getCode()
  244.     {
  245.         return $this->code;
  246.     }
  247.     /**
  248.      * Set code3
  249.      *
  250.      * @param string $code3
  251.      * @return Language
  252.      */
  253.     public function setCode3($code3)
  254.     {
  255.         $this->code3 $code3;
  256.         return $this;
  257.     }
  258.     /**
  259.      * Get code3
  260.      *
  261.      * @return string
  262.      */
  263.     public function getCode3()
  264.     {
  265.         return $this->code3;
  266.     }
  267.     /**
  268.      * Set r2l
  269.      *
  270.      * @param boolean $r2l
  271.      * @return Language
  272.      */
  273.     public function setR2l($r2l)
  274.     {
  275.         $this->r2l $r2l;
  276.         return $this;
  277.     }
  278.     /**
  279.      * Get r2l
  280.      *
  281.      * @return boolean
  282.      */
  283.     public function getR2l()
  284.     {
  285.         return $this->r2l;
  286.     }
  287.     /**
  288.      * Set added
  289.      *
  290.      * @param boolean $added
  291.      * @return Language
  292.      */
  293.     public function setAdded($added)
  294.     {
  295.         $this->added $added;
  296.         return $this;
  297.     }
  298.     /**
  299.      * Get added
  300.      *
  301.      * @return boolean
  302.      */
  303.     public function getAdded()
  304.     {
  305.         return $this->added;
  306.     }
  307.     /**
  308.      * Set enabled
  309.      *
  310.      * @param boolean $enabled
  311.      * @return Language
  312.      */
  313.     public function setEnabled($enabled)
  314.     {
  315.         $this->enabled = (bool)$enabled;
  316.         return $this;
  317.     }
  318.     /**
  319.      * Get enabled
  320.      *
  321.      * @return boolean
  322.      */
  323.     public function getEnabled()
  324.     {
  325.         return $this->enabled;
  326.     }
  327.     /**
  328.      * Set module
  329.      *
  330.      * @param string $module
  331.      * @return Language
  332.      */
  333.     public function setModule($module)
  334.     {
  335.         $this->module $module;
  336.         return $this;
  337.     }
  338.     /**
  339.      * Get module
  340.      *
  341.      * @return string
  342.      */
  343.     public function getModule()
  344.     {
  345.         return $this->module;
  346.     }
  347.     /**
  348.      * Set Countries
  349.      *
  350.      * @param \XLite\Model\Country[] $countries
  351.      *
  352.      * @return $this
  353.      */
  354.     public function setCountries($countries)
  355.     {
  356.         foreach ($countries as $country) {
  357.             $country->setLanguage($this);
  358.         }
  359.         $this->countries $countries;
  360.         return $this;
  361.     }
  362.     /**
  363.      * Get countries
  364.      *
  365.      * @return \Doctrine\Common\Collections\Collection
  366.      */
  367.     public function getCountries()
  368.     {
  369.         return $this->countries;
  370.     }
  371. }