PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Form/View/Helper/FormMultiCheckbox.php

https://bitbucket.org/saifshuvo/zf2
PHP | 467 lines | 251 code | 60 blank | 156 comment | 34 complexity | a12fd3aba1c890e2a09d46def8bf0413 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\View\Helper;
  10. use Zend\Form\ElementInterface;
  11. use Zend\Form\Element\MultiCheckbox as MultiCheckboxElement;
  12. use Zend\Form\Exception;
  13. class FormMultiCheckbox extends FormInput
  14. {
  15. const LABEL_APPEND = 'append';
  16. const LABEL_PREPEND = 'prepend';
  17. /**
  18. * The attributes applied to option label
  19. *
  20. * @var array
  21. */
  22. protected $labelAttributes;
  23. /**
  24. * Where will be label rendered?
  25. *
  26. * @var string
  27. */
  28. protected $labelPosition = self::LABEL_APPEND;
  29. /**
  30. * Separator for checkbox elements
  31. *
  32. * @var string
  33. */
  34. protected $separator = '';
  35. /**
  36. * Prefixing the element with a hidden element for the unset value?
  37. *
  38. * @var bool
  39. */
  40. protected $useHiddenElement = false;
  41. /**
  42. * The unchecked value used when "UseHiddenElement" is turned on
  43. *
  44. * @var string
  45. */
  46. protected $uncheckedValue = '';
  47. /**
  48. * Form input helper instance
  49. *
  50. * @var FormInput
  51. */
  52. protected $inputHelper;
  53. /**
  54. * Form label helper instance
  55. *
  56. * @var FormLabel
  57. */
  58. protected $labelHelper;
  59. /**
  60. * Invoke helper as functor
  61. *
  62. * Proxies to {@link render()}.
  63. *
  64. * @param ElementInterface|null $element
  65. * @param null|string $labelPosition
  66. * @return string|FormMultiCheckbox
  67. */
  68. public function __invoke(ElementInterface $element = null, $labelPosition = null)
  69. {
  70. if (!$element) {
  71. return $this;
  72. }
  73. if ($labelPosition !== null) {
  74. $this->setLabelPosition($labelPosition);
  75. }
  76. return $this->render($element);
  77. }
  78. /**
  79. * Render a form <input> element from the provided $element
  80. *
  81. * @param ElementInterface $element
  82. * @throws Exception\InvalidArgumentException
  83. * @throws Exception\DomainException
  84. * @return string
  85. */
  86. public function render(ElementInterface $element)
  87. {
  88. if (!$element instanceof MultiCheckboxElement) {
  89. throw new Exception\InvalidArgumentException(sprintf(
  90. '%s requires that the element is of type Zend\Form\Element\MultiCheckbox',
  91. __METHOD__
  92. ));
  93. }
  94. $name = static::getName($element);
  95. $options = $element->getValueOptions();
  96. if (empty($options)) {
  97. throw new Exception\DomainException(sprintf(
  98. '%s requires that the element has "value_options"; none found',
  99. __METHOD__
  100. ));
  101. }
  102. $attributes = $element->getAttributes();
  103. $attributes['name'] = $name;
  104. $attributes['type'] = $this->getInputType();
  105. $selectedOptions = (array) $element->getValue();
  106. $rendered = $this->renderOptions($element, $options, $selectedOptions, $attributes);
  107. // Render hidden element
  108. $useHiddenElement = method_exists($element, 'useHiddenElement') && $element->useHiddenElement()
  109. ? $element->useHiddenElement()
  110. : $this->useHiddenElement;
  111. if ($useHiddenElement) {
  112. $rendered = $this->renderHiddenElement($element, $attributes) . $rendered;
  113. }
  114. return $rendered;
  115. }
  116. /**
  117. * Render options
  118. *
  119. * @param MultiCheckboxElement $element
  120. * @param array $options
  121. * @param array $selectedOptions
  122. * @param array $attributes
  123. * @return string
  124. */
  125. protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions,
  126. array $attributes)
  127. {
  128. $escapeHtmlHelper = $this->getEscapeHtmlHelper();
  129. $labelHelper = $this->getLabelHelper();
  130. $labelClose = $labelHelper->closeTag();
  131. $labelPosition = $this->getLabelPosition();
  132. $globalLabelAttributes = $element->getLabelAttributes();
  133. $closingBracket = $this->getInlineClosingBracket();
  134. if (empty($globalLabelAttributes)) {
  135. $globalLabelAttributes = $this->labelAttributes;
  136. }
  137. $combinedMarkup = array();
  138. $count = 0;
  139. foreach ($options as $key => $optionSpec) {
  140. $count++;
  141. if ($count > 1 && array_key_exists('id', $attributes)) {
  142. unset($attributes['id']);
  143. }
  144. $value = '';
  145. $label = '';
  146. $inputAttributes = $attributes;
  147. $labelAttributes = $globalLabelAttributes;
  148. $selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected'] != false ? true : false;
  149. $disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled'] != false ? true : false;
  150. if (is_scalar($optionSpec)) {
  151. $optionSpec = array(
  152. 'label' => $optionSpec,
  153. 'value' => $key
  154. );
  155. }
  156. if (isset($optionSpec['value'])) {
  157. $value = $optionSpec['value'];
  158. }
  159. if (isset($optionSpec['label'])) {
  160. $label = $optionSpec['label'];
  161. }
  162. if (isset($optionSpec['selected'])) {
  163. $selected = $optionSpec['selected'];
  164. }
  165. if (isset($optionSpec['disabled'])) {
  166. $disabled = $optionSpec['disabled'];
  167. }
  168. if (isset($optionSpec['label_attributes'])) {
  169. $labelAttributes = (isset($labelAttributes))
  170. ? array_merge($labelAttributes, $optionSpec['label_attributes'])
  171. : $optionSpec['label_attributes'];
  172. }
  173. if (isset($optionSpec['attributes'])) {
  174. $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
  175. }
  176. if (in_array($value, $selectedOptions)) {
  177. $selected = true;
  178. }
  179. $inputAttributes['value'] = $value;
  180. $inputAttributes['checked'] = $selected;
  181. $inputAttributes['disabled'] = $disabled;
  182. $input = sprintf(
  183. '<input %s%s',
  184. $this->createAttributesString($inputAttributes),
  185. $closingBracket
  186. );
  187. if (null !== ($translator = $this->getTranslator())) {
  188. $label = $translator->translate(
  189. $label, $this->getTranslatorTextDomain()
  190. );
  191. }
  192. $label = $escapeHtmlHelper($label);
  193. $labelOpen = $labelHelper->openTag($labelAttributes);
  194. $template = $labelOpen . '%s%s' . $labelClose;
  195. switch ($labelPosition) {
  196. case self::LABEL_PREPEND:
  197. $markup = sprintf($template, $label, $input);
  198. break;
  199. case self::LABEL_APPEND:
  200. default:
  201. $markup = sprintf($template, $input, $label);
  202. break;
  203. }
  204. $combinedMarkup[] = $markup;
  205. }
  206. return implode($this->getSeparator(), $combinedMarkup);
  207. }
  208. /**
  209. * Render a hidden element for empty/unchecked value
  210. *
  211. * @param MultiCheckboxElement $element
  212. * @param array $attributes
  213. * @return string
  214. */
  215. protected function renderHiddenElement(MultiCheckboxElement $element, array $attributes)
  216. {
  217. $closingBracket = $this->getInlineClosingBracket();
  218. $uncheckedValue = $element->getUncheckedValue()
  219. ? $element->getUncheckedValue()
  220. : $this->uncheckedValue;
  221. $hiddenAttributes = array(
  222. 'name' => $element->getName(),
  223. 'value' => $uncheckedValue,
  224. );
  225. return sprintf(
  226. '<input type="hidden" %s%s',
  227. $this->createAttributesString($hiddenAttributes),
  228. $closingBracket
  229. );
  230. }
  231. /**
  232. * Sets the attributes applied to option label.
  233. *
  234. * @param array|null $attributes
  235. * @return FormMultiCheckbox
  236. */
  237. public function setLabelAttributes($attributes)
  238. {
  239. $this->labelAttributes = $attributes;
  240. return $this;
  241. }
  242. /**
  243. * Returns the attributes applied to each option label.
  244. *
  245. * @return array|null
  246. */
  247. public function getLabelAttributes()
  248. {
  249. return $this->labelAttributes;
  250. }
  251. /**
  252. * Set value for labelPosition
  253. *
  254. * @param mixed $labelPosition
  255. * @throws Exception\InvalidArgumentException
  256. * @return FormMultiCheckbox
  257. */
  258. public function setLabelPosition($labelPosition)
  259. {
  260. $labelPosition = strtolower($labelPosition);
  261. if (!in_array($labelPosition, array(self::LABEL_APPEND, self::LABEL_PREPEND))) {
  262. throw new Exception\InvalidArgumentException(sprintf(
  263. '%s expects either %s::LABEL_APPEND or %s::LABEL_PREPEND; received "%s"',
  264. __METHOD__,
  265. __CLASS__,
  266. __CLASS__,
  267. (string) $labelPosition
  268. ));
  269. }
  270. $this->labelPosition = $labelPosition;
  271. return $this;
  272. }
  273. /**
  274. * Get position of label
  275. *
  276. * @return string
  277. */
  278. public function getLabelPosition()
  279. {
  280. return $this->labelPosition;
  281. }
  282. /**
  283. * Set separator string for checkbox elements
  284. *
  285. * @param string $separator
  286. * @return FormMultiCheckbox
  287. */
  288. public function setSeparator($separator)
  289. {
  290. $this->separator = (string) $separator;
  291. return $this;
  292. }
  293. /**
  294. * Get separator for checkbox elements
  295. *
  296. * @return string
  297. */
  298. public function getSeparator()
  299. {
  300. return $this->separator;
  301. }
  302. /**
  303. * Sets the option for prefixing the element with a hidden element
  304. * for the unset value.
  305. *
  306. * @param bool $useHiddenElement
  307. * @return FormMultiCheckbox
  308. */
  309. public function setUseHiddenElement($useHiddenElement)
  310. {
  311. $this->useHiddenElement = (bool) $useHiddenElement;
  312. return $this;
  313. }
  314. /**
  315. * Returns the option for prefixing the element with a hidden element
  316. * for the unset value.
  317. *
  318. * @return bool
  319. */
  320. public function getUseHiddenElement()
  321. {
  322. return $this->useHiddenElement;
  323. }
  324. /**
  325. * Sets the unchecked value used when "UseHiddenElement" is turned on.
  326. *
  327. * @param bool $value
  328. * @return FormMultiCheckbox
  329. */
  330. public function setUncheckedValue($value)
  331. {
  332. $this->uncheckedValue = $value;
  333. return $this;
  334. }
  335. /**
  336. * Returns the unchecked value used when "UseHiddenElement" is turned on.
  337. *
  338. * @return string
  339. */
  340. public function getUncheckedValue()
  341. {
  342. return $this->uncheckedValue;
  343. }
  344. /**
  345. * Return input type
  346. *
  347. * @return string
  348. */
  349. protected function getInputType()
  350. {
  351. return 'checkbox';
  352. }
  353. /**
  354. * Get element name
  355. *
  356. * @param ElementInterface $element
  357. * @throws Exception\DomainException
  358. * @return string
  359. */
  360. protected static function getName(ElementInterface $element)
  361. {
  362. $name = $element->getName();
  363. if ($name === null || $name === '') {
  364. throw new Exception\DomainException(sprintf(
  365. '%s requires that the element has an assigned name; none discovered',
  366. __METHOD__
  367. ));
  368. }
  369. return $name . '[]';
  370. }
  371. /**
  372. * Retrieve the FormInput helper
  373. *
  374. * @return FormInput
  375. */
  376. protected function getInputHelper()
  377. {
  378. if ($this->inputHelper) {
  379. return $this->inputHelper;
  380. }
  381. if (method_exists($this->view, 'plugin')) {
  382. $this->inputHelper = $this->view->plugin('form_input');
  383. }
  384. if (!$this->inputHelper instanceof FormInput) {
  385. $this->inputHelper = new FormInput();
  386. }
  387. return $this->inputHelper;
  388. }
  389. /**
  390. * Retrieve the FormLabel helper
  391. *
  392. * @return FormLabel
  393. */
  394. protected function getLabelHelper()
  395. {
  396. if ($this->labelHelper) {
  397. return $this->labelHelper;
  398. }
  399. if (method_exists($this->view, 'plugin')) {
  400. $this->labelHelper = $this->view->plugin('form_label');
  401. }
  402. if (!$this->labelHelper instanceof FormLabel) {
  403. $this->labelHelper = new FormLabel();
  404. }
  405. return $this->labelHelper;
  406. }
  407. }