vendor/pimcore/pimcore/models/Document/Editable/Snippet.php line 29

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\Cache;
  16. use Pimcore\Document\Editable\EditableHandler;
  17. use Pimcore\Model;
  18. use Pimcore\Model\Document;
  19. use Pimcore\Model\Site;
  20. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  21. use Pimcore\Tool\DeviceDetector;
  22. /**
  23.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  24.  */
  25. class Snippet extends Model\Document\Editable
  26. {
  27.     /**
  28.      * Contains the ID of the linked snippet
  29.      *
  30.      * @internal
  31.      *
  32.      * @var int|null
  33.      */
  34.     protected ?int $id null;
  35.     /**
  36.      * Contains the object for the snippet
  37.      *
  38.      * @internal
  39.      *
  40.      * @var Document\Snippet|null
  41.      */
  42.     protected $snippet null;
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function getType()
  47.     {
  48.         return 'snippet';
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function getData()
  54.     {
  55.         return $this->id;
  56.     }
  57.     /**
  58.      * @param int $id
  59.      */
  60.     public function setId($id)
  61.     {
  62.         $this->id $id;
  63.     }
  64.     /**
  65.      * @return int
  66.      */
  67.     public function getId()
  68.     {
  69.         return (int) $this->id;
  70.     }
  71.     /**
  72.      * Converts the data so it's suitable for the editmode
  73.      *
  74.      * @return mixed
  75.      */
  76.     public function getDataEditmode()
  77.     {
  78.         if ($this->snippet instanceof Document\Snippet) {
  79.             return [
  80.                 'id' => $this->id,
  81.                 'path' => $this->snippet->getFullPath(),
  82.             ];
  83.         }
  84.         return null;
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function frontend()
  90.     {
  91.         // TODO inject services via DI when editables are built through container
  92.         $container \Pimcore::getContainer();
  93.         $editableHandler $container->get(EditableHandler::class);
  94.         $targetingConfigurator $container->get(DocumentTargetingConfigurator::class);
  95.         if (!$this->snippet instanceof Document\Snippet) {
  96.             return '';
  97.         }
  98.         if (!$this->snippet->isPublished()) {
  99.             return '';
  100.         }
  101.         // apply best matching target group (if any)
  102.         $targetingConfigurator->configureTargetGroup($this->snippet);
  103.         $params $this->config;
  104.         $params['document'] = $this->snippet;
  105.         // check if output-cache is enabled, if so, we're also using the cache here
  106.         $cacheKey null;
  107.         $cacheConfig \Pimcore\Tool\Frontend::isOutputCacheEnabled();
  108.         if ((isset($params['cache']) && $params['cache'] === true) || $cacheConfig) {
  109.             // cleanup params to avoid serializing Element\ElementInterface objects
  110.             $cacheParams $params;
  111.             array_walk($cacheParams, function (&$value$key) {
  112.                 if ($value instanceof Model\Element\ElementInterface) {
  113.                     $value $value->getId();
  114.                 }
  115.             });
  116.             // TODO is this enough for cache or should we disable caching completely?
  117.             if ($this->snippet->getUseTargetGroup()) {
  118.                 $cacheParams['target_group'] = $this->snippet->getUseTargetGroup();
  119.             }
  120.             if (Site::isSiteRequest()) {
  121.                 $cacheParams['siteId'] = Site::getCurrentSite()->getId();
  122.             }
  123.             $cacheKey 'editable_snippet__' md5(serialize($cacheParams));
  124.             if ($content Cache::load($cacheKey)) {
  125.                 return $content;
  126.             }
  127.         }
  128.         $content $editableHandler->renderAction($this->snippet->getController(), $params);
  129.         // write contents to the cache, if output-cache is enabled
  130.         if ($cacheConfig && !DeviceDetector::getInstance()->wasUsed()) {
  131.             $cacheTags = ['output_inline'];
  132.             $cacheTags[] = $cacheConfig['lifetime'] ? 'output_lifetime' 'output';
  133.             Cache::save($content$cacheKey$cacheTags$cacheConfig['lifetime']);
  134.         } elseif (isset($params['cache']) && $params['cache'] === true) {
  135.             Cache::save($content$cacheKey, ['output']);
  136.         }
  137.         return $content;
  138.     }
  139.     /**
  140.      * {@inheritdoc}
  141.      */
  142.     public function setDataFromResource($data)
  143.     {
  144.         if ((int)$data 0) {
  145.             $this->id $data;
  146.             $this->snippet Document\Snippet::getById($this->id);
  147.         }
  148.         return $this;
  149.     }
  150.     /**
  151.      * {@inheritdoc}
  152.      */
  153.     public function setDataFromEditmode($data)
  154.     {
  155.         if ((int)$data 0) {
  156.             $this->id $data;
  157.             $this->snippet Document\Snippet::getById($this->id);
  158.         }
  159.         return $this;
  160.     }
  161.     /**
  162.      * {@inheritdoc}
  163.      */
  164.     public function isEmpty()
  165.     {
  166.         $this->load();
  167.         if ($this->snippet instanceof Document\Snippet) {
  168.             return false;
  169.         }
  170.         return true;
  171.     }
  172.     /**
  173.      * {@inheritdoc}
  174.      */
  175.     public function resolveDependencies()
  176.     {
  177.         $dependencies = [];
  178.         if ($this->snippet instanceof Document\Snippet) {
  179.             $key 'document_' $this->snippet->getId();
  180.             $dependencies[$key] = [
  181.                 'id' => $this->snippet->getId(),
  182.                 'type' => 'document',
  183.             ];
  184.         }
  185.         return $dependencies;
  186.     }
  187.     /**
  188.      * {@inheritdoc}
  189.      */
  190.     public function __sleep()
  191.     {
  192.         $finalVars = [];
  193.         $parentVars parent::__sleep();
  194.         $blockedVars = ['snippet'];
  195.         foreach ($parentVars as $key) {
  196.             if (!in_array($key$blockedVars)) {
  197.                 $finalVars[] = $key;
  198.             }
  199.         }
  200.         return $finalVars;
  201.     }
  202.     /**
  203.      * this method is called by Document\Service::loadAllDocumentFields() to load all lazy loading fields
  204.      */
  205.     public function load()
  206.     {
  207.         if (!$this->snippet && $this->id) {
  208.             $this->snippet Document\Snippet::getById($this->id);
  209.         }
  210.     }
  211.     /**
  212.      * Rewrites id from source to target, $idMapping contains
  213.      * array(
  214.      *  "document" => array(
  215.      *      SOURCE_ID => TARGET_ID,
  216.      *      SOURCE_ID => TARGET_ID
  217.      *  ),
  218.      *  "object" => array(...),
  219.      *  "asset" => array(...)
  220.      * )
  221.      *
  222.      * @param array $idMapping
  223.      */
  224.     public function rewriteIds($idMapping)
  225.     {
  226.         $id $this->getId();
  227.         if (array_key_exists('document'$idMapping) && array_key_exists($id$idMapping['document'])) {
  228.             $this->id $idMapping['document'][$id];
  229.         }
  230.     }
  231.     /**
  232.      * @param Document\Snippet $snippet
  233.      */
  234.     public function setSnippet($snippet)
  235.     {
  236.         if ($snippet instanceof Document\Snippet) {
  237.             $this->id $snippet->getId();
  238.             $this->snippet $snippet;
  239.         }
  240.     }
  241.     /**
  242.      * @return Document\Snippet
  243.      */
  244.     public function getSnippet()
  245.     {
  246.         $this->load();
  247.         return $this->snippet;
  248.     }
  249. }