Source for file editor.php

Documentation is available at editor.php

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

Documentation generated on Tue, 19 Nov 2013 15:02:14 +0100 by phpDocumentor 1.4.3