vendor/pimcore/pimcore/models/DataObject/Data/Link.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 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\DataObject\Data;
  15. use Pimcore\Model\Asset;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\DataObject\Concrete;
  18. use Pimcore\Model\DataObject\OwnerAwareFieldInterface;
  19. use Pimcore\Model\DataObject\Traits\ObjectVarTrait;
  20. use Pimcore\Model\DataObject\Traits\OwnerAwareFieldTrait;
  21. use Pimcore\Model\Document;
  22. use Pimcore\Model\Element\ElementInterface;
  23. use Pimcore\Model\Element\Service;
  24. class Link implements OwnerAwareFieldInterface
  25. {
  26.     use OwnerAwareFieldTrait;
  27.     use ObjectVarTrait;
  28.     /**
  29.      * @var string
  30.      */
  31.     protected $text;
  32.     /**
  33.      * @var string|null
  34.      */
  35.     protected $internalType;
  36.     /**
  37.      * @var int|null
  38.      */
  39.     protected $internal;
  40.     /**
  41.      * @var string|null
  42.      */
  43.     protected $direct;
  44.     /**
  45.      * @var string|null
  46.      */
  47.     protected $linktype;
  48.     /**
  49.      * @var string
  50.      */
  51.     protected $target;
  52.     /**
  53.      * @var string
  54.      */
  55.     protected $parameters;
  56.     /**
  57.      * @var string
  58.      */
  59.     protected $anchor;
  60.     /**
  61.      * @var string
  62.      */
  63.     protected $title;
  64.     /**
  65.      * @var string
  66.      */
  67.     protected $accesskey;
  68.     /**
  69.      * @var string
  70.      */
  71.     protected $rel;
  72.     /**
  73.      * @var string
  74.      */
  75.     protected $tabindex;
  76.     /**
  77.      * @var string
  78.      */
  79.     protected $class;
  80.     /**
  81.      * @var string
  82.      */
  83.     protected $attributes;
  84.     /**
  85.      * @return string
  86.      */
  87.     public function getText()
  88.     {
  89.         return $this->text;
  90.     }
  91.     /**
  92.      * @param string $text
  93.      *
  94.      * @return $this
  95.      */
  96.     public function setText($text)
  97.     {
  98.         $this->text $text;
  99.         $this->markMeDirty();
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return string|null
  104.      */
  105.     public function getInternalType()
  106.     {
  107.         return $this->internalType;
  108.     }
  109.     /**
  110.      * @param string|null $internalType
  111.      *
  112.      * @return $this
  113.      */
  114.     public function setInternalType($internalType)
  115.     {
  116.         $this->internalType $internalType;
  117.         $this->markMeDirty();
  118.         return $this;
  119.     }
  120.     /**
  121.      * @return int|null
  122.      */
  123.     public function getInternal()
  124.     {
  125.         return $this->internal;
  126.     }
  127.     /**
  128.      * @param int|null $internal
  129.      *
  130.      * @return $this
  131.      */
  132.     public function setInternal($internal)
  133.     {
  134.         $this->internal $internal;
  135.         $this->markMeDirty();
  136.         return $this;
  137.     }
  138.     /**
  139.      * @return string|null
  140.      */
  141.     public function getDirect()
  142.     {
  143.         return $this->direct;
  144.     }
  145.     /**
  146.      * @param string|null $direct
  147.      *
  148.      * @return $this
  149.      */
  150.     public function setDirect($direct null)
  151.     {
  152.         $this->direct $direct;
  153.         $this->markMeDirty();
  154.         return $this;
  155.     }
  156.     /**
  157.      * @return string|null
  158.      */
  159.     public function getLinktype()
  160.     {
  161.         return $this->linktype;
  162.     }
  163.     /**
  164.      * @param string|null $linktype
  165.      *
  166.      * @return $this
  167.      */
  168.     public function setLinktype($linktype)
  169.     {
  170.         $this->linktype $linktype;
  171.         $this->markMeDirty();
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return string
  176.      */
  177.     public function getTarget()
  178.     {
  179.         return $this->target;
  180.     }
  181.     /**
  182.      * @param string $target
  183.      *
  184.      * @return $this
  185.      */
  186.     public function setTarget($target)
  187.     {
  188.         $this->target $target;
  189.         $this->markMeDirty();
  190.         return $this;
  191.     }
  192.     /**
  193.      * @return string
  194.      */
  195.     public function getParameters()
  196.     {
  197.         return $this->parameters;
  198.     }
  199.     /**
  200.      * @param string $parameters
  201.      *
  202.      * @return $this
  203.      */
  204.     public function setParameters($parameters)
  205.     {
  206.         $this->parameters $parameters;
  207.         $this->markMeDirty();
  208.         return $this;
  209.     }
  210.     /**
  211.      * @return string
  212.      */
  213.     public function getAnchor()
  214.     {
  215.         return $this->anchor;
  216.     }
  217.     /**
  218.      * @param string $anchor
  219.      *
  220.      * @return $this
  221.      */
  222.     public function setAnchor($anchor)
  223.     {
  224.         $this->anchor $anchor;
  225.         $this->markMeDirty();
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return string
  230.      */
  231.     public function getTitle()
  232.     {
  233.         return $this->title;
  234.     }
  235.     /**
  236.      * @param string $title
  237.      *
  238.      * @return $this
  239.      */
  240.     public function setTitle($title)
  241.     {
  242.         $this->title $title;
  243.         $this->markMeDirty();
  244.         return $this;
  245.     }
  246.     /**
  247.      * @return string
  248.      */
  249.     public function getAccesskey()
  250.     {
  251.         return $this->accesskey;
  252.     }
  253.     /**
  254.      * @param string $accesskey
  255.      *
  256.      * @return $this
  257.      */
  258.     public function setAccesskey($accesskey)
  259.     {
  260.         $this->accesskey $accesskey;
  261.         $this->markMeDirty();
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return string
  266.      */
  267.     public function getRel()
  268.     {
  269.         return $this->rel;
  270.     }
  271.     /**
  272.      * @param string $rel
  273.      *
  274.      * @return $this
  275.      */
  276.     public function setRel($rel)
  277.     {
  278.         $this->rel $rel;
  279.         $this->markMeDirty();
  280.         return $this;
  281.     }
  282.     /**
  283.      * @return string
  284.      */
  285.     public function getTabindex()
  286.     {
  287.         return $this->tabindex;
  288.     }
  289.     /**
  290.      * @param string $tabindex
  291.      *
  292.      * @return $this
  293.      */
  294.     public function setTabindex($tabindex)
  295.     {
  296.         $this->tabindex $tabindex;
  297.         $this->markMeDirty();
  298.         return $this;
  299.     }
  300.     /**
  301.      * @param string $attributes
  302.      */
  303.     public function setAttributes($attributes)
  304.     {
  305.         $this->attributes $attributes;
  306.         $this->markMeDirty();
  307.     }
  308.     /**
  309.      * @return string
  310.      */
  311.     public function getAttributes()
  312.     {
  313.         return $this->attributes;
  314.     }
  315.     /**
  316.      * @param string $class
  317.      */
  318.     public function setClass($class)
  319.     {
  320.         $this->class $class;
  321.         $this->markMeDirty();
  322.     }
  323.     /**
  324.      * @return string
  325.      */
  326.     public function getClass()
  327.     {
  328.         return $this->class;
  329.     }
  330.     /**
  331.      * @param string $path
  332.      *
  333.      * @return $this
  334.      */
  335.     public function setPath($path)
  336.     {
  337.         if (!empty($path)) {
  338.             $matchedElement null;
  339.             if ($this->getLinktype() == 'internal' && $this->getInternalType()) {
  340.                 $matchedElement Service::getElementByPath($this->getInternalType(), $path);
  341.                 if ($matchedElement) {
  342.                     $this->linktype 'internal';
  343.                     $this->internalType $this->getInternalType();
  344.                     $this->internal $matchedElement->getId();
  345.                 }
  346.             }
  347.             if (!$matchedElement) {
  348.                 if ($document Document::getByPath($path)) {
  349.                     $this->linktype 'internal';
  350.                     $this->internalType 'document';
  351.                     $this->internal $document->getId();
  352.                 } elseif ($asset Asset::getByPath($path)) {
  353.                     $this->linktype 'internal';
  354.                     $this->internalType 'asset';
  355.                     $this->internal $asset->getId();
  356.                 } elseif ($object Concrete::getByPath($path)) {
  357.                     $this->linktype 'internal';
  358.                     $this->internalType 'object';
  359.                     $this->internal $object->getId();
  360.                 } else {
  361.                     $this->linktype 'direct';
  362.                     $this->internalType null;
  363.                     $this->direct $path;
  364.                 }
  365.             }
  366.         }
  367.         $this->markMeDirty();
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return string
  372.      */
  373.     public function getPath()
  374.     {
  375.         $path '';
  376.         if ($this->getLinktype() == 'internal') {
  377.             if ($this->getElement() instanceof ElementInterface) {
  378.                 $path $this->getElement()->getFullPath();
  379.             }
  380.         } else {
  381.             $path $this->getDirect();
  382.         }
  383.         return $path;
  384.     }
  385.     /**
  386.      * Returns the plain text path of the link
  387.      *
  388.      * @return string
  389.      */
  390.     public function getHref()
  391.     {
  392.         $path '';
  393.         if ($this->getLinktype() == 'internal') {
  394.             if ($this->getElement() instanceof Document || $this->getElement() instanceof Asset) {
  395.                 $path $this->getElement()->getFullPath();
  396.             } elseif ($this->getElement() instanceof Concrete) {
  397.                 if ($linkGenerator $this->getElement()->getClass()->getLinkGenerator()) {
  398.                     $path $linkGenerator->generate($this->getElement(), [
  399.                         'context' => $this,
  400.                     ]);
  401.                 }
  402.             }
  403.         } else {
  404.             $path $this->getDirect();
  405.         }
  406.         if (strlen($this->getParameters()) > 0) {
  407.             $path .= '?' str_replace('?'''$this->getParameters());
  408.         }
  409.         if (strlen($this->getAnchor()) > 0) {
  410.             $path .= '#' str_replace('#'''$this->getAnchor());
  411.         }
  412.         return $path;
  413.     }
  414.     /**
  415.      * @return DataObject|Asset|Document|null
  416.      */
  417.     public function getElement()
  418.     {
  419.         $element null;
  420.         if ($this->internalType == 'document') {
  421.             $element Document::getById($this->internal);
  422.         } elseif ($this->internalType == 'asset') {
  423.             $element Asset::getById($this->internal);
  424.         } elseif ($this->internalType == 'object') {
  425.             $element Concrete::getById($this->internal);
  426.         }
  427.         return $element;
  428.     }
  429.     /**
  430.      * @param ElementInterface $object
  431.      *
  432.      * @return $this
  433.      */
  434.     public function setElement($object)
  435.     {
  436.         if ($object instanceof ElementInterface) {
  437.             $this->internal $object->getId();
  438.             $this->internalType Service::getElementType($object);
  439.         }
  440.         $this->markMeDirty();
  441.         return $this;
  442.     }
  443.     /**
  444.      * @deprecated use getElement() instead - will be removed in Pimcore 11
  445.      *
  446.      * @return DataObject|Asset|Document|null
  447.      */
  448.     public function getObject()
  449.     {
  450.         trigger_deprecation(
  451.             'pimcore/pimcore',
  452.             '10.0',
  453.             'The Link::getObject() method is deprecated, use Link::getElement() instead.'
  454.         );
  455.         return $this->getElement();
  456.     }
  457.     /**
  458.      * @deprecated use setElement() instead - will be removed in Pimcore 11
  459.      *
  460.      * @param ElementInterface $object
  461.      */
  462.     public function setObject($object)
  463.     {
  464.         trigger_deprecation(
  465.             'pimcore/pimcore',
  466.             '10.0',
  467.             'The Link::setObject() method is deprecated, use Link::setElement() instead.'
  468.         );
  469.         return $this->setElement($object);
  470.     }
  471.     /**
  472.      * @return string
  473.      */
  474.     public function getHtml()
  475.     {
  476.         $attributes = ['rel''tabindex''accesskey''title''target''class'];
  477.         $attribs = [];
  478.         foreach ($attributes as $a) {
  479.             if ($this->$a) {
  480.                 $attribs[] = $a '="' $this->$a '"';
  481.             }
  482.         }
  483.         if ($this->getAttributes()) {
  484.             $attribs[] = $this->getAttributes();
  485.         }
  486.         if (empty($this->text)) {
  487.             return '';
  488.         }
  489.         return '<a href="' $this->getHref() . '" ' implode(' '$attribs) . '>' htmlspecialchars($this->getText()) . '</a>';
  490.     }
  491.     /**
  492.      * @return bool
  493.      */
  494.     public function isEmpty()
  495.     {
  496.         $vars get_object_vars($this);
  497.         foreach ($vars as $key => $value) {
  498.             if (!empty($value)) {
  499.                 return false;
  500.             }
  501.         }
  502.         return true;
  503.     }
  504.     /**
  505.      * @param array $data
  506.      *
  507.      * @return $this
  508.      */
  509.     public function setValues($data = [])
  510.     {
  511.         if (is_array($data) && count($data) > 0) {
  512.             foreach ($data as $key => $value) {
  513.                 $method 'set' $key;
  514.                 if (method_exists($this$method)) {
  515.                     $this->$method($value);
  516.                 }
  517.             }
  518.         }
  519.         $this->markMeDirty();
  520.         return $this;
  521.     }
  522.     /**
  523.      * @return string
  524.      */
  525.     public function __toString()
  526.     {
  527.         return $this->getHtml();
  528.     }
  529. }