vendor/pimcore/pimcore/models/Document/Page.php line 28

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;
  18. use Pimcore\Model\Redirect;
  19. use Pimcore\Model\Site;
  20. use Pimcore\Model\Tool\Targeting\TargetGroup;
  21. use Pimcore\Tool\Frontend;
  22. /**
  23.  * @method \Pimcore\Model\Document\Page\Dao getDao()
  24.  */
  25. class Page extends TargetingDocument
  26. {
  27.     /**
  28.      * Contains the title of the page (meta-title)
  29.      *
  30.      * @var string
  31.      */
  32.     protected $title '';
  33.     /**
  34.      * Contains the description of the page (meta-description)
  35.      *
  36.      * @var string
  37.      */
  38.     protected $description '';
  39.     /**
  40.      * @var array
  41.      */
  42.     protected $metaData = [];
  43.     /**
  44.      * Static type of the document
  45.      *
  46.      * @var string
  47.      */
  48.     protected $type 'page';
  49.     /**
  50.      * @var string
  51.      */
  52.     protected $prettyUrl;
  53.     /**
  54.      * Comma separated IDs of target groups
  55.      *
  56.      * @var string
  57.      */
  58.     protected $targetGroupIds '';
  59.     /**
  60.      * @inheritdoc
  61.      */
  62.     public function delete(bool $isNested false)
  63.     {
  64.         if ($this->getId() == 1) {
  65.             throw new \Exception('root-node cannot be deleted');
  66.         }
  67.         // check for redirects pointing to this document, and delete them too
  68.         $redirects = new Redirect\Listing();
  69.         $redirects->setCondition('target = ?'$this->getId());
  70.         $redirects->load();
  71.         foreach ($redirects->getRedirects() as $redirect) {
  72.             $redirect->delete();
  73.         }
  74.         if ($site Site::getByRootId($this->getId())) {
  75.             $site->delete();
  76.         }
  77.         parent::delete($isNested);
  78.     }
  79.     /**
  80.      * @param array $params additional parameters (e.g. "versionNote" for the version note)
  81.      *
  82.      * @throws \Exception
  83.      */
  84.     protected function update($params = [])
  85.     {
  86.         $oldPath $this->getDao()->getCurrentFullPath();
  87.         parent::update($params);
  88.         $config = \Pimcore\Config::getSystemConfig();
  89.         if ($oldPath && $config->documents->createredirectwhenmoved && $oldPath != $this->getRealFullPath()) {
  90.             // create redirect for old path
  91.             $redirect = new Redirect();
  92.             $redirect->setType(Redirect::TYPE_PATH);
  93.             $redirect->setRegex(true);
  94.             $redirect->setTarget($this->getId());
  95.             $redirect->setSource('@' $oldPath '/?@');
  96.             $redirect->setStatusCode(301);
  97.             $redirect->setExpiry(time() + 86400 60); // this entry is removed automatically after 60 days
  98.             $site Frontend::getSiteForDocument($this);
  99.             if ($site) {
  100.                 $redirect->setSourceSite($site->getId());
  101.                 $oldPath preg_replace('@^' preg_quote($site->getRootPath()) . '@'''$oldPath);
  102.                 $redirect->setSource('@' $oldPath '/?@');
  103.             }
  104.             $redirect->save();
  105.         }
  106.     }
  107.     /**
  108.      * @return string
  109.      */
  110.     public function getDescription()
  111.     {
  112.         return $this->description;
  113.     }
  114.     /**
  115.      * @return string
  116.      */
  117.     public function getTitle()
  118.     {
  119.         return \Pimcore\Tool\Text::removeLineBreaks($this->title);
  120.     }
  121.     /**
  122.      * @param string $description
  123.      *
  124.      * @return $this
  125.      */
  126.     public function setDescription($description)
  127.     {
  128.         $this->description str_replace("\n"' '$description);
  129.         return $this;
  130.     }
  131.     /**
  132.      * @param string $title
  133.      *
  134.      * @return $this
  135.      */
  136.     public function setTitle($title)
  137.     {
  138.         $this->title $title;
  139.         return $this;
  140.     }
  141.     /**
  142.      * @param $metaData
  143.      *
  144.      * @return $this
  145.      */
  146.     public function setMetaData($metaData)
  147.     {
  148.         $this->metaData $metaData;
  149.         return $this;
  150.     }
  151.     /**
  152.      * @return array
  153.      */
  154.     public function getMetaData()
  155.     {
  156.         return $this->metaData;
  157.     }
  158.     public function getFullPath()
  159.     {
  160.         $path parent::getFullPath();
  161.         // do not use pretty url's when in admin, the current document is wrapped by a hardlink or this document isn't in the current site
  162.         if (!\Pimcore::inAdmin() && !($this instanceof Hardlink\Wrapper\WrapperInterface) && \Pimcore\Tool\Frontend::isDocumentInCurrentSite($this)) {
  163.             // check for a pretty url
  164.             $prettyUrl $this->getPrettyUrl();
  165.             if (!empty($prettyUrl) && strlen($prettyUrl) > 1) {
  166.                 return $prettyUrl;
  167.             }
  168.         }
  169.         return $path;
  170.     }
  171.     /**
  172.      * @param $prettyUrl
  173.      *
  174.      * @return $this
  175.      */
  176.     public function setPrettyUrl($prettyUrl)
  177.     {
  178.         $this->prettyUrl '/' trim($prettyUrl' /');
  179.         if (strlen($this->prettyUrl) < 2) {
  180.             $this->prettyUrl null;
  181.         }
  182.         return $this;
  183.     }
  184.     /**
  185.      * @return string
  186.      */
  187.     public function getPrettyUrl()
  188.     {
  189.         return $this->prettyUrl;
  190.     }
  191.     /**
  192.      * Set linked Target Groups as set in properties panel as list of IDs
  193.      *
  194.      * @param string|array $targetGroupIds
  195.      */
  196.     public function setTargetGroupIds($targetGroupIds)
  197.     {
  198.         if (is_array($targetGroupIds)) {
  199.             $targetGroupIds implode(','$targetGroupIds);
  200.         }
  201.         $targetGroupIds trim($targetGroupIds' ,');
  202.         if (!empty($targetGroupIds)) {
  203.             $targetGroupIds ',' $targetGroupIds ',';
  204.         }
  205.         $this->targetGroupIds $targetGroupIds;
  206.     }
  207.     /**
  208.      * Get serialized list of Target Group IDs
  209.      *
  210.      * @return string
  211.      */
  212.     public function getTargetGroupIds(): string
  213.     {
  214.         return $this->targetGroupIds;
  215.     }
  216.     /**
  217.      * Set assigned target groups
  218.      *
  219.      * @param TargetGroup[]|int[] $targetGroups
  220.      */
  221.     public function setTargetGroups(array $targetGroups)
  222.     {
  223.         $ids array_map(function ($targetGroup) {
  224.             if (is_numeric($targetGroup)) {
  225.                 return (int)$targetGroup;
  226.             } elseif ($targetGroup instanceof TargetGroup) {
  227.                 return $targetGroup->getId();
  228.             }
  229.             return null;
  230.         }, $targetGroups);
  231.         $ids array_filter($ids, function ($id) {
  232.             return null !== $id && $id 0;
  233.         });
  234.         $this->setTargetGroupIds($ids);
  235.     }
  236.     /**
  237.      * Return list of assigned target groups (via properties panel)
  238.      *
  239.      * @return TargetGroup[]
  240.      */
  241.     public function getTargetGroups(): array
  242.     {
  243.         $ids explode(','$this->targetGroupIds);
  244.         $targetGroups array_map(function ($id) {
  245.             $id trim($id);
  246.             if (!empty($id)) {
  247.                 $targetGroup TargetGroup::getById($id);
  248.                 if ($targetGroup) {
  249.                     return $targetGroup;
  250.                 }
  251.             }
  252.         }, $ids);
  253.         $targetGroups array_filter($targetGroups);
  254.         return $targetGroups;
  255.     }
  256.     /**
  257.      * @param bool $hdpi
  258.      *
  259.      * @return string
  260.      */
  261.     public function getPreviewImageFilesystemPath($hdpi false)
  262.     {
  263.         $suffix '';
  264.         if ($hdpi) {
  265.             $suffix '@2x';
  266.         }
  267.         return PIMCORE_SYSTEM_TEMP_DIRECTORY '/document-page-previews/document-page-screenshot-' $this->getId() . $suffix '.jpg';
  268.     }
  269. }