Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 522 lines (446 sloc) 10.389 kb
f73b659 Imported the new trunk.
Rob Schley authored
1 <?php
2 /**
89e3d3a @LouisLandry Fixed up file headers.
LouisLandry authored
3 * @package Joomla.Platform
4 * @subpackage HTML
0a72863 @LouisLandry Fixing file headers for both unit test and library files.
LouisLandry authored
5 *
4329ba0 @chdemko Tagging 11.4
chdemko authored
6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
0a72863 @LouisLandry Fixing file headers for both unit test and library files.
LouisLandry authored
7 * @license GNU General Public License version 2 or later; see LICENSE
f73b659 Imported the new trunk.
Rob Schley authored
8 */
9
89e3d3a @LouisLandry Fixed up file headers.
LouisLandry authored
10 defined('JPATH_PLATFORM') or die;
f73b659 Imported the new trunk.
Rob Schley authored
11
12 jimport('joomla.event.dispatcher');
13
14 /**
15 * JEditor class to handle WYSIWYG editors
16 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
17 * @package Joomla.Platform
18 * @subpackage HTML
19 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
20 */
6eab224 @Hackwar Removed the dependencies of JEditor and JAuthentication on JObservable
Hackwar authored
21 class JEditor extends JObject
f73b659 Imported the new trunk.
Rob Schley authored
22 {
23 /**
6eab224 @Hackwar Removed the dependencies of JEditor and JAuthentication on JObservable
Hackwar authored
24 * An array of Observer objects to notify
25 *
26 * @var array
27 * @since 11.1
28 */
29 protected $_observers = array();
30
31 /**
32 * The state of the observable object
33 *
34 * @var mixed
35 * @since 11.1
36 */
37 protected $_state = null;
38
39 /**
40 * A multi dimensional array of [function][] = key for observers
41 *
42 * @var array
43 * @since 11.1
44 */
45 protected $_methods = array();
b646828 @Hackwar fixing code style
Hackwar authored
46
6eab224 @Hackwar Removed the dependencies of JEditor and JAuthentication on JObservable
Hackwar authored
47 /**
f73b659 Imported the new trunk.
Rob Schley authored
48 * Editor Plugin object
49 *
7fe7bfa @elinw More on doc blocks.
elinw authored
50 * @var object
f73b659 Imported the new trunk.
Rob Schley authored
51 */
52 protected $_editor = null;
53
54 /**
55 * Editor Plugin name
56 *
7fe7bfa @elinw More on doc blocks.
elinw authored
57 * @var string
f73b659 Imported the new trunk.
Rob Schley authored
58 */
59 protected $_name = null;
60
61 /**
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
62 * Object asset
63 *
7fe7bfa @elinw More on doc blocks.
elinw authored
64 * @var string
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
65 */
66 protected $asset = null;
67
68 /**
69 * Object author
70 *
7fe7bfa @elinw More on doc blocks.
elinw authored
71 * @var string
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
72 */
73 protected $author = null;
74
75 /**
5c175f4 @Hackwar Changing all cases of static instance variables in functions to class st...
Hackwar authored
76 * @var array JEditor instances container.
77 * @since 11.3
78 */
79 protected static $instances = array();
80
81 /**
1644ef5 @elinw html.
elinw authored
82 * Constructor
f73b659 Imported the new trunk.
Rob Schley authored
83 *
b54434f @elinw Missed this one
elinw authored
84 * @param string $editor The editor name
f73b659 Imported the new trunk.
Rob Schley authored
85 */
86 public function __construct($editor = 'none')
87 {
88 $this->_name = $editor;
89 }
90
91 /**
8699a0c @LouisLandry Clean up of reference operators as they aren't needed in PHP5
LouisLandry authored
92 * Returns the global Editor object, only creating it
f73b659 Imported the new trunk.
Rob Schley authored
93 * if it doesn't already exist.
94 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
95 * @param string $editor The editor to use.
d751c2c @eddieajau Trim trailing white space.
eddieajau authored
96 *
7fe7bfa @elinw More on doc blocks.
elinw authored
97 * @return object JEditor The Editor object.
64c87b6 @elinw Docblocks, mainly in files with missing @return tags
elinw authored
98 *
99 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
100 */
8699a0c @LouisLandry Clean up of reference operators as they aren't needed in PHP5
LouisLandry authored
101 public static function getInstance($editor = 'none')
f73b659 Imported the new trunk.
Rob Schley authored
102 {
103 $signature = serialize($editor);
104
5c175f4 @Hackwar Changing all cases of static instance variables in functions to class st...
Hackwar authored
105 if (empty(self::$instances[$signature]))
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
106 {
5c175f4 @Hackwar Changing all cases of static instance variables in functions to class st...
Hackwar authored
107 self::$instances[$signature] = new JEditor($editor);
f73b659 Imported the new trunk.
Rob Schley authored
108 }
109
5c175f4 @Hackwar Changing all cases of static instance variables in functions to class st...
Hackwar authored
110 return self::$instances[$signature];
f73b659 Imported the new trunk.
Rob Schley authored
111 }
112
113 /**
6eab224 @Hackwar Removed the dependencies of JEditor and JAuthentication on JObservable
Hackwar authored
114 * Get the state of the JEditor object
115 *
116 * @return mixed The state of the object.
117 *
118 * @since 11.1
119 */
120 public function getState()
121 {
122 return $this->_state;
123 }
124
125 /**
126 * Attach an observer object
127 *
128 * @param object $observer An observer object to attach
129 *
130 * @return void
131 *
132 * @since 11.1
133 */
134 public function attach($observer)
135 {
136 if (is_array($observer))
137 {
138 if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler']))
139 {
140 return;
141 }
142
143 // Make sure we haven't already attached this array as an observer
144 foreach ($this->_observers as $check)
145 {
146 if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler'])
147 {
148 return;
149 }
150 }
151
152 $this->_observers[] = $observer;
153 end($this->_observers);
154 $methods = array($observer['event']);
155 }
156 else
157 {
158 if (!($observer instanceof JEditor))
159 {
160 return;
161 }
162
163 // Make sure we haven't already attached this object as an observer
164 $class = get_class($observer);
165
166 foreach ($this->_observers as $check)
167 {
168 if ($check instanceof $class)
169 {
170 return;
171 }
172 }
173
174 $this->_observers[] = $observer;
175 $methods = array_diff(get_class_methods($observer), get_class_methods('JPlugin'));
176 }
177
178 $key = key($this->_observers);
179
180 foreach ($methods as $method)
181 {
182 $method = strtolower($method);
183
184 if (!isset($this->_methods[$method]))
185 {
186 $this->_methods[$method] = array();
187 }
188
189 $this->_methods[$method][] = $key;
190 }
191 }
192
193 /**
194 * Detach an observer object
195 *
196 * @param object $observer An observer object to detach.
197 *
198 * @return boolean True if the observer object was detached.
199 *
200 * @since 11.1
201 */
202 public function detach($observer)
203 {
204 // Initialise variables.
205 $retval = false;
206
207 $key = array_search($observer, $this->_observers);
208
209 if ($key !== false)
210 {
211 unset($this->_observers[$key]);
212 $retval = true;
213
214 foreach ($this->_methods as &$method)
215 {
216 $k = array_search($key, $method);
217
218 if ($k !== false)
219 {
220 unset($method[$k]);
221 }
222 }
223 }
224
225 return $retval;
226 }
227
228 /**
2cb0804 @eddieajau Fix up spelling of initialise.
eddieajau authored
229 * Initialise the editor
64c87b6 @elinw Docblocks, mainly in files with missing @return tags
elinw authored
230 *
231 * @return void
232 *
233 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
234 */
235 public function initialise()
236 {
f9434c6 @elkuku Code style: fix single comments
elkuku authored
237 // Check if editor is already loaded
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
238 if (is_null(($this->_editor)))
239 {
f73b659 Imported the new trunk.
Rob Schley authored
240 return;
241 }
242
243 $args['event'] = 'onInit';
244
245 $return = '';
246 $results[] = $this->_editor->update($args);
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
247
248 foreach ($results as $result)
249 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
250 if (trim($result))
251 {
f9434c6 @elkuku Code style: fix single comments
elkuku authored
252 // @todo remove code: $return .= $result;
f73b659 Imported the new trunk.
Rob Schley authored
253 $return = $result;
254 }
255 }
256
aa5891a @eddieajau Fixed issue [#20907] Making Joomla more compliant to PHP strict standard
eddieajau authored
257 $document = JFactory::getDocument();
f73b659 Imported the new trunk.
Rob Schley authored
258 $document->addCustomTag($return);
259 }
260
261 /**
1b37c34 @eddieajau Added new argument in JEditor::display to take an optional 'id' for the ...
eddieajau authored
262 * Display the editor area.
f73b659 Imported the new trunk.
Rob Schley authored
263 *
d043449 @eddieajau Fixing check style warnings.
eddieajau authored
264 * @param string $name The control name.
265 * @param string $html The contents of the text area.
266 * @param string $width The width of the text area (px or %).
267 * @param string $height The height of the text area (px or %).
268 * @param integer $col The number of columns for the textarea.
269 * @param integer $row The number of rows for the textarea.
270 * @param boolean $buttons True and the editor buttons will be displayed.
271 * @param string $id An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
272 * @param string $asset The object asset
292901d @elkuku Code formatting
elkuku authored
273 * @param object $author The author.
d043449 @eddieajau Fixing check style warnings.
eddieajau authored
274 * @param array $params Associative array of editor parameters.
d751c2c @eddieajau Trim trailing white space.
eddieajau authored
275 *
7fe7bfa @elinw More on doc blocks.
elinw authored
276 * @return string
d751c2c @eddieajau Trim trailing white space.
eddieajau authored
277 *
7fe7bfa @elinw More on doc blocks.
elinw authored
278 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
279 */
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
280 public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
f73b659 Imported the new trunk.
Rob Schley authored
281 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
282 $this->asset = $asset;
283 $this->author = $author;
f73b659 Imported the new trunk.
Rob Schley authored
284 $this->_loadEditor($params);
285
1644ef5 @elinw html.
elinw authored
286 // Check whether editor is already loaded
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
287 if (is_null(($this->_editor)))
288 {
f73b659 Imported the new trunk.
Rob Schley authored
289 return;
290 }
291
292 // Backwards compatibility. Width and height should be passed without a semicolon from now on.
293 // If editor plugins need a unit like "px" for CSS styling, they need to take care of that
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
294 $width = str_replace(';', '', $width);
295 $height = str_replace(';', '', $height);
f73b659 Imported the new trunk.
Rob Schley authored
296
2cb0804 @eddieajau Fix up spelling of initialise.
eddieajau authored
297 // Initialise variables.
f73b659 Imported the new trunk.
Rob Schley authored
298 $return = null;
299
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
300 $args['name'] = $name;
301 $args['content'] = $html;
302 $args['width'] = $width;
303 $args['height'] = $height;
304 $args['col'] = $col;
305 $args['row'] = $row;
306 $args['buttons'] = $buttons;
307 $args['id'] = $id ? $id : $name;
308 $args['event'] = 'onDisplay';
f73b659 Imported the new trunk.
Rob Schley authored
309
310 $results[] = $this->_editor->update($args);
311
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
312 foreach ($results as $result)
313 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
314 if (trim($result))
315 {
f73b659 Imported the new trunk.
Rob Schley authored
316 $return .= $result;
317 }
318 }
319 return $return;
320 }
321
322 /**
323 * Save the editor content
324 *
b54434f @elinw Missed this one
elinw authored
325 * @param string $editor The name of the editor control
d751c2c @eddieajau Trim trailing white space.
eddieajau authored
326 *
7fe7bfa @elinw More on doc blocks.
elinw authored
327 * @return string
ccb71ca @LouisLandry Code cleaning.
LouisLandry authored
328 *
7fe7bfa @elinw More on doc blocks.
elinw authored
329 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
330 */
331 public function save($editor)
332 {
333 $this->_loadEditor();
334
1644ef5 @elinw html.
elinw authored
335 // Check whether editor is already loaded
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
336 if (is_null(($this->_editor)))
337 {
f73b659 Imported the new trunk.
Rob Schley authored
338 return;
339 }
340
341 $args[] = $editor;
342 $args['event'] = 'onSave';
343
344 $return = '';
345 $results[] = $this->_editor->update($args);
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
346
347 foreach ($results as $result)
348 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
349 if (trim($result))
350 {
f73b659 Imported the new trunk.
Rob Schley authored
351 $return .= $result;
352 }
353 }
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
354
f73b659 Imported the new trunk.
Rob Schley authored
355 return $return;
356 }
357
358 /**
359 * Get the editor contents
360 *
7fe7bfa @elinw More on doc blocks.
elinw authored
361 * @param string $editor The name of the editor control
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
362 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
363 * @return string
64c87b6 @elinw Docblocks, mainly in files with missing @return tags
elinw authored
364 *
365 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
366 */
367 public function getContent($editor)
368 {
369 $this->_loadEditor();
370
371 $args['name'] = $editor;
372 $args['event'] = 'onGetContent';
373
374 $return = '';
375 $results[] = $this->_editor->update($args);
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
376
377 foreach ($results as $result)
378 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
379 if (trim($result))
380 {
f73b659 Imported the new trunk.
Rob Schley authored
381 $return .= $result;
382 }
383 }
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
384
f73b659 Imported the new trunk.
Rob Schley authored
385 return $return;
386 }
387
388 /**
389 * Set the editor contents
390 *
7fe7bfa @elinw More on doc blocks.
elinw authored
391 * @param string $editor The name of the editor control
392 * @param string $html The contents of the text area
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
393 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
394 * @return string
64c87b6 @elinw Docblocks, mainly in files with missing @return tags
elinw authored
395 *
396 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
397 */
398 public function setContent($editor, $html)
399 {
400 $this->_loadEditor();
401
402 $args['name'] = $editor;
403 $args['html'] = $html;
404 $args['event'] = 'onSetContent';
405
406 $return = '';
407 $results[] = $this->_editor->update($args);
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
408
409 foreach ($results as $result)
410 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
411 if (trim($result))
412 {
f73b659 Imported the new trunk.
Rob Schley authored
413 $return .= $result;
414 }
415 }
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
416
f73b659 Imported the new trunk.
Rob Schley authored
417 return $return;
418 }
419
420 /**
4cb198d @elinw Work on doc blocks in html
elinw authored
421 * Get the editor extended buttons (usually from plugins)
f73b659 Imported the new trunk.
Rob Schley authored
422 *
d043449 @eddieajau Fixing check style warnings.
eddieajau authored
423 * @param string $editor The name of the editor.
424 * @param mixed $buttons Can be boolean or array, if boolean defines if the buttons are
425 * displayed, if array defines a list of buttons not to show.
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
426 *
4cb198d @elinw Work on doc blocks in html
elinw authored
427 * @return array
428 *
a860668 @LouisLandry Fix up some more @since tags as well as platform configuration file load...
LouisLandry authored
429 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
430 */
1c39e7c @eddieajau Convert space indents to tabs
eddieajau authored
431 public function getButtons($editor, $buttons = true)
432 {
f73b659 Imported the new trunk.
Rob Schley authored
433 $result = array();
434
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
435 if (is_bool($buttons) && !$buttons)
436 {
f73b659 Imported the new trunk.
Rob Schley authored
437 return $result;
438 }
439
440 // Get plugins
441 $plugins = JPluginHelper::getPlugin('editors-xtd');
442
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
443 foreach ($plugins as $plugin)
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
444 {
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
445 if (is_array($buttons) && in_array($plugin->name, $buttons))
446 {
f73b659 Imported the new trunk.
Rob Schley authored
447 continue;
448 }
449
7de97a3 @realityking Remove unused local variables. (PMD warnings)
realityking authored
450 JPluginHelper::importPlugin('editors-xtd', $plugin->name, false);
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
451 $className = 'plgButton' . $plugin->name;
f73b659 Imported the new trunk.
Rob Schley authored
452
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
453 if (class_exists($className))
454 {
455 $plugin = new $className($this, (array) $plugin);
f73b659 Imported the new trunk.
Rob Schley authored
456 }
457
458 // Try to authenticate
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
459 if ($temp = $plugin->onDisplay($editor, $this->asset, $this->author))
460 {
f73b659 Imported the new trunk.
Rob Schley authored
461 $result[] = $temp;
462 }
463 }
464
465 return $result;
1c39e7c @eddieajau Convert space indents to tabs
eddieajau authored
466 }
f73b659 Imported the new trunk.
Rob Schley authored
467
468 /**
469 * Load the editor
470 *
7fe7bfa @elinw More on doc blocks.
elinw authored
471 * @param array $config Associative array of editor config paramaters
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
472 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
473 * @return mixed
ccb71ca @LouisLandry Code cleaning.
LouisLandry authored
474 *
53daea8 @eddieajau Code cleaning.
eddieajau authored
475 * @since 11.1
f73b659 Imported the new trunk.
Rob Schley authored
476 */
477 protected function _loadEditor($config = array())
478 {
1644ef5 @elinw html.
elinw authored
479 // Check whether editor is already loaded
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
480 if (!is_null(($this->_editor)))
481 {
f73b659 Imported the new trunk.
Rob Schley authored
482 return;
483 }
484
485 jimport('joomla.filesystem.file');
486
487 // Build the path to the needed editor plugin
f0b5641 @eddieajau Change JFilterInput::clean to object usage.
eddieajau authored
488 $name = JFilterInput::getInstance()->clean($this->_name, 'cmd');
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
489 $path = JPATH_PLUGINS . '/editors/' . $name . '.php';
fad252c @eddieajau Added fix to cope with multiple plugin locations.
eddieajau authored
490
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
491 if (!JFile::exists($path))
492 {
493 $path = JPATH_PLUGINS . '/editors/' . $name . '/' . $name . '.php';
494 if (!JFile::exists($path))
495 {
595a6e2 JError to JLog
Amy Stephen authored
496 JLog::add(JText::_('JLIB_HTML_EDITOR_CANNOT_LOAD'), JLog::WARNING, 'jerror');
fad252c @eddieajau Added fix to cope with multiple plugin locations.
eddieajau authored
497 return false;
498 }
f73b659 Imported the new trunk.
Rob Schley authored
499 }
500
501 // Require plugin file
502 require_once $path;
503
504 // Get the plugin
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
505 $plugin = JPluginHelper::getPlugin('editors', $this->_name);
416c722 @LouisLandry Merging JForm branch down to trunk.
LouisLandry authored
506 $params = new JRegistry;
6871679 @eddieajau Registry package docblocks/code formatting sync'd with Joomla 1.7 repo.
eddieajau authored
507 $params->loadString($plugin->params);
f73b659 Imported the new trunk.
Rob Schley authored
508 $params->loadArray($config);
509 $plugin->params = $params;
510
511 // Build editor plugin classname
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
512 $name = 'plgEditor' . $this->_name;
f9271ad @eddieajau Fixed issue [#23495] ACL for Image Manager access to the uploader (Elin ...
eddieajau authored
513
78e6bc0 @eddieajau Code cleaning.
eddieajau authored
514 if ($this->_editor = new $name($this, (array) $plugin))
515 {
1644ef5 @elinw html.
elinw authored
516 // Load plugin parameters
f73b659 Imported the new trunk.
Rob Schley authored
517 $this->initialise();
518 JPluginHelper::importPlugin('editors-xtd');
519 }
520 }
3edd744 @eddieajau Fixed issue [#24114] Article editor fails to define global settings if p...
eddieajau authored
521 }
Something went wrong with that request. Please try again.