Editor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * JSON-encoded parameters
  47. * @var array
  48. */
  49. protected $_jsonParams = array('captureEvents', 'events', 'plugins');
  50. /**
  51. * dijit.Editor
  52. *
  53. * @param string $id
  54. * @param string $value
  55. * @param array $params
  56. * @param array $attribs
  57. * @return string
  58. */
  59. public function editor($id, $value = null, $params = array(), $attribs = array())
  60. {
  61. $hiddenName = $id;
  62. if (array_key_exists('id', $attribs)) {
  63. $hiddenId = $attribs['id'];
  64. } else {
  65. $hiddenId = $hiddenName;
  66. }
  67. $hiddenId = $this->_normalizeId($hiddenId);
  68. $textareaName = $this->_normalizeEditorName($hiddenName);
  69. $textareaId = $hiddenId . '-Editor';
  70. $hiddenAttribs = array(
  71. 'id' => $hiddenId,
  72. 'name' => $hiddenName,
  73. 'value' => $value,
  74. 'type' => 'hidden',
  75. );
  76. $attribs['id'] = $textareaId;
  77. $this->_createGetParentFormFunction();
  78. $this->_createEditorOnSubmit($hiddenId, $textareaId);
  79. $html = '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket()
  80. . $this->textarea($textareaName, $value, $params, $attribs);
  81. return $html;
  82. }
  83. /**
  84. * Normalize editor element name
  85. *
  86. * @param string $name
  87. * @return string
  88. */
  89. protected function _normalizeEditorName($name)
  90. {
  91. if ('[]' == substr($name, -2)) {
  92. $name = substr($name, 0, strlen($name) - 2);
  93. $name .= '[Editor][]';
  94. } else {
  95. $name .= '[Editor]';
  96. }
  97. return $name;
  98. }
  99. /**
  100. * Create onSubmit binding for element
  101. *
  102. * @param string $hiddenId
  103. * @param string $editorId
  104. * @return void
  105. */
  106. protected function _createEditorOnSubmit($hiddenId, $editorId)
  107. {
  108. $this->dojo->onLoadCaptureStart();
  109. echo <<<EOJ
  110. function() {
  111. var form = zend.findParentForm(dojo.byId('$hiddenId'));
  112. dojo.connect(form, 'onsubmit', function () {
  113. dojo.byId('$hiddenId').value = dijit.byId('$editorId').getValue(false);
  114. });
  115. }
  116. EOJ;
  117. $this->dojo->onLoadCaptureEnd();
  118. }
  119. }