PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/smarty/plugins/function.html_checkboxes.php

http://github.com/modxcms/revolution
PHP | 286 lines | 227 code | 1 blank | 58 comment | 38 complexity | b5a04508992fbd75b4d6538847d9149f MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_checkboxes} function plugin
  10. * File: function.html_checkboxes.php
  11. * Type: function
  12. * Name: html_checkboxes
  13. * Date: 24.Feb.2003
  14. * Purpose: Prints out a list of checkbox input types
  15. * Examples:
  16. *
  17. * {html_checkboxes values=$ids output=$names}
  18. * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
  19. * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
  20. *
  21. * Params:
  22. *
  23. * - name (optional) - string default "checkbox"
  24. * - values (required) - array
  25. * - options (optional) - associative array
  26. * - checked (optional) - array default not set
  27. * - separator (optional) - ie <br> or &nbsp;
  28. * - output (optional) - the output next to each checkbox
  29. * - assign (optional) - assign the output as an array to this variable
  30. * - escape (optional) - escape the content (not value), defaults to true
  31. *
  32. * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
  33. * (Smarty online manual)
  34. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  35. * @author credits to Monte Ohrt <monte at ohrt dot com>
  36. * @version 1.0
  37. *
  38. * @param array $params parameters
  39. * @param Smarty_Internal_Template $template template object
  40. *
  41. * @return string
  42. * @uses smarty_function_escape_special_chars()
  43. * @throws \SmartyException
  44. */
  45. function smarty_function_html_checkboxes($params, Smarty_Internal_Template $template)
  46. {
  47. $template->_checkPlugins(
  48. array(
  49. array(
  50. 'function' => 'smarty_function_escape_special_chars',
  51. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
  52. )
  53. )
  54. );
  55. $name = 'checkbox';
  56. $values = null;
  57. $options = null;
  58. $selected = array();
  59. $separator = '';
  60. $escape = true;
  61. $labels = true;
  62. $label_ids = false;
  63. $output = null;
  64. $extra = '';
  65. foreach ($params as $_key => $_val) {
  66. switch ($_key) {
  67. case 'name':
  68. case 'separator':
  69. $$_key = (string)$_val;
  70. break;
  71. case 'escape':
  72. case 'labels':
  73. case 'label_ids':
  74. $$_key = (bool)$_val;
  75. break;
  76. case 'options':
  77. $$_key = (array)$_val;
  78. break;
  79. case 'values':
  80. case 'output':
  81. $$_key = array_values((array)$_val);
  82. break;
  83. case 'checked':
  84. case 'selected':
  85. if (is_array($_val)) {
  86. $selected = array();
  87. foreach ($_val as $_sel) {
  88. if (is_object($_sel)) {
  89. if (method_exists($_sel, '__toString')) {
  90. $_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
  91. } else {
  92. trigger_error(
  93. 'html_checkboxes: selected attribute contains an object of class \'' .
  94. get_class($_sel) . '\' without __toString() method',
  95. E_USER_NOTICE
  96. );
  97. continue;
  98. }
  99. } else {
  100. $_sel = smarty_function_escape_special_chars((string)$_sel);
  101. }
  102. $selected[ $_sel ] = true;
  103. }
  104. } elseif (is_object($_val)) {
  105. if (method_exists($_val, '__toString')) {
  106. $selected = smarty_function_escape_special_chars((string)$_val->__toString());
  107. } else {
  108. trigger_error(
  109. 'html_checkboxes: selected attribute is an object of class \'' . get_class($_val) .
  110. '\' without __toString() method',
  111. E_USER_NOTICE
  112. );
  113. }
  114. } else {
  115. $selected = smarty_function_escape_special_chars((string)$_val);
  116. }
  117. break;
  118. case 'checkboxes':
  119. trigger_error(
  120. 'html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead',
  121. E_USER_WARNING
  122. );
  123. $options = (array)$_val;
  124. break;
  125. case 'assign':
  126. break;
  127. case 'strict':
  128. break;
  129. case 'disabled':
  130. case 'readonly':
  131. if (!empty($params[ 'strict' ])) {
  132. if (!is_scalar($_val)) {
  133. trigger_error(
  134. "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
  135. E_USER_NOTICE
  136. );
  137. }
  138. if ($_val === true || $_val === $_key) {
  139. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  140. }
  141. break;
  142. }
  143. // omit break; to fall through!
  144. // no break
  145. default:
  146. if (!is_array($_val)) {
  147. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  148. } else {
  149. trigger_error("html_checkboxes: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  150. }
  151. break;
  152. }
  153. }
  154. if (!isset($options) && !isset($values)) {
  155. return '';
  156. } /* raise error here? */
  157. $_html_result = array();
  158. if (isset($options)) {
  159. foreach ($options as $_key => $_val) {
  160. $_html_result[] =
  161. smarty_function_html_checkboxes_output(
  162. $name,
  163. $_key,
  164. $_val,
  165. $selected,
  166. $extra,
  167. $separator,
  168. $labels,
  169. $label_ids,
  170. $escape
  171. );
  172. }
  173. } else {
  174. foreach ($values as $_i => $_key) {
  175. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  176. $_html_result[] =
  177. smarty_function_html_checkboxes_output(
  178. $name,
  179. $_key,
  180. $_val,
  181. $selected,
  182. $extra,
  183. $separator,
  184. $labels,
  185. $label_ids,
  186. $escape
  187. );
  188. }
  189. }
  190. if (!empty($params[ 'assign' ])) {
  191. $template->assign($params[ 'assign' ], $_html_result);
  192. } else {
  193. return implode("\n", $_html_result);
  194. }
  195. }
  196. /**
  197. * @param $name
  198. * @param $value
  199. * @param $output
  200. * @param $selected
  201. * @param $extra
  202. * @param $separator
  203. * @param $labels
  204. * @param $label_ids
  205. * @param bool $escape
  206. *
  207. * @return string
  208. */
  209. function smarty_function_html_checkboxes_output(
  210. $name,
  211. $value,
  212. $output,
  213. $selected,
  214. $extra,
  215. $separator,
  216. $labels,
  217. $label_ids,
  218. $escape = true
  219. ) {
  220. $_output = '';
  221. if (is_object($value)) {
  222. if (method_exists($value, '__toString')) {
  223. $value = (string)$value->__toString();
  224. } else {
  225. trigger_error(
  226. 'html_options: value is an object of class \'' . get_class($value) .
  227. '\' without __toString() method',
  228. E_USER_NOTICE
  229. );
  230. return '';
  231. }
  232. } else {
  233. $value = (string)$value;
  234. }
  235. if (is_object($output)) {
  236. if (method_exists($output, '__toString')) {
  237. $output = (string)$output->__toString();
  238. } else {
  239. trigger_error(
  240. 'html_options: output is an object of class \'' . get_class($output) .
  241. '\' without __toString() method',
  242. E_USER_NOTICE
  243. );
  244. return '';
  245. }
  246. } else {
  247. $output = (string)$output;
  248. }
  249. if ($labels) {
  250. if ($label_ids) {
  251. $_id = smarty_function_escape_special_chars(
  252. preg_replace(
  253. '![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER,
  254. '_',
  255. $name . '_' . $value
  256. )
  257. );
  258. $_output .= '<label for="' . $_id . '">';
  259. } else {
  260. $_output .= '<label>';
  261. }
  262. }
  263. $name = smarty_function_escape_special_chars($name);
  264. $value = smarty_function_escape_special_chars($value);
  265. if ($escape) {
  266. $output = smarty_function_escape_special_chars($output);
  267. }
  268. $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
  269. if ($labels && $label_ids) {
  270. $_output .= ' id="' . $_id . '"';
  271. }
  272. if (is_array($selected)) {
  273. if (isset($selected[ $value ])) {
  274. $_output .= ' checked="checked"';
  275. }
  276. } elseif ($value === $selected) {
  277. $_output .= ' checked="checked"';
  278. }
  279. $_output .= $extra . ' />' . $output;
  280. if ($labels) {
  281. $_output .= '</label>';
  282. }
  283. $_output .= $separator;
  284. return $_output;
  285. }