vendor/pimcore/pimcore/models/Document/Editable/Relation.php line 137

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\Document\Editable;
  15. use Pimcore\Logger;
  16. use Pimcore\Model;
  17. use Pimcore\Model\Asset;
  18. use Pimcore\Model\Document;
  19. use Pimcore\Model\Element;
  20. /**
  21. * @method \Pimcore\Model\Document\Editable\Dao getDao()
  22. */
  23. class Relation extends Model\Document\Editable implements IdRewriterInterface, EditmodeDataInterface, LazyLoadingInterface
  24. {
  25. /**
  26. * ID of the source object
  27. *
  28. * @internal
  29. *
  30. * @var int|null
  31. */
  32. protected $id;
  33. /**
  34. * Type of the source object (document, asset, object)
  35. *
  36. * @internal
  37. *
  38. * @var string|null
  39. */
  40. protected $type;
  41. /**
  42. * Subtype of the source object (eg. page, link, video, news, ...)
  43. *
  44. * @internal
  45. *
  46. * @var string|null
  47. */
  48. protected $subtype;
  49. /**
  50. * Contains the source object
  51. *
  52. * @internal
  53. *
  54. * @var mixed
  55. */
  56. protected $element;
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getType()
  61. {
  62. //TODO: getType != $type ... that might be dangerous
  63. return 'relation';
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getData()
  69. {
  70. return [
  71. 'id' => $this->id,
  72. 'type' => $this->type,
  73. 'subtype' => $this->subtype,
  74. ];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getDataEditmode() /** : mixed */
  80. {
  81. $this->setElement();
  82. if ($this->element instanceof Element\ElementInterface) {
  83. return [
  84. 'id' => $this->id,
  85. 'path' => $this->element->getRealFullPath(),
  86. 'elementType' => $this->type,
  87. 'subtype' => $this->subtype,
  88. ];
  89. }
  90. return null;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function frontend()
  96. {
  97. $this->setElement();
  98. //don't give unpublished elements in frontend
  99. if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  100. return '';
  101. }
  102. if ($this->element instanceof Element\ElementInterface) {
  103. return $this->element->getFullPath();
  104. }
  105. return '';
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function setDataFromResource($data)
  111. {
  112. if (!empty($data)) {
  113. $data = \Pimcore\Tool\Serialize::unserialize($data);
  114. }
  115. $this->id = $data['id'] ?? null;
  116. $this->type = $data['type'] ?? null;
  117. $this->subtype = $data['subtype'] ?? null;
  118. $this->setElement();
  119. return $this;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function setDataFromEditmode($data)
  125. {
  126. $this->id = $data['id'] ?? null;
  127. $this->type = $data['type'] ?? null;
  128. $this->subtype = $data['subtype'] ?? null;
  129. $this->setElement();
  130. return $this;
  131. }
  132. /**
  133. * Sets the element by the data stored for the object
  134. *
  135. * @return $this
  136. */
  137. private function setElement()
  138. {
  139. if (!$this->element) {
  140. $this->element = Element\Service::getElementById($this->type, $this->id);
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Returns one of them: Document, Object, Asset
  146. *
  147. * @return Element\ElementInterface|false|null
  148. */
  149. public function getElement()
  150. {
  151. $this->setElement();
  152. //don't give unpublished elements in frontend
  153. if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  154. return false;
  155. }
  156. return $this->element;
  157. }
  158. /**
  159. * Returns the path of the linked element
  160. *
  161. * @return string|false|null
  162. */
  163. public function getFullPath()
  164. {
  165. $this->setElement();
  166. //don't give unpublished elements in frontend
  167. if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  168. return false;
  169. }
  170. if ($this->element instanceof Element\ElementInterface) {
  171. return $this->element->getFullPath();
  172. }
  173. return null;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function isEmpty()
  179. {
  180. $this->setElement();
  181. if ($this->getElement() instanceof Element\ElementInterface) {
  182. return false;
  183. }
  184. return true;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function resolveDependencies()
  190. {
  191. $dependencies = [];
  192. $this->setElement();
  193. if ($this->element instanceof Element\ElementInterface) {
  194. $elementType = Element\Service::getElementType($this->element);
  195. $key = $elementType . '_' . $this->element->getId();
  196. $dependencies[$key] = [
  197. 'id' => $this->element->getId(),
  198. 'type' => $elementType,
  199. ];
  200. }
  201. return $dependencies;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function checkValidity()
  207. {
  208. $sane = true;
  209. if ($this->id) {
  210. $el = Element\Service::getElementById($this->type, $this->id);
  211. if (!$el instanceof Element\ElementInterface) {
  212. $sane = false;
  213. Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  214. $this->id = null;
  215. $this->type = null;
  216. $this->subtype = null;
  217. $this->element = null;
  218. }
  219. }
  220. return $sane;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function __sleep()
  226. {
  227. $finalVars = [];
  228. $parentVars = parent::__sleep();
  229. $blockedVars = ['element'];
  230. foreach ($parentVars as $key) {
  231. if (!in_array($key, $blockedVars)) {
  232. $finalVars[] = $key;
  233. }
  234. }
  235. return $finalVars;
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function load() /** : void */
  241. {
  242. if (!$this->element) {
  243. $this->setElement();
  244. }
  245. }
  246. /**
  247. * @param int $id
  248. *
  249. * @return $this
  250. */
  251. public function setId($id)
  252. {
  253. $this->id = $id;
  254. return $this;
  255. }
  256. /**
  257. * @return int
  258. */
  259. public function getId()
  260. {
  261. return (int) $this->id;
  262. }
  263. /**
  264. * @param string $subtype
  265. *
  266. * @return $this
  267. */
  268. public function setSubtype($subtype)
  269. {
  270. $this->subtype = $subtype;
  271. return $this;
  272. }
  273. /**
  274. * @return string
  275. */
  276. public function getSubtype()
  277. {
  278. return $this->subtype;
  279. }
  280. /**
  281. * { @inheritdoc }
  282. */
  283. public function rewriteIds($idMapping) /** : void */
  284. {
  285. if (array_key_exists($this->type, $idMapping) && array_key_exists($this->getId(), $idMapping[$this->type])) {
  286. $this->id = $idMapping[$this->type][$this->getId()];
  287. }
  288. }
  289. }