Editor.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2009 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_Textarea */
  23. require_once 'Zend/Dojo/View/Helper/Textarea.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-2009 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_Textarea
  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. $hiddenName = $id;
  79. if (array_key_exists('id', $attribs)) {
  80. $hiddenId = $attribs['id'];
  81. } else {
  82. $hiddenId = $hiddenName;
  83. }
  84. $hiddenId = $this->_normalizeId($hiddenId);
  85. $textareaName = $this->_normalizeEditorName($hiddenName);
  86. $textareaId = $hiddenId . '-Editor';
  87. $hiddenAttribs = array(
  88. 'id' => $hiddenId,
  89. 'name' => $hiddenName,
  90. 'value' => $value,
  91. 'type' => 'hidden',
  92. );
  93. $attribs['id'] = $textareaId;
  94. $this->_createGetParentFormFunction();
  95. $this->_createEditorOnSubmit($hiddenId, $textareaId);
  96. $html = '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket()
  97. . $this->textarea($textareaName, $value, $params, $attribs);
  98. return $html;
  99. }
  100. /**
  101. * Generates the list of required modules to include, if any is needed.
  102. *
  103. * @param array $plugins plugins to include
  104. * @return array
  105. */
  106. protected function _getRequiredModules(array $plugins)
  107. {
  108. $modules = array();
  109. foreach ($plugins as $commandName) {
  110. if (isset($this->_pluginsModules[$commandName])) {
  111. $pluginName = $this->_pluginsModules[$commandName];
  112. $modules[] = 'dijit._editor.plugins.' . $pluginName;
  113. }
  114. }
  115. return array_unique($modules);
  116. }
  117. /**
  118. * Normalize editor element name
  119. *
  120. * @param string $name
  121. * @return string
  122. */
  123. protected function _normalizeEditorName($name)
  124. {
  125. if ('[]' == substr($name, -2)) {
  126. $name = substr($name, 0, strlen($name) - 2);
  127. $name .= '[Editor][]';
  128. } else {
  129. $name .= '[Editor]';
  130. }
  131. return $name;
  132. }
  133. /**
  134. * Create onSubmit binding for element
  135. *
  136. * @param string $hiddenId
  137. * @param string $editorId
  138. * @return void
  139. */
  140. protected function _createEditorOnSubmit($hiddenId, $editorId)
  141. {
  142. $this->dojo->onLoadCaptureStart();
  143. echo <<<EOJ
  144. function() {
  145. var form = zend.findParentForm(dojo.byId('$hiddenId'));
  146. dojo.connect(form, 'onsubmit', function () {
  147. dojo.byId('$hiddenId').value = dijit.byId('$editorId').getValue(false);
  148. });
  149. }
  150. EOJ;
  151. $this->dojo->onLoadCaptureEnd();
  152. }
  153. }