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|null|DataObject|Element\ElementDescriptor
  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.      * @return array|null
  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.      * @return $this
  147.      */
  148.     public function setDataFromResource($data)
  149.     {
  150.         $data \Pimcore\Tool\Serialize::unserialize($data);
  151.         $this->id $data['id'];
  152.         $this->type $data['type'];
  153.         $this->subtype $data['subtype'];
  154.         $this->setElement();
  155.         return $this;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      *
  160.      * @return $this
  161.      */
  162.     public function setDataFromEditmode($data)
  163.     {
  164.         if (is_array($data) && isset($data['id'])) {
  165.             $this->id $data['id'];
  166.             $this->type $data['type'];
  167.             $this->subtype $data['subtype'];
  168.             $this->setElement();
  169.         }
  170.         return $this;
  171.     }
  172.     /**
  173.      * Sets the element by the data stored for the object
  174.      *
  175.      * @return $this
  176.      */
  177.     public function setElement()
  178.     {
  179.         $this->Element\Service::getElementById($this->type$this->id);
  180.         return $this;
  181.     }
  182.     /**
  183.      * {@inheritdoc}
  184.      */
  185.     public function resolveDependencies()
  186.     {
  187.         $this->load();
  188.         $dependencies = [];
  189.         if ($this->instanceof Element\ElementInterface) {
  190.             $elementType Element\Service::getElementType($this->o);
  191.             $key $elementType '_' $this->o->getId();
  192.             $dependencies[$key] = [
  193.                 'id' => $this->o->getId(),
  194.                 'type' => $elementType,
  195.             ];
  196.         }
  197.         return $dependencies;
  198.     }
  199.     /**
  200.      * get correct type of object as string
  201.      *
  202.      * @param Element\ElementInterface|null $object
  203.      *
  204.      * @return string|null
  205.      *
  206.      * @internal
  207.      */
  208.     private function getObjectType($object null)
  209.     {
  210.         $this->load();
  211.         if (!$object) {
  212.             $object $this->o;
  213.         }
  214.         if ($object instanceof Element\ElementInterface) {
  215.             return Element\Service::getElementType($object);
  216.         }
  217.         return null;
  218.     }
  219.     /**
  220.      * {@inheritdoc}
  221.      */
  222.     public function isEmpty()
  223.     {
  224.         $this->load();
  225.         if ($this->instanceof Element\ElementInterface) {
  226.             return false;
  227.         }
  228.         return true;
  229.     }
  230.     /**
  231.      * {@inheritdoc}
  232.      */
  233.     public function checkValidity()
  234.     {
  235.         $sane true;
  236.         if ($this->id) {
  237.             $el Element\Service::getElementById($this->type$this->id);
  238.             if (!$el instanceof Element\ElementInterface) {
  239.                 $sane false;
  240.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  241.                 $this->id null;
  242.                 $this->type null;
  243.                 $this->null;
  244.                 $this->subtype null;
  245.             }
  246.         }
  247.         return $sane;
  248.     }
  249.     /**
  250.      * {@inheritdoc}
  251.      */
  252.     public function __sleep()
  253.     {
  254.         $finalVars = [];
  255.         $parentVars parent::__sleep();
  256.         $blockedVars = ['o'];
  257.         foreach ($parentVars as $key) {
  258.             if (!in_array($key$blockedVars)) {
  259.                 $finalVars[] = $key;
  260.             }
  261.         }
  262.         return $finalVars;
  263.     }
  264.     /**
  265.      * {@inheritdoc}
  266.      */
  267.     public function load() /** : void */
  268.     {
  269.         if (!$this->o) {
  270.             $this->setElement();
  271.         }
  272.     }
  273.     /**
  274.      * @param int $id
  275.      *
  276.      * @return Document\Editable\Renderlet
  277.      */
  278.     public function setId($id)
  279.     {
  280.         $this->id $id;
  281.         return $this;
  282.     }
  283.     /**
  284.      * @return int
  285.      */
  286.     public function getId()
  287.     {
  288.         return (int) $this->id;
  289.     }
  290.     /**
  291.      * @param Asset|Document|DataObject|null $o
  292.      *
  293.      * @return $this
  294.      */
  295.     public function setO($o)
  296.     {
  297.         $this->$o;
  298.         return $this;
  299.     }
  300.     /**
  301.      * @return Asset|Document|DataObject|null
  302.      */
  303.     public function getO()
  304.     {
  305.         return $this->o;
  306.     }
  307.     /**
  308.      * @param string $subtype
  309.      *
  310.      * @return $this
  311.      */
  312.     public function setSubtype($subtype)
  313.     {
  314.         $this->subtype $subtype;
  315.         return $this;
  316.     }
  317.     /**
  318.      * @return string|null
  319.      */
  320.     public function getSubtype()
  321.     {
  322.         return $this->subtype;
  323.     }
  324.     /**
  325.      * {@inheritdoc}
  326.      */
  327.     public function rewriteIds($idMapping/** : void */
  328.     {
  329.         $type = (string) $this->type;
  330.         if ($type && array_key_exists($this->type$idMapping) && array_key_exists($this->getId(), $idMapping[$this->type])) {
  331.             $this->setId($idMapping[$this->type][$this->getId()]);
  332.             $this->setO(null);
  333.         }
  334.     }
  335. }