vendor/pimcore/pimcore/models/DataObject/Classificationstore/KeyConfig.php line 97

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\DataObject\Classificationstore;
  15. use Pimcore\Cache;
  16. use Pimcore\Event\DataObjectClassificationStoreEvents;
  17. use Pimcore\Event\Model\DataObject\ClassificationStore\KeyConfigEvent;
  18. use Pimcore\Model;
  19. /**
  20. * @method \Pimcore\Model\DataObject\Classificationstore\KeyConfig\Dao getDao()
  21. */
  22. final class KeyConfig extends Model\AbstractModel
  23. {
  24. /**
  25. * @var array
  26. */
  27. public static $cache = [];
  28. /**
  29. * @var bool
  30. */
  31. public static $cacheEnabled = false;
  32. /**
  33. * @var int
  34. */
  35. protected $id;
  36. /**
  37. * Store ID
  38. *
  39. * @var int
  40. */
  41. protected $storeId = 1;
  42. /** The key
  43. * @var string
  44. */
  45. protected $name;
  46. /** Pseudo column for title
  47. * @var string|null
  48. */
  49. protected $title;
  50. /**
  51. * The key description.
  52. *
  53. * @var string
  54. */
  55. protected $description;
  56. /**
  57. * The key type ("text", "number", etc...)
  58. *
  59. * @var string
  60. */
  61. protected $type;
  62. /**
  63. * @var int
  64. */
  65. protected $creationDate;
  66. /**
  67. * @var int
  68. */
  69. protected $modificationDate;
  70. /**
  71. * @var string
  72. */
  73. protected $definition;
  74. /** @var bool */
  75. protected $enabled;
  76. /**
  77. * @param int $id
  78. *
  79. * @return self|null
  80. */
  81. public static function getById($id)
  82. {
  83. try {
  84. $id = (int)$id;
  85. if (self::$cacheEnabled && self::$cache[$id]) {
  86. return self::$cache[$id];
  87. }
  88. $cacheKey = 'cs_keyconfig_' . $id;
  89. $config = Cache::load($cacheKey);
  90. if ($config) {
  91. return $config;
  92. }
  93. $config = new self();
  94. $config->getDao()->getById($id);
  95. if (self::$cacheEnabled) {
  96. self::$cache[$id] = $config;
  97. }
  98. Cache::save($config, $cacheKey, [], null, 0, true);
  99. return $config;
  100. } catch (\Exception $e) {
  101. return null;
  102. }
  103. }
  104. /**
  105. * @param bool $cacheEnabled
  106. */
  107. public static function setCacheEnabled($cacheEnabled)
  108. {
  109. self::$cacheEnabled = $cacheEnabled;
  110. if (!$cacheEnabled) {
  111. self::$cache = [];
  112. }
  113. }
  114. /**
  115. * @return bool
  116. */
  117. public static function getCacheEnabled()
  118. {
  119. return self::$cacheEnabled;
  120. }
  121. /**
  122. * @param string $name
  123. * @param int $storeId
  124. *
  125. * @return self|null
  126. *
  127. * @throws \Exception
  128. */
  129. public static function getByName($name, $storeId = 1)
  130. {
  131. try {
  132. $cacheKey = 'cs_keyconfig_' . $storeId . '_' . md5($name);
  133. if (self::$cacheEnabled && Cache\Runtime::isRegistered($cacheKey)) {
  134. $config = Cache\Runtime::get($cacheKey);
  135. if ($config) {
  136. return $config;
  137. }
  138. }
  139. $config = Cache::load($cacheKey);
  140. if ($config) {
  141. return $config;
  142. }
  143. $config = new self();
  144. $config->setName($name);
  145. $config->setStoreId($storeId ? $storeId : 1);
  146. $config->getDao()->getByName();
  147. if (self::$cacheEnabled) {
  148. Cache\Runtime::set($cacheKey, $config);
  149. }
  150. Cache::save($config, $cacheKey, [], null, 0, true);
  151. return $config;
  152. } catch (Model\Exception\NotFoundException $e) {
  153. return null;
  154. }
  155. }
  156. /**
  157. * @return Model\DataObject\Classificationstore\KeyConfig
  158. */
  159. public static function create()
  160. {
  161. $config = new self();
  162. $config->save();
  163. return $config;
  164. }
  165. /**
  166. * @param int $id
  167. *
  168. * @return $this
  169. */
  170. public function setId($id)
  171. {
  172. $this->id = (int) $id;
  173. return $this;
  174. }
  175. /**
  176. * @return int
  177. */
  178. public function getId()
  179. {
  180. return $this->id;
  181. }
  182. /**
  183. * @param string $name
  184. *
  185. * @return $this
  186. */
  187. public function setName($name)
  188. {
  189. $this->name = $name;
  190. return $this;
  191. }
  192. /**
  193. * @return string
  194. */
  195. public function getName()
  196. {
  197. return $this->name;
  198. }
  199. /**
  200. * Returns the key description.
  201. *
  202. * @return string
  203. */
  204. public function getDescription()
  205. {
  206. return $this->description;
  207. }
  208. /**
  209. * Sets the key description
  210. *
  211. * @param string $description
  212. *
  213. * @return Model\DataObject\Classificationstore\KeyConfig
  214. */
  215. public function setDescription($description)
  216. {
  217. $this->description = $description;
  218. return $this;
  219. }
  220. /**
  221. * Deletes the key value key configuration
  222. */
  223. public function delete()
  224. {
  225. DefinitionCache::clear($this);
  226. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_DELETE);
  227. if ($this->getId()) {
  228. unset(self::$cache[$this->getId()]);
  229. $cacheKey = 'cs_keyconfig_' . $this->getId();
  230. Cache::remove($cacheKey);
  231. }
  232. $this->getDao()->delete();
  233. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_DELETE);
  234. }
  235. /**
  236. * Saves the key config
  237. */
  238. public function save()
  239. {
  240. DefinitionCache::clear($this);
  241. $isUpdate = false;
  242. $def = json_decode($this->definition, true);
  243. if ($def && isset($def['title'])) {
  244. $this->title = $def['title'];
  245. } else {
  246. $this->title = null;
  247. }
  248. if ($this->getId()) {
  249. unset(self::$cache[$this->getId()]);
  250. $cacheKey = 'cs_keyconfig_' . $this->getId();
  251. Cache::remove($cacheKey);
  252. $isUpdate = true;
  253. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_UPDATE);
  254. } else {
  255. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_ADD);
  256. }
  257. $this->getDao()->save();
  258. if ($isUpdate) {
  259. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_UPDATE);
  260. } else {
  261. \Pimcore::getEventDispatcher()->dispatch(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_ADD);
  262. }
  263. }
  264. /**
  265. * @return int
  266. */
  267. public function getCreationDate()
  268. {
  269. return $this->creationDate;
  270. }
  271. /**
  272. * @param int $creationDate
  273. */
  274. public function setCreationDate($creationDate)
  275. {
  276. $this->creationDate = $creationDate;
  277. }
  278. /**
  279. * @return int
  280. */
  281. public function getModificationDate()
  282. {
  283. return $this->modificationDate;
  284. }
  285. /**
  286. * @param int $modificationDate
  287. */
  288. public function setModificationDate($modificationDate)
  289. {
  290. $this->modificationDate = $modificationDate;
  291. }
  292. /**
  293. * @return string
  294. */
  295. public function getType()
  296. {
  297. return $this->type;
  298. }
  299. /**
  300. * @param string $type
  301. */
  302. public function setType($type)
  303. {
  304. $this->type = $type;
  305. }
  306. /**
  307. * @return string
  308. */
  309. public function getDefinition()
  310. {
  311. return $this->definition;
  312. }
  313. /**
  314. * @param string $definition
  315. */
  316. public function setDefinition($definition)
  317. {
  318. $this->definition = $definition;
  319. }
  320. /**
  321. * @return bool
  322. */
  323. public function getEnabled()
  324. {
  325. return $this->enabled;
  326. }
  327. /**
  328. * @param bool $enabled
  329. */
  330. public function setEnabled($enabled)
  331. {
  332. $this->enabled = $enabled;
  333. }
  334. /**
  335. * @return string
  336. */
  337. public function getTitle()
  338. {
  339. return $this->title;
  340. }
  341. /**
  342. * @param string $title
  343. */
  344. public function setTitle($title)
  345. {
  346. $this->title = $title;
  347. }
  348. /**
  349. * @return int
  350. */
  351. public function getStoreId()
  352. {
  353. return $this->storeId;
  354. }
  355. /**
  356. * @param int $storeId
  357. */
  358. public function setStoreId($storeId)
  359. {
  360. $this->storeId = $storeId;
  361. }
  362. }