vendor/pimcore/pimcore/models/Document/Editable/Link.php line 26

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\Editable;
  15. use Pimcore\Logger;
  16. use Pimcore\Model;
  17. use Pimcore\Model\Asset;
  18. use Pimcore\Model\Document;
  19. /**
  20.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  21.  */
  22. class Link extends Model\Document\Editable implements IdRewriterInterfaceEditmodeDataInterface
  23. {
  24.     /**
  25.      * Contains the data for the link
  26.      *
  27.      * @internal
  28.      *
  29.      * @var array|null
  30.      */
  31.     protected $data;
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function getType()
  36.     {
  37.         return 'link';
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function getData()
  43.     {
  44.         // update path if internal link
  45.         $this->updatePathFromInternal(true);
  46.         return $this->data;
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function getDataEditmode() /** : mixed */
  52.     {
  53.         // update path if internal link
  54.         $this->updatePathFromInternal(truetrue);
  55.         return $this->data;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     protected function getEditmodeElementClasses($options = []): array
  61.     {
  62.         // we don't want the class attribute being applied to the editable container element (<div>, only to the <a> tag inside
  63.         // the default behavior of the parent method is to include the "class" attribute
  64.         $classes = [
  65.             'pimcore_editable',
  66.             'pimcore_editable_' $this->getType(),
  67.         ];
  68.         return $classes;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function frontend()
  74.     {
  75.         $url $this->getHref();
  76.         if (strlen($url) > 0) {
  77.             if (!is_array($this->config)) {
  78.                 $this->config = [];
  79.             }
  80.             $prefix '';
  81.             $suffix '';
  82.             $noText false;
  83.             if (array_key_exists('textPrefix'$this->config)) {
  84.                 $prefix $this->config['textPrefix'];
  85.                 unset($this->config['textPrefix']);
  86.             }
  87.             if (array_key_exists('textSuffix'$this->config)) {
  88.                 $suffix $this->config['textSuffix'];
  89.                 unset($this->config['textSuffix']);
  90.             }
  91.             if (isset($this->config['noText']) && $this->config['noText'] == true) {
  92.                 $noText true;
  93.                 unset($this->config['noText']);
  94.             }
  95.             // add attributes to link
  96.             $allowedAttributes = [
  97.                 'charset',
  98.                 'coords',
  99.                 'hreflang',
  100.                 'name',
  101.                 'rel',
  102.                 'rev',
  103.                 'shape',
  104.                 'target',
  105.                 'accesskey',
  106.                 'class',
  107.                 'dir',
  108.                 'draggable',
  109.                 'dropzone',
  110.                 'contextmenu',
  111.                 'id',
  112.                 'lang',
  113.                 'style',
  114.                 'tabindex',
  115.                 'title',
  116.                 'media',
  117.                 'download',
  118.                 'ping',
  119.                 'type',
  120.                 'referrerpolicy',
  121.                 'xml:lang',
  122.                 'onblur',
  123.                 'onclick',
  124.                 'ondblclick',
  125.                 'onfocus',
  126.                 'onmousedown',
  127.                 'onmousemove',
  128.                 'onmouseout',
  129.                 'onmouseover',
  130.                 'onmouseup',
  131.                 'onkeydown',
  132.                 'onkeypress',
  133.                 'onkeyup',
  134.             ];
  135.             $defaultAttributes = [];
  136.             if (!is_array($this->data)) {
  137.                 $this->data = [];
  138.             }
  139.             $availableAttribs array_merge($defaultAttributes$this->data$this->config);
  140.             // add attributes to link
  141.             $attribs = [];
  142.             foreach ($availableAttribs as $key => $value) {
  143.                 if ((is_string($value) || is_numeric($value))
  144.                     && (strpos($key'data-') === ||
  145.                         strpos($key'aria-') === ||
  146.                         in_array($key$allowedAttributes))) {
  147.                     if (!empty($this->data[$key]) && !empty($this->config[$key])) {
  148.                         $attribs[] = $key.'="'$this->data[$key] .' '$this->config[$key] .'"';
  149.                     } elseif (!empty($value)) {
  150.                         $attribs[] = $key.'="'.$value.'"';
  151.                     }
  152.                 }
  153.             }
  154.             $attribs array_unique($attribs);
  155.             if (array_key_exists('attributes'$this->data) && !empty($this->data['attributes'])) {
  156.                 $attribs[] = $this->data['attributes'];
  157.             }
  158.             return '<a href="'.$url.'" '.implode(' '$attribs).'>' $prefix . ($noText '' htmlspecialchars($this->data['text'])) . $suffix '</a>';
  159.         }
  160.         return '';
  161.     }
  162.     /**
  163.      * {@inheritdoc}
  164.      */
  165.     public function checkValidity()
  166.     {
  167.         $sane true;
  168.         if (is_array($this->data) && isset($this->data['internal']) && $this->data['internal']) {
  169.             if ($this->data['internalType'] == 'document') {
  170.                 $doc Document::getById($this->data['internalId']);
  171.                 if (!$doc) {
  172.                     $sane false;
  173.                     Logger::notice(
  174.                         'Detected insane relation, removing reference to non existent document with id ['.$this->getDocumentId(
  175.                         ).']'
  176.                     );
  177.                     $this->data null;
  178.                 }
  179.             } elseif ($this->data['internalType'] == 'asset') {
  180.                 $asset Asset::getById($this->data['internalId']);
  181.                 if (!$asset) {
  182.                     $sane false;
  183.                     Logger::notice(
  184.                         'Detected insane relation, removing reference to non existent asset with id ['.$this->getDocumentId(
  185.                         ).']'
  186.                     );
  187.                     $this->data null;
  188.                 }
  189.             } elseif ($this->data['internalType'] == 'object') {
  190.                 $object Model\DataObject\Concrete::getById($this->data['internalId']);
  191.                 if (!$object) {
  192.                     $sane false;
  193.                     Logger::notice(
  194.                         'Detected insane relation, removing reference to non existent object with id ['.$this->getDocumentId(
  195.                         ).']'
  196.                     );
  197.                     $this->data null;
  198.                 }
  199.             }
  200.         }
  201.         return $sane;
  202.     }
  203.     /**
  204.      * @return string
  205.      */
  206.     public function getHref()
  207.     {
  208.         $this->updatePathFromInternal();
  209.         $url $this->data['path'] ?? '';
  210.         if (strlen($this->data['parameters'] ?? '') > 0) {
  211.             $url .= (strpos($url'?') !== false '&' '?') . str_replace('?'''$this->getParameters());
  212.         }
  213.         if (strlen($this->data['anchor'] ?? '') > 0) {
  214.             $anchor $this->getAnchor();
  215.             $anchor str_replace('"'urlencode('"'), $anchor);
  216.             $url .= '#' str_replace('#'''$anchor);
  217.         }
  218.         return $url;
  219.     }
  220.     /**
  221.      * @param bool $realPath
  222.      * @param bool $editmode
  223.      */
  224.     private function updatePathFromInternal($realPath false$editmode false)
  225.     {
  226.         $method 'getFullPath';
  227.         if ($realPath) {
  228.             $method 'getRealFullPath';
  229.         }
  230.         if (isset($this->data['internal']) && $this->data['internal']) {
  231.             if ($this->data['internalType'] == 'document') {
  232.                 if ($doc Document::getById($this->data['internalId'])) {
  233.                     if ($editmode || (!Document::doHideUnpublished() || $doc->isPublished())) {
  234.                         $this->data['path'] = $doc->$method();
  235.                     } else {
  236.                         $this->data['path'] = '';
  237.                     }
  238.                 }
  239.             } elseif ($this->data['internalType'] == 'asset') {
  240.                 if ($asset Asset::getById($this->data['internalId'])) {
  241.                     $this->data['path'] = $asset->$method();
  242.                 }
  243.             } elseif ($this->data['internalType'] == 'object') {
  244.                 if ($object Model\DataObject::getById($this->data['internalId'])) {
  245.                     if ($editmode) {
  246.                         $this->data['path'] = $object->getFullPath();
  247.                     } else {
  248.                         if ($object instanceof Model\DataObject\Concrete) {
  249.                             if ($linkGenerator $object->getClass()->getLinkGenerator()) {
  250.                                 if ($realPath) {
  251.                                     $this->data['path'] = $object->getFullPath();
  252.                                 } else {
  253.                                     $this->data['path'] = $linkGenerator->generate(
  254.                                         $object,
  255.                                         [
  256.                                             'document' => $this->getDocument(),
  257.                                             'context' => $this,
  258.                                         ]
  259.                                     );
  260.                                 }
  261.                             }
  262.                         }
  263.                     }
  264.                 }
  265.             }
  266.         }
  267.         // sanitize attributes
  268.         if (isset($this->data['attributes'])) {
  269.             $this->data['attributes'] = htmlspecialchars($this->data['attributes'], HTML_ENTITIES);
  270.         }
  271.         // deletes unnecessary attribute, which was set by mistake in earlier versions, see also
  272.         // https://github.com/pimcore/pimcore/issues/7394
  273.         if (isset($this->data['type'])) {
  274.             unset($this->data['type']);
  275.         }
  276.     }
  277.     /**
  278.      * @return string
  279.      */
  280.     public function getText()
  281.     {
  282.         return $this->data['text'] ?? '';
  283.     }
  284.     /**
  285.      * @param string $text
  286.      */
  287.     public function setText($text)
  288.     {
  289.         $this->data['text'] = $text;
  290.     }
  291.     /**
  292.      * @return string
  293.      */
  294.     public function getTarget()
  295.     {
  296.         return $this->data['target'] ?? '';
  297.     }
  298.     /**
  299.      * @return string
  300.      */
  301.     public function getParameters()
  302.     {
  303.         return $this->data['parameters'] ?? '';
  304.     }
  305.     /**
  306.      * @return string
  307.      */
  308.     public function getAnchor()
  309.     {
  310.         return $this->data['anchor'] ?? '';
  311.     }
  312.     /**
  313.      * @return string
  314.      */
  315.     public function getTitle()
  316.     {
  317.         return $this->data['title'] ?? '';
  318.     }
  319.     /**
  320.      * @return string
  321.      */
  322.     public function getRel()
  323.     {
  324.         return $this->data['rel'] ?? '';
  325.     }
  326.     /**
  327.      * @return string
  328.      */
  329.     public function getTabindex()
  330.     {
  331.         return $this->data['tabindex'] ?? '';
  332.     }
  333.     /**
  334.      * @return string
  335.      */
  336.     public function getAccesskey()
  337.     {
  338.         return $this->data['accesskey'] ?? '';
  339.     }
  340.     /**
  341.      * @return mixed
  342.      */
  343.     public function getClass()
  344.     {
  345.         return $this->data['class'] ?? '';
  346.     }
  347.     /**
  348.      * @return mixed
  349.      */
  350.     public function getAttributes()
  351.     {
  352.         return $this->data['attributes'] ?? '';
  353.     }
  354.     /**
  355.      * {@inheritdoc}
  356.      */
  357.     public function setDataFromResource($data)
  358.     {
  359.         $this->data \Pimcore\Tool\Serialize::unserialize($data);
  360.         if (!is_array($this->data)) {
  361.             $this->data = [];
  362.         }
  363.         return $this;
  364.     }
  365.     /**
  366.      * {@inheritdoc}
  367.      */
  368.     public function setDataFromEditmode($data)
  369.     {
  370.         if (!is_array($data)) {
  371.             $data = [];
  372.         }
  373.         $path $data['path'] ?? null;
  374.         if (!empty($path)) {
  375.             $target null;
  376.             if ($data['linktype'] == 'internal' && $data['internalType']) {
  377.                 $target Model\Element\Service::getElementByPath($data['internalType'], $path);
  378.                 if ($target) {
  379.                     $data['internal'] = true;
  380.                     $data['internalId'] = $target->getId();
  381.                 }
  382.             }
  383.             if (!$target) {
  384.                 if ($target Document::getByPath($path)) {
  385.                     $data['internal'] = true;
  386.                     $data['internalId'] = $target->getId();
  387.                     $data['internalType'] = 'document';
  388.                 } elseif ($target Asset::getByPath($path)) {
  389.                     $data['internal'] = true;
  390.                     $data['internalId'] = $target->getId();
  391.                     $data['internalType'] = 'asset';
  392.                 } elseif ($target Model\DataObject\Concrete::getByPath($path)) {
  393.                     $data['internal'] = true;
  394.                     $data['internalId'] = $target->getId();
  395.                     $data['internalType'] = 'object';
  396.                 } else {
  397.                     $data['internal'] = false;
  398.                     $data['internalId'] = null;
  399.                     $data['internalType'] = null;
  400.                     $data['linktype'] = 'direct';
  401.                 }
  402.                 if ($target) {
  403.                     $data['linktype'] = 'internal';
  404.                 }
  405.             }
  406.         }
  407.         $this->data $data;
  408.         return $this;
  409.     }
  410.     /**
  411.      * {@inheritdoc}
  412.      */
  413.     public function isEmpty()
  414.     {
  415.         return strlen($this->getHref()) < 1;
  416.     }
  417.     /**
  418.      * {@inheritdoc}
  419.      */
  420.     public function resolveDependencies()
  421.     {
  422.         $dependencies = [];
  423.         $isInternal $this->data['internal'] ?? false;
  424.         if (is_array($this->data) && $isInternal) {
  425.             if ((int)$this->data['internalId'] > 0) {
  426.                 if ($this->data['internalType'] == 'document') {
  427.                     if ($doc Document::getById($this->data['internalId'])) {
  428.                         $key 'document_'.$doc->getId();
  429.                         $dependencies[$key] = [
  430.                             'id' => $doc->getId(),
  431.                             'type' => 'document',
  432.                         ];
  433.                     }
  434.                 } elseif ($this->data['internalType'] == 'asset') {
  435.                     if ($asset Asset::getById($this->data['internalId'])) {
  436.                         $key 'asset_'.$asset->getId();
  437.                         $dependencies[$key] = [
  438.                             'id' => $asset->getId(),
  439.                             'type' => 'asset',
  440.                         ];
  441.                     }
  442.                 }
  443.             }
  444.         }
  445.         return $dependencies;
  446.     }
  447.     /**
  448.      * { @inheritdoc }
  449.      */
  450.     public function rewriteIds($idMapping/** : void */
  451.     {
  452.         if (isset($this->data['internal']) && $this->data['internal']) {
  453.             $type $this->data['internalType'];
  454.             $id = (int)$this->data['internalId'];
  455.             if (array_key_exists($type$idMapping)) {
  456.                 if (array_key_exists($id$idMapping[$type])) {
  457.                     $this->data['internalId'] = $idMapping[$type][$id];
  458.                     $this->getHref();
  459.                 }
  460.             }
  461.         }
  462.     }
  463. }