vendor/pimcore/pimcore/models/WebsiteSetting.php line 124

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model;
  15. use Pimcore\Model\Element\ElementInterface;
  16. use Pimcore\Model\Element\Service;
  17. use Pimcore\Model\Exception\NotFoundException;
  18. /**
  19.  * @method \Pimcore\Model\WebsiteSetting\Dao getDao()
  20.  * @method void save()
  21.  */
  22. final class WebsiteSetting extends AbstractModel
  23. {
  24.     /**
  25.      * @var int|null
  26.      */
  27.     protected $id;
  28.     /**
  29.      * @var string
  30.      */
  31.     protected $name;
  32.     /**
  33.      * @var string
  34.      */
  35.     protected $language;
  36.     /**
  37.      * @var string
  38.      */
  39.     protected $type;
  40.     /**
  41.      * @var mixed
  42.      */
  43.     protected $data;
  44.     /**
  45.      * @var int|null
  46.      */
  47.     protected $siteId;
  48.     /**
  49.      * @var int|null
  50.      */
  51.     protected $creationDate;
  52.     /**
  53.      * @var int|null
  54.      */
  55.     protected $modificationDate;
  56.     /**
  57.      * this is a small per request cache to know which website setting is which is, this info is used in self::getByName()
  58.      *
  59.      * @var array
  60.      */
  61.     protected static $nameIdMappingCache = [];
  62.     /**
  63.      * @param string $name
  64.      * @param int|null $siteId
  65.      * @param string|null $language
  66.      *
  67.      * @return string
  68.      */
  69.     protected static function getCacheKey($name$siteId null$language null): string
  70.     {
  71.         return $name '~~~' $siteId '~~~' $language;
  72.     }
  73.     /**
  74.      * @param int $id
  75.      *
  76.      * @return self|null
  77.      */
  78.     public static function getById($id)
  79.     {
  80.         $cacheKey 'website_setting_' $id;
  81.         try {
  82.             $setting \Pimcore\Cache\RuntimeCache::get($cacheKey);
  83.             if (!$setting) {
  84.                 throw new \Exception('Website setting in registry is null');
  85.             }
  86.         } catch (\Exception $e) {
  87.             try {
  88.                 $setting = new self();
  89.                 $setting->getDao()->getById((int)$id);
  90.                 \Pimcore\Cache\RuntimeCache::set($cacheKey$setting);
  91.             } catch (NotFoundException $e) {
  92.                 return null;
  93.             }
  94.         }
  95.         return $setting;
  96.     }
  97.     /**
  98.      * @param string $name name of the config
  99.      * @param int|null $siteId site ID
  100.      * @param string|null $language language, if property cannot be found the value of property without language is returned
  101.      * @param string|null $fallbackLanguage fallback language
  102.      *
  103.      * @return WebsiteSetting|null
  104.      *
  105.      * @throws \Exception
  106.      */
  107.     public static function getByName($name$siteId null$language null$fallbackLanguage null)
  108.     {
  109.         $nameCacheKey = static::getCacheKey($name$siteId$language);
  110.         // check if pimcore already knows the id for this $name, if yes just return it
  111.         if (array_key_exists($nameCacheKeyself::$nameIdMappingCache)) {
  112.             return self::getById(self::$nameIdMappingCache[$nameCacheKey]);
  113.         }
  114.         // create a tmp object to obtain the id
  115.         $setting = new self();
  116.         try {
  117.             $setting->getDao()->getByName($name$siteId$language);
  118.         } catch (NotFoundException $e) {
  119.             if ($language != $fallbackLanguage) {
  120.                 $result self::getByName($name$siteId$fallbackLanguage$fallbackLanguage);
  121.                 return $result;
  122.             }
  123.             return null;
  124.         }
  125.         // to have a singleton in a way. like all instances of Element\ElementInterface do also, like DataObject\AbstractObject
  126.         if ($setting->getId() > 0) {
  127.             // add it to the mini-per request cache
  128.             self::$nameIdMappingCache[$nameCacheKey] = $setting->getId();
  129.             return self::getById($setting->getId());
  130.         }
  131.         return $setting;
  132.     }
  133.     /**
  134.      * @return int|null
  135.      */
  136.     public function getId()
  137.     {
  138.         return $this->id;
  139.     }
  140.     /**
  141.      * @param int $id
  142.      *
  143.      * @return $this
  144.      */
  145.     public function setId($id)
  146.     {
  147.         $this->id = (int) $id;
  148.         return $this;
  149.     }
  150.     /**
  151.      * @param string $name
  152.      *
  153.      * @return $this
  154.      */
  155.     public function setName($name)
  156.     {
  157.         $this->name $name;
  158.         return $this;
  159.     }
  160.     /**
  161.      * @return string
  162.      */
  163.     public function getName()
  164.     {
  165.         return $this->name;
  166.     }
  167.     /**
  168.      * @param int $creationDate
  169.      *
  170.      * @return $this
  171.      */
  172.     public function setCreationDate($creationDate)
  173.     {
  174.         $this->creationDate = (int) $creationDate;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @return int|null
  179.      */
  180.     public function getCreationDate()
  181.     {
  182.         return $this->creationDate;
  183.     }
  184.     /**
  185.      * @param mixed $data
  186.      *
  187.      * @return $this
  188.      */
  189.     public function setData($data)
  190.     {
  191.         if ($data instanceof ElementInterface) {
  192.             $this->setType(Service::getElementType($data));
  193.             $data $data->getId();
  194.         }
  195.         $this->data $data;
  196.         return $this;
  197.     }
  198.     /**
  199.      * @return mixed
  200.      */
  201.     public function getData()
  202.     {
  203.         // lazy-load data of type asset, document, object
  204.         if (in_array($this->getType(), ['document''asset''object']) && !$this->data instanceof ElementInterface && is_numeric($this->data)) {
  205.             return Element\Service::getElementById($this->getType(), $this->data);
  206.         }
  207.         return $this->data;
  208.     }
  209.     /**
  210.      * @param int $modificationDate
  211.      *
  212.      * @return $this
  213.      */
  214.     public function setModificationDate($modificationDate)
  215.     {
  216.         $this->modificationDate = (int) $modificationDate;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @return int|null
  221.      */
  222.     public function getModificationDate()
  223.     {
  224.         return $this->modificationDate;
  225.     }
  226.     /**
  227.      * @param int $siteId
  228.      *
  229.      * @return $this
  230.      */
  231.     public function setSiteId($siteId)
  232.     {
  233.         $this->siteId = (int) $siteId;
  234.         return $this;
  235.     }
  236.     /**
  237.      * @return int|null
  238.      */
  239.     public function getSiteId()
  240.     {
  241.         return $this->siteId;
  242.     }
  243.     /**
  244.      * enum('text','document','asset','object','bool')
  245.      *
  246.      * @param string $type
  247.      *
  248.      * @return $this
  249.      */
  250.     public function setType($type)
  251.     {
  252.         $this->type $type;
  253.         return $this;
  254.     }
  255.     /**
  256.      * enum('text','document','asset','object','bool')
  257.      *
  258.      * @return string
  259.      */
  260.     public function getType()
  261.     {
  262.         return $this->type;
  263.     }
  264.     /**
  265.      * @return string
  266.      */
  267.     public function getLanguage()
  268.     {
  269.         return $this->language;
  270.     }
  271.     /**
  272.      * @param string $language
  273.      */
  274.     public function setLanguage($language)
  275.     {
  276.         $this->language $language;
  277.     }
  278.     /**
  279.      * @internal
  280.      */
  281.     public function clearDependentCache()
  282.     {
  283.         \Pimcore\Cache::clearTag('website_config');
  284.     }
  285.     public function delete(): void
  286.     {
  287.         $nameCacheKey self::getCacheKey($this->getName(), $this->getSiteId(), $this->getLanguage());
  288.         // Remove cached element to avoid returning it with e.g. getByName() after if it is deleted
  289.         if (array_key_exists($nameCacheKeyself::$nameIdMappingCache)) {
  290.             unset(self::$nameIdMappingCache[$nameCacheKey]);
  291.         }
  292.         $this->getDao()->delete();
  293.     }
  294. }