PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/html/editor.php

https://github.com/CCI-Studios/Wee-Magazine
PHP | 522 lines | 259 code | 75 blank | 188 comment | 33 complexity | 41ad17e34ed8267b387bd646156cc45b MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.event.dispatcher');
  11. /**
  12. * JEditor class to handle WYSIWYG editors
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage HTML
  16. * @since 11.1
  17. */
  18. class JEditor extends JObject
  19. {
  20. /**
  21. * An array of Observer objects to notify
  22. *
  23. * @var array
  24. * @since 11.1
  25. */
  26. protected $_observers = array();
  27. /**
  28. * The state of the observable object
  29. *
  30. * @var mixed
  31. * @since 11.1
  32. */
  33. protected $_state = null;
  34. /**
  35. * A multi dimensional array of [function][] = key for observers
  36. *
  37. * @var array
  38. * @since 11.1
  39. */
  40. protected $_methods = array();
  41. /**
  42. * Editor Plugin object
  43. *
  44. * @var object
  45. */
  46. protected $_editor = null;
  47. /**
  48. * Editor Plugin name
  49. *
  50. * @var string
  51. */
  52. protected $_name = null;
  53. /**
  54. * Object asset
  55. *
  56. * @var string
  57. */
  58. protected $asset = null;
  59. /**
  60. * Object author
  61. *
  62. * @var string
  63. */
  64. protected $author = null;
  65. /**
  66. * @var array JEditor instances container.
  67. * @since 11.3
  68. */
  69. protected static $instances = array();
  70. /**
  71. * Constructor
  72. *
  73. * @param string $editor The editor name
  74. */
  75. public function __construct($editor = 'none')
  76. {
  77. $this->_name = $editor;
  78. }
  79. /**
  80. * Returns the global Editor object, only creating it
  81. * if it doesn't already exist.
  82. *
  83. * @param string $editor The editor to use.
  84. *
  85. * @return object JEditor The Editor object.
  86. *
  87. * @since 11.1
  88. */
  89. public static function getInstance($editor = 'none')
  90. {
  91. $signature = serialize($editor);
  92. if (empty(self::$instances[$signature]))
  93. {
  94. self::$instances[$signature] = new JEditor($editor);
  95. }
  96. return self::$instances[$signature];
  97. }
  98. /**
  99. * Get the state of the JEditor object
  100. *
  101. * @return mixed The state of the object.
  102. *
  103. * @since 11.1
  104. */
  105. public function getState()
  106. {
  107. return $this->_state;
  108. }
  109. /**
  110. * Attach an observer object
  111. *
  112. * @param object $observer An observer object to attach
  113. *
  114. * @return void
  115. *
  116. * @since 11.1
  117. */
  118. public function attach($observer)
  119. {
  120. if (is_array($observer))
  121. {
  122. if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler']))
  123. {
  124. return;
  125. }
  126. // Make sure we haven't already attached this array as an observer
  127. foreach ($this->_observers as $check)
  128. {
  129. if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler'])
  130. {
  131. return;
  132. }
  133. }
  134. $this->_observers[] = $observer;
  135. end($this->_observers);
  136. $methods = array($observer['event']);
  137. }
  138. else
  139. {
  140. if (!($observer instanceof JEditor))
  141. {
  142. return;
  143. }
  144. // Make sure we haven't already attached this object as an observer
  145. $class = get_class($observer);
  146. foreach ($this->_observers as $check)
  147. {
  148. if ($check instanceof $class)
  149. {
  150. return;
  151. }
  152. }
  153. $this->_observers[] = $observer;
  154. $methods = array_diff(get_class_methods($observer), get_class_methods('JPlugin'));
  155. }
  156. $key = key($this->_observers);
  157. foreach ($methods as $method)
  158. {
  159. $method = strtolower($method);
  160. if (!isset($this->_methods[$method]))
  161. {
  162. $this->_methods[$method] = array();
  163. }
  164. $this->_methods[$method][] = $key;
  165. }
  166. }
  167. /**
  168. * Detach an observer object
  169. *
  170. * @param object $observer An observer object to detach.
  171. *
  172. * @return boolean True if the observer object was detached.
  173. *
  174. * @since 11.1
  175. */
  176. public function detach($observer)
  177. {
  178. // Initialise variables.
  179. $retval = false;
  180. $key = array_search($observer, $this->_observers);
  181. if ($key !== false)
  182. {
  183. unset($this->_observers[$key]);
  184. $retval = true;
  185. foreach ($this->_methods as &$method)
  186. {
  187. $k = array_search($key, $method);
  188. if ($k !== false)
  189. {
  190. unset($method[$k]);
  191. }
  192. }
  193. }
  194. return $retval;
  195. }
  196. /**
  197. * Initialise the editor
  198. *
  199. * @return void
  200. *
  201. * @since 11.1
  202. */
  203. public function initialise()
  204. {
  205. //check if editor is already loaded
  206. if (is_null(($this->_editor)))
  207. {
  208. return;
  209. }
  210. $args['event'] = 'onInit';
  211. $return = '';
  212. $results[] = $this->_editor->update($args);
  213. foreach ($results as $result)
  214. {
  215. if (trim($result))
  216. {
  217. //$return .= $result;
  218. $return = $result;
  219. }
  220. }
  221. $document = JFactory::getDocument();
  222. $document->addCustomTag($return);
  223. }
  224. /**
  225. * Display the editor area.
  226. *
  227. * @param string $name The control name.
  228. * @param string $html The contents of the text area.
  229. * @param string $width The width of the text area (px or %).
  230. * @param string $height The height of the text area (px or %).
  231. * @param integer $col The number of columns for the textarea.
  232. * @param integer $row The number of rows for the textarea.
  233. * @param boolean $buttons True and the editor buttons will be displayed.
  234. * @param string $id An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
  235. * @param string $asset The object asset
  236. * @param object $author The author.
  237. * @param array $params Associative array of editor parameters.
  238. *
  239. * @return string
  240. *
  241. * @since 11.1
  242. */
  243. public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
  244. {
  245. $this->asset = $asset;
  246. $this->author = $author;
  247. $this->_loadEditor($params);
  248. // Check whether editor is already loaded
  249. if (is_null(($this->_editor)))
  250. {
  251. return;
  252. }
  253. // Backwards compatibility. Width and height should be passed without a semicolon from now on.
  254. // If editor plugins need a unit like "px" for CSS styling, they need to take care of that
  255. $width = str_replace(';', '', $width);
  256. $height = str_replace(';', '', $height);
  257. // Initialise variables.
  258. $return = null;
  259. $args['name'] = $name;
  260. $args['content'] = $html;
  261. $args['width'] = $width;
  262. $args['height'] = $height;
  263. $args['col'] = $col;
  264. $args['row'] = $row;
  265. $args['buttons'] = $buttons;
  266. $args['id'] = $id ? $id : $name;
  267. $args['event'] = 'onDisplay';
  268. $results[] = $this->_editor->update($args);
  269. foreach ($results as $result)
  270. {
  271. if (trim($result))
  272. {
  273. $return .= $result;
  274. }
  275. }
  276. return $return;
  277. }
  278. /**
  279. * Save the editor content
  280. *
  281. * @param string $editor The name of the editor control
  282. *
  283. * @return string
  284. *
  285. * @since 11.1
  286. */
  287. public function save($editor)
  288. {
  289. $this->_loadEditor();
  290. // Check whether editor is already loaded
  291. if (is_null(($this->_editor)))
  292. {
  293. return;
  294. }
  295. $args[] = $editor;
  296. $args['event'] = 'onSave';
  297. $return = '';
  298. $results[] = $this->_editor->update($args);
  299. foreach ($results as $result)
  300. {
  301. if (trim($result))
  302. {
  303. $return .= $result;
  304. }
  305. }
  306. return $return;
  307. }
  308. /**
  309. * Get the editor contents
  310. *
  311. * @param string $editor The name of the editor control
  312. *
  313. * @return string
  314. *
  315. * @since 11.1
  316. */
  317. public function getContent($editor)
  318. {
  319. $this->_loadEditor();
  320. $args['name'] = $editor;
  321. $args['event'] = 'onGetContent';
  322. $return = '';
  323. $results[] = $this->_editor->update($args);
  324. foreach ($results as $result)
  325. {
  326. if (trim($result))
  327. {
  328. $return .= $result;
  329. }
  330. }
  331. return $return;
  332. }
  333. /**
  334. * Set the editor contents
  335. *
  336. * @param string $editor The name of the editor control
  337. * @param string $html The contents of the text area
  338. *
  339. * @return string
  340. *
  341. * @since 11.1
  342. */
  343. public function setContent($editor, $html)
  344. {
  345. $this->_loadEditor();
  346. $args['name'] = $editor;
  347. $args['html'] = $html;
  348. $args['event'] = 'onSetContent';
  349. $return = '';
  350. $results[] = $this->_editor->update($args);
  351. foreach ($results as $result)
  352. {
  353. if (trim($result))
  354. {
  355. $return .= $result;
  356. }
  357. }
  358. return $return;
  359. }
  360. /**
  361. * Get the editor extended buttons (usually from plugins)
  362. *
  363. * @param string $editor The name of the editor.
  364. * @param mixed $buttons Can be boolean or array, if boolean defines if the buttons are
  365. * displayed, if array defines a list of buttons not to show.
  366. *
  367. * @return array
  368. *
  369. * @since 11.1
  370. */
  371. public function getButtons($editor, $buttons = true)
  372. {
  373. $result = array();
  374. if (is_bool($buttons) && !$buttons)
  375. {
  376. return $result;
  377. }
  378. // Get plugins
  379. $plugins = JPluginHelper::getPlugin('editors-xtd');
  380. foreach ($plugins as $plugin)
  381. {
  382. if (is_array($buttons) && in_array($plugin->name, $buttons))
  383. {
  384. continue;
  385. }
  386. JPluginHelper::importPlugin('editors-xtd', $plugin->name, false);
  387. $className = 'plgButton' . $plugin->name;
  388. if (class_exists($className))
  389. {
  390. $plugin = new $className($this, (array) $plugin);
  391. }
  392. // Try to authenticate
  393. if ($temp = $plugin->onDisplay($editor, $this->asset, $this->author))
  394. {
  395. $result[] = $temp;
  396. }
  397. }
  398. return $result;
  399. }
  400. /**
  401. * Load the editor
  402. *
  403. * @param array $config Associative array of editor config paramaters
  404. *
  405. * @return mixed
  406. *
  407. * @since 11.1
  408. */
  409. protected function _loadEditor($config = array())
  410. {
  411. // Check whether editor is already loaded
  412. if (!is_null(($this->_editor)))
  413. {
  414. return;
  415. }
  416. jimport('joomla.filesystem.file');
  417. // Build the path to the needed editor plugin
  418. $name = JFilterInput::getInstance()->clean($this->_name, 'cmd');
  419. $path = JPATH_PLUGINS . '/editors/' . $name . '.php';
  420. if (!JFile::exists($path))
  421. {
  422. $path = JPATH_PLUGINS . '/editors/' . $name . '/' . $name . '.php';
  423. if (!JFile::exists($path))
  424. {
  425. $message = JText::_('JLIB_HTML_EDITOR_CANNOT_LOAD');
  426. JError::raiseWarning(500, $message);
  427. return false;
  428. }
  429. }
  430. // Require plugin file
  431. require_once $path;
  432. // Get the plugin
  433. $plugin = JPluginHelper::getPlugin('editors', $this->_name);
  434. $params = new JRegistry;
  435. $params->loadString($plugin->params);
  436. $params->loadArray($config);
  437. $plugin->params = $params;
  438. // Build editor plugin classname
  439. $name = 'plgEditor' . $this->_name;
  440. if ($this->_editor = new $name($this, (array) $plugin))
  441. {
  442. // Load plugin parameters
  443. $this->initialise();
  444. JPluginHelper::importPlugin('editors-xtd');
  445. }
  446. }
  447. }