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

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\Model;
  16. use Pimcore\Tool\DomCrawler;
  17. use Pimcore\Tool\Text;
  18. /**
  19.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  20.  */
  21. class Wysiwyg extends Model\Document\Editable
  22. {
  23.     /**
  24.      * Contains the text
  25.      *
  26.      * @internal
  27.      *
  28.      * @var string
  29.      */
  30.     protected $text;
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function getType()
  35.     {
  36.         return 'wysiwyg';
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function getData()
  42.     {
  43.         return $this->text;
  44.     }
  45.     /**
  46.      * @return string
  47.      */
  48.     public function getText()
  49.     {
  50.         return $this->getData();
  51.     }
  52.     /**
  53.      * Converts the data so it's suitable for the editmode
  54.      *
  55.      * @return mixed
  56.      */
  57.     public function getDataEditmode()
  58.     {
  59.         $document $this->getDocument();
  60.         return Text::wysiwygText($this->text, [
  61.             'document' => $document,
  62.             'context' => $this,
  63.         ]);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function frontend()
  69.     {
  70.         $document $this->getDocument();
  71.         return Text::wysiwygText($this->text, [
  72.                 'document' => $document,
  73.                 'context' => $this,
  74.             ]);
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function setDataFromResource($data)
  80.     {
  81.         $this->text $data;
  82.         return $this;
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function setDataFromEditmode($data)
  88.     {
  89.         $this->text $data;
  90.         return $this;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function isEmpty()
  96.     {
  97.         return empty($this->text);
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function resolveDependencies()
  103.     {
  104.         return Text::getDependenciesOfWysiwygText($this->text);
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function getCacheTags(Model\Document\PageSnippet $ownerDocument, array $tags = []): array
  110.     {
  111.         return Text::getCacheTagsOfWysiwygText($this->text$tags);
  112.     }
  113.     /**
  114.      * Rewrites id from source to target, $idMapping contains
  115.      * array(
  116.      *  "document" => array(
  117.      *      SOURCE_ID => TARGET_ID,
  118.      *      SOURCE_ID => TARGET_ID
  119.      *  ),
  120.      *  "object" => array(...),
  121.      *  "asset" => array(...)
  122.      * )
  123.      *
  124.      * @param array $idMapping
  125.      *
  126.      * @return void
  127.      *
  128.      */
  129.     public function rewriteIds($idMapping)
  130.     {
  131.         $html = new DomCrawler($this->text);
  132.         $elements $html->filter('a[pimcore_id], img[pimcore_id]');
  133.         /** @var \DOMElement $el */
  134.         foreach ($elements as $el) {
  135.             if ($el->hasAttribute('href') || $el->hasAttribute('src')) {
  136.                 $type $el->getAttribute('pimcore_type');
  137.                 $id = (int)$el->getAttribute('pimcore_id');
  138.                 if ($idMapping[$type][$id] ?? false) {
  139.                     $el->setAttribute('pimcore_id'strtr($el->getAttribute('pimcore_id'), $idMapping[$type]));
  140.                 }
  141.             }
  142.         }
  143.         $this->text $html->html();
  144.         $html->clear();
  145.         unset($html);
  146.     }
  147. }