/src/frapi/library/Zend/Form/Element/Multi.php

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