vendor/pimcore/pimcore/models/DataObject/Classificationstore/GroupConfig.php line 78

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\GroupConfigEvent;
  18. use Pimcore\Model;
  19. /**
  20. * @method \Pimcore\Model\DataObject\Classificationstore\GroupConfig\Dao getDao()
  21. */
  22. final class GroupConfig extends Model\AbstractModel
  23. {
  24. use Model\Element\ChildsCompatibilityTrait;
  25. /**
  26. * @var int
  27. */
  28. protected $id;
  29. /**
  30. * Store ID
  31. *
  32. * @var int
  33. */
  34. protected $storeId = 1;
  35. /**
  36. * Parent id
  37. *
  38. * @var int
  39. */
  40. protected $parentId;
  41. /**
  42. * The group name.
  43. *
  44. * @var string
  45. */
  46. protected $name;
  47. /**
  48. * The group description.
  49. *
  50. * @var string
  51. */
  52. protected $description;
  53. /**
  54. * @var int
  55. */
  56. protected $creationDate;
  57. /**
  58. * @var int
  59. */
  60. protected $modificationDate;
  61. /**
  62. * @param int $id
  63. *
  64. * @return self|null
  65. */
  66. public static function getById($id)
  67. {
  68. try {
  69. $cacheKey = 'cs_groupconfig_' . $id;
  70. if (Cache\Runtime::isRegistered($cacheKey)) {
  71. $config = Cache\Runtime::get($cacheKey);
  72. if ($config) {
  73. return $config;
  74. }
  75. }
  76. if (!$config = Cache::load($cacheKey)) {
  77. $config = new self();
  78. $config->getDao()->getById((int)$id);
  79. Cache::save($config, $cacheKey, [], null, 0, true);
  80. }
  81. Cache\Runtime::set($cacheKey, $config);
  82. return $config;
  83. } catch (\Exception $e) {
  84. return null;
  85. }
  86. }
  87. /**
  88. * @param string $name
  89. * @param int $storeId
  90. *
  91. * @return self|null
  92. *
  93. * @throws \Exception
  94. */
  95. public static function getByName($name, $storeId = 1)
  96. {
  97. try {
  98. $config = new self();
  99. $config->setName($name);
  100. $config->setStoreId($storeId ? $storeId : 1);
  101. $config->getDao()->getByName();
  102. return $config;
  103. } catch (Model\Exception\NotFoundException $e) {
  104. return null;
  105. }
  106. }
  107. /**
  108. * @return int
  109. */
  110. public function hasChildren()
  111. {
  112. return $this->getDao()->hasChildren();
  113. }
  114. /**
  115. * @return Model\DataObject\Classificationstore\GroupConfig
  116. */
  117. public static function create()
  118. {
  119. $config = new self();
  120. $config->save();
  121. return $config;
  122. }
  123. /**
  124. * @param int $id
  125. *
  126. * @return $this
  127. */
  128. public function setId($id)
  129. {
  130. $this->id = (int) $id;
  131. return $this;
  132. }
  133. /**
  134. * @return int
  135. */
  136. public function getId()
  137. {
  138. return $this->id;
  139. }
  140. /**
  141. * @return int
  142. */
  143. public function getParentId()
  144. {
  145. return $this->parentId;
  146. }
  147. /**
  148. * @param int $parentId
  149. */
  150. public function setParentId($parentId)
  151. {
  152. $this->parentId = $parentId;
  153. }
  154. /**
  155. * @param string $name
  156. *
  157. * @return self
  158. */
  159. public function setName($name)
  160. {
  161. $this->name = $name;
  162. return $this;
  163. }
  164. /**
  165. * @return string
  166. */
  167. public function getName()
  168. {
  169. return $this->name;
  170. }
  171. /**
  172. * Returns the description.
  173. *
  174. * @return string
  175. */
  176. public function getDescription()
  177. {
  178. return $this->description;
  179. }
  180. /**
  181. * Sets the description.
  182. *
  183. * @param string $description
  184. *
  185. * @return Model\DataObject\Classificationstore\GroupConfig
  186. */
  187. public function setDescription($description)
  188. {
  189. $this->description = $description;
  190. return $this;
  191. }
  192. /**
  193. * Deletes the key value group configuration
  194. */
  195. public function delete()
  196. {
  197. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_PRE_DELETE);
  198. $cacheKey = 'cs_groupconfig_' . $this->getId();
  199. Cache\Runtime::set($cacheKey, null);
  200. Cache::remove($cacheKey);
  201. $this->getDao()->delete();
  202. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_POST_DELETE);
  203. }
  204. /**
  205. * Saves the group config
  206. */
  207. public function save()
  208. {
  209. $isUpdate = false;
  210. if ($this->getId()) {
  211. $cacheKey = 'cs_groupconfig_' . $this->getId();
  212. Cache\Runtime::set($cacheKey, null);
  213. Cache::remove($cacheKey);
  214. $isUpdate = true;
  215. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_PRE_UPDATE);
  216. } else {
  217. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_PRE_ADD);
  218. }
  219. $model = $this->getDao()->save();
  220. if ($isUpdate) {
  221. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_POST_UPDATE);
  222. } else {
  223. \Pimcore::getEventDispatcher()->dispatch(new GroupConfigEvent($this), DataObjectClassificationStoreEvents::GROUP_CONFIG_POST_ADD);
  224. }
  225. return $model;
  226. }
  227. /**
  228. * @param int $modificationDate
  229. *
  230. * @return $this
  231. */
  232. public function setModificationDate($modificationDate)
  233. {
  234. $this->modificationDate = (int) $modificationDate;
  235. return $this;
  236. }
  237. /**
  238. * @return int
  239. */
  240. public function getModificationDate()
  241. {
  242. return $this->modificationDate;
  243. }
  244. /**
  245. * @param int $creationDate
  246. *
  247. * @return $this
  248. */
  249. public function setCreationDate($creationDate)
  250. {
  251. $this->creationDate = (int) $creationDate;
  252. return $this;
  253. }
  254. /**
  255. * @return int
  256. */
  257. public function getCreationDate()
  258. {
  259. return $this->creationDate;
  260. }
  261. /**
  262. * Returns all keys belonging to this group
  263. *
  264. * @return KeyGroupRelation[]
  265. */
  266. public function getRelations()
  267. {
  268. $list = new KeyGroupRelation\Listing();
  269. $list->setCondition('groupId = ' . $this->id);
  270. $list = $list->load();
  271. return $list;
  272. }
  273. /**
  274. * @return int
  275. */
  276. public function getStoreId()
  277. {
  278. return $this->storeId;
  279. }
  280. /**
  281. * @param int $storeId
  282. */
  283. public function setStoreId($storeId)
  284. {
  285. $this->storeId = $storeId;
  286. }
  287. }