Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Editor.php
Go to the documentation of this file.
1 <?php
8 
10 
16 class Editor extends Textarea
17 {
21  private $serializer;
22 
32  public function __construct(
33  Factory $factoryElement,
34  CollectionFactory $factoryCollection,
35  Escaper $escaper,
36  $data = [],
37  \Magento\Framework\Serialize\Serializer\Json $serializer = null
38  ) {
39  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
40 
41  if ($this->isEnabled()) {
42  $this->setType('wysiwyg');
43  $this->setExtType('wysiwyg');
44  } else {
45  $this->setType('textarea');
46  $this->setExtType('textarea');
47  }
48  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
49  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
50  }
51 
55  protected function getButtonTranslations()
56  {
57  $buttonTranslations = [
58  'Insert Image...' => $this->translate('Insert Image...'),
59  'Insert Media...' => $this->translate('Insert Media...'),
60  'Insert File...' => $this->translate('Insert File...'),
61  ];
62 
63  return $buttonTranslations;
64  }
65 
70  protected function getJsonConfig()
71  {
72  if (is_object($this->getConfig()) && method_exists($this->getConfig(), 'toJson')) {
73  return $this->getConfig()->toJson();
74  } else {
75  return $this->serializer->serialize(
76  $this->getConfig()
77  );
78  }
79  }
80 
87  public function getPluginConfigOptions($pluginName, $key = null)
88  {
89  if (!is_array($this->getConfig('plugins'))) {
90  return null;
91  }
92 
93  $plugins = $this->getConfig('plugins');
94 
95  $pluginArrIndex = array_search($pluginName, array_column($plugins, 'name'));
96 
97  if ($pluginArrIndex === false || !isset($plugins[$pluginArrIndex]['options'])) {
98  return null;
99  }
100 
101  $pluginOptions = $plugins[$pluginArrIndex]['options'];
102 
103  if ($key !== null) {
104  return isset($pluginOptions[$key]) ? $pluginOptions[$key] : null;
105  } else {
106  return $pluginOptions;
107  }
108  }
109 
114  public function getElementHtml()
115  {
116  $js = '
117  <script type="text/javascript">
118  //<![CDATA[
119  openEditorPopup = function(url, name, specs, parent) {
120  if ((typeof popups == "undefined") || popups[name] == undefined || popups[name].closed) {
121  if (typeof popups == "undefined") {
122  popups = new Array();
123  }
124  var opener = (parent != undefined ? parent : window);
125  popups[name] = opener.open(url, name, specs);
126  } else {
127  popups[name].focus();
128  }
129  return popups[name];
130  }
131 
132  closeEditorPopup = function(name) {
133  if ((typeof popups != "undefined") && popups[name] != undefined && !popups[name].closed) {
134  popups[name].close();
135  }
136  }
137  //]]>
138  </script>';
139 
140  if ($this->isEnabled()) {
141  $jsSetupObject = 'wysiwyg' . $this->getHtmlId();
142 
143  $forceLoad = '';
144  if (!$this->isHidden()) {
145  if ($this->getForceLoad()) {
146  $forceLoad = $jsSetupObject . '.setup("exact");';
147  } else {
148  $forceLoad = 'jQuery(window).on("load", ' .
149  $jsSetupObject .
150  '.setup.bind(' .
151  $jsSetupObject .
152  ', "exact"));';
153  }
154  }
155 
156  $html = $this->_getButtonsHtml() .
157  '<textarea name="' .
158  $this->getName() .
159  '" title="' .
160  $this->getTitle() .
161  '" ' .
162  $this->_getUiId() .
163  ' id="' .
164  $this->getHtmlId() .
165  '"' .
166  ' class="textarea' .
167  $this->getClass() .
168  '" ' .
169  $this->serialize(
170  $this->getHtmlAttributes()
171  ) .
172  ' >' .
173  $this->getEscapedValue() .
174  '</textarea>' .
175  $js . $this->getInlineJs($jsSetupObject, $forceLoad);
176 
177  $html = $this->_wrapIntoContainer($html);
178  $html .= $this->getAfterElementHtml();
179  return $html;
180  } else {
181  // Display only buttons to additional features
182  if ($this->getPluginConfigOptions('magentowidget', 'window_url')) {
183  $html = $this->_getButtonsHtml() . $js . parent::getElementHtml();
184  if ($this->getConfig('add_widgets')) {
185  $html .= '<script type="text/javascript">
186  //<![CDATA[
187  require(["jquery", "mage/translate", "mage/adminhtml/wysiwyg/widget"], function(jQuery){
188  (function($) {
189  $.mage.translate.add(' . $this->serializer->serialize($this->getButtonTranslations()) . ')
190  })(jQuery);
191  });
192  //]]>
193  </script>';
194  }
195  $html = $this->_wrapIntoContainer($html);
196  return $html;
197  }
198  return parent::getElementHtml();
199  }
200  }
201 
205  public function getTheme()
206  {
207  if (!$this->hasData('theme')) {
208  return 'simple';
209  }
210 
211  return $this->_getData('theme');
212  }
213 
219  protected function _getButtonsHtml()
220  {
221  $buttonsHtml = '<div id="buttons' . $this->getHtmlId() . '" class="buttons-set">';
222  if ($this->isEnabled()) {
223  $buttonsHtml .= $this->_getToggleButtonHtml($this->isToggleButtonVisible());
224  $buttonsHtml .= $this->_getPluginButtonsHtml($this->isHidden());
225  } else {
226  $buttonsHtml .= $this->_getPluginButtonsHtml(true);
227  }
228  $buttonsHtml .= '</div>';
229 
230  return $buttonsHtml;
231  }
232 
239  protected function _getToggleButtonHtml($visible = true)
240  {
241  $html = $this->_getButtonHtml(
242  [
243  'title' => $this->translate('Show / Hide Editor'),
244  'class' => 'action-show-hide',
245  'style' => $visible ? '' : 'display:none',
246  'id' => 'toggle' . $this->getHtmlId(),
247  ]
248  );
249  return $html;
250  }
251 
260  protected function _getPluginButtonsHtml($visible = true)
261  {
262  $buttonsHtml = '';
263 
264  // Button to widget insertion window
265  if ($this->getConfig('add_widgets')) {
266  $buttonsHtml .= $this->_getButtonHtml(
267  [
268  'title' => $this->translate('Insert Widget...'),
269  'onclick' => "widgetTools.openDialog('"
270  . $this->getPluginConfigOptions('magentowidget', 'window_url')
271  . "widget_target_id/" . $this->getHtmlId() . "/')",
272  'class' => 'action-add-widget plugin',
273  'style' => $visible ? '' : 'display:none',
274  ]
275  );
276  }
277 
278  // Button to media images insertion window
279  if ($this->getConfig('add_images')) {
280  $buttonsHtml .= $this->_getButtonHtml(
281  [
282  'title' => $this->translate('Insert Image...'),
283  'onclick' => "MediabrowserUtility.openDialog('"
284  . $this->getConfig('files_browser_window_url')
285  . "target_element_id/" . $this->getHtmlId() . "/"
286  . (null !== $this->getConfig('store_id') ? 'store/'
287  . $this->getConfig('store_id') . '/' : '')
288  . "')",
289  'class' => 'action-add-image plugin',
290  'style' => $visible ? '' : 'display:none',
291  ]
292  );
293  }
294 
295  if (is_array($this->getConfig('plugins'))) {
296  foreach ($this->getConfig('plugins') as $plugin) {
297  if (isset($plugin['options']) && $this->_checkPluginButtonOptions($plugin['options'])) {
298  $buttonOptions = $this->_prepareButtonOptions($plugin['options']);
299  if (!$visible) {
300  $configStyle = '';
301  if (isset($buttonOptions['style'])) {
302  $configStyle = $buttonOptions['style'];
303  }
304  $buttonOptions = array_merge($buttonOptions, ['style' => 'display:none;' . $configStyle]);
305  }
306  $buttonsHtml .= $this->_getButtonHtml($buttonOptions);
307  }
308  }
309  }
310 
311  return $buttonsHtml;
312  }
313 
320  protected function _prepareButtonOptions($options)
321  {
322  $buttonOptions = [];
323  $buttonOptions['class'] = 'plugin';
324  foreach ($options as $name => $value) {
325  $buttonOptions[$name] = $value;
326  }
327  $buttonOptions = $this->_prepareOptions($buttonOptions);
328  return $buttonOptions;
329  }
330 
337  protected function _checkPluginButtonOptions($pluginOptions)
338  {
339  if (!isset($pluginOptions['title'])) {
340  return false;
341  }
342  return true;
343  }
344 
352  protected function _prepareOptions($options)
353  {
354  $preparedOptions = [];
355  foreach ($options as $name => $value) {
356  if (is_array($value) && isset($value['search']) && isset($value['subject'])) {
357  $subject = $value['subject'];
358  foreach ($value['search'] as $part) {
359  $subject = str_replace('{{' . $part . '}}', $this->getDataUsingMethod($part), $subject);
360  }
361  $preparedOptions[$name] = $subject;
362  } else {
363  $preparedOptions[$name] = $value;
364  }
365  }
366  return $preparedOptions;
367  }
368 
376  protected function _getButtonHtml($data)
377  {
378  $html = '<button type="button"';
379  $html .= ' class="scalable ' . (isset($data['class']) ? $data['class'] : '') . '"';
380  $html .= isset($data['onclick']) ? ' onclick="' . $data['onclick'] . '"' : '';
381  $html .= isset($data['style']) ? ' style="' . $data['style'] . '"' : '';
382  $html .= isset($data['id']) ? ' id="' . $data['id'] . '"' : '';
383  $html .= '>';
384  $html .= isset($data['title']) ? '<span><span><span>' . $data['title'] . '</span></span></span>' : '';
385  $html .= '</button>';
386 
387  return $html;
388  }
389 
397  protected function _wrapIntoContainer($html)
398  {
399  if (!$this->getConfig('use_container')) {
400  return '<div class="admin__control-wysiwig">' .$html . '</div>';
401  }
402 
403  $html = '<div id="editor' . $this->getHtmlId() . '"'
404  . ($this->getConfig('no_display') ? ' style="display:none;"' : '')
405  . ($this->getConfig('container_class') ? ' class="admin__control-wysiwig '
406  . $this->getConfig('container_class') . '"' : '')
407  . '>' . $html . '</div>';
408 
409  return $html;
410  }
411 
418  public function getConfig($key = null)
419  {
420  if (!$this->_getData('config') instanceof \Magento\Framework\DataObject) {
421  $config = new \Magento\Framework\DataObject();
422  $this->setConfig($config);
423  }
424  if ($key !== null) {
425  return $this->_getData('config')->getData($key);
426  }
427  return $this->_getData('config');
428  }
429 
436  public function translate($string)
437  {
438  return (string)new \Magento\Framework\Phrase($string);
439  }
440 
446  public function isEnabled()
447  {
448  $result = false;
449  if ($this->getConfig('enabled')) {
450  $result = $this->hasData('wysiwyg') ? $result = $this->getWysiwyg() : true;
451  }
452  return $result;
453  }
454 
460  public function isHidden()
461  {
462  return $this->getConfig('hidden');
463  }
464 
468  protected function isToggleButtonVisible()
469  {
470  return !$this->getConfig()->hasData('toggle_button') || $this->getConfig('toggle_button');
471  }
472 
480  protected function getInlineJs($jsSetupObject, $forceLoad)
481  {
482  $jsString = '
483  <script type="text/javascript">
484  //<![CDATA[
485  window.tinyMCE_GZ = window.tinyMCE_GZ || {};
486  window.tinyMCE_GZ.loaded = true;
487  require([
488  "jquery",
489  "mage/translate",
490  "mage/adminhtml/events",
491  "mage/adminhtml/wysiwyg/tiny_mce/setup",
492  "mage/adminhtml/wysiwyg/widget"
493  ], function(jQuery){' .
494  "\n" .
495  ' (function($) {$.mage.translate.add(' .
496  $this->serializer->serialize(
497  $this->getButtonTranslations()
498  ) .
499  ')})(jQuery);' .
500  "\n" .
501  $jsSetupObject .
502  ' = new wysiwygSetup("' .
503  $this->getHtmlId() .
504  '", ' .
505  $this->getJsonConfig() .
506  ');' .
507  $forceLoad .
508  '
509  editorFormValidationHandler = ' .
510  $jsSetupObject .
511  '.onFormValidation.bind(' .
512  $jsSetupObject .
513  ');
514  Event.observe("toggle' .
515  $this->getHtmlId() .
516  '", "click", ' .
517  $jsSetupObject .
518  '.toggle.bind(' .
519  $jsSetupObject .
520  '));
521  varienGlobalEvents.attachEventHandler("formSubmit", editorFormValidationHandler);
522  varienGlobalEvents.clearEventHandlers("open_browser_callback");
523  varienGlobalEvents.attachEventHandler("open_browser_callback", ' .
524  $jsSetupObject .
525  '.openFileBrowser);
526  //]]>
527  });
528  </script>';
529  return $jsString;
530  }
531 }
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Editor.php:32
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16
getPluginConfigOptions($pluginName, $key=null)
Definition: Editor.php:87
serialize($attributes=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
getInlineJs($jsSetupObject, $forceLoad)
Definition: Editor.php:480
if(!isset($_GET['name'])) $name
Definition: log.php:14