PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/library/Zend/Dojo/View/Helper/Editor.php

https://github.com/Martin1982/IBMessagingWorkshopServer
PHP | 187 lines | 88 code | 20 blank | 79 comment | 8 complexity | ef1dc1c8dedadbfd070407c661b40fe6 MD5 | raw file
  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. /** 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-2010 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. );
  57. /**
  58. * JSON-encoded parameters
  59. * @var array
  60. */
  61. protected $_jsonParams = array('captureEvents', 'events', 'plugins');
  62. /**
  63. * dijit.Editor
  64. *
  65. * @param string $id
  66. * @param string $value
  67. * @param array $params
  68. * @param array $attribs
  69. * @return string
  70. */
  71. public function editor($id, $value = null, $params = array(), $attribs = array())
  72. {
  73. if (isset($params['plugins'])) {
  74. foreach ($this->_getRequiredModules($params['plugins']) as $module) {
  75. $this->dojo->requireModule($module);
  76. }
  77. }
  78. // Previous versions allowed specifying "degrade" to allow using a
  79. // textarea instead of a div -- but this is insecure. Removing the
  80. // parameter if set to prevent its injection in the dijit.
  81. if (isset($params['degrade'])) {
  82. unset($params['degrade']);
  83. }
  84. $hiddenName = $id;
  85. if (array_key_exists('id', $attribs)) {
  86. $hiddenId = $attribs['id'];
  87. } else {
  88. $hiddenId = $hiddenName;
  89. }
  90. $hiddenId = $this->_normalizeId($hiddenId);
  91. $textareaName = $this->_normalizeEditorName($hiddenName);
  92. $textareaId = $hiddenId . '-Editor';
  93. $hiddenAttribs = array(
  94. 'id' => $hiddenId,
  95. 'name' => $hiddenName,
  96. 'value' => $value,
  97. 'type' => 'hidden',
  98. );
  99. $attribs['id'] = $textareaId;
  100. $this->_createGetParentFormFunction();
  101. $this->_createEditorOnSubmit($hiddenId, $textareaId);
  102. $attribs = $this->_prepareDijit($attribs, $params, 'textarea');
  103. $html = '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
  104. $html .= '<div' . $this->_htmlAttribs($attribs) . '>'
  105. . $value
  106. . "</div>\n";
  107. // Embed a textarea in a <noscript> tag to allow for graceful
  108. // degradation
  109. $html .= '<noscript>'
  110. . $this->view->formTextarea($hiddenId, $value, $attribs)
  111. . '</noscript>';
  112. return $html;
  113. }
  114. /**
  115. * Generates the list of required modules to include, if any is needed.
  116. *
  117. * @param array $plugins plugins to include
  118. * @return array
  119. */
  120. protected function _getRequiredModules(array $plugins)
  121. {
  122. $modules = array();
  123. foreach ($plugins as $commandName) {
  124. if (isset($this->_pluginsModules[$commandName])) {
  125. $pluginName = $this->_pluginsModules[$commandName];
  126. $modules[] = 'dijit._editor.plugins.' . $pluginName;
  127. }
  128. }
  129. return array_unique($modules);
  130. }
  131. /**
  132. * Normalize editor element name
  133. *
  134. * @param string $name
  135. * @return string
  136. */
  137. protected function _normalizeEditorName($name)
  138. {
  139. if ('[]' == substr($name, -2)) {
  140. $name = substr($name, 0, strlen($name) - 2);
  141. $name .= '[Editor][]';
  142. } else {
  143. $name .= '[Editor]';
  144. }
  145. return $name;
  146. }
  147. /**
  148. * Create onSubmit binding for element
  149. *
  150. * @param string $hiddenId
  151. * @param string $editorId
  152. * @return void
  153. */
  154. protected function _createEditorOnSubmit($hiddenId, $editorId)
  155. {
  156. $this->dojo->onLoadCaptureStart();
  157. echo <<<EOJ
  158. function() {
  159. var form = zend.findParentForm(dojo.byId('$hiddenId'));
  160. dojo.connect(form, 'submit', function(e) {
  161. dojo.byId('$hiddenId').value = dijit.byId('$editorId').getValue(false);
  162. });
  163. }
  164. EOJ;
  165. $this->dojo->onLoadCaptureEnd();
  166. }
  167. }