vendor/pimcore/pimcore/models/DataObject/ClassDefinition/Data/Wysiwyg.php line 219

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\DataObject\ClassDefinition\Data;
  15. use Pimcore\Model;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\DataObject\ClassDefinition\Data;
  18. use Pimcore\Model\Element;
  19. use Pimcore\Normalizer\NormalizerInterface;
  20. use Pimcore\Tool\DomCrawler;
  21. use Pimcore\Tool\Text;
  22. class Wysiwyg extends Data implements ResourcePersistenceAwareInterfaceQueryResourcePersistenceAwareInterfaceTypeDeclarationSupportInterfaceEqualComparisonInterfaceVarExporterInterfaceNormalizerInterfaceIdRewriterInterfacePreGetDataInterface
  23. {
  24.     use DataObject\Traits\SimpleComparisonTrait;
  25.     use Model\DataObject\ClassDefinition\Data\Extension\Text;
  26.     use Extension\ColumnType;
  27.     use Extension\QueryColumnType;
  28.     use DataObject\Traits\SimpleNormalizerTrait;
  29.     /**
  30.      * Static type of this element
  31.      *
  32.      * @internal
  33.      *
  34.      * @var string
  35.      */
  36.     public $fieldtype 'wysiwyg';
  37.     /**
  38.      * @internal
  39.      *
  40.      * @var string|int
  41.      */
  42.     public $width 0;
  43.     /**
  44.      * @internal
  45.      *
  46.      * @var string|int
  47.      */
  48.     public $height 0;
  49.     /**
  50.      * Type for the column to query
  51.      *
  52.      * @internal
  53.      *
  54.      * @var string
  55.      */
  56.     public $queryColumnType 'longtext';
  57.     /**
  58.      * Type for the column
  59.      *
  60.      * @internal
  61.      *
  62.      * @var string
  63.      */
  64.     public $columnType 'longtext';
  65.     /**
  66.      * @internal
  67.      *
  68.      * @var string
  69.      */
  70.     public string $toolbarConfig '';
  71.     /**
  72.      * @internal
  73.      *
  74.      * @var bool
  75.      */
  76.     public $excludeFromSearchIndex false;
  77.     /**
  78.      * @internal
  79.      *
  80.      * @var string|int
  81.      */
  82.     public $maxCharacters 0;
  83.     /**
  84.      * @return string|int
  85.      */
  86.     public function getWidth()
  87.     {
  88.         return $this->width;
  89.     }
  90.     /**
  91.      * @return string|int
  92.      */
  93.     public function getHeight()
  94.     {
  95.         return $this->height;
  96.     }
  97.     /**
  98.      * @param string|int $width
  99.      *
  100.      * @return $this
  101.      */
  102.     public function setWidth($width)
  103.     {
  104.         if (is_numeric($width)) {
  105.             $width = (int)$width;
  106.         }
  107.         $this->width $width;
  108.         return $this;
  109.     }
  110.     /**
  111.      * @param string|int $height
  112.      *
  113.      * @return $this
  114.      */
  115.     public function setHeight($height)
  116.     {
  117.         if (is_numeric($height)) {
  118.             $height = (int)$height;
  119.         }
  120.         $this->height $height;
  121.         return $this;
  122.     }
  123.     /**
  124.      * @param string $toolbarConfig
  125.      */
  126.     public function setToolbarConfig(string $toolbarConfig)
  127.     {
  128.         $this->toolbarConfig $toolbarConfig;
  129.     }
  130.     /**
  131.      * @return string
  132.      */
  133.     public function getToolbarConfig(): string
  134.     {
  135.         return $this->toolbarConfig;
  136.     }
  137.     /**
  138.      * @return bool
  139.      */
  140.     public function isExcludeFromSearchIndex(): bool
  141.     {
  142.         return $this->excludeFromSearchIndex;
  143.     }
  144.     /**
  145.      * @param bool $excludeFromSearchIndex
  146.      *
  147.      * @return $this
  148.      */
  149.     public function setExcludeFromSearchIndex(bool $excludeFromSearchIndex)
  150.     {
  151.         $this->excludeFromSearchIndex $excludeFromSearchIndex;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @return string|int
  156.      */
  157.     public function getMaxCharacters()
  158.     {
  159.         return $this->maxCharacters;
  160.     }
  161.     /**
  162.      * @param string|int $maxCharacters
  163.      */
  164.     public function setMaxCharacters($maxCharacters)
  165.     {
  166.         $this->maxCharacters $maxCharacters;
  167.     }
  168.     /**
  169.      * @see ResourcePersistenceAwareInterface::getDataForResource
  170.      *
  171.      * @param string|null $data
  172.      * @param null|DataObject\Concrete $object
  173.      * @param mixed $params
  174.      *
  175.      * @return string|null
  176.      */
  177.     public function getDataForResource($data$object null$params = [])
  178.     {
  179.         return Text::wysiwygText($data);
  180.     }
  181.     /**
  182.      * @see ResourcePersistenceAwareInterface::getDataFromResource
  183.      *
  184.      * @param string|null $data
  185.      * @param null|DataObject\Concrete $object
  186.      * @param mixed $params
  187.      *
  188.      * @return string|null
  189.      */
  190.     public function getDataFromResource($data$object null$params = [])
  191.     {
  192.         return Text::wysiwygText($data);
  193.     }
  194.     /**
  195.      * @see QueryResourcePersistenceAwareInterface::getDataForQueryResource
  196.      *
  197.      * @param string|null $data
  198.      * @param null|DataObject\Concrete $object
  199.      * @param mixed $params
  200.      *
  201.      * @return string|null
  202.      */
  203.     public function getDataForQueryResource($data$object null$params = [])
  204.     {
  205.         $data $this->getDataForResource($data$object$params);
  206.         if (null !== $data) {
  207.             $data strip_tags($data'<a><img>');
  208.             $data str_replace("\r\n"' '$data);
  209.             $data str_replace("\n"' '$data);
  210.             $data str_replace("\r"' '$data);
  211.             $data str_replace("\t"''$data);
  212.             $data preg_replace('#[ ]+#'' '$data);
  213.         }
  214.         return $data;
  215.     }
  216.     /**
  217.      * {@inheritdoc}
  218.      */
  219.     public function getDataForSearchIndex($object$params = [])
  220.     {
  221.         if ($this->isExcludeFromSearchIndex()) {
  222.             return '';
  223.         } else {
  224.             return parent::getDataForSearchIndex($object$params);
  225.         }
  226.     }
  227.     /**
  228.      * @see Data::getDataForEditmode
  229.      *
  230.      * @param string $data
  231.      * @param null|DataObject\Concrete $object
  232.      * @param mixed $params
  233.      *
  234.      * @return string
  235.      */
  236.     public function getDataForEditmode($data$object null$params = [])
  237.     {
  238.         return $this->getDataForResource($data$object$params);
  239.     }
  240.     /**
  241.      * @see Data::getDataFromEditmode
  242.      *
  243.      * @param string $data
  244.      * @param null|DataObject\Concrete $object
  245.      * @param mixed $params
  246.      *
  247.      * @return string
  248.      */
  249.     public function getDataFromEditmode($data$object null$params = [])
  250.     {
  251.         return $data;
  252.     }
  253.     /**
  254.      * @param string|null $data
  255.      *
  256.      * @return array
  257.      */
  258.     public function resolveDependencies($data)
  259.     {
  260.         return Text::getDependenciesOfWysiwygText($data);
  261.     }
  262.     /**
  263.      * {@inheritdoc}
  264.      */
  265.     public function getCacheTags($data$tags = [])
  266.     {
  267.         return Text::getCacheTagsOfWysiwygText($data$tags);
  268.     }
  269.     /**
  270.      * {@inheritdoc}
  271.      */
  272.     public function checkValidity($data$omitMandatoryCheck false$params = [])
  273.     {
  274.         if (!$omitMandatoryCheck && $this->getMandatory() && empty($data)) {
  275.             throw new Element\ValidationException('Empty mandatory field [ '.$this->getName().' ]');
  276.         }
  277.         $dependencies Text::getDependenciesOfWysiwygText($data);
  278.         if (is_array($dependencies)) {
  279.             foreach ($dependencies as $key => $value) {
  280.                 $el Element\Service::getElementById($value['type'], $value['id']);
  281.                 if (!$el) {
  282.                     throw new Element\ValidationException('Invalid dependency in wysiwyg text');
  283.                 }
  284.             }
  285.         }
  286.     }
  287.     /**
  288.      * @param DataObject\Concrete|DataObject\Localizedfield|DataObject\Classificationstore|DataObject\Objectbrick\Data\AbstractData|DataObject\Fieldcollection\Data\AbstractData $container
  289.      * @param array $params
  290.      *
  291.      * @return string
  292.      */
  293.     public function preGetData(/** mixed */ $container/** array */ $params = []) // : mixed
  294.     {
  295.         $data '';
  296.         if ($container instanceof DataObject\Concrete) {
  297.             $data $container->getObjectVar($this->getName());
  298.         } elseif ($container instanceof DataObject\Localizedfield || $container instanceof DataObject\Classificationstore) {
  299.             $data $params['data'];
  300.         } elseif ($container instanceof DataObject\Fieldcollection\Data\AbstractData) {
  301.             $data $container->getObjectVar($this->getName());
  302.         } elseif ($container instanceof DataObject\Objectbrick\Data\AbstractData) {
  303.             $data $container->getObjectVar($this->getName());
  304.         }
  305.         return Text::wysiwygText($data, [
  306.                 'object' => $container,
  307.                 'context' => $this,
  308.                 'language' => $params['language'] ?? null,
  309.             ]);
  310.     }
  311.     /** Generates a pretty version preview (similar to getVersionPreview) can be either html or
  312.      * a image URL. See the https://github.com/pimcore/object-merger bundle documentation for details
  313.      *
  314.      * @param string|null $data
  315.      * @param DataObject\Concrete|null $object
  316.      * @param mixed $params
  317.      *
  318.      * @return array|string
  319.      */
  320.     public function getDiffVersionPreview($data$object null$params = [])
  321.     {
  322.         if ($data) {
  323.             $value = [];
  324.             $value['html'] = $data;
  325.             $value['type'] = 'html';
  326.             return $value;
  327.         } else {
  328.             return '';
  329.         }
  330.     }
  331.     /**
  332.      * { @inheritdoc }
  333.      */
  334.     public function rewriteIds(/** mixed */ $container/** array */ $idMapping/** array */ $params = []) /** :mixed */
  335.     {
  336.         $data $this->getDataFromObjectParam($container$params);
  337.         if ($data) {
  338.             $html = new DomCrawler($data);
  339.             $es $html->filter('a[pimcore_id], img[pimcore_id]');
  340.             /** @var \DOMElement $el */
  341.             foreach ($es as $el) {
  342.                 if ($el->hasAttribute('href') || $el->hasAttribute('src')) {
  343.                     $type $el->getAttribute('pimcore_type');
  344.                     $id = (int) $el->getAttribute('pimcore_id');
  345.                     if ($idMapping[$type][$id] ?? false) {
  346.                         $el->setAttribute('pimcore_id'strtr($el->getAttribute('pimcore_id'), $idMapping[$type]));
  347.                     }
  348.                 }
  349.             }
  350.             $data $html->html();
  351.             $html->clear();
  352.             unset($html);
  353.         }
  354.         return $data;
  355.     }
  356.     /**
  357.      * {@inheritdoc}
  358.      */
  359.     public function isFilterable(): bool
  360.     {
  361.         return true;
  362.     }
  363.     /**
  364.      * {@inheritdoc}
  365.      */
  366.     public function getParameterTypeDeclaration(): ?string
  367.     {
  368.         return '?string';
  369.     }
  370.     /**
  371.      * {@inheritdoc}
  372.      */
  373.     public function getReturnTypeDeclaration(): ?string
  374.     {
  375.         return '?string';
  376.     }
  377.     /**
  378.      * {@inheritdoc}
  379.      */
  380.     public function getPhpdocInputType(): ?string
  381.     {
  382.         return 'string|null';
  383.     }
  384.     /**
  385.      * {@inheritdoc}
  386.      */
  387.     public function getPhpdocReturnType(): ?string
  388.     {
  389.         return 'string|null';
  390.     }
  391. }