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 implements IdRewriterInterface, EditmodeDataInterface
  22. {
  23. /**
  24. * Contains the text
  25. *
  26. * @internal
  27. *
  28. * @var string|null
  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. * @return string|null
  54. */
  55. public function getDataEditmode() /** : mixed */
  56. {
  57. $document = $this->getDocument();
  58. return Text::wysiwygText($this->text, [
  59. 'document' => $document,
  60. 'context' => $this,
  61. ]);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function frontend()
  67. {
  68. $document = $this->getDocument();
  69. return Text::wysiwygText($this->text, [
  70. 'document' => $document,
  71. 'context' => $this,
  72. ]);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function setDataFromResource($data)
  78. {
  79. $this->text = $data;
  80. return $this;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setDataFromEditmode($data)
  86. {
  87. $this->text = $data;
  88. return $this;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function isEmpty()
  94. {
  95. return empty($this->text);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function resolveDependencies()
  101. {
  102. return Text::getDependenciesOfWysiwygText($this->text);
  103. }
  104. public function getCacheTags(Model\Document\PageSnippet $ownerDocument, array $tags = []): array
  105. {
  106. return Text::getCacheTagsOfWysiwygText($this->text, $tags);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function rewriteIds($idMapping) /** : void */
  112. {
  113. $html = new DomCrawler($this->text);
  114. $elements = $html->filter('a[pimcore_id], img[pimcore_id]');
  115. /** @var \DOMElement $el */
  116. foreach ($elements as $el) {
  117. if ($el->hasAttribute('href') || $el->hasAttribute('src')) {
  118. $type = $el->getAttribute('pimcore_type');
  119. $id = (int)$el->getAttribute('pimcore_id');
  120. if ($idMapping[$type][$id] ?? false) {
  121. $el->setAttribute('pimcore_id', strtr($el->getAttribute('pimcore_id'), $idMapping[$type]));
  122. }
  123. }
  124. }
  125. $this->text = $html->html();
  126. $html->clear();
  127. unset($html);
  128. }
  129. }