vendor/pimcore/pimcore/models/Document/Tag/Snippet.php line 31

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\Cache;
  19. use Pimcore\Logger;
  20. use Pimcore\Model;
  21. use Pimcore\Model\Document;
  22. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  23. use Pimcore\Tool\DeviceDetector;
  24. use Pimcore\Tool\Frontend;
  25. /**
  26.  * @method \Pimcore\Model\Document\Tag\Dao getDao()
  27.  */
  28. class Snippet extends Model\Document\Tag
  29. {
  30.     /**
  31.      * Contains the ID of the linked snippet
  32.      *
  33.      * @var int
  34.      */
  35.     public $id;
  36.     /**
  37.      * Contains the object for the snippet
  38.      *
  39.      * @var Document\Snippet
  40.      */
  41.     public $snippet;
  42.     /**
  43.      * @see Document\Tag\TagInterface::getType
  44.      *
  45.      * @return string
  46.      */
  47.     public function getType()
  48.     {
  49.         return 'snippet';
  50.     }
  51.     /**
  52.      * @see Document\Tag\TagInterface::getData
  53.      *
  54.      * @return mixed
  55.      */
  56.     public function getData()
  57.     {
  58.         return $this->id;
  59.     }
  60.     /**
  61.      * @param int $id
  62.      */
  63.     public function setId($id)
  64.     {
  65.         $this->id $id;
  66.     }
  67.     /**
  68.      * @return int
  69.      */
  70.     public function getId()
  71.     {
  72.         return (int) $this->id;
  73.     }
  74.     /**
  75.      * Converts the data so it's suitable for the editmode
  76.      *
  77.      * @return mixed
  78.      */
  79.     public function getDataEditmode()
  80.     {
  81.         if ($this->snippet instanceof Document\Snippet) {
  82.             return [
  83.                 'id' => $this->id,
  84.                 'path' => $this->snippet->getFullPath()
  85.             ];
  86.         }
  87.         return null;
  88.     }
  89.     /**
  90.      * @see Document\Tag\TagInterface::frontend
  91.      *
  92.      * @return string
  93.      */
  94.     public function frontend()
  95.     {
  96.         // TODO inject services via DI when tags are built through container
  97.         $container = \Pimcore::getContainer();
  98.         $tagHandler $container->get('pimcore.document.tag.handler');
  99.         $targetingConfigurator $container->get(DocumentTargetingConfigurator::class);
  100.         if (!$tagHandler->supports($this->view)) {
  101.             return null;
  102.         }
  103.         try {
  104.             if (!$this->snippet instanceof Document\Snippet) {
  105.                 return null;
  106.             }
  107.             if (!$this->snippet->isPublished()) {
  108.                 return '';
  109.             }
  110.             // apply best matching target group (if any)
  111.             $targetingConfigurator->configureTargetGroup($this->snippet);
  112.             $params $this->options;
  113.             $params['document'] = $this->snippet;
  114.             // check if output-cache is enabled, if so, we're also using the cache here
  115.             $cacheKey null;
  116.             if ($cacheConfig = \Pimcore\Tool\Frontend::isOutputCacheEnabled()) {
  117.                 // cleanup params to avoid serializing Element\ElementInterface objects
  118.                 $cacheParams $params;
  119.                 array_walk($cacheParams, function (&$value$key) {
  120.                     if ($value instanceof Model\Element\ElementInterface) {
  121.                         $value $value->getId();
  122.                     }
  123.                 });
  124.                 // TODO is this enough for cache or should we disable caching completely?
  125.                 if ($this->snippet->getUseTargetGroup()) {
  126.                     $cacheParams['target_group'] = $this->snippet->getUseTargetGroup();
  127.                 }
  128.                 if (Frontend::hasWebpSupport()) {
  129.                     $cacheParams['webp'] = true;
  130.                 }
  131.                 $cacheKey 'tag_snippet__' md5(serialize($cacheParams));
  132.                 if ($content Cache::load($cacheKey)) {
  133.                     return $content;
  134.                 }
  135.             }
  136.             $content $tagHandler->renderAction(
  137.                 $this->view,
  138.                 $this->snippet->getController(),
  139.                 $this->snippet->getAction(),
  140.                 $this->snippet->getModule(),
  141.                 $params
  142.             );
  143.             // write contents to the cache, if output-cache is enabled
  144.             if ($cacheConfig && !DeviceDetector::getInstance()->wasUsed()) {
  145.                 Cache::save($content$cacheKey, ['output''output_inline'], $cacheConfig['lifetime']);
  146.             }
  147.             return $content;
  148.         } catch (\Exception $e) {
  149.             Logger::error($e);
  150.             if (\Pimcore::inDebugMode()) {
  151.                 return 'ERROR: ' $e->getMessage() . ' (for details see log files in /var/logs)';
  152.             }
  153.         }
  154.     }
  155.     /**
  156.      * @see Document\Tag\TagInterface::setDataFromResource
  157.      *
  158.      * @param mixed $data
  159.      *
  160.      * @return $this
  161.      */
  162.     public function setDataFromResource($data)
  163.     {
  164.         if (intval($data) > 0) {
  165.             $this->id $data;
  166.             $this->snippet Document\Snippet::getById($this->id);
  167.         }
  168.         return $this;
  169.     }
  170.     /**
  171.      * @see Document\Tag\TagInterface::setDataFromEditmode
  172.      *
  173.      * @param mixed $data
  174.      *
  175.      * @return $this
  176.      */
  177.     public function setDataFromEditmode($data)
  178.     {
  179.         if (intval($data) > 0) {
  180.             $this->id $data;
  181.             $this->snippet Document\Snippet::getById($this->id);
  182.         }
  183.         return $this;
  184.     }
  185.     /**
  186.      * @return bool
  187.      */
  188.     public function isEmpty()
  189.     {
  190.         $this->load();
  191.         if ($this->snippet instanceof Document\Snippet) {
  192.             return false;
  193.         }
  194.         return true;
  195.     }
  196.     /**
  197.      * @return array
  198.      */
  199.     public function resolveDependencies()
  200.     {
  201.         $dependencies = [];
  202.         if ($this->snippet instanceof Document\Snippet) {
  203.             $key 'document_' $this->snippet->getId();
  204.             $dependencies[$key] = [
  205.                 'id' => $this->snippet->getId(),
  206.                 'type' => 'document'
  207.             ];
  208.         }
  209.         return $dependencies;
  210.     }
  211.     /**
  212.      * @deprecated
  213.      *
  214.      * @param Model\Webservice\Data\Document\Element $wsElement
  215.      * @param Model\Document\PageSnippet $document
  216.      * @param array $params
  217.      * @param Model\Webservice\IdMapperInterface|null $idMapper
  218.      *
  219.      * @throws \Exception
  220.      */
  221.     public function getFromWebserviceImport($wsElement$document null$params = [], $idMapper null)
  222.     {
  223.         $data $this->sanitizeWebserviceData($wsElement->value);
  224.         if ($data->id !== null) {
  225.             $this->id $data->id;
  226.             if (is_numeric($this->id)) {
  227.                 $this->snippet Document\Snippet::getById($this->id);
  228.                 if (!$this->snippet instanceof Document\Snippet) {
  229.                     throw new \Exception('cannot get values from web service import - referenced snippet with id [ ' $this->id ' ] is unknown');
  230.                 }
  231.             } else {
  232.                 throw new \Exception('cannot get values from web service import - id is not valid');
  233.             }
  234.         }
  235.     }
  236.     /**
  237.      * @return array
  238.      */
  239.     public function __sleep()
  240.     {
  241.         $finalVars = [];
  242.         $parentVars parent::__sleep();
  243.         $blockedVars = ['snippet'];
  244.         foreach ($parentVars as $key) {
  245.             if (!in_array($key$blockedVars)) {
  246.                 $finalVars[] = $key;
  247.             }
  248.         }
  249.         return $finalVars;
  250.     }
  251.     /**
  252.      * this method is called by Document\Service::loadAllDocumentFields() to load all lazy loading fields
  253.      */
  254.     public function load()
  255.     {
  256.         if (!$this->snippet && $this->id) {
  257.             $this->snippet Document::getById($this->id);
  258.         }
  259.     }
  260.     /**
  261.      * Rewrites id from source to target, $idMapping contains
  262.      * array(
  263.      *  "document" => array(
  264.      *      SOURCE_ID => TARGET_ID,
  265.      *      SOURCE_ID => TARGET_ID
  266.      *  ),
  267.      *  "object" => array(...),
  268.      *  "asset" => array(...)
  269.      * )
  270.      *
  271.      * @param array $idMapping
  272.      */
  273.     public function rewriteIds($idMapping)
  274.     {
  275.         $id $this->getId();
  276.         if (array_key_exists('document'$idMapping) && array_key_exists($id$idMapping['document'])) {
  277.             $this->id $idMapping['document'][$id];
  278.         }
  279.     }
  280.     /**
  281.      * @param Document\Snippet $snippet
  282.      */
  283.     public function setSnippet($snippet)
  284.     {
  285.         if ($snippet instanceof Document\Snippet) {
  286.             $this->id $snippet->getId();
  287.             $this->snippet $snippet;
  288.         }
  289.     }
  290.     /**
  291.      * @return Document\Snippet
  292.      */
  293.     public function getSnippet()
  294.     {
  295.         $this->load();
  296.         return $this->snippet;
  297.     }
  298. }