ComboBox.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Dijit */
  23. require_once 'Zend/Dojo/View/Helper/Dijit.php';
  24. /**
  25. * Dojo ComboBox dijit
  26. *
  27. * @uses Zend_Dojo_View_Helper_Dijit
  28. * @package Zend_Dojo
  29. * @subpackage View
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Dojo_View_Helper_ComboBox extends Zend_Dojo_View_Helper_Dijit
  34. {
  35. /**
  36. * Dijit being used
  37. * @var string
  38. */
  39. protected $_dijit = 'dijit.form.ComboBox';
  40. /**
  41. * HTML element type
  42. * @var string
  43. */
  44. protected $_elementType = 'text';
  45. /**
  46. * Dojo module to use
  47. * @var string
  48. */
  49. protected $_module = 'dijit.form.ComboBox';
  50. /**
  51. * dijit.form.ComboBox
  52. *
  53. * @param int $id
  54. * @param mixed $value
  55. * @param array $params Parameters to use for dijit creation
  56. * @param array $attribs HTML attributes
  57. * @param array|null $options Select options
  58. * @return string
  59. */
  60. public function comboBox($id, $value = null, array $params = array(), array $attribs = array(), array $options = null)
  61. {
  62. $html = '';
  63. if (!array_key_exists('id', $attribs)) {
  64. $attribs['id'] = $id;
  65. }
  66. if (array_key_exists('store', $params) && is_array($params['store'])) {
  67. // using dojo.data datastore
  68. if (false !== ($store = $this->_renderStore($params['store'], $id))) {
  69. $params['store'] = $params['store']['store'];
  70. if ($this->_useProgrammatic()) {
  71. unset($params['store']);
  72. }
  73. if (is_string($store)) {
  74. $html .= $store;
  75. }
  76. $html .= $this->_createFormElement($id, $value, $params, $attribs);
  77. return $html;
  78. }
  79. unset($params['store']);
  80. } elseif (array_key_exists('store', $params)) {
  81. if (array_key_exists('storeType', $params)) {
  82. $storeParams = array(
  83. 'store' => $params['store'],
  84. 'type' => $params['storeType'],
  85. );
  86. unset($params['storeType']);
  87. if (array_key_exists('storeParams', $params)) {
  88. $storeParams['params'] = $params['storeParams'];
  89. unset($params['storeParams']);
  90. }
  91. if (false !== ($store = $this->_renderStore($storeParams, $id))) {
  92. if (is_string($store)) {
  93. $html .= $store;
  94. }
  95. }
  96. }
  97. if ($this->_useProgrammatic()) {
  98. unset($params['store']);
  99. }
  100. $html .= $this->_createFormElement($id, $value, $params, $attribs);
  101. return $html;
  102. }
  103. // do as normal select
  104. $attribs = $this->_prepareDijit($attribs, $params, 'element');
  105. return $this->view->formSelect($id, $value, $attribs, $options);
  106. }
  107. /**
  108. * Render data store element
  109. *
  110. * Renders to dojo view helper
  111. *
  112. * @param array $params
  113. * @return string|false
  114. */
  115. protected function _renderStore(array $params, $id)
  116. {
  117. if (!array_key_exists('store', $params) || !array_key_exists('type', $params)) {
  118. return false;
  119. }
  120. $this->dojo->requireModule($params['type']);
  121. $extraParams = array();
  122. $storeParams = array(
  123. 'dojoType' => $params['type'],
  124. 'jsId' => $params['store'],
  125. );
  126. if (array_key_exists('params', $params)) {
  127. $storeParams = array_merge($storeParams, $params['params']);
  128. $extraParams = $params['params'];
  129. }
  130. if ($this->_useProgrammatic()) {
  131. if (!$this->_useProgrammaticNoScript()) {
  132. require_once 'Zend/Json.php';
  133. $js = 'var ' . $storeParams['jsId'] . ' = '
  134. . 'new ' . $storeParams['dojoType'] . '('
  135. . Zend_Json::encode($extraParams)
  136. . ");\n"
  137. . 'dijit.byId("' . $id . '").attr("store", '
  138. . $storeParams['jsId'] . ');';
  139. $js = "function() {\n$js\n}";
  140. $this->dojo->prependOnLoad($js);
  141. }
  142. return true;
  143. }
  144. return '<div' . $this->_htmlAttribs($storeParams) . '></div>';
  145. }
  146. }