vendor/pimcore/pimcore/models/Document/Hardlink.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\Document;
  15. use Pimcore\Model;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Model\Redirect;
  18. /**
  19.  * @method \Pimcore\Model\Document\Hardlink\Dao getDao()
  20.  */
  21. class Hardlink extends Document
  22. {
  23.     use Model\Element\Traits\ScheduledTasksTrait;
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     protected string $type 'hardlink';
  28.     /**
  29.      * @internal
  30.      *
  31.      * @var int
  32.      */
  33.     protected $sourceId;
  34.     /**
  35.      * @internal
  36.      *
  37.      * @var bool
  38.      */
  39.     protected $propertiesFromSource;
  40.     /**
  41.      * @internal
  42.      *
  43.      * @var bool
  44.      */
  45.     protected $childrenFromSource;
  46.     /**
  47.      * @return Document|null
  48.      */
  49.     public function getSourceDocument(): ?Document
  50.     {
  51.         if ($this->getSourceId()) {
  52.             return Document::getById($this->getSourceId());
  53.         }
  54.         return null;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     protected function resolveDependencies(): array
  60.     {
  61.         $dependencies parent::resolveDependencies();
  62.         $sourceDocument $this->getSourceDocument();
  63.         if ($sourceDocument instanceof Document) {
  64.             $key 'document_' $sourceDocument->getId();
  65.             $dependencies[$key] = [
  66.                 'id' => $sourceDocument->getId(),
  67.                 'type' => 'document',
  68.             ];
  69.         }
  70.         return $dependencies;
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function getCacheTags(array $tags = []): array
  76.     {
  77.         $tags parent::getCacheTags($tags);
  78.         if ($this->getSourceDocument()) {
  79.             if ($this->getSourceDocument()->getId() != $this->getId() && !array_key_exists($this->getSourceDocument()->getCacheTag(), $tags)) {
  80.                 $tags $this->getSourceDocument()->getCacheTags($tags);
  81.             }
  82.         }
  83.         return $tags;
  84.     }
  85.     /**
  86.      * @param bool $childrenFromSource
  87.      *
  88.      * @return Hardlink
  89.      */
  90.     public function setChildrenFromSource($childrenFromSource)
  91.     {
  92.         $this->childrenFromSource = (bool) $childrenFromSource;
  93.         return $this;
  94.     }
  95.     /**
  96.      * @return bool
  97.      */
  98.     public function getChildrenFromSource()
  99.     {
  100.         return $this->childrenFromSource;
  101.     }
  102.     /**
  103.      * @param int $sourceId
  104.      *
  105.      * @return $this
  106.      */
  107.     public function setSourceId($sourceId)
  108.     {
  109.         $this->sourceId = (int) $sourceId;
  110.         return $this;
  111.     }
  112.     /**
  113.      * @return int
  114.      */
  115.     public function getSourceId()
  116.     {
  117.         return $this->sourceId;
  118.     }
  119.     /**
  120.      * @param bool $propertiesFromSource
  121.      *
  122.      * @return $this
  123.      */
  124.     public function setPropertiesFromSource($propertiesFromSource)
  125.     {
  126.         $this->propertiesFromSource = (bool) $propertiesFromSource;
  127.         return $this;
  128.     }
  129.     /**
  130.      * @return bool
  131.      */
  132.     public function getPropertiesFromSource()
  133.     {
  134.         return $this->propertiesFromSource;
  135.     }
  136.     /**
  137.      * {@inheritdoc}
  138.      */
  139.     public function getProperties()
  140.     {
  141.         if ($this->properties === null) {
  142.             $properties parent::getProperties();
  143.             if ($this->getPropertiesFromSource() && $this->getSourceDocument()) {
  144.                 $sourceProperties $this->getSourceDocument()->getProperties();
  145.                 foreach ($sourceProperties as &$prop) {
  146.                     $prop = clone $prop// because of cache
  147.                     $prop->setInherited(true);
  148.                 }
  149.                 $properties array_merge($sourceProperties$properties);
  150.             } elseif ($this->getSourceDocument()) {
  151.                 $sourceProperties $this->getSourceDocument()->getDao()->getProperties(falsetrue);
  152.                 foreach ($sourceProperties as &$prop) {
  153.                     /**
  154.                      * @var Model\Property $prop
  155.                      */
  156.                     $prop = clone $prop// because of cache
  157.                     $prop->setInherited(true);
  158.                 }
  159.                 $properties array_merge($sourceProperties$properties);
  160.             }
  161.             $this->setProperties($properties);
  162.         }
  163.         return $this->properties;
  164.     }
  165.     /**
  166.      * {@inheritdoc}
  167.      */
  168.     public function getChildren($includingUnpublished false)
  169.     {
  170.         $cacheKey $this->getListingCacheKey(func_get_args());
  171.         if (!isset($this->children[$cacheKey])) {
  172.             $children parent::getChildren($includingUnpublished);
  173.             $sourceChildren = [];
  174.             if ($this->getChildrenFromSource() && $this->getSourceDocument() && !\Pimcore::inAdmin()) {
  175.                 $sourceChildren $this->getSourceDocument()->getChildren($includingUnpublished);
  176.                 foreach ($sourceChildren as &$c) {
  177.                     $c Document\Hardlink\Service::wrap($c);
  178.                     $c->setHardLinkSource($this);
  179.                     $c->setPath(preg_replace('@^' preg_quote($this->getSourceDocument()->getRealFullPath(), '@') . '@'$this->getRealFullPath(), $c->getRealPath()));
  180.                 }
  181.             }
  182.             $children array_merge($sourceChildren$children);
  183.             $this->setChildren($children$includingUnpublished);
  184.         }
  185.         return $this->children[$cacheKey] ?? [];
  186.     }
  187.     /**
  188.      * {@inheritdoc}
  189.      */
  190.     public function hasChildren($unpublished false)
  191.     {
  192.         return count($this->getChildren($unpublished)) > 0;
  193.     }
  194.     /**
  195.      * {@inheritdoc}
  196.      */
  197.     protected function doDelete()
  198.     {
  199.         // check for redirects pointing to this document, and delete them too
  200.         $redirects = new Redirect\Listing();
  201.         $redirects->setCondition('target = ?'$this->getId());
  202.         $redirects->load();
  203.         foreach ($redirects->getRedirects() as $redirect) {
  204.             $redirect->delete();
  205.         }
  206.         parent::doDelete();
  207.     }
  208.     /**
  209.      * {@inheritdoc}
  210.      */
  211.     protected function update($params = [])
  212.     {
  213.         parent::update($params);
  214.         $this->saveScheduledTasks();
  215.     }
  216. }