vendor/pimcore/pimcore/models/Document/Hardlink/Wrapper.php line 113

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\Hardlink;
  15. use Pimcore\Model\Document;
  16. /**
  17.  * @internal
  18.  *
  19.  * @method Document\Dao getDao()
  20.  */
  21. trait Wrapper
  22. {
  23.     /**
  24.      * @var Document\Hardlink
  25.      */
  26.     protected $hardLinkSource;
  27.     /**
  28.      * @var Document|null
  29.      */
  30.     protected ?Document $sourceDocument null;
  31.     /**
  32.      * OVERWRITTEN METHODS
  33.      *
  34.      * @throws \Exception
  35.      */
  36.     public function save()
  37.     {
  38.         throw $this->getHardlinkError();
  39.     }
  40.     /**
  41.      * @param array $params
  42.      *
  43.      * @throws \Exception
  44.      */
  45.     protected function update($params = [])
  46.     {
  47.         throw $this->getHardlinkError();
  48.     }
  49.     /**
  50.      * @throws \Exception
  51.      */
  52.     public function delete()
  53.     {
  54.         throw $this->getHardlinkError();
  55.     }
  56.     /**
  57.      * @return array|null
  58.      */
  59.     public function getProperties()
  60.     {
  61.         if ($this->properties == null) {
  62.             $hardLink $this->getHardLinkSource();
  63.             if ($hardLink->getPropertiesFromSource()) {
  64.                 $sourceProperties $this->getDao()->getProperties();
  65.             } else {
  66.                 $sourceProperties = [];
  67.             }
  68.             if ($this->getSourceDocument()) {
  69.                 // if we have a source document, it means that this document is not directly linked, it's a
  70.                 // child of a hardlink that uses "childFromSource", so in this case we use the source properties
  71.                 // this is especially important for the navigation, otherwise all children will have the same
  72.                 // navigation_name as the source hardlink, which doesn't make sense at all
  73.                 $sourceProperties $this->getSourceDocument()->getProperties();
  74.             }
  75.             $hardLinkProperties = [];
  76.             $hardLinkSourceProperties $hardLink->getProperties();
  77.             foreach ($hardLinkSourceProperties as $key => $prop) {
  78.                 $prop = clone $prop;
  79.                 // if the property doesn't exist in the source-properties just add it
  80.                 if (!array_key_exists($key$sourceProperties)) {
  81.                     $hardLinkProperties[$key] = $prop;
  82.                 } else {
  83.                     // if the property does exist in the source properties but it is inherited, then overwrite it with the hardlink property
  84.                     // or if the property is set directly on the hardlink itself
  85.                     if ($sourceProperties[$key]->isInherited() || !$prop->isInherited()) {
  86.                         $hardLinkProperties[$key] = $prop;
  87.                     }
  88.                 }
  89.                 $prop->setInherited(true);
  90.             }
  91.             $properties array_merge($sourceProperties$hardLinkProperties);
  92.             $this->setProperties($properties);
  93.         }
  94.         return $this->properties;
  95.     }
  96.     public function getProperty($name$asContainer false)
  97.     {
  98.         $result parent::getProperty($name$asContainer);
  99.         if ($result instanceof Document) {
  100.             $hardLink $this->getHardLinkSource();
  101.             if (strpos($result->getRealFullPath(), $hardLink->getSourceDocument()->getRealFullPath() . '/') === 0
  102.                 || $hardLink->getSourceDocument()->getRealFullPath() === $result->getRealFullPath()
  103.             ) {
  104.                 $c Service::wrap($result);
  105.                 if ($c instanceof Document\Hardlink\Wrapper\WrapperInterface) {
  106.                     $c->setHardLinkSource($hardLink);
  107.                     $c->setPath(preg_replace('@^' preg_quote($hardLink->getSourceDocument()->getRealPath(), '@') . '@',
  108.                         $hardLink->getRealPath(), $c->getRealPath()));
  109.                     return $c;
  110.                 }
  111.             }
  112.         }
  113.         return $result;
  114.     }
  115.     /**
  116.      * @param bool $includingUnpublished
  117.      *
  118.      * @return Document[]
  119.      */
  120.     public function getChildren($includingUnpublished false)
  121.     {
  122.         $cacheKey $this->getListingCacheKey(func_get_args());
  123.         if (!isset($this->children[$cacheKey])) {
  124.             $hardLink $this->getHardLinkSource();
  125.             $children = [];
  126.             if ($hardLink->getChildrenFromSource() && $hardLink->getSourceDocument() && !\Pimcore::inAdmin()) {
  127.                 foreach (parent::getChildren($includingUnpublished) as $c) {
  128.                     $c Service::wrap($c);
  129.                     if ($c instanceof Document\Hardlink\Wrapper\WrapperInterface) {
  130.                         $c->setHardLinkSource($hardLink);
  131.                         $c->setPath(preg_replace('@^' preg_quote($hardLink->getSourceDocument()->getRealFullpath(), '@') . '@'$hardLink->getRealFullpath(), $c->getRealPath()));
  132.                         $children[] = $c;
  133.                     }
  134.                 }
  135.             }
  136.             $this->setChildren($children$includingUnpublished);
  137.         }
  138.         return $this->children[$cacheKey];
  139.     }
  140.     /**
  141.      * @param bool $unpublished
  142.      *
  143.      * @return bool
  144.      */
  145.     public function hasChildren($unpublished false)
  146.     {
  147.         $hardLink $this->getHardLinkSource();
  148.         if ($hardLink->getChildrenFromSource() && $hardLink->getSourceDocument() && !\Pimcore::inAdmin()) {
  149.             return parent::hasChildren($unpublished);
  150.         }
  151.         return false;
  152.     }
  153.     /**
  154.      * @return \Exception
  155.      */
  156.     protected function getHardlinkError(): \Exception
  157.     {
  158.         return new \Exception('Method not supported by hard linked documents');
  159.     }
  160.     /**
  161.      * @param Document\Hardlink $hardLinkSource
  162.      *
  163.      * @return $this
  164.      */
  165.     public function setHardLinkSource($hardLinkSource)
  166.     {
  167.         $this->hardLinkSource $hardLinkSource;
  168.         return $this;
  169.     }
  170.     /**
  171.      * @return Document\Hardlink
  172.      */
  173.     public function getHardLinkSource()
  174.     {
  175.         return $this->hardLinkSource;
  176.     }
  177.     /**
  178.      * @return Document|null
  179.      */
  180.     public function getSourceDocument(): ?Document
  181.     {
  182.         return $this->sourceDocument;
  183.     }
  184.     /**
  185.      * @param Document $sourceDocument
  186.      *
  187.      * @return $this
  188.      */
  189.     public function setSourceDocument(Document $sourceDocument): static
  190.     {
  191.         $this->sourceDocument $sourceDocument;
  192.         return $this;
  193.     }
  194. }