/Zend/Dojo/View/Helper/Slider.php

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