MultiCheckboxWidget.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. use Cake\View\Widget\WidgetInterface;
  19. /**
  20. * Input widget class for generating multiple checkboxes.
  21. *
  22. */
  23. class MultiCheckboxWidget implements WidgetInterface
  24. {
  25. use IdGeneratorTrait;
  26. /**
  27. * Template instance to use.
  28. *
  29. * @var \Cake\View\StringTemplate
  30. */
  31. protected $_templates;
  32. /**
  33. * Label widget instance.
  34. *
  35. * @var \Cake\View\Widget\LabelWidget
  36. */
  37. protected $_label;
  38. /**
  39. * Render multi-checkbox widget.
  40. *
  41. * This class uses the following templates:
  42. *
  43. * - `checkbox` Renders checkbox input controls. Accepts
  44. * the `name`, `value` and `attrs` variables.
  45. * - `checkboxWrapper` Renders the containing div/element for
  46. * a checkbox and its label. Accepts the `input`, and `label`
  47. * variables.
  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. ];
  109. $out = [];
  110. $this->_idPrefix = $data['idPrefix'];
  111. $this->_clearIds();
  112. foreach ($data['options'] as $key => $val) {
  113. $checkbox = [
  114. 'value' => $key,
  115. 'text' => $val,
  116. ];
  117. if (is_array($val) && isset($val['text'], $val['value'])) {
  118. $checkbox = $val;
  119. }
  120. $checkbox['name'] = $data['name'];
  121. $checkbox['escape'] = $data['escape'];
  122. if ($this->_isSelected($checkbox['value'], $data['val'])) {
  123. $checkbox['checked'] = true;
  124. }
  125. if ($this->_isDisabled($checkbox['value'], $data['disabled'])) {
  126. $checkbox['disabled'] = true;
  127. }
  128. if (empty($checkbox['id'])) {
  129. $checkbox['id'] = $this->_id($checkbox['name'], $checkbox['value']);
  130. }
  131. $out[] = $this->_renderInput($checkbox, $context);
  132. }
  133. return implode('', $out);
  134. }
  135. /**
  136. * Render a single checkbox & wrapper.
  137. *
  138. * @param array $checkbox An array containing checkbox key/value option pairs
  139. * @param \Cake\View\Form\ContextInterface $context Context object.
  140. * @return string
  141. */
  142. protected function _renderInput($checkbox, $context)
  143. {
  144. $input = $this->_templates->format('checkbox', [
  145. 'name' => $checkbox['name'] . '[]',
  146. 'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'],
  147. 'attrs' => $this->_templates->formatAttributes(
  148. $checkbox,
  149. ['name', 'value', 'text']
  150. )
  151. ]);
  152. $labelAttrs = [
  153. 'for' => $checkbox['id'],
  154. 'escape' => $checkbox['escape'],
  155. 'text' => $checkbox['text'],
  156. 'input' => $input,
  157. ];
  158. if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
  159. $labelAttrs['class'] = 'selected';
  160. }
  161. $label = $this->_label->render($labelAttrs, $context);
  162. return $this->_templates->format('checkboxWrapper', [
  163. 'label' => $label,
  164. 'input' => $input
  165. ]);
  166. }
  167. /**
  168. * Helper method for deciding what options are selected.
  169. *
  170. * @param string $key The key to test.
  171. * @param array|string|null $selected The selected values.
  172. * @return bool
  173. */
  174. protected function _isSelected($key, $selected)
  175. {
  176. if ($selected === null) {
  177. return false;
  178. }
  179. $isArray = is_array($selected);
  180. if (!$isArray) {
  181. return (string)$key === (string)$selected;
  182. }
  183. $strict = !is_numeric($key);
  184. return in_array((string)$key, $selected, $strict);
  185. }
  186. /**
  187. * Helper method for deciding what options are disabled.
  188. *
  189. * @param string $key The key to test.
  190. * @param array|null $disabled The disabled values.
  191. * @return bool
  192. */
  193. protected function _isDisabled($key, $disabled)
  194. {
  195. if ($disabled === null || $disabled === false) {
  196. return false;
  197. }
  198. if ($disabled === true || is_string($disabled)) {
  199. return true;
  200. }
  201. $strict = !is_numeric($key);
  202. return in_array((string)$key, $disabled, $strict);
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. public function secureFields(array $data)
  208. {
  209. return [$data['name']];
  210. }
  211. }