vendor/pimcore/pimcore/models/Document/Tag/Renderlet.php line 32

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 Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @category   Pimcore
  12.  * @package    Document
  13.  *
  14.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  15.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  16.  */
  17. namespace Pimcore\Model\Document\Tag;
  18. use Pimcore\Logger;
  19. use Pimcore\Model;
  20. use Pimcore\Model\Asset;
  21. use Pimcore\Model\DataObject;
  22. use Pimcore\Model\Document;
  23. use Pimcore\Model\Element;
  24. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  25. use Pimcore\Tool;
  26. /**
  27.  * @method \Pimcore\Model\Document\Tag\Dao getDao()
  28.  */
  29. class Renderlet extends Model\Document\Tag
  30. {
  31.     /**
  32.      * Contains the ID of the linked object
  33.      *
  34.      * @var int
  35.      */
  36.     public $id;
  37.     /**
  38.      * Contains the object
  39.      *
  40.      * @var Document | Asset | DataObject\AbstractObject
  41.      */
  42.     public $o;
  43.     /**
  44.      * Contains the type
  45.      *
  46.      * @var string
  47.      */
  48.     public $type;
  49.     /**
  50.      * Contains the subtype
  51.      *
  52.      * @var string
  53.      */
  54.     public $subtype;
  55.     /**
  56.      * @see Document\Tag\TagInterface::getType
  57.      *
  58.      * @return string
  59.      */
  60.     public function getType()
  61.     {
  62.         return 'renderlet';
  63.     }
  64.     /**
  65.      * @see Document\Tag\TagInterface::getData
  66.      *
  67.      * @return mixed
  68.      */
  69.     public function getData()
  70.     {
  71.         return [
  72.             'id' => $this->id,
  73.             'type' => $this->getObjectType(),
  74.             'subtype' => $this->subtype
  75.         ];
  76.     }
  77.     /**
  78.      * Converts the data so it's suitable for the editmode
  79.      *
  80.      * @return mixed
  81.      */
  82.     public function getDataEditmode()
  83.     {
  84.         if ($this->instanceof Element\ElementInterface) {
  85.             return [
  86.                 'id' => $this->id,
  87.                 'type' => $this->getObjectType(),
  88.                 'subtype' => $this->subtype
  89.             ];
  90.         }
  91.         return null;
  92.     }
  93.     /**
  94.      * @see Document\Tag\TagInterface::frontend
  95.      *
  96.      * @return string
  97.      */
  98.     public function frontend()
  99.     {
  100.         // TODO inject services via DI when tags are built through container
  101.         $container = \Pimcore::getContainer();
  102.         $tagHandler $container->get('pimcore.document.tag.handler');
  103.         if (!$tagHandler->supports($this->view)) {
  104.             return null;
  105.         }
  106.         if (!$this->options['controller'] && !$this->options['action']) {
  107.             if (is_null($this->options)) {
  108.                 $this->options = [];
  109.             }
  110.             $this->options += Tool::getRoutingDefaults();
  111.         }
  112.         if (method_exists($this->o'isPublished')) {
  113.             if (!$this->o->isPublished()) {
  114.                 return '';
  115.             }
  116.         }
  117.         if ($this->instanceof Element\ElementInterface) {
  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 = ['action''controller''module''bundle''template'];
  124.             $params = [
  125.                 'template' => isset($this->options['template']) ? $this->options['template'] : null,
  126.                 'id' => $this->id,
  127.                 'type' => $this->type,
  128.                 'subtype' => $this->subtype,
  129.                 'pimcore_request_source' => 'renderlet',
  130.             ];
  131.             foreach ($this->options as $key => $value) {
  132.                 if (!array_key_exists($key$params) && !in_array($key$blockparams)) {
  133.                     $params[$key] = $value;
  134.                 }
  135.             }
  136.             try {
  137.                 $moduleOrBundle null;
  138.                 if (isset($this->options['bundle'])) {
  139.                     $moduleOrBundle $this->options['bundle'];
  140.                 } elseif (isset($this->options['module'])) {
  141.                     $moduleOrBundle $this->options['module'];
  142.                 }
  143.                 $content $tagHandler->renderAction(
  144.                     $this->view,
  145.                     $this->options['controller'],
  146.                     $this->options['action'],
  147.                     $moduleOrBundle,
  148.                     $params
  149.                 );
  150.                 return $content;
  151.             } catch (\Exception $e) {
  152.                 if (\Pimcore::inDebugMode()) {
  153.                     return 'ERROR: ' $e->getMessage() . ' (for details see log files in /var/logs)';
  154.                 }
  155.                 Logger::error($e);
  156.             }
  157.         }
  158.     }
  159.     /**
  160.      * @see Document\Tag\TagInterface::setDataFromResource
  161.      *
  162.      * @param mixed $data
  163.      *
  164.      * @return $this
  165.      */
  166.     public function setDataFromResource($data)
  167.     {
  168.         $data = \Pimcore\Tool\Serialize::unserialize($data);
  169.         $this->id $data['id'];
  170.         $this->type $data['type'];
  171.         $this->subtype $data['subtype'];
  172.         $this->setElement();
  173.         return $this;
  174.     }
  175.     /**
  176.      * @see Document\Tag\TagInterface::setDataFromEditmode
  177.      *
  178.      * @param mixed $data
  179.      *
  180.      * @return $this
  181.      */
  182.     public function setDataFromEditmode($data)
  183.     {
  184.         $this->id $data['id'];
  185.         $this->type $data['type'];
  186.         $this->subtype $data['subtype'];
  187.         $this->setElement();
  188.         return $this;
  189.     }
  190.     /**
  191.      * Sets the element by the data stored for the object
  192.      *
  193.      * @return $this
  194.      */
  195.     public function setElement()
  196.     {
  197.         $this->Element\Service::getElementById($this->type$this->id);
  198.         return $this;
  199.     }
  200.     /**
  201.      * @return array
  202.      */
  203.     public function resolveDependencies()
  204.     {
  205.         $this->load();
  206.         $dependencies = [];
  207.         if ($this->instanceof Element\ElementInterface) {
  208.             $elementType Element\Service::getElementType($this->o);
  209.             $key $elementType '_' $this->o->getId();
  210.             $dependencies[$key] = [
  211.                 'id' => $this->o->getId(),
  212.                 'type' => $elementType
  213.             ];
  214.         }
  215.         return $dependencies;
  216.     }
  217.     /**
  218.      * get correct type of object as string
  219.      *
  220.      * @param Element\ElementInterface|null $object
  221.      *
  222.      * @return string|null
  223.      *
  224.      * @internal param mixed $data
  225.      */
  226.     public function getObjectType($object null)
  227.     {
  228.         $this->load();
  229.         if (!$object) {
  230.             $object $this->o;
  231.         }
  232.         if ($object instanceof Element\ElementInterface) {
  233.             return Element\Service::getType($object);
  234.         } else {
  235.             return false;
  236.         }
  237.     }
  238.     /**
  239.      * @return bool
  240.      */
  241.     public function isEmpty()
  242.     {
  243.         $this->load();
  244.         if ($this->instanceof Element\ElementInterface) {
  245.             return false;
  246.         }
  247.         return true;
  248.     }
  249.     /**
  250.      * @param Model\Webservice\Data\Document\Element $wsElement
  251.      * @param Model\Document\PageSnippet $document
  252.      * @param array $params
  253.      * @param Model\Webservice\IdMapperInterface|null $idMapper
  254.      *
  255.      * @throws \Exception
  256.      */
  257.     public function getFromWebserviceImport($wsElement$document null$params = [], $idMapper null)
  258.     {
  259.         $data $this->sanitizeWebserviceData($wsElement->value);
  260.         if ($data->id !== null) {
  261.             $this->type $data->type;
  262.             $this->subtype $data->subtype;
  263.             if (is_numeric($data->id)) {
  264.                 $id $data->id;
  265.                 if ($idMapper) {
  266.                     $id $idMapper->getMappedId($data->type$data->id);
  267.                 }
  268.                 $this->id $id;
  269.                 if ($this->type == 'asset') {
  270.                     $this->Asset::getById($id);
  271.                     if (!$this->instanceof Asset) {
  272.                         if ($idMapper && $idMapper->ignoreMappingFailures()) {
  273.                             $idMapper->recordMappingFailure('document'$this->getDocumentId(), $this->type$this->id);
  274.                         } else {
  275.                             throw new \Exception('cannot get values from web service import - referenced asset with id [ '.$this->id.' ] is unknown');
  276.                         }
  277.                     }
  278.                 } elseif ($this->type == 'document') {
  279.                     $this->Document::getById($id);
  280.                     if (!$this->instanceof Document) {
  281.                         if ($idMapper && $idMapper->ignoreMappingFailures()) {
  282.                             $idMapper->recordMappingFailure('document'$this->getDocumentId(), $this->type$this->id);
  283.                         } else {
  284.                             throw new \Exception('cannot get values from web service import - referenced document with id [ '.$this->id.' ] is unknown');
  285.                         }
  286.                     }
  287.                 } elseif ($this->type == 'object') {
  288.                     $this->DataObject::getById($id);
  289.                     if (!$this->instanceof DataObject\AbstractObject) {
  290.                         if ($idMapper && $idMapper->ignoreMappingFailures()) {
  291.                             $idMapper->recordMappingFailure('document'$this->getDocumentId(), $this->type$this->id);
  292.                         } else {
  293.                             throw new \Exception('cannot get values from web service import - referenced object with id [ '.$this->id.' ] is unknown');
  294.                         }
  295.                     }
  296.                 } else {
  297.                     p_r($this);
  298.                     throw new \Exception('cannot get values from web service import - type is not valid');
  299.                 }
  300.             } else {
  301.                 throw new \Exception('cannot get values from web service import - id is not valid');
  302.             }
  303.         }
  304.     }
  305.     /**
  306.      * @return bool
  307.      */
  308.     public function checkValidity()
  309.     {
  310.         $sane true;
  311.         if ($this->id) {
  312.             $el Element\Service::getElementById($this->type$this->id);
  313.             if (!$el instanceof Element\ElementInterface) {
  314.                 $sane false;
  315.                 Logger::notice('Detected insane relation, removing reference to non existent '.$this->type.' with id ['.$this->id.']');
  316.                 $this->id null;
  317.                 $this->type null;
  318.                 $this->null;
  319.                 $this->subtype null;
  320.             }
  321.         }
  322.         return $sane;
  323.     }
  324.     /**
  325.      * @return array
  326.      */
  327.     public function __sleep()
  328.     {
  329.         $finalVars = [];
  330.         $parentVars parent::__sleep();
  331.         $blockedVars = ['o'];
  332.         foreach ($parentVars as $key) {
  333.             if (!in_array($key$blockedVars)) {
  334.                 $finalVars[] = $key;
  335.             }
  336.         }
  337.         return $finalVars;
  338.     }
  339.     /**
  340.      * this method is called by Document\Service::loadAllDocumentFields() to load all lazy loading fields
  341.      */
  342.     public function load()
  343.     {
  344.         if (!$this->o) {
  345.             $this->setElement();
  346.         }
  347.     }
  348.     /**
  349.      * @param int $id
  350.      *
  351.      * @return Document\Tag\Renderlet
  352.      */
  353.     public function setId($id)
  354.     {
  355.         $this->id $id;
  356.         return $this;
  357.     }
  358.     /**
  359.      * @return int
  360.      */
  361.     public function getId()
  362.     {
  363.         return (int) $this->id;
  364.     }
  365.     /**
  366.      * @param Asset|Document|Object $o
  367.      *
  368.      * @return Document\Tag\Renderlet
  369.      */
  370.     public function setO($o)
  371.     {
  372.         $this->$o;
  373.         return $this;
  374.     }
  375.     /**
  376.      * @return Asset|Document|Object
  377.      */
  378.     public function getO()
  379.     {
  380.         return $this->o;
  381.     }
  382.     /**
  383.      * @param string $subtype
  384.      *
  385.      * @return Document\Tag\Renderlet
  386.      */
  387.     public function setSubtype($subtype)
  388.     {
  389.         $this->subtype $subtype;
  390.         return $this;
  391.     }
  392.     /**
  393.      * @return string
  394.      */
  395.     public function getSubtype()
  396.     {
  397.         return $this->subtype;
  398.     }
  399.     /**
  400.      * Rewrites id from source to target, $idMapping contains
  401.      * array(
  402.      *  "document" => array(
  403.      *      SOURCE_ID => TARGET_ID,
  404.      *      SOURCE_ID => TARGET_ID
  405.      *  ),
  406.      *  "object" => array(...),
  407.      *  "asset" => array(...)
  408.      * )
  409.      *
  410.      * @param array $idMapping
  411.      */
  412.     public function rewriteIds($idMapping)
  413.     {
  414.         $type = (string) $this->type;
  415.         if ($type && array_key_exists($this->type$idMapping) and array_key_exists($this->getId(), $idMapping[$this->type])) {
  416.             $this->setId($idMapping[$this->type][$this->getId()]);
  417.             $this->setO(null);
  418.         }
  419.     }
  420. }