/library/Zend/Dojo/Form/Element/DijitMulti.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 324 lines · 139 code · 37 blank · 148 comment · 17 complexity · 418fe311d24741a9bbb1f994a686c494 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 Form_Element
  18. * @copyright Copyright (c) 2005-2012 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id: DijitMulti.php 24593 2012-01-05 20:35:02Z matthew $
  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. *
  71. * @return self
  72. */
  73. public function setSeparator ($separator)
  74. {
  75. $this->_separator = $separator;
  76. return $this;
  77. }
  78. /**
  79. * Retrieve options array
  80. *
  81. * @return array
  82. */
  83. protected function _getMultiOptions ()
  84. {
  85. if (null === $this->options || !is_array($this->options)) {
  86. $this->options = array();
  87. }
  88. return $this->options;
  89. }
  90. /**
  91. * Add an option
  92. *
  93. * @param string $option
  94. * @param string $value
  95. *
  96. * @return Zend_Form_Element_Multi
  97. */
  98. public function addMultiOption ($option, $value = '')
  99. {
  100. $option = (string)$option;
  101. $this->_getMultiOptions();
  102. if (!$this->_translateOption($option, $value)) {
  103. $this->options[$option] = $value;
  104. }
  105. return $this;
  106. }
  107. /**
  108. * Add many options at once
  109. *
  110. * @param array $options
  111. *
  112. * @return Zend_Form_Element_Multi
  113. */
  114. public function addMultiOptions (array $options)
  115. {
  116. foreach ($options as $option => $value) {
  117. if (is_array($value)
  118. && array_key_exists('key', $value)
  119. && array_key_exists('value', $value)
  120. ) {
  121. $this->addMultiOption($value['key'], $value['value']);
  122. } else {
  123. $this->addMultiOption($option, $value);
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Set all options at once (overwrites)
  130. *
  131. * @param array $options
  132. *
  133. * @return Zend_Form_Element_Multi
  134. */
  135. public function setMultiOptions (array $options)
  136. {
  137. $this->clearMultiOptions();
  138. return $this->addMultiOptions($options);
  139. }
  140. /**
  141. * Retrieve single multi option
  142. *
  143. * @param string $option
  144. *
  145. * @return mixed
  146. */
  147. public function getMultiOption ($option)
  148. {
  149. $option = (string)$option;
  150. $this->_getMultiOptions();
  151. if (isset( $this->options[$option] )) {
  152. $this->_translateOption($option, $this->options[$option]);
  153. return $this->options[$option];
  154. }
  155. return null;
  156. }
  157. /**
  158. * Retrieve options
  159. *
  160. * @return array
  161. */
  162. public function getMultiOptions ()
  163. {
  164. $this->_getMultiOptions();
  165. foreach ($this->options as $option => $value) {
  166. $this->_translateOption($option, $value);
  167. }
  168. return $this->options;
  169. }
  170. /**
  171. * Remove a single multi option
  172. *
  173. * @param string $option
  174. *
  175. * @return bool
  176. */
  177. public function removeMultiOption ($option)
  178. {
  179. $option = (string)$option;
  180. $this->_getMultiOptions();
  181. if (isset( $this->options[$option] )) {
  182. unset( $this->options[$option] );
  183. if (isset( $this->_translated[$option] )) {
  184. unset( $this->_translated[$option] );
  185. }
  186. return true;
  187. }
  188. return false;
  189. }
  190. /**
  191. * Clear all options
  192. *
  193. * @return Zend_Form_Element_Multi
  194. */
  195. public function clearMultiOptions ()
  196. {
  197. $this->options = array();
  198. $this->_translated = array();
  199. return $this;
  200. }
  201. /**
  202. * Set flag indicating whether or not to auto-register inArray validator
  203. *
  204. * @param bool $flag
  205. *
  206. * @return Zend_Form_Element_Multi
  207. */
  208. public function setRegisterInArrayValidator ($flag)
  209. {
  210. $this->_registerInArrayValidator = (bool)$flag;
  211. return $this;
  212. }
  213. /**
  214. * Get status of auto-register inArray validator flag
  215. *
  216. * @return bool
  217. */
  218. public function registerInArrayValidator ()
  219. {
  220. return $this->_registerInArrayValidator;
  221. }
  222. /**
  223. * Is the value provided valid?
  224. *
  225. * Autoregisters InArray validator if necessary.
  226. *
  227. * @param string $value
  228. * @param mixed $context
  229. *
  230. * @return bool
  231. */
  232. public function isValid ($value, $context = null)
  233. {
  234. if ($this->registerInArrayValidator()) {
  235. if (!$this->getValidator('InArray')) {
  236. $options = $this->getMultiOptions();
  237. $this->addValidator(
  238. 'InArray',
  239. true,
  240. array( array_keys($options) )
  241. );
  242. }
  243. }
  244. return parent::isValid($value, $context);
  245. }
  246. /**
  247. * Translate an option
  248. *
  249. * @param string $option
  250. * @param string $value
  251. *
  252. * @return bool
  253. */
  254. protected function _translateOption ($option, $value)
  255. {
  256. if (!isset( $this->_translated[$option] )) {
  257. $this->options[$option] = $this->_translateValue($value);
  258. if ($this->options[$option] === $value) {
  259. return false;
  260. }
  261. $this->_translated[$option] = true;
  262. return true;
  263. }
  264. return false;
  265. }
  266. /**
  267. * Translate a value
  268. *
  269. * @param array|string $value
  270. *
  271. * @return array|string
  272. */
  273. protected function _translateValue ($value)
  274. {
  275. if (is_array($value)) {
  276. foreach ($value as $key => $val) {
  277. $value[$key] = $this->_translateValue($val);
  278. }
  279. return $value;
  280. } else {
  281. if (null !== ( $translator = $this->getTranslator() )) {
  282. return $translator->translate($value);
  283. }
  284. return $value;
  285. }
  286. }
  287. }