vendor/pimcore/pimcore/models/Property.php line 25

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. /**
  18. * @method \Pimcore\Model\Property\Dao getDao()
  19. * @method void save()
  20. */
  21. final class Property extends AbstractModel
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name;
  27. /**
  28. * @var mixed
  29. */
  30. protected $data;
  31. /**
  32. * @var string
  33. */
  34. protected $type;
  35. /**
  36. * @var string
  37. */
  38. protected $ctype;
  39. /**
  40. * @var string|null
  41. */
  42. protected $cpath;
  43. /**
  44. * @var int
  45. */
  46. protected $cid;
  47. /**
  48. * @var bool
  49. */
  50. protected $inheritable;
  51. /**
  52. * @var bool
  53. */
  54. protected $inherited = false;
  55. /**
  56. * @internal
  57. *
  58. * @param mixed $data
  59. *
  60. * @return $this
  61. */
  62. public function setDataFromEditmode($data)
  63. {
  64. // IMPORTANT: if you use this method be sure that the type of the property is already set
  65. if (in_array($this->getType(), ['document', 'asset', 'object'])) {
  66. $el = Element\Service::getElementByPath($this->getType(), $data);
  67. $this->data = null;
  68. if ($el) {
  69. $this->data = $el->getId();
  70. }
  71. } elseif ($this->type == 'bool') {
  72. $this->data = false;
  73. if (!empty($data)) {
  74. $this->data = true;
  75. }
  76. } else {
  77. // plain text
  78. $this->data = $data;
  79. }
  80. return $this;
  81. }
  82. /**
  83. * @internal
  84. *
  85. * @param mixed $data
  86. *
  87. * @return $this
  88. */
  89. public function setDataFromResource($data)
  90. {
  91. // IMPORTANT: if you use this method be sure that the type of the property is already set
  92. // do not set data for object, asset and document here, this is loaded dynamically when calling $this->getData();
  93. if ($this->type == 'date') {
  94. $this->data = \Pimcore\Tool\Serialize::unserialize($data);
  95. } elseif ($this->type == 'bool') {
  96. $this->data = false;
  97. if (!empty($data)) {
  98. $this->data = true;
  99. }
  100. } else {
  101. // plain text
  102. $this->data = $data;
  103. }
  104. return $this;
  105. }
  106. /**
  107. * @return int
  108. */
  109. public function getCid()
  110. {
  111. return $this->cid;
  112. }
  113. /**
  114. * enum('document','asset','object')
  115. *
  116. * @return string
  117. */
  118. public function getCtype()
  119. {
  120. return $this->ctype;
  121. }
  122. /**
  123. * @return mixed
  124. */
  125. public function getData()
  126. {
  127. // lazy-load data of type asset, document, object
  128. if (in_array($this->getType(), ['document', 'asset', 'object']) && !$this->data instanceof ElementInterface && is_numeric($this->data)) {
  129. return Element\Service::getElementById($this->getType(), $this->data);
  130. }
  131. return $this->data;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getName()
  137. {
  138. return $this->name;
  139. }
  140. /**
  141. * enum('text','document','asset','object','bool','select')
  142. *
  143. * @return string
  144. */
  145. public function getType()
  146. {
  147. return $this->type;
  148. }
  149. /**
  150. * @param int $cid
  151. *
  152. * @return $this
  153. */
  154. public function setCid($cid)
  155. {
  156. $this->cid = (int) $cid;
  157. return $this;
  158. }
  159. /**
  160. * enum('document','asset','object')
  161. *
  162. * @param string $ctype
  163. *
  164. * @return $this
  165. */
  166. public function setCtype($ctype)
  167. {
  168. $this->ctype = $ctype;
  169. return $this;
  170. }
  171. /**
  172. * @param mixed $data
  173. *
  174. * @return $this
  175. */
  176. public function setData($data)
  177. {
  178. if ($data instanceof ElementInterface) {
  179. $this->setType(Service::getElementType($data));
  180. $data = $data->getId();
  181. }
  182. $this->data = $data;
  183. return $this;
  184. }
  185. /**
  186. * @param string $name
  187. *
  188. * @return $this
  189. */
  190. public function setName($name)
  191. {
  192. $this->name = $name;
  193. return $this;
  194. }
  195. /**
  196. * enum('text','document','asset','object','bool','select')
  197. *
  198. * @param string $type
  199. *
  200. * @return $this
  201. */
  202. public function setType($type)
  203. {
  204. $this->type = $type;
  205. return $this;
  206. }
  207. /**
  208. * @return string|null
  209. */
  210. public function getCpath()
  211. {
  212. return $this->cpath;
  213. }
  214. /**
  215. * @return bool
  216. */
  217. public function getInherited()
  218. {
  219. return $this->inherited;
  220. }
  221. /**
  222. * Alias for getInherited()
  223. *
  224. * @return bool
  225. */
  226. public function isInherited()
  227. {
  228. return $this->getInherited();
  229. }
  230. /**
  231. * @param string|null $cpath
  232. *
  233. * @return $this
  234. */
  235. public function setCpath($cpath)
  236. {
  237. $this->cpath = $cpath;
  238. return $this;
  239. }
  240. /**
  241. * @param bool $inherited
  242. *
  243. * @return $this
  244. */
  245. public function setInherited($inherited)
  246. {
  247. $this->inherited = (bool) $inherited;
  248. return $this;
  249. }
  250. /**
  251. * @return bool
  252. */
  253. public function getInheritable()
  254. {
  255. return $this->inheritable;
  256. }
  257. /**
  258. * @param bool $inheritable
  259. *
  260. * @return $this
  261. */
  262. public function setInheritable($inheritable)
  263. {
  264. $this->inheritable = (bool) $inheritable;
  265. return $this;
  266. }
  267. /**
  268. * @internal
  269. *
  270. * @return array
  271. */
  272. public function resolveDependencies()
  273. {
  274. $dependencies = [];
  275. if ($this->getData() instanceof ElementInterface) {
  276. $elementType = Element\Service::getElementType($this->getData());
  277. $key = $elementType . '_' . $this->getData()->getId();
  278. $dependencies[$key] = [
  279. 'id' => $this->getData()->getId(),
  280. 'type' => $elementType,
  281. ];
  282. }
  283. return $dependencies;
  284. }
  285. /**
  286. * Rewrites id from source to target, $idMapping contains
  287. * array(
  288. * "document" => array(
  289. * SOURCE_ID => TARGET_ID,
  290. * SOURCE_ID => TARGET_ID
  291. * ),
  292. * "object" => array(...),
  293. * "asset" => array(...)
  294. * )
  295. *
  296. * @internal
  297. *
  298. * @param array $idMapping
  299. */
  300. public function rewriteIds($idMapping)
  301. {
  302. if (!$this->isInherited()) {
  303. if (array_key_exists($this->getType(), $idMapping)) {
  304. if ($this->getData() instanceof ElementInterface) {
  305. if (array_key_exists((int) $this->getData()->getId(), $idMapping[$this->getType()])) {
  306. $this->setData(Element\Service::getElementById($this->getType(), $idMapping[$this->getType()][$this->getData()->getId()]));
  307. }
  308. }
  309. }
  310. }
  311. }
  312. /**
  313. * @internal
  314. *
  315. * @return array
  316. */
  317. public function serialize()
  318. {
  319. return [
  320. 'name' => $this->getName(),
  321. 'type' => $this->getType(),
  322. 'data' => $this->getData(),
  323. ];
  324. }
  325. }