/zf/library/Zend/Dojo/View/Helper/ComboBox.php

http://github.com/eryx/php-framework-benchmark · PHP · 155 lines · 80 code · 12 blank · 63 comment · 15 complexity · fbc1c7fa333930b2c430e1f1bd16d9d0 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ComboBox.php 23931 2011-05-02 19:32:51Z matthew $
  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-2011 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 (is_string($store)) {
  71. $html .= $store;
  72. }
  73. $html .= $this->_createFormElement($id, $value, $params, $attribs);
  74. return $html;
  75. }
  76. unset($params['store']);
  77. } elseif (array_key_exists('store', $params)) {
  78. if (array_key_exists('storeType', $params)) {
  79. $storeParams = array(
  80. 'store' => $params['store'],
  81. 'type' => $params['storeType'],
  82. );
  83. unset($params['storeType']);
  84. if (array_key_exists('storeParams', $params)) {
  85. $storeParams['params'] = $params['storeParams'];
  86. unset($params['storeParams']);
  87. }
  88. if (false !== ($store = $this->_renderStore($storeParams, $id))) {
  89. if (is_string($store)) {
  90. $html .= $store;
  91. }
  92. }
  93. }
  94. $html .= $this->_createFormElement($id, $value, $params, $attribs);
  95. return $html;
  96. }
  97. // required for correct type casting in declerative mode
  98. if (isset($params['autocomplete'])) {
  99. $params['autocomplete'] = ($params['autocomplete']) ? 'true' : 'false';
  100. }
  101. // do as normal select
  102. $attribs = $this->_prepareDijit($attribs, $params, 'element');
  103. return $this->view->formSelect($id, $value, $attribs, $options);
  104. }
  105. /**
  106. * Render data store element
  107. *
  108. * Renders to dojo view helper
  109. *
  110. * @param array $params
  111. * @return string|false
  112. */
  113. protected function _renderStore(array $params, $id)
  114. {
  115. if (!array_key_exists('store', $params) || !array_key_exists('type', $params)) {
  116. return false;
  117. }
  118. $this->dojo->requireModule($params['type']);
  119. $extraParams = array();
  120. $storeParams = array(
  121. 'dojoType' => $params['type'],
  122. 'jsId' => $params['store'],
  123. );
  124. if (array_key_exists('params', $params)) {
  125. $storeParams = array_merge($storeParams, $params['params']);
  126. $extraParams = $params['params'];
  127. }
  128. if ($this->_useProgrammatic()) {
  129. if (!$this->_useProgrammaticNoScript()) {
  130. require_once 'Zend/Json.php';
  131. $this->dojo->addJavascript('var ' . $storeParams['jsId'] . ";\n");
  132. $js = $storeParams['jsId'] . ' = '
  133. . 'new ' . $storeParams['dojoType'] . '('
  134. . Zend_Json::encode($extraParams)
  135. . ");\n";
  136. $js = "function() {\n$js\n}";
  137. $this->dojo->_addZendLoad($js);
  138. }
  139. return true;
  140. }
  141. return '<div' . $this->_htmlAttribs($storeParams) . '></div>';
  142. }
  143. }