web/enmi/Zend/Dojo/View/Helper/Editor.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Dojo
       
    17  * @subpackage View
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Editor.php 20116 2010-01-07 14:18:34Z matthew $
       
    21  */
       
    22 
       
    23 /** Zend_Dojo_View_Helper_Dijit */
       
    24 require_once 'Zend/Dojo/View/Helper/Dijit.php';
       
    25 
       
    26 /** Zend_Json */
       
    27 require_once 'Zend/Json.php';
       
    28 
       
    29 /**
       
    30  * Dojo Editor dijit
       
    31  *
       
    32  * @uses       Zend_Dojo_View_Helper_Textarea
       
    33  * @package    Zend_Dojo
       
    34  * @subpackage View
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Dijit
       
    39 {
       
    40     /**
       
    41      * @param string Dijit type
       
    42      */
       
    43     protected $_dijit = 'dijit.Editor';
       
    44 
       
    45     /**
       
    46      * @var string Dijit module to load
       
    47      */
       
    48     protected $_module = 'dijit.Editor';
       
    49 
       
    50     /**
       
    51      * @var array Maps non-core plugin to module basename
       
    52      */
       
    53     protected $_pluginsModules = array(
       
    54         'createLink' => 'LinkDialog',
       
    55         'insertImage' => 'LinkDialog',
       
    56         'fontName' => 'FontChoice',
       
    57         'fontSize' => 'FontChoice',
       
    58         'formatBlock' => 'FontChoice',
       
    59         'foreColor' => 'TextColor',
       
    60         'hiliteColor' => 'TextColor'
       
    61     );
       
    62 
       
    63     /**
       
    64      * JSON-encoded parameters
       
    65      * @var array
       
    66      */
       
    67     protected $_jsonParams = array('captureEvents', 'events', 'plugins');
       
    68 
       
    69     /**
       
    70      * dijit.Editor
       
    71      *
       
    72      * @param  string $id
       
    73      * @param  string $value
       
    74      * @param  array $params
       
    75      * @param  array $attribs
       
    76      * @return string
       
    77      */
       
    78     public function editor($id, $value = null, $params = array(), $attribs = array())
       
    79     {
       
    80         if (isset($params['plugins'])) {
       
    81             foreach ($this->_getRequiredModules($params['plugins']) as $module) {
       
    82                 $this->dojo->requireModule($module);
       
    83             }
       
    84         }
       
    85 
       
    86         // Previous versions allowed specifying "degrade" to allow using a 
       
    87         // textarea instead of a div -- but this is insecure. Removing the 
       
    88         // parameter if set to prevent its injection in the dijit.
       
    89         if (isset($params['degrade'])) {
       
    90             unset($params['degrade']);
       
    91         }
       
    92 
       
    93         $hiddenName = $id;
       
    94         if (array_key_exists('id', $attribs)) {
       
    95             $hiddenId = $attribs['id'];
       
    96         } else {
       
    97             $hiddenId = $hiddenName;
       
    98         }
       
    99         $hiddenId = $this->_normalizeId($hiddenId);
       
   100 
       
   101         $textareaName = $this->_normalizeEditorName($hiddenName);
       
   102         $textareaId   = $hiddenId . '-Editor';
       
   103 
       
   104         $hiddenAttribs = array(
       
   105             'id'    => $hiddenId,
       
   106             'name'  => $hiddenName,
       
   107             'value' => $value,
       
   108             'type'  => 'hidden',
       
   109         );
       
   110         $attribs['id'] = $textareaId;
       
   111 
       
   112         $this->_createGetParentFormFunction();
       
   113         $this->_createEditorOnSubmit($hiddenId, $textareaId);
       
   114 
       
   115         $attribs = $this->_prepareDijit($attribs, $params, 'textarea');
       
   116 
       
   117         $html  = '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
       
   118         $html .= '<div' . $this->_htmlAttribs($attribs) . '>'
       
   119                . $value
       
   120                . "</div>\n";
       
   121 
       
   122         // Embed a textarea in a <noscript> tag to allow for graceful 
       
   123         // degradation
       
   124         $html .= '<noscript>'
       
   125                . $this->view->formTextarea($hiddenId, $value, $attribs)
       
   126                . '</noscript>';
       
   127 
       
   128         return $html;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Generates the list of required modules to include, if any is needed.
       
   133      *
       
   134      * @param array $plugins plugins to include
       
   135      * @return array
       
   136      */
       
   137     protected function _getRequiredModules(array $plugins)
       
   138     {
       
   139         $modules = array();
       
   140         foreach ($plugins as $commandName) {
       
   141             if (isset($this->_pluginsModules[$commandName])) {
       
   142                 $pluginName = $this->_pluginsModules[$commandName];
       
   143                 $modules[] = 'dijit._editor.plugins.' . $pluginName;
       
   144             }
       
   145         }
       
   146 
       
   147         return array_unique($modules);
       
   148     }
       
   149 
       
   150     /**
       
   151      * Normalize editor element name
       
   152      *
       
   153      * @param  string $name
       
   154      * @return string
       
   155      */
       
   156     protected function _normalizeEditorName($name)
       
   157     {
       
   158         if ('[]' == substr($name, -2)) {
       
   159             $name = substr($name, 0, strlen($name) - 2);
       
   160             $name .= '[Editor][]';
       
   161         } else {
       
   162             $name .= '[Editor]';
       
   163         }
       
   164         return $name;
       
   165     }
       
   166 
       
   167     /**
       
   168      * Create onSubmit binding for element
       
   169      *
       
   170      * @param  string $hiddenId
       
   171      * @param  string $editorId
       
   172      * @return void
       
   173      */
       
   174     protected function _createEditorOnSubmit($hiddenId, $editorId)
       
   175     {
       
   176         $this->dojo->onLoadCaptureStart();
       
   177         echo <<<EOJ
       
   178 function() {
       
   179     var form = zend.findParentForm(dojo.byId('$hiddenId'));
       
   180     dojo.connect(form, 'submit', function(e) {
       
   181         dojo.byId('$hiddenId').value = dijit.byId('$editorId').getValue(false);
       
   182     });
       
   183 }
       
   184 EOJ;
       
   185         $this->dojo->onLoadCaptureEnd();
       
   186     }
       
   187 }