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

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Model\Document\Editable;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Model\Document;
  20. use Pimcore\Model\Element;
  21. /**
  22.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  23.  */
  24. class Relation extends Model\Document\Editable implements IdRewriterInterfaceEditmodeDataInterfaceLazyLoadingInterface
  25. {
  26.     /**
  27.      * ID of the source object
  28.      *
  29.      * @internal
  30.      *
  31.      */
  32.     protected ?int $id null;
  33.     /**
  34.      * Type of the source object (document, asset, object)
  35.      *
  36.      * @internal
  37.      *
  38.      */
  39.     protected ?string $type null;
  40.     /**
  41.      * Subtype of the source object (eg. page, link, video, news, ...)
  42.      *
  43.      * @internal
  44.      *
  45.      */
  46.     protected ?string $subtype null;
  47.     /**
  48.      * Contains the source object
  49.      *
  50.      * @internal
  51.      *
  52.      */
  53.     protected mixed $element null;
  54.     public function getType(): string
  55.     {
  56.         //TODO: getType != $type ... that might be dangerous
  57.         return 'relation';
  58.     }
  59.     public function getData(): mixed
  60.     {
  61.         return [
  62.             'id' => $this->id,
  63.             'type' => $this->type,
  64.             'subtype' => $this->subtype,
  65.         ];
  66.     }
  67.     public function getDataEditmode(): ?array
  68.     {
  69.         $this->setElement();
  70.         if ($this->element instanceof Element\ElementInterface) {
  71.             return [
  72.                 'id' => $this->id,
  73.                 'path' => $this->element->getRealFullPath(),
  74.                 'elementType' => $this->type,
  75.                 'subtype' => $this->subtype,
  76.             ];
  77.         }
  78.         return null;
  79.     }
  80.     public function frontend()
  81.     {
  82.         $this->setElement();
  83.         //don't give unpublished elements in frontend
  84.         if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  85.             return '';
  86.         }
  87.         if ($this->element instanceof Element\ElementInterface) {
  88.             return $this->element->getFullPath();
  89.         }
  90.         return '';
  91.     }
  92.     public function setDataFromResource(mixed $data): static
  93.     {
  94.         if (!empty($data)) {
  95.             $data \Pimcore\Tool\Serialize::unserialize($data);
  96.         }
  97.         $this->id $data['id'] ?? null;
  98.         $this->type $data['type'] ?? null;
  99.         $this->subtype $data['subtype'] ?? null;
  100.         $this->setElement();
  101.         return $this;
  102.     }
  103.     public function setDataFromEditmode(mixed $data): static
  104.     {
  105.         $this->id $data['id'] ?? null;
  106.         $this->type $data['type'] ?? null;
  107.         $this->subtype $data['subtype'] ?? null;
  108.         $this->setElement();
  109.         return $this;
  110.     }
  111.     /**
  112.      * Sets the element by the data stored for the object
  113.      *
  114.      * @return $this
  115.      */
  116.     private function setElement(): static
  117.     {
  118.         if (!$this->element && $this->type && $this->id) {
  119.             $this->element Element\Service::getElementById($this->type$this->id);
  120.         }
  121.         return $this;
  122.     }
  123.     /**
  124.      * Returns one of them: Document, Object, Asset
  125.      *
  126.      * @return Element\ElementInterface|false|null
  127.      */
  128.     public function getElement(): bool|Element\ElementInterface|null
  129.     {
  130.         $this->setElement();
  131.         //don't give unpublished elements in frontend
  132.         if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  133.             return false;
  134.         }
  135.         return $this->element;
  136.     }
  137.     /**
  138.      * Returns the path of the linked element
  139.      *
  140.      * @return string|false|null
  141.      */
  142.     public function getFullPath(): bool|string|null
  143.     {
  144.         $this->setElement();
  145.         //don't give unpublished elements in frontend
  146.         if (Element\Service::doHideUnpublished($this->element) && !Element\Service::isPublished($this->element)) {
  147.             return false;
  148.         }
  149.         if ($this->element instanceof Element\ElementInterface) {
  150.             return $this->element->getFullPath();
  151.         }
  152.         return null;
  153.     }
  154.     public function isEmpty(): bool
  155.     {
  156.         $this->setElement();
  157.         if ($this->getElement() instanceof Element\ElementInterface) {
  158.             return false;
  159.         }
  160.         return true;
  161.     }
  162.     public function resolveDependencies(): array
  163.     {
  164.         $dependencies = [];
  165.         $this->setElement();
  166.         if ($this->element instanceof Element\ElementInterface) {
  167.             $elementType Element\Service::getElementType($this->element);
  168.             $key $elementType '_' $this->element->getId();
  169.             $dependencies[$key] = [
  170.                 'id' => $this->element->getId(),
  171.                 'type' => $elementType,
  172.             ];
  173.         }
  174.         return $dependencies;
  175.     }
  176.     public function checkValidity(): bool
  177.     {
  178.         $sane true;
  179.         if ($this->id) {
  180.             $el Element\Service::getElementById($this->type$this->id);
  181.             if (!$el instanceof Element\ElementInterface) {
  182.                 $sane false;
  183.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  184.                 $this->id null;
  185.                 $this->type null;
  186.                 $this->subtype null;
  187.                 $this->element null;
  188.             }
  189.         }
  190.         return $sane;
  191.     }
  192.     public function __sleep(): array
  193.     {
  194.         $finalVars = [];
  195.         $parentVars parent::__sleep();
  196.         $blockedVars = ['element'];
  197.         foreach ($parentVars as $key) {
  198.             if (!in_array($key$blockedVars)) {
  199.                 $finalVars[] = $key;
  200.             }
  201.         }
  202.         return $finalVars;
  203.     }
  204.     public function load(): void
  205.     {
  206.         if (!$this->element) {
  207.             $this->setElement();
  208.         }
  209.     }
  210.     public function setId(int $id): static
  211.     {
  212.         $this->id $id;
  213.         return $this;
  214.     }
  215.     public function getId(): int
  216.     {
  217.         return (int) $this->id;
  218.     }
  219.     public function setSubtype(string $subtype): static
  220.     {
  221.         $this->subtype $subtype;
  222.         return $this;
  223.     }
  224.     public function getSubtype(): ?string
  225.     {
  226.         return $this->subtype;
  227.     }
  228.     public function rewriteIds(array $idMapping): void
  229.     {
  230.         if (array_key_exists($this->type$idMapping) && array_key_exists($this->getId(), $idMapping[$this->type])) {
  231.             $this->id $idMapping[$this->type][$this->getId()];
  232.         }
  233.     }
  234. }