vendor/pimcore/pimcore/models/Document/Tag/Relation.php line 30

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 Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @category   Pimcore
  12.  * @package    Document
  13.  *
  14.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  15.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  16.  */
  17. namespace Pimcore\Model\Document\Tag;
  18. use Pimcore\Logger;
  19. use Pimcore\Model;
  20. use Pimcore\Model\Asset;
  21. use Pimcore\Model\DataObject;
  22. use Pimcore\Model\Document;
  23. use Pimcore\Model\Element;
  24. /**
  25.  * @method \Pimcore\Model\Document\Tag\Dao getDao()
  26.  */
  27. class Relation extends Model\Document\Tag
  28. {
  29.     /**
  30.      * ID of the source object
  31.      *
  32.      * @var int|null
  33.      */
  34.     public $id;
  35.     /**
  36.      * Type of the source object (document, asset, object)
  37.      *
  38.      * @var string|null
  39.      */
  40.     public $type;
  41.     /**
  42.      * Subtype of the source object (eg. page, link, video, news, ...)
  43.      *
  44.      * @var string|null
  45.      */
  46.     public $subtype;
  47.     /**
  48.      * Contains the source object
  49.      *
  50.      * @var mixed
  51.      */
  52.     public $element;
  53.     /**
  54.      * @see Document\Tag\TagInterface::getType
  55.      *
  56.      * @return string
  57.      */
  58.     public function getType()
  59.     {
  60.         //TODO: getType != $type ... that might be dangerous
  61.         return 'relation';
  62.     }
  63.     /**
  64.      * @see Document\Tag\TagInterface::getData
  65.      *
  66.      * @return mixed
  67.      */
  68.     public function getData()
  69.     {
  70.         return [
  71.             'id' => $this->id,
  72.             'type' => $this->type,
  73.             'subtype' => $this->subtype
  74.         ];
  75.     }
  76.     /**
  77.      * Converts the data so it's suitable for the editmode
  78.      *
  79.      * @return mixed
  80.      */
  81.     public function getDataEditmode()
  82.     {
  83.         $this->setElement();
  84.         if ($this->element instanceof Element\ElementInterface) {
  85.             return [
  86.                 'id' => $this->id,
  87.                 'path' => $this->element->getRealFullPath(),
  88.                 'elementType' => $this->type,
  89.                 'subtype' => $this->subtype
  90.             ];
  91.         }
  92.         return null;
  93.     }
  94.     /**
  95.      * @see Document\Tag\TagInterface::frontend
  96.      *
  97.      * @return string
  98.      */
  99.     public function frontend()
  100.     {
  101.         $this->setElement();
  102.         //don't give unpublished elements in frontend
  103.         if (Document::doHideUnpublished() and !Element\Service::isPublished($this->element)) {
  104.             return '';
  105.         }
  106.         if ($this->element instanceof Element\ElementInterface) {
  107.             return $this->element->getFullPath();
  108.         }
  109.         return '';
  110.     }
  111.     /**
  112.      * @see Document\Tag\TagInterface::setDataFromResource
  113.      *
  114.      * @param mixed $data
  115.      *
  116.      * @return $this
  117.      */
  118.     public function setDataFromResource($data)
  119.     {
  120.         if (!empty($data)) {
  121.             $data = \Pimcore\Tool\Serialize::unserialize($data);
  122.         }
  123.         $this->id $data['id'] ?? null;
  124.         $this->type $data['type'] ?? null;
  125.         $this->subtype $data['subtype'] ?? null;
  126.         $this->setElement();
  127.         return $this;
  128.     }
  129.     /**
  130.      * @see Document\Tag\TagInterface::setDataFromEditmode
  131.      *
  132.      * @param mixed $data
  133.      *
  134.      * @return $this
  135.      */
  136.     public function setDataFromEditmode($data)
  137.     {
  138.         $this->id $data['id'] ?? null;
  139.         $this->type $data['type'] ?? null;
  140.         $this->subtype $data['subtype'] ?? null;
  141.         $this->setElement();
  142.         return $this;
  143.     }
  144.     /**
  145.      * Sets the element by the data stored for the object
  146.      *
  147.      * @return $this
  148.      */
  149.     protected function setElement()
  150.     {
  151.         if (!$this->element) {
  152.             $this->element Element\Service::getElementById($this->type$this->id);
  153.         }
  154.         return $this;
  155.     }
  156.     /**
  157.      * Returns one of them: Document, Object, Asset
  158.      *
  159.      * @return mixed
  160.      */
  161.     public function getElement()
  162.     {
  163.         $this->setElement();
  164.         //don't give unpublished elements in frontend
  165.         if (Document::doHideUnpublished() and !Element\Service::isPublished($this->element)) {
  166.             return false;
  167.         }
  168.         return $this->element;
  169.     }
  170.     /**
  171.      * Returns teh path of the linked element
  172.      *
  173.      * @return mixed
  174.      */
  175.     public function getFullPath()
  176.     {
  177.         $this->setElement();
  178.         //don't give unpublished elements in frontend
  179.         if (Document::doHideUnpublished() and !Element\Service::isPublished($this->element)) {
  180.             return false;
  181.         }
  182.         if ($this->element instanceof Element\ElementInterface) {
  183.             return $this->element->getFullPath();
  184.         }
  185.         return;
  186.     }
  187.     /**
  188.      * @return bool
  189.      */
  190.     public function isEmpty()
  191.     {
  192.         $this->setElement();
  193.         if ($this->getElement() instanceof Element\ElementInterface) {
  194.             return false;
  195.         }
  196.         return true;
  197.     }
  198.     /**
  199.      * @return array
  200.      */
  201.     public function resolveDependencies()
  202.     {
  203.         $dependencies = [];
  204.         $this->setElement();
  205.         if ($this->element instanceof Element\ElementInterface) {
  206.             $elementType Element\Service::getElementType($this->element);
  207.             $key $elementType '_' $this->element->getId();
  208.             $dependencies[$key] = [
  209.                 'id' => $this->element->getId(),
  210.                 'type' => $elementType
  211.             ];
  212.         }
  213.         return $dependencies;
  214.     }
  215.     /**
  216.      * @deprecated
  217.      *
  218.      * @param Model\Webservice\Data\Document\Element $wsElement
  219.      * @param Model\Document\PageSnippet $document
  220.      * @param array $params
  221.      * @param Model\Webservice\IdMapperInterface|null $idMapper
  222.      *
  223.      * @throws \Exception
  224.      */
  225.     public function getFromWebserviceImport($wsElement$document null$params = [], $idMapper null)
  226.     {
  227.         $data $this->sanitizeWebserviceData($wsElement->value);
  228.         if ($data->id !== null) {
  229.             $this->type $data->type;
  230.             $this->subtype $data->subtype;
  231.             $this->id $data->id;
  232.             if (!is_numeric($this->id)) {
  233.                 throw new \Exception('cannot get values from web service import - id is not valid');
  234.             }
  235.             if ($idMapper) {
  236.                 $this->id $idMapper->getMappedId($this->type$data->id);
  237.             }
  238.             if ($this->type == 'asset') {
  239.                 $this->element Asset::getById($this->id);
  240.                 if (!$this->element instanceof Asset) {
  241.                     if ($idMapper && $idMapper->ignoreMappingFailures()) {
  242.                         $idMapper->recordMappingFailure('document'$this->getDocumentId(), $data->type$data->id);
  243.                     } else {
  244.                         throw new \Exception('cannot get values from web service import - referenced asset with id [ '.$data->id.' ] is unknown');
  245.                     }
  246.                 }
  247.             } elseif ($this->type == 'document') {
  248.                 $this->element Document::getById($this->id);
  249.                 if (!$this->element instanceof Document) {
  250.                     if ($idMapper && $idMapper->ignoreMappingFailures()) {
  251.                         $idMapper->recordMappingFailure('document'$this->getDocumentId(), $data->type$data->id);
  252.                     } else {
  253.                         throw new \Exception('cannot get values from web service import - referenced document with id [ '.$data->id.' ] is unknown');
  254.                     }
  255.                 }
  256.             } elseif ($this->type == 'object') {
  257.                 $this->element DataObject\AbstractObject::getById($this->id);
  258.                 if (!$this->element instanceof DataObject\AbstractObject) {
  259.                     if ($idMapper && $idMapper->ignoreMappingFailures()) {
  260.                         $idMapper->recordMappingFailure('document'$this->getDocumentId(), $data->type$data->id);
  261.                     } else {
  262.                         throw new \Exception('cannot get values from web service import - referenced object with id [ '.$data->id.' ] is unknown');
  263.                     }
  264.                 }
  265.             } else {
  266.                 if ($idMapper && $idMapper->ignoreMappingFailures()) {
  267.                     $idMapper->recordMappingFailure('document'$this->getDocumentId(), $data->type$data->id);
  268.                 } else {
  269.                     throw new \Exception('cannot get values from web service import - type is not valid');
  270.                 }
  271.             }
  272.         }
  273.     }
  274.     /**
  275.      * @return bool
  276.      */
  277.     public function checkValidity()
  278.     {
  279.         $sane true;
  280.         if ($this->id) {
  281.             $el Element\Service::getElementById($this->type$this->id);
  282.             if (!$el instanceof Element\ElementInterface) {
  283.                 $sane false;
  284.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  285.                 $this->id null;
  286.                 $this->type null;
  287.                 $this->subtype null;
  288.                 $this->element null;
  289.             }
  290.         }
  291.         return $sane;
  292.     }
  293.     /**
  294.      * @return array
  295.      */
  296.     public function __sleep()
  297.     {
  298.         $finalVars = [];
  299.         $parentVars parent::__sleep();
  300.         $blockedVars = ['element'];
  301.         foreach ($parentVars as $key) {
  302.             if (!in_array($key$blockedVars)) {
  303.                 $finalVars[] = $key;
  304.             }
  305.         }
  306.         return $finalVars;
  307.     }
  308.     /**
  309.      * this method is called by Document\Service::loadAllDocumentFields() to load all lazy loading fields
  310.      */
  311.     public function load()
  312.     {
  313.         if (!$this->element) {
  314.             $this->setElement();
  315.         }
  316.     }
  317.     /**
  318.      * @param int $id
  319.      *
  320.      * @return self
  321.      */
  322.     public function setId($id)
  323.     {
  324.         $this->id $id;
  325.         return $this;
  326.     }
  327.     /**
  328.      * @return int
  329.      */
  330.     public function getId()
  331.     {
  332.         return (int) $this->id;
  333.     }
  334.     /**
  335.      * @param string $subtype
  336.      *
  337.      * @return self
  338.      */
  339.     public function setSubtype($subtype)
  340.     {
  341.         $this->subtype $subtype;
  342.         return $this;
  343.     }
  344.     /**
  345.      * @return string
  346.      */
  347.     public function getSubtype()
  348.     {
  349.         return $this->subtype;
  350.     }
  351.     /**
  352.      * Rewrites id from source to target, $idMapping contains
  353.      * array(
  354.      *  "document" => array(
  355.      *      SOURCE_ID => TARGET_ID,
  356.      *      SOURCE_ID => TARGET_ID
  357.      *  ),
  358.      *  "object" => array(...),
  359.      *  "asset" => array(...)
  360.      * )
  361.      *
  362.      * @param array $idMapping
  363.      */
  364.     public function rewriteIds($idMapping)
  365.     {
  366.         if (array_key_exists($this->type$idMapping) and array_key_exists($this->getId(), $idMapping[$this->type])) {
  367.             $this->id $idMapping[$this->type][$this->getId()];
  368.         }
  369.     }
  370. }
  371. class_alias(Relation::class, 'Pimcore\Model\Document\Tag\Href');