2
0

Editor.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Dojo_View_Helper_Dijit */
  23. require_once 'Zend/Dojo/View/Helper/Dijit.php';
  24. /** Zend_Json */
  25. require_once 'Zend/Json.php';
  26. /**
  27. * Dojo Editor dijit
  28. *
  29. * @uses Zend_Dojo_View_Helper_Textarea
  30. * @package Zend_Dojo
  31. * @subpackage View
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Dojo_View_Helper_Editor extends Zend_Dojo_View_Helper_Dijit
  36. {
  37. /**
  38. * @param string Dijit type
  39. */
  40. protected $_dijit = 'dijit.Editor';
  41. /**
  42. * @var string Dijit module to load
  43. */
  44. protected $_module = 'dijit.Editor';
  45. /**
  46. * @var array Maps non-core plugin to module basename
  47. */
  48. protected $_pluginsModules = array(
  49. 'createLink' => 'LinkDialog',
  50. 'insertImage' => 'LinkDialog',
  51. 'fontName' => 'FontChoice',
  52. 'fontSize' => 'FontChoice',
  53. 'formatBlock' => 'FontChoice',
  54. 'foreColor' => 'TextColor',
  55. 'hiliteColor' => 'TextColor',
  56. 'enterKeyHandling' => 'EnterKeyHandling',
  57. 'fullScreen' => 'FullScreen',
  58. 'newPage' => 'NewPage',
  59. 'print' => 'Print',
  60. 'tabIndent' => 'TabIndent',
  61. 'toggleDir' => 'ToggleDir',
  62. 'viewSource' => 'ViewSource'
  63. );
  64. /**
  65. * JSON-encoded parameters
  66. * @var array
  67. */
  68. protected $_jsonParams = array('captureEvents', 'events', 'plugins', 'extraPlugins');
  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. // Previous versions allowed specifying "degrade" to allow using a
  86. // textarea instead of a div -- but this is insecure. Removing the
  87. // parameter if set to prevent its injection in the dijit.
  88. if (isset($params['degrade'])) {
  89. unset($params['degrade']);
  90. }
  91. $hiddenName = $id;
  92. if (array_key_exists('id', $attribs)) {
  93. $hiddenId = $attribs['id'];
  94. } else {
  95. $hiddenId = $hiddenName;
  96. }
  97. $hiddenId = $this->_normalizeId($hiddenId);
  98. $textareaName = $this->_normalizeEditorName($hiddenName);
  99. $textareaId = $hiddenId . '-Editor';
  100. $hiddenAttribs = array(
  101. 'id' => $hiddenId,
  102. 'name' => $hiddenName,
  103. 'value' => $value,
  104. 'type' => 'hidden',
  105. );
  106. $attribs['id'] = $textareaId;
  107. $this->_createGetParentFormFunction();
  108. $this->_createEditorOnSubmit($hiddenId, $textareaId);
  109. $attribs = $this->_prepareDijit($attribs, $params, 'textarea');
  110. $html = '<div' . $this->_htmlAttribs($attribs) . '>'
  111. . $value
  112. . "</div>\n";
  113. // Embed a textarea in a <noscript> tag to allow for graceful
  114. // degradation
  115. $html .= '<noscript>'
  116. . $this->view->formTextarea($hiddenId, $value, $attribs)
  117. . '</noscript>';
  118. $html .= '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
  119. return $html;
  120. }
  121. /**
  122. * Generates the list of required modules to include, if any is needed.
  123. *
  124. * @param array $plugins plugins to include
  125. * @return array
  126. */
  127. protected function _getRequiredModules(array $plugins)
  128. {
  129. $modules = array();
  130. foreach ($plugins as $commandName) {
  131. if (isset($this->_pluginsModules[$commandName])) {
  132. $pluginName = $this->_pluginsModules[$commandName];
  133. $modules[] = 'dijit._editor.plugins.' . $pluginName;
  134. }
  135. }
  136. return array_unique($modules);
  137. }
  138. /**
  139. * Normalize editor element name
  140. *
  141. * @param string $name
  142. * @return string
  143. */
  144. protected function _normalizeEditorName($name)
  145. {
  146. if ('[]' == substr($name, -2)) {
  147. $name = substr($name, 0, strlen($name) - 2);
  148. $name .= '[Editor][]';
  149. } else {
  150. $name .= '[Editor]';
  151. }
  152. return $name;
  153. }
  154. /**
  155. * Create onSubmit binding for element
  156. *
  157. * @param string $hiddenId
  158. * @param string $editorId
  159. * @return void
  160. */
  161. protected function _createEditorOnSubmit($hiddenId, $editorId)
  162. {
  163. $this->dojo->onLoadCaptureStart();
  164. echo <<<EOJ
  165. function() {
  166. var form = zend.findParentForm(dojo.byId('$hiddenId'));
  167. dojo.connect(form, 'submit', function(e) {
  168. var value = dijit.byId('$editorId').getValue(false);
  169. if(dojo.isFF) {
  170. value = value.replace(/<br _moz_editor_bogus_node="TRUE" \/>/, '');
  171. }
  172. dojo.byId('$hiddenId').value = value;
  173. });
  174. }
  175. EOJ;
  176. $this->dojo->onLoadCaptureEnd();
  177. }
  178. }