MultiCheckboxWidget.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Widget;
  16. use Cake\View\Form\ContextInterface;
  17. use Cake\View\Helper\IdGeneratorTrait;
  18. /**
  19. * Input widget class for generating multiple checkboxes.
  20. */
  21. class MultiCheckboxWidget implements WidgetInterface
  22. {
  23. use IdGeneratorTrait;
  24. /**
  25. * Template instance to use.
  26. *
  27. * @var \Cake\View\StringTemplate
  28. */
  29. protected $_templates;
  30. /**
  31. * Label widget instance.
  32. *
  33. * @var \Cake\View\Widget\LabelWidget
  34. */
  35. protected $_label;
  36. /**
  37. * Render multi-checkbox widget.
  38. *
  39. * This class uses the following templates:
  40. *
  41. * - `checkbox` Renders checkbox input controls. Accepts
  42. * the `name`, `value` and `attrs` variables.
  43. * - `checkboxWrapper` Renders the containing div/element for
  44. * a checkbox and its label. Accepts the `input`, and `label`
  45. * variables.
  46. * - `multicheckboxWrapper` Renders a wrapper around grouped inputs.
  47. * - `multicheckboxTitle` Renders the title element for grouped inputs.
  48. *
  49. * @param \Cake\View\StringTemplate $templates Templates list.
  50. * @param \Cake\View\Widget\LabelWidget $label Label widget instance.
  51. */
  52. public function __construct($templates, $label)
  53. {
  54. $this->_templates = $templates;
  55. $this->_label = $label;
  56. }
  57. /**
  58. * Render multi-checkbox widget.
  59. *
  60. * Data supports the following options.
  61. *
  62. * - `name` The name attribute of the inputs to create.
  63. * `[]` will be appended to the name.
  64. * - `options` An array of options to create checkboxes out of.
  65. * - `val` Either a string/integer or array of values that should be
  66. * checked. Can also be a complex options set.
  67. * - `disabled` Either a boolean or an array of checkboxes to disable.
  68. * - `escape` Set to false to disable HTML escaping.
  69. * - `options` An associative array of value=>labels to generate options for.
  70. * - `idPrefix` Prefix for generated ID attributes.
  71. *
  72. * ### Options format
  73. *
  74. * The options option can take a variety of data format depending on
  75. * the complexity of HTML you want generated.
  76. *
  77. * You can generate simple options using a basic associative array:
  78. *
  79. * ```
  80. * 'options' => ['elk' => 'Elk', 'beaver' => 'Beaver']
  81. * ```
  82. *
  83. * If you need to define additional attributes on your option elements
  84. * you can use the complex form for options:
  85. *
  86. * ```
  87. * 'options' => [
  88. * ['value' => 'elk', 'text' => 'Elk', 'data-foo' => 'bar'],
  89. * ]
  90. * ```
  91. *
  92. * This form **requires** that both the `value` and `text` keys be defined.
  93. * If either is not set options will not be generated correctly.
  94. *
  95. * @param array $data The data to generate a checkbox set with.
  96. * @param \Cake\View\Form\ContextInterface $context The current form context.
  97. * @return string
  98. */
  99. public function render(array $data, ContextInterface $context)
  100. {
  101. $data += [
  102. 'name' => '',
  103. 'escape' => true,
  104. 'options' => [],
  105. 'disabled' => null,
  106. 'val' => null,
  107. 'idPrefix' => null,
  108. 'templateVars' => []
  109. ];
  110. $this->_idPrefix = $data['idPrefix'];
  111. $this->_clearIds();
  112. return implode('', $this->_renderInputs($data, $context));
  113. }
  114. /**
  115. * Render the checkbox inputs.
  116. *
  117. * @param array $data The data array defining the checkboxes.
  118. * @param \Cake\View\Form\ContextInterface $context The current form context.
  119. * @return array An array of rendered inputs.
  120. */
  121. protected function _renderInputs($data, $context)
  122. {
  123. $out = [];
  124. foreach ($data['options'] as $key => $val) {
  125. // Grouped inputs in a fieldset.
  126. if (is_string($key) && is_array($val) && !isset($val['text'], $val['value'])) {
  127. $inputs = $this->_renderInputs(['options' => $val] + $data, $context);
  128. $title = $this->_templates->format('multicheckboxTitle', ['text' => $key]);
  129. $out[] = $this->_templates->format('multicheckboxWrapper', [
  130. 'content' => $title . implode('', $inputs)
  131. ]);
  132. continue;
  133. }
  134. // Standard inputs.
  135. $checkbox = [
  136. 'value' => $key,
  137. 'text' => $val,
  138. ];
  139. if (is_array($val) && isset($val['text'], $val['value'])) {
  140. $checkbox = $val;
  141. }
  142. if (!isset($checkbox['templateVars'])) {
  143. $checkbox['templateVars'] = $data['templateVars'];
  144. }
  145. if (!empty($data['templateVars'])) {
  146. $checkbox['templateVars'] = array_merge($data['templateVars'], $checkbox['templateVars']);
  147. }
  148. $checkbox['name'] = $data['name'];
  149. $checkbox['escape'] = $data['escape'];
  150. if ($this->_isSelected($checkbox['value'], $data['val'])) {
  151. $checkbox['checked'] = true;
  152. }
  153. if ($this->_isDisabled($checkbox['value'], $data['disabled'])) {
  154. $checkbox['disabled'] = true;
  155. }
  156. if (empty($checkbox['id'])) {
  157. $checkbox['id'] = $this->_id($checkbox['name'], $checkbox['value']);
  158. }
  159. $out[] = $this->_renderInput($checkbox + $data, $context);
  160. }
  161. return $out;
  162. }
  163. /**
  164. * Render a single checkbox & wrapper.
  165. *
  166. * @param array $checkbox An array containing checkbox key/value option pairs
  167. * @param \Cake\View\Form\ContextInterface $context Context object.
  168. * @return string
  169. */
  170. protected function _renderInput($checkbox, $context)
  171. {
  172. $input = $this->_templates->format('checkbox', [
  173. 'name' => $checkbox['name'] . '[]',
  174. 'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'],
  175. 'templateVars' => $checkbox['templateVars'],
  176. 'attrs' => $this->_templates->formatAttributes(
  177. $checkbox,
  178. ['name', 'value', 'text', 'options', 'label', 'val', 'type', 'hiddenField']
  179. )
  180. ]);
  181. $labelAttrs = [
  182. 'for' => $checkbox['id'],
  183. 'escape' => $checkbox['escape'],
  184. 'text' => $checkbox['text'],
  185. 'templateVars' => $checkbox['templateVars'],
  186. 'input' => $input,
  187. ];
  188. if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
  189. $labelAttrs['class'] = 'selected';
  190. }
  191. $label = $this->_label->render($labelAttrs, $context);
  192. return $this->_templates->format('checkboxWrapper', [
  193. 'templateVars' => $checkbox['templateVars'],
  194. 'label' => $label,
  195. 'input' => $input
  196. ]);
  197. }
  198. /**
  199. * Helper method for deciding what options are selected.
  200. *
  201. * @param string $key The key to test.
  202. * @param array|string|null $selected The selected values.
  203. * @return bool
  204. */
  205. protected function _isSelected($key, $selected)
  206. {
  207. if ($selected === null) {
  208. return false;
  209. }
  210. $isArray = is_array($selected);
  211. if (!$isArray) {
  212. return (string)$key === (string)$selected;
  213. }
  214. $strict = !is_numeric($key);
  215. return in_array((string)$key, $selected, $strict);
  216. }
  217. /**
  218. * Helper method for deciding what options are disabled.
  219. *
  220. * @param string $key The key to test.
  221. * @param array|null $disabled The disabled values.
  222. * @return bool
  223. */
  224. protected function _isDisabled($key, $disabled)
  225. {
  226. if ($disabled === null || $disabled === false) {
  227. return false;
  228. }
  229. if ($disabled === true || is_string($disabled)) {
  230. return true;
  231. }
  232. $strict = !is_numeric($key);
  233. return in_array((string)$key, $disabled, $strict);
  234. }
  235. /**
  236. * {@inheritDoc}
  237. */
  238. public function secureFields(array $data)
  239. {
  240. return [$data['name']];
  241. }
  242. }