vendor/pimcore/pimcore/models/Document/Editable/Wysiwyg.php line 25

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Model\Document\Editable;
  16. use Pimcore\Model;
  17. use Pimcore\Tool\DomCrawler;
  18. use Pimcore\Tool\Text;
  19. use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
  20. /**
  21.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  22.  */
  23. class Wysiwyg extends Model\Document\Editable implements IdRewriterInterfaceEditmodeDataInterface
  24. {
  25.     private static HtmlSanitizer $pimcoreWysiwygSanitizer;
  26.     /**
  27.      * Contains the text
  28.      *
  29.      * @internal
  30.      */
  31.     protected ?string $text null;
  32.     private static function getWysiwygSanitizer(): HtmlSanitizer
  33.     {
  34.         return self::$pimcoreWysiwygSanitizer ??= \Pimcore::getContainer()->get(Text::PIMCORE_WYSIWYG_SANITIZER_ID);
  35.     }
  36.     public function getType(): string
  37.     {
  38.         return 'wysiwyg';
  39.     }
  40.     public function getData(): mixed
  41.     {
  42.         return (string) $this->text;
  43.     }
  44.     public function getText(): string
  45.     {
  46.         return $this->getData();
  47.     }
  48.     public function getDataEditmode(): ?string
  49.     {
  50.         $document $this->getDocument();
  51.         return Text::wysiwygText($this->text, [
  52.             'document' => $document,
  53.             'context' => $this,
  54.         ]);
  55.     }
  56.     public function frontend()
  57.     {
  58.         $document $this->getDocument();
  59.         return Text::wysiwygText($this->text, [
  60.                 'document' => $document,
  61.                 'context' => $this,
  62.             ]);
  63.     }
  64.     public function setDataFromResource(mixed $data): static
  65.     {
  66.         $this->text $data;
  67.         return $this;
  68.     }
  69.     public function setDataFromEditmode(mixed $data): static
  70.     {
  71.         $this->text $data;
  72.         return $this;
  73.     }
  74.     public function isEmpty(): bool
  75.     {
  76.         return empty($this->text);
  77.     }
  78.     public function resolveDependencies(): array
  79.     {
  80.         return Text::getDependenciesOfWysiwygText($this->text);
  81.     }
  82.     public function getCacheTags(Model\Document\PageSnippet $ownerDocument, array $tags = []): array
  83.     {
  84.         return Text::getCacheTagsOfWysiwygText($this->text$tags);
  85.     }
  86.     public function rewriteIds(array $idMapping): void
  87.     {
  88.         $html = new DomCrawler($this->text);
  89.         $elements $html->filter('a[pimcore_id], img[pimcore_id]');
  90.         /** @var \DOMElement $el */
  91.         foreach ($elements as $el) {
  92.             if ($el->hasAttribute('href') || $el->hasAttribute('src')) {
  93.                 $type $el->getAttribute('pimcore_type');
  94.                 $id = (int)$el->getAttribute('pimcore_id');
  95.                 if ($idMapping[$type][$id] ?? false) {
  96.                     $el->setAttribute('pimcore_id'strtr($el->getAttribute('pimcore_id'), $idMapping[$type]));
  97.                 }
  98.             }
  99.         }
  100.         $this->text $html->html();
  101.         $html->clear();
  102.         unset($html);
  103.     }
  104.     public function save(): void
  105.     {
  106.         if(is_string($this->text)) {
  107.             $helper self::getWysiwygSanitizer();
  108.             $this->text $helper->sanitizeFor('body'$this->text);
  109.         }
  110.         $this->getDao()->save();
  111.     }
  112. }