DijitMulti.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Form_Element
  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. */
  21. /** Zend_Dojo_Form_Element_Dijit */
  22. require_once 'Zend/Dojo/Form/Element/Dijit.php';
  23. /**
  24. * CheckBox dijit
  25. *
  26. * Note: this would be easier with mixins or traits...
  27. *
  28. * @uses Zend_Dojo_Form_Element_Dijit
  29. * @package Zend_Dojo
  30. * @subpackage Form_Element
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id$
  34. */
  35. abstract class Zend_Dojo_Form_Element_DijitMulti extends Zend_Dojo_Form_Element_Dijit
  36. {
  37. /**
  38. * Array of options for multi-item
  39. * @var array
  40. */
  41. public $options = array();
  42. /**
  43. * Flag: autoregister inArray validator?
  44. * @var bool
  45. */
  46. protected $_registerInArrayValidator = true;
  47. /**
  48. * Separator to use between options; defaults to '<br />'.
  49. * @var string
  50. */
  51. protected $_separator = '<br />';
  52. /**
  53. * Which values are translated already?
  54. * @var array
  55. */
  56. protected $_translated = array();
  57. /**
  58. * Retrieve separator
  59. *
  60. * @return mixed
  61. */
  62. public function getSeparator()
  63. {
  64. return $this->_separator;
  65. }
  66. /**
  67. * Set separator
  68. *
  69. * @param mixed $separator
  70. * @return self
  71. */
  72. public function setSeparator($separator)
  73. {
  74. $this->_separator = $separator;
  75. return $this;
  76. }
  77. /**
  78. * Retrieve options array
  79. *
  80. * @return array
  81. */
  82. protected function _getMultiOptions()
  83. {
  84. if (null === $this->options || !is_array($this->options)) {
  85. $this->options = array();
  86. }
  87. return $this->options;
  88. }
  89. /**
  90. * Add an option
  91. *
  92. * @param string $option
  93. * @param string $value
  94. * @return Zend_Form_Element_Multi
  95. */
  96. public function addMultiOption($option, $value = '')
  97. {
  98. $option = (string) $option;
  99. $this->_getMultiOptions();
  100. if (!$this->_translateOption($option, $value)) {
  101. $this->options[$option] = $value;
  102. }
  103. return $this;
  104. }
  105. /**
  106. * Add many options at once
  107. *
  108. * @param array $options
  109. * @return Zend_Form_Element_Multi
  110. */
  111. public function addMultiOptions(array $options)
  112. {
  113. foreach ($options as $option => $value) {
  114. if (is_array($value)
  115. && array_key_exists('key', $value)
  116. && array_key_exists('value', $value)
  117. ) {
  118. $this->addMultiOption($value['key'], $value['value']);
  119. } else {
  120. $this->addMultiOption($option, $value);
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Set all options at once (overwrites)
  127. *
  128. * @param array $options
  129. * @return Zend_Form_Element_Multi
  130. */
  131. public function setMultiOptions(array $options)
  132. {
  133. $this->clearMultiOptions();
  134. return $this->addMultiOptions($options);
  135. }
  136. /**
  137. * Retrieve single multi option
  138. *
  139. * @param string $option
  140. * @return mixed
  141. */
  142. public function getMultiOption($option)
  143. {
  144. $option = (string) $option;
  145. $this->_getMultiOptions();
  146. if (isset($this->options[$option])) {
  147. $this->_translateOption($option, $this->options[$option]);
  148. return $this->options[$option];
  149. }
  150. return null;
  151. }
  152. /**
  153. * Retrieve options
  154. *
  155. * @return array
  156. */
  157. public function getMultiOptions()
  158. {
  159. $this->_getMultiOptions();
  160. foreach ($this->options as $option => $value) {
  161. $this->_translateOption($option, $value);
  162. }
  163. return $this->options;
  164. }
  165. /**
  166. * Remove a single multi option
  167. *
  168. * @param string $option
  169. * @return bool
  170. */
  171. public function removeMultiOption($option)
  172. {
  173. $option = (string) $option;
  174. $this->_getMultiOptions();
  175. if (isset($this->options[$option])) {
  176. unset($this->options[$option]);
  177. if (isset($this->_translated[$option])) {
  178. unset($this->_translated[$option]);
  179. }
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Clear all options
  186. *
  187. * @return Zend_Form_Element_Multi
  188. */
  189. public function clearMultiOptions()
  190. {
  191. $this->options = array();
  192. $this->_translated = array();
  193. return $this;
  194. }
  195. /**
  196. * Set flag indicating whether or not to auto-register inArray validator
  197. *
  198. * @param bool $flag
  199. * @return Zend_Form_Element_Multi
  200. */
  201. public function setRegisterInArrayValidator($flag)
  202. {
  203. $this->_registerInArrayValidator = (bool) $flag;
  204. return $this;
  205. }
  206. /**
  207. * Get status of auto-register inArray validator flag
  208. *
  209. * @return bool
  210. */
  211. public function registerInArrayValidator()
  212. {
  213. return $this->_registerInArrayValidator;
  214. }
  215. /**
  216. * Is the value provided valid?
  217. *
  218. * Autoregisters InArray validator if necessary.
  219. *
  220. * @param string $value
  221. * @param mixed $context
  222. * @return bool
  223. */
  224. public function isValid($value, $context = null)
  225. {
  226. if ($this->registerInArrayValidator()) {
  227. if (!$this->getValidator('InArray')) {
  228. $options = $this->getMultiOptions();
  229. $this->addValidator(
  230. 'InArray',
  231. true,
  232. array(array_keys($options))
  233. );
  234. }
  235. }
  236. return parent::isValid($value, $context);
  237. }
  238. /**
  239. * Translate an option
  240. *
  241. * @param string $option
  242. * @param string $value
  243. * @return bool
  244. */
  245. protected function _translateOption($option, $value)
  246. {
  247. if (!isset($this->_translated[$option])) {
  248. $this->options[$option] = $this->_translateValue($value);
  249. if ($this->options[$option] === $value) {
  250. return false;
  251. }
  252. $this->_translated[$option] = true;
  253. return true;
  254. }
  255. return false;
  256. }
  257. /**
  258. * Translate a value
  259. *
  260. * @param array|string $value
  261. * @return array|string
  262. */
  263. protected function _translateValue($value)
  264. {
  265. if (is_array($value)) {
  266. foreach ($value as $key => $val) {
  267. $value[$key] = $this->_translateValue($val);
  268. }
  269. return $value;
  270. } else {
  271. if (null !== ($translator = $this->getTranslator())) {
  272. return $translator->translate($value);
  273. }
  274. return $value;
  275. }
  276. }
  277. }