classes/XLite/Model/Language.php line 28

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