vendor/pimcore/pimcore/models/Document/Editable/Renderlet.php line 30

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\Document\Editable\EditableHandler;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\Asset;
  19. use Pimcore\Model\DataObject;
  20. use Pimcore\Model\Document;
  21. use Pimcore\Model\Element;
  22. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  23. /**
  24.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  25.  */
  26. class Renderlet extends Model\Document\Editable implements IdRewriterInterfaceEditmodeDataInterfaceLazyLoadingInterface
  27. {
  28.     /**
  29.      * Contains the ID of the linked object
  30.      *
  31.      * @internal
  32.      *
  33.      * @var int|null
  34.      */
  35.     protected $id;
  36.     /**
  37.      * Contains the object
  38.      *
  39.      * @internal
  40.      *
  41.      * @var Document|Asset|DataObject|null
  42.      */
  43.     protected $o;
  44.     /**
  45.      * Contains the type
  46.      *
  47.      * @internal
  48.      *
  49.      * @var string|null
  50.      */
  51.     protected $type;
  52.     /**
  53.      * Contains the subtype
  54.      *
  55.      * @internal
  56.      *
  57.      * @var string|null
  58.      */
  59.     protected $subtype;
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getType()
  64.     {
  65.         return 'renderlet';
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getData()
  71.     {
  72.         return [
  73.             'id' => $this->id,
  74.             'type' => $this->getObjectType(),
  75.             'subtype' => $this->subtype,
  76.         ];
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function getDataEditmode() /** : mixed */
  82.     {
  83.         if ($this->instanceof Element\ElementInterface) {
  84.             return [
  85.                 'id' => $this->id,
  86.                 'type' => $this->getObjectType(),
  87.                 'subtype' => $this->subtype,
  88.             ];
  89.         }
  90.         return null;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function frontend()
  96.     {
  97.         // TODO inject services via DI when editables are built through container
  98.         $container \Pimcore::getContainer();
  99.         $editableHandler $container->get(EditableHandler::class);
  100.         if (!is_array($this->config)) {
  101.             $this->config = [];
  102.         }
  103.         if (empty($this->config['controller']) && !empty($this->config['template'])) {
  104.             $this->config['controller'] = $container->getParameter('pimcore.documents.default_controller');
  105.         }
  106.         if (empty($this->config['controller'])) {
  107.             // this can be the case e.g. in \Pimcore\Model\Search\Backend\Data::setDataFromElement() where
  108.             // this method is called without the config, so it would just render the default controller with the default template
  109.             return '';
  110.         }
  111.         $this->load();
  112.         if ($this->instanceof Element\ElementInterface) {
  113.             if (method_exists($this->o'isPublished')) {
  114.                 if (!$this->o->isPublished()) {
  115.                     return '';
  116.                 }
  117.             }
  118.             // apply best matching target group (if any)
  119.             if ($this->instanceof Document\Targeting\TargetingDocumentInterface) {
  120.                 $targetingConfigurator $container->get(DocumentTargetingConfigurator::class);
  121.                 $targetingConfigurator->configureTargetGroup($this->o);
  122.             }
  123.             $blockparams = ['controller''template'];
  124.             $params = [
  125.                 'template' => isset($this->config['template']) ? $this->config['template'] : null,
  126.                 'id' => $this->id,
  127.                 'type' => $this->type,
  128.                 'subtype' => $this->subtype,
  129.                 'pimcore_request_source' => 'renderlet',
  130.             ];
  131.             foreach ($this->config as $key => $value) {
  132.                 if (!array_key_exists($key$params) && !in_array($key$blockparams)) {
  133.                     $params[$key] = $value;
  134.                 }
  135.             }
  136.             return $editableHandler->renderAction(
  137.                 $this->config['controller'],
  138.                 $params
  139.             );
  140.         }
  141.         return '';
  142.     }
  143.     /**
  144.      * {@inheritdoc}
  145.      */
  146.     public function setDataFromResource($data)
  147.     {
  148.         $data \Pimcore\Tool\Serialize::unserialize($data);
  149.         $this->id $data['id'];
  150.         $this->type $data['type'];
  151.         $this->subtype $data['subtype'];
  152.         $this->setElement();
  153.         return $this;
  154.     }
  155.     /**
  156.      * {@inheritdoc}
  157.      */
  158.     public function setDataFromEditmode($data)
  159.     {
  160.         if (is_array($data) && isset($data['id'])) {
  161.             $this->id $data['id'];
  162.             $this->type $data['type'];
  163.             $this->subtype $data['subtype'];
  164.             $this->setElement();
  165.         }
  166.         return $this;
  167.     }
  168.     /**
  169.      * Sets the element by the data stored for the object
  170.      *
  171.      * @return $this
  172.      */
  173.     public function setElement()
  174.     {
  175.         $this->Element\Service::getElementById($this->type$this->id);
  176.         return $this;
  177.     }
  178.     /**
  179.      * {@inheritdoc}
  180.      */
  181.     public function resolveDependencies()
  182.     {
  183.         $this->load();
  184.         $dependencies = [];
  185.         if ($this->instanceof Element\ElementInterface) {
  186.             $elementType Element\Service::getElementType($this->o);
  187.             $key $elementType '_' $this->o->getId();
  188.             $dependencies[$key] = [
  189.                 'id' => $this->o->getId(),
  190.                 'type' => $elementType,
  191.             ];
  192.         }
  193.         return $dependencies;
  194.     }
  195.     /**
  196.      * get correct type of object as string
  197.      *
  198.      * @param Element\ElementInterface|null $object
  199.      *
  200.      * @return string|null
  201.      *
  202.      * @internal param mixed $data
  203.      */
  204.     private function getObjectType($object null)
  205.     {
  206.         $this->load();
  207.         if (!$object) {
  208.             $object $this->o;
  209.         }
  210.         if ($object instanceof Element\ElementInterface) {
  211.             return Element\Service::getElementType($object);
  212.         }
  213.         return null;
  214.     }
  215.     /**
  216.      * {@inheritdoc}
  217.      */
  218.     public function isEmpty()
  219.     {
  220.         $this->load();
  221.         if ($this->instanceof Element\ElementInterface) {
  222.             return false;
  223.         }
  224.         return true;
  225.     }
  226.     /**
  227.      * {@inheritdoc}
  228.      */
  229.     public function checkValidity()
  230.     {
  231.         $sane true;
  232.         if ($this->id) {
  233.             $el Element\Service::getElementById($this->type$this->id);
  234.             if (!$el instanceof Element\ElementInterface) {
  235.                 $sane false;
  236.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  237.                 $this->id null;
  238.                 $this->type null;
  239.                 $this->null;
  240.                 $this->subtype null;
  241.             }
  242.         }
  243.         return $sane;
  244.     }
  245.     /**
  246.      * {@inheritdoc}
  247.      */
  248.     public function __sleep()
  249.     {
  250.         $finalVars = [];
  251.         $parentVars parent::__sleep();
  252.         $blockedVars = ['o'];
  253.         foreach ($parentVars as $key) {
  254.             if (!in_array($key$blockedVars)) {
  255.                 $finalVars[] = $key;
  256.             }
  257.         }
  258.         return $finalVars;
  259.     }
  260.     /**
  261.      * {@inheritdoc}
  262.      */
  263.     public function load() /** : void */
  264.     {
  265.         if (!$this->o) {
  266.             $this->setElement();
  267.         }
  268.     }
  269.     /**
  270.      * @param int $id
  271.      *
  272.      * @return Document\Editable\Renderlet
  273.      */
  274.     public function setId($id)
  275.     {
  276.         $this->id $id;
  277.         return $this;
  278.     }
  279.     /**
  280.      * @return int
  281.      */
  282.     public function getId()
  283.     {
  284.         return (int) $this->id;
  285.     }
  286.     /**
  287.      * @param Asset|Document|DataObject|null $o
  288.      *
  289.      * @return Document\Editable\Renderlet
  290.      */
  291.     public function setO($o)
  292.     {
  293.         $this->$o;
  294.         return $this;
  295.     }
  296.     /**
  297.      * @return Asset|Document|DataObject|null
  298.      */
  299.     public function getO()
  300.     {
  301.         return $this->o;
  302.     }
  303.     /**
  304.      * @param string $subtype
  305.      *
  306.      * @return Document\Editable\Renderlet
  307.      */
  308.     public function setSubtype($subtype)
  309.     {
  310.         $this->subtype $subtype;
  311.         return $this;
  312.     }
  313.     /**
  314.      * @return string
  315.      */
  316.     public function getSubtype()
  317.     {
  318.         return $this->subtype;
  319.     }
  320.     /**
  321.      * { @inheritdoc }
  322.      */
  323.     public function rewriteIds($idMapping/** : void */
  324.     {
  325.         $type = (string) $this->type;
  326.         if ($type && array_key_exists($this->type$idMapping) && array_key_exists($this->getId(), $idMapping[$this->type])) {
  327.             $this->setId($idMapping[$this->type][$this->getId()]);
  328.             $this->setO(null);
  329.         }
  330.     }
  331. }