Editor.php
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Editor;
defined('JPATH_PLATFORM') or die;
use Joomla\Registry\Registry;
/**
* Editor class to handle WYSIWYG editors
*
* @since 1.5
*/
class Editor extends \JObject
{
/**
* An array of Observer objects to notify
*
* @var array
* @since 1.5
*/
protected $_observers = array();
/**
* The state of the observable object
*
* @var mixed
* @since 1.5
*/
protected $_state = null;
/**
* A multi dimensional array of [function][] = key for observers
*
* @var array
* @since 1.5
*/
protected $_methods = array();
/**
* Editor Plugin object
*
* @var object
* @since 1.5
*/
protected $_editor = null;
/**
* Editor Plugin name
*
* @var string
* @since 1.5
*/
protected $_name = null;
/**
* Object asset
*
* @var string
* @since 1.6
*/
protected $asset = null;
/**
* Object author
*
* @var string
* @since 1.6
*/
protected $author = null;
/**
* Editor instances container.
*
* @var Editor[]
* @since 2.5
*/
protected static $instances = array();
/**
* Constructor
*
* @param string $editor The editor name
*/
public function __construct($editor = 'none')
{
$this->_name = $editor;
}
/**
* Returns the global Editor object, only creating it
* if it doesn't already exist.
*
* @param string $editor The editor to use.
*
* @return Editor The Editor object.
*
* @since 1.5
*/
public static function getInstance($editor = 'none')
{
$signature = serialize($editor);
if (empty(self::$instances[$signature]))
{
self::$instances[$signature] = new Editor($editor);
}
return self::$instances[$signature];
}
/**
* Get the state of the Editor object
*
* @return mixed The state of the object.
*
* @since 1.5
*/
public function getState()
{
return $this->_state;
}
/**
* Attach an observer object
*
* @param array|object $observer An observer object to attach or an array with handler and event keys
*
* @return void
*
* @since 1.5
*/
public function attach($observer)
{
if (is_array($observer))
{
if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler']))
{
return;
}
// Make sure we haven't already attached this array as an observer
foreach ($this->_observers as $check)
{
if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler'])
{
return;
}
}
$this->_observers[] = $observer;
end($this->_observers);
$methods = array($observer['event']);
}
else
{
if (!($observer instanceof Editor))
{
return;
}
// Make sure we haven't already attached this object as an observer
$class = get_class($observer);
foreach ($this->_observers as $check)
{
if ($check instanceof $class)
{
return;
}
}
$this->_observers[] = $observer;
// @todo We require an Editor object above but get the methods from \JPlugin - something isn't right here!
$methods = array_diff(get_class_methods($observer), get_class_methods('\JPlugin'));
}
$key = key($this->_observers);
foreach ($methods as $method)
{
$method = strtolower($method);
if (!isset($this->_methods[$method]))
{
$this->_methods[$method] = array();
}
$this->_methods[$method][] = $key;
}
}
/**
* Detach an observer object
*
* @param object $observer An observer object to detach.
*
* @return boolean True if the observer object was detached.
*
* @since 1.5
*/
public function detach($observer)
{
$retval = false;
$key = array_search($observer, $this->_observers);
if ($key !== false)
{
unset($this->_observers[$key]);
$retval = true;
foreach ($this->_methods as &$method)
{
$k = array_search($key, $method);
if ($k !== false)
{
unset($method[$k]);
}
}
}
return $retval;
}
/**
* Initialise the editor
*
* @return void
*
* @since 1.5
*
* @deprecated 4.0 This function will not load any custom tag from 4.0 forward, use JHtml::script
*/
public function initialise()
{
// Check if editor is already loaded
if ($this->_editor === null)
{
return;
}
$args['event'] = 'onInit';
$return = '';
$results[] = $this->_editor->update($args);
foreach ($results as $result)
{
if (trim($result))
{
// @todo remove code: $return .= $result;
$return = $result;
}
}
$document = \JFactory::getDocument();
if (!empty($return) && method_exists($document, 'addCustomTag'))
{
$document->addCustomTag($return);
}
}
/**
* Display the editor area.
*
* @param string $name The control name.
* @param string $html The contents of the text area.
* @param string $width The width of the text area (px or %).
* @param string $height The height of the text area (px or %).
* @param integer $col The number of columns for the textarea.
* @param integer $row The number of rows for the textarea.
* @param boolean $buttons True and the editor buttons will be displayed.
* @param string $id An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
* @param string $asset The object asset
* @param object $author The author.
* @param array $params Associative array of editor parameters.
*
* @return string
*
* @since 1.5
*/
public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
{
$this->asset = $asset;
$this->author = $author;
$this->_loadEditor($params);
// Check whether editor is already loaded
if ($this->_editor === null)
{
\JFactory::getApplication()->enqueueMessage(\JText::_('JLIB_NO_EDITOR_PLUGIN_PUBLISHED'), 'error');
return;
}
// Backwards compatibility. Width and height should be passed without a semicolon from now on.
// If editor plugins need a unit like "px" for CSS styling, they need to take care of that
$width = str_replace(';', '', $width);
$height = str_replace(';', '', $height);
$return = null;
$args['name'] = $name;
$args['content'] = $html;
$args['width'] = $width;
$args['height'] = $height;
$args['col'] = $col;
$args['row'] = $row;
$args['buttons'] = $buttons;
$args['id'] = $id ?: $name;
$args['asset'] = $asset;
$args['author'] = $author;
$args['params'] = $params;
$args['event'] = 'onDisplay';
$results[] = $this->_editor->update($args);
foreach ($results as $result)
{
if (trim($result))
{
$return .= $result;
}
}
return $return;
}
/**
* Save the editor content
*
* @param string $editor The name of the editor control
*
* @return string
*
* @since 1.5
*
* @deprecated 4.0 Bind functionality to form submit through javascript
*/
public function save($editor)
{
$this->_loadEditor();
// Check whether editor is already loaded
if ($this->_editor === null)
{
return;
}
$args[] = $editor;
$args['event'] = 'onSave';
$return = '';
$results[] = $this->_editor->update($args);
foreach ($results as $result)
{
if (trim($result))
{
$return .= $result;
}
}
return $return;
}
/**
* Get the editor contents
*
* @param string $editor The name of the editor control
*
* @return string
*
* @since 1.5
*
* @deprecated 4.0 Use Joomla.editors API, see core.js
*/
public function getContent($editor)
{
$this->_loadEditor();
$args['name'] = $editor;
$args['event'] = 'onGetContent';
$return = '';
$results[] = $this->_editor->update($args);
foreach ($results as $result)
{
if (trim($result))
{
$return .= $result;
}
}
return $return;
}
/**
* Set the editor contents
*
* @param string $editor The name of the editor control
* @param string $html The contents of the text area
*
* @return string
*
* @since 1.5
*
* @deprecated 4.0 Use Joomla.editors API, see core.js
*/
public function setContent($editor, $html)
{
$this->_loadEditor();
$args['name'] = $editor;
$args['html'] = $html;
$args['event'] = 'onSetContent';
$return = '';
$results[] = $this->_editor->update($args);
foreach ($results as $result)
{
if (trim($result))
{
$return .= $result;
}
}
return $return;
}
/**
* Get the editor extended buttons (usually from plugins)
*
* @param string $editor The name of the editor.
* @param mixed $buttons Can be boolean or array, if boolean defines if the buttons are
* displayed, if array defines a list of buttons not to show.
*
* @return array
*
* @since 1.5
*/
public function getButtons($editor, $buttons = true)
{
$result = array();
if (is_bool($buttons) && !$buttons)
{
return $result;
}
// Get plugins
$plugins = \JPluginHelper::getPlugin('editors-xtd');
foreach ($plugins as $plugin)
{
if (is_array($buttons) && in_array($plugin->name, $buttons))
{
continue;
}
\JPluginHelper::importPlugin('editors-xtd', $plugin->name, false);
$className = 'PlgEditorsXtd' . $plugin->name;
if (!class_exists($className))
{
$className = 'PlgButton' . $plugin->name;
}
if (class_exists($className))
{
$plugin = new $className($this, (array) $plugin);
}
// Try to authenticate
if (!method_exists($plugin, 'onDisplay'))
{
continue;
}
$button = $plugin->onDisplay($editor, $this->asset, $this->author);
if (empty($button))
{
continue;
}
if (is_array($button))
{
$result = array_merge($result, $button);
continue;
}
$result[] = $button;
}
return $result;
}
/**
* Load the editor
*
* @param array $config Associative array of editor config parameters
*
* @return mixed
*
* @since 1.5
*/
protected function _loadEditor($config = array())
{
// Check whether editor is already loaded
if ($this->_editor !== null)
{
return;
}
// Build the path to the needed editor plugin
$name = \JFilterInput::getInstance()->clean($this->_name, 'cmd');
$path = JPATH_PLUGINS . '/editors/' . $name . '/' . $name . '.php';
if (!is_file($path))
{
\JLog::add(\JText::_('JLIB_HTML_EDITOR_CANNOT_LOAD'), \JLog::WARNING, 'jerror');
return false;
}
// Require plugin file
require_once $path;
// Get the plugin
$plugin = \JPluginHelper::getPlugin('editors', $this->_name);
// If no plugin is published we get an empty array and there not so much to do with it
if (empty($plugin))
{
return false;
}
$params = new Registry($plugin->params);
$params->loadArray($config);
$plugin->params = $params;
// Build editor plugin classname
$name = 'PlgEditor' . $this->_name;
if ($this->_editor = new $name($this, (array) $plugin))
{
// Load plugin parameters
$this->initialise();
\JPluginHelper::importPlugin('editors-xtd');
}
}
}