Slider.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-2015 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. * Abstract class for Dojo Slider dijits
  26. *
  27. * @uses Zend_Dojo_View_Helper_Dijit
  28. * @package Zend_Dojo
  29. * @subpackage View
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Dojo_View_Helper_Slider extends Zend_Dojo_View_Helper_Dijit
  34. {
  35. /**
  36. * Dojo module to use
  37. * @var string
  38. */
  39. protected $_module = 'dijit.form.Slider';
  40. /**
  41. * Required slider parameters
  42. * @var array
  43. */
  44. protected $_requiredParams = array('minimum', 'maximum', 'discreteValues');
  45. /**
  46. * Slider type -- vertical or horizontal
  47. * @var string
  48. */
  49. protected $_sliderType;
  50. /**
  51. * dijit.form.Slider
  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. * @return string
  58. */
  59. public function prepareSlider($id, $value = null, array $params = array(), array $attribs = array())
  60. {
  61. $this->_sliderType = strtolower($this->_sliderType);
  62. // Prepare two items: a hidden element to store the value, and the slider
  63. $hidden = $this->_renderHiddenElement($id, $value);
  64. $hidden = preg_replace('/(name=")([^"]*)"/', 'id="$2" $1$2"', $hidden);
  65. foreach ($this->_requiredParams as $param) {
  66. if (!array_key_exists($param, $params)) {
  67. require_once 'Zend/Dojo/View/Exception.php';
  68. throw new Zend_Dojo_View_Exception('prepareSlider() requires minimally the "minimum", "maximum", and "discreteValues" parameters');
  69. }
  70. }
  71. $content = '';
  72. $attribs['value'] = $value;
  73. if (!array_key_exists('onChange', $attribs)) {
  74. $attribs['onChange'] = "dojo.byId('" . $id . "').value = arguments[0];";
  75. }
  76. $id = str_replace('][', '-', $id);
  77. $id = str_replace(array('[', ']'), '-', $id);
  78. $id = rtrim($id, '-');
  79. $id .= '-slider';
  80. switch ($this->_sliderType) {
  81. case 'horizontal':
  82. if (array_key_exists('topDecoration', $params)) {
  83. $content .= $this->_prepareDecoration('topDecoration', $id, $params['topDecoration']);
  84. unset($params['topDecoration']);
  85. }
  86. if (array_key_exists('bottomDecoration', $params)) {
  87. $content .= $this->_prepareDecoration('bottomDecoration', $id, $params['bottomDecoration']);
  88. unset($params['bottomDecoration']);
  89. }
  90. if (array_key_exists('leftDecoration', $params)) {
  91. unset($params['leftDecoration']);
  92. }
  93. if (array_key_exists('rightDecoration', $params)) {
  94. unset($params['rightDecoration']);
  95. }
  96. break;
  97. case 'vertical':
  98. if (array_key_exists('leftDecoration', $params)) {
  99. $content .= $this->_prepareDecoration('leftDecoration', $id, $params['leftDecoration']);
  100. unset($params['leftDecoration']);
  101. }
  102. if (array_key_exists('rightDecoration', $params)) {
  103. $content .= $this->_prepareDecoration('rightDecoration', $id, $params['rightDecoration']);
  104. unset($params['rightDecoration']);
  105. }
  106. if (array_key_exists('topDecoration', $params)) {
  107. unset($params['topDecoration']);
  108. }
  109. if (array_key_exists('bottomDecoration', $params)) {
  110. unset($params['bottomDecoration']);
  111. }
  112. break;
  113. default:
  114. require_once 'Zend/Dojo/View/Exception.php';
  115. throw new Zend_Dojo_View_Exception('Invalid slider type; slider must be horizontal or vertical');
  116. }
  117. return $hidden . $this->_createLayoutContainer($id, $content, $params, $attribs);
  118. }
  119. /**
  120. * Prepare slider decoration
  121. *
  122. * @param string $position
  123. * @param string $id
  124. * @param array $decInfo
  125. * @return string
  126. */
  127. protected function _prepareDecoration($position, $id, $decInfo)
  128. {
  129. if (!in_array($position, array('topDecoration', 'bottomDecoration', 'leftDecoration', 'rightDecoration'))) {
  130. return '';
  131. }
  132. if (!is_array($decInfo)
  133. || !array_key_exists('labels', $decInfo)
  134. || !is_array($decInfo['labels'])
  135. ) {
  136. return '';
  137. }
  138. $id .= '-' . $position;
  139. if (!array_key_exists('dijit', $decInfo)) {
  140. $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'Rule';
  141. } else {
  142. $dijit = $decInfo['dijit'];
  143. if ('dijit.form.' != substr($dijit, 0, 10)) {
  144. $dijit = 'dijit.form.' . $dijit;
  145. }
  146. }
  147. $params = array();
  148. $attribs = array();
  149. $labels = $decInfo['labels'];
  150. if (array_key_exists('params', $decInfo)) {
  151. $params = $decInfo['params'];
  152. }
  153. if (array_key_exists('attribs', $decInfo)) {
  154. $attribs = $decInfo['attribs'];
  155. }
  156. $containerParams = null;
  157. if (array_key_exists('container', $params)) {
  158. $containerParams = $params['container'];
  159. unset($params['container']);
  160. }
  161. if (array_key_exists('labels', $params)) {
  162. $labelsParams = $params['labels'];
  163. unset($params['labels']);
  164. } else {
  165. $labelsParams = $params;
  166. }
  167. if (null === $containerParams) {
  168. $containerParams = $params;
  169. }
  170. $containerAttribs = null;
  171. if (array_key_exists('container', $attribs)) {
  172. $containerAttribs = $attribs['container'];
  173. unset($attribs['container']);
  174. }
  175. if (array_key_exists('labels', $attribs)) {
  176. $labelsAttribs = $attribs['labels'];
  177. unset($attribs['labels']);
  178. } else {
  179. $labelsAttribs = $attribs;
  180. }
  181. if (null === $containerAttribs) {
  182. $containerAttribs = $attribs;
  183. }
  184. $containerParams['container'] = $position;
  185. $labelsParams['container'] = $position;
  186. $labelList = $this->_prepareLabelsList($id, $labelsParams, $labelsAttribs, $labels);
  187. $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'Rule';
  188. $containerAttribs['id'] = $id;
  189. $containerAttribs = $this->_prepareDijit($containerAttribs, $containerParams, 'layout', $dijit);
  190. $containerHtml = '<div' . $this->_htmlAttribs($containerAttribs) . "></div>\n";
  191. switch ($position) {
  192. case 'topDecoration':
  193. case 'leftDecoration':
  194. return $labelList . $containerHtml;
  195. case 'bottomDecoration':
  196. case 'rightDecoration':
  197. return $containerHtml . $labelList;
  198. }
  199. }
  200. /**
  201. * Prepare slider label list
  202. *
  203. * @param string $id
  204. * @param array $params
  205. * @param array $attribs
  206. * @param array $labels
  207. * @return string
  208. */
  209. protected function _prepareLabelsList($id, array $params, array $attribs, array $labels)
  210. {
  211. $attribs['id'] = $id . '-labels';
  212. $dijit = 'dijit.form.' . ucfirst($this->_sliderType) . 'RuleLabels';
  213. $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit);
  214. return $this->view->htmlList($labels, true, $attribs);
  215. }
  216. }