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

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
  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.      * Converts the data so it's suitable for the editmode
  80.      *
  81.      * @return mixed
  82.      */
  83.     public function getDataEditmode()
  84.     {
  85.         if ($this->instanceof Element\ElementInterface) {
  86.             return [
  87.                 'id' => $this->id,
  88.                 'type' => $this->getObjectType(),
  89.                 'subtype' => $this->subtype,
  90.             ];
  91.         }
  92.         return null;
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function frontend()
  98.     {
  99.         // TODO inject services via DI when editables are built through container
  100.         $container \Pimcore::getContainer();
  101.         $editableHandler $container->get(EditableHandler::class);
  102.         if (!is_array($this->config)) {
  103.             $this->config = [];
  104.         }
  105.         if (empty($this->config['controller']) && !empty($this->config['template'])) {
  106.             $this->config['controller'] = $container->getParameter('pimcore.documents.default_controller');
  107.         }
  108.         if (empty($this->config['controller'])) {
  109.             // this can be the case e.g. in \Pimcore\Model\Search\Backend\Data::setDataFromElement() where
  110.             // this method is called without the config, so it would just render the default controller with the default template
  111.             return '';
  112.         }
  113.         $this->load();
  114.         if ($this->instanceof Element\ElementInterface) {
  115.             if (method_exists($this->o'isPublished')) {
  116.                 if (!$this->o->isPublished()) {
  117.                     return '';
  118.                 }
  119.             }
  120.             // apply best matching target group (if any)
  121.             if ($this->instanceof Document\Targeting\TargetingDocumentInterface) {
  122.                 $targetingConfigurator $container->get(DocumentTargetingConfigurator::class);
  123.                 $targetingConfigurator->configureTargetGroup($this->o);
  124.             }
  125.             $blockparams = ['controller''template'];
  126.             $params = [
  127.                 'template' => isset($this->config['template']) ? $this->config['template'] : null,
  128.                 'id' => $this->id,
  129.                 'type' => $this->type,
  130.                 'subtype' => $this->subtype,
  131.                 'pimcore_request_source' => 'renderlet',
  132.             ];
  133.             foreach ($this->config as $key => $value) {
  134.                 if (!array_key_exists($key$params) && !in_array($key$blockparams)) {
  135.                     $params[$key] = $value;
  136.                 }
  137.             }
  138.             return $editableHandler->renderAction(
  139.                 $this->config['controller'],
  140.                 $params
  141.             );
  142.         }
  143.         return '';
  144.     }
  145.     /**
  146.      * {@inheritdoc}
  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.     public function setDataFromEditmode($data)
  161.     {
  162.         if (is_array($data) && isset($data['id'])) {
  163.             $this->id $data['id'];
  164.             $this->type $data['type'];
  165.             $this->subtype $data['subtype'];
  166.             $this->setElement();
  167.         }
  168.         return $this;
  169.     }
  170.     /**
  171.      * Sets the element by the data stored for the object
  172.      *
  173.      * @return $this
  174.      */
  175.     public function setElement()
  176.     {
  177.         $this->Element\Service::getElementById($this->type$this->id);
  178.         return $this;
  179.     }
  180.     /**
  181.      * {@inheritdoc}
  182.      */
  183.     public function resolveDependencies()
  184.     {
  185.         $this->load();
  186.         $dependencies = [];
  187.         if ($this->instanceof Element\ElementInterface) {
  188.             $elementType Element\Service::getElementType($this->o);
  189.             $key $elementType '_' $this->o->getId();
  190.             $dependencies[$key] = [
  191.                 'id' => $this->o->getId(),
  192.                 'type' => $elementType,
  193.             ];
  194.         }
  195.         return $dependencies;
  196.     }
  197.     /**
  198.      * get correct type of object as string
  199.      *
  200.      * @param Element\ElementInterface|null $object
  201.      *
  202.      * @return string|null
  203.      *
  204.      * @internal param mixed $data
  205.      */
  206.     private function getObjectType($object null)
  207.     {
  208.         $this->load();
  209.         if (!$object) {
  210.             $object $this->o;
  211.         }
  212.         if ($object instanceof Element\ElementInterface) {
  213.             return Element\Service::getElementType($object);
  214.         }
  215.         return null;
  216.     }
  217.     /**
  218.      * {@inheritdoc}
  219.      */
  220.     public function isEmpty()
  221.     {
  222.         $this->load();
  223.         if ($this->instanceof Element\ElementInterface) {
  224.             return false;
  225.         }
  226.         return true;
  227.     }
  228.     /**
  229.      * {@inheritdoc}
  230.      */
  231.     public function checkValidity()
  232.     {
  233.         $sane true;
  234.         if ($this->id) {
  235.             $el Element\Service::getElementById($this->type$this->id);
  236.             if (!$el instanceof Element\ElementInterface) {
  237.                 $sane false;
  238.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  239.                 $this->id null;
  240.                 $this->type null;
  241.                 $this->null;
  242.                 $this->subtype null;
  243.             }
  244.         }
  245.         return $sane;
  246.     }
  247.     /**
  248.      * {@inheritdoc}
  249.      */
  250.     public function __sleep()
  251.     {
  252.         $finalVars = [];
  253.         $parentVars parent::__sleep();
  254.         $blockedVars = ['o'];
  255.         foreach ($parentVars as $key) {
  256.             if (!in_array($key$blockedVars)) {
  257.                 $finalVars[] = $key;
  258.             }
  259.         }
  260.         return $finalVars;
  261.     }
  262.     /**
  263.      * this method is called by Document\Service::loadAllDocumentFields() to load all lazy loading fields
  264.      */
  265.     public function load()
  266.     {
  267.         if (!$this->o) {
  268.             $this->setElement();
  269.         }
  270.     }
  271.     /**
  272.      * @param int $id
  273.      *
  274.      * @return Document\Editable\Renderlet
  275.      */
  276.     public function setId($id)
  277.     {
  278.         $this->id $id;
  279.         return $this;
  280.     }
  281.     /**
  282.      * @return int
  283.      */
  284.     public function getId()
  285.     {
  286.         return (int) $this->id;
  287.     }
  288.     /**
  289.      * @param Asset|Document|Object|null $o
  290.      *
  291.      * @return Document\Editable\Renderlet
  292.      */
  293.     public function setO($o)
  294.     {
  295.         $this->$o;
  296.         return $this;
  297.     }
  298.     /**
  299.      * @return Asset|Document|Object|null
  300.      */
  301.     public function getO()
  302.     {
  303.         return $this->o;
  304.     }
  305.     /**
  306.      * @param string $subtype
  307.      *
  308.      * @return Document\Editable\Renderlet
  309.      */
  310.     public function setSubtype($subtype)
  311.     {
  312.         $this->subtype $subtype;
  313.         return $this;
  314.     }
  315.     /**
  316.      * @return string
  317.      */
  318.     public function getSubtype()
  319.     {
  320.         return $this->subtype;
  321.     }
  322.     /**
  323.      * Rewrites id from source to target, $idMapping contains
  324.      * array(
  325.      *  "document" => array(
  326.      *      SOURCE_ID => TARGET_ID,
  327.      *      SOURCE_ID => TARGET_ID
  328.      *  ),
  329.      *  "object" => array(...),
  330.      *  "asset" => array(...)
  331.      * )
  332.      *
  333.      * @param array $idMapping
  334.      */
  335.     public function rewriteIds($idMapping)
  336.     {
  337.         $type = (string) $this->type;
  338.         if ($type && array_key_exists($this->type$idMapping) && array_key_exists($this->getId(), $idMapping[$this->type])) {
  339.             $this->setId($idMapping[$this->type][$this->getId()]);
  340.             $this->setO(null);
  341.         }
  342.     }
  343. }