File indexing completed on 2024-06-23 05:55:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Dojo
0017  * @subpackage View
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Dojo_View_Helper_Dijit */
0024 // require_once 'Zend/Dojo/View/Helper/Dijit.php';
0025 
0026 /** Zend_Json */
0027 // require_once 'Zend/Json.php';
0028 
0029 /**
0030  * Dojo Editor dijit
0031  *
0032  * @uses       Zend_Dojo_View_Helper_Textarea
0033  * @package    Zend_Dojo
0034  * @subpackage View
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Dijit
0039 {
0040     /**
0041      * @param string Dijit type
0042      */
0043     protected $_dijit = 'dijit.Editor';
0044 
0045     /**
0046      * @var string Dijit module to load
0047      */
0048     protected $_module = 'dijit.Editor';
0049 
0050     /**
0051      * @var array Maps non-core plugin to module basename
0052      */
0053     protected $_pluginsModules = array(
0054         'createLink' => 'LinkDialog',
0055         'insertImage' => 'LinkDialog',
0056         'fontName' => 'FontChoice',
0057         'fontSize' => 'FontChoice',
0058         'formatBlock' => 'FontChoice',
0059         'foreColor' => 'TextColor',
0060         'hiliteColor' => 'TextColor',
0061         'enterKeyHandling' => 'EnterKeyHandling',
0062         'fullScreen' => 'FullScreen',
0063         'newPage' => 'NewPage',
0064         'print' => 'Print',
0065         'tabIndent' => 'TabIndent',
0066         'toggleDir' => 'ToggleDir',
0067         'viewSource' => 'ViewSource'
0068     );
0069 
0070     /**
0071      * JSON-encoded parameters
0072      * @var array
0073      */
0074     protected $_jsonParams = array('captureEvents', 'events', 'plugins', 'extraPlugins');
0075 
0076     /**
0077      * dijit.Editor
0078      *
0079      * @param  string $id
0080      * @param  string $value
0081      * @param  array $params
0082      * @param  array $attribs
0083      * @return string
0084      */
0085     public function editor($id, $value = null, $params = array(), $attribs = array())
0086     {
0087         if (isset($params['plugins'])) {
0088             foreach ($this->_getRequiredModules($params['plugins']) as $module) {
0089                 $this->dojo->requireModule($module);
0090             }
0091         }
0092 
0093         // Previous versions allowed specifying "degrade" to allow using a
0094         // textarea instead of a div -- but this is insecure. Removing the
0095         // parameter if set to prevent its injection in the dijit.
0096         if (isset($params['degrade'])) {
0097             unset($params['degrade']);
0098         }
0099 
0100         $hiddenName = $id;
0101         if (array_key_exists('id', $attribs)) {
0102             $hiddenId = $attribs['id'];
0103         } else {
0104             $hiddenId = $hiddenName;
0105         }
0106         $hiddenId = $this->_normalizeId($hiddenId);
0107 
0108         $textareaName = $this->_normalizeEditorName($hiddenName);
0109         $textareaId   = $hiddenId . '-Editor';
0110 
0111         $hiddenAttribs = array(
0112             'id'    => $hiddenId,
0113             'name'  => $hiddenName,
0114             'value' => $value,
0115             'type'  => 'hidden',
0116         );
0117         $attribs['id'] = $textareaId;
0118 
0119         $this->_createGetParentFormFunction();
0120         $this->_createEditorOnSubmit($hiddenId, $textareaId);
0121 
0122         $attribs = $this->_prepareDijit($attribs, $params, 'textarea');
0123 
0124         $html  = '<div' . $this->_htmlAttribs($attribs) . '>'
0125                . $value
0126                . "</div>\n";
0127 
0128         // Embed a textarea in a <noscript> tag to allow for graceful
0129         // degradation
0130         $html .= '<noscript>'
0131                . $this->view->formTextarea($hiddenId, $value, $attribs)
0132                . '</noscript>';
0133 
0134         $html  .= '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
0135         
0136         return $html;
0137     }
0138 
0139     /**
0140      * Generates the list of required modules to include, if any is needed.
0141      *
0142      * @param array $plugins plugins to include
0143      * @return array
0144      */
0145     protected function _getRequiredModules(array $plugins)
0146     {
0147         $modules = array();
0148         foreach ($plugins as $commandName) {
0149             if (isset($this->_pluginsModules[$commandName])) {
0150                 $pluginName = $this->_pluginsModules[$commandName];
0151                 $modules[] = 'dijit._editor.plugins.' . $pluginName;
0152             }
0153         }
0154 
0155         return array_unique($modules);
0156     }
0157 
0158     /**
0159      * Normalize editor element name
0160      *
0161      * @param  string $name
0162      * @return string
0163      */
0164     protected function _normalizeEditorName($name)
0165     {
0166         if ('[]' == substr($name, -2)) {
0167             $name = substr($name, 0, strlen($name) - 2);
0168             $name .= '[Editor][]';
0169         } else {
0170             $name .= '[Editor]';
0171         }
0172         return $name;
0173     }
0174 
0175     /**
0176      * Create onSubmit binding for element
0177      *
0178      * @param  string $hiddenId
0179      * @param  string $editorId
0180      * @return void
0181      */
0182     protected function _createEditorOnSubmit($hiddenId, $editorId)
0183     {
0184         $this->dojo->onLoadCaptureStart();
0185         echo <<<EOJ
0186 function() {
0187     var form = zend.findParentForm(dojo.byId('$hiddenId'));
0188     dojo.connect(form, 'submit', function(e) {
0189         var value = dijit.byId('$editorId').getValue(false);
0190         if(dojo.isFF) {
0191             value = value.replace(/<br _moz_editor_bogus_node="TRUE" \/>/, '');
0192         }
0193         dojo.byId('$hiddenId').value = value;
0194     });
0195 }
0196 EOJ;
0197         $this->dojo->onLoadCaptureEnd();
0198     }
0199 }