PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/smarty/lib/plugins/function.html_checkboxes.php

https://gitlab.com/alvarobacelar/cms-admin-marko
PHP | 233 lines | 159 code | 31 blank | 43 comment | 37 complexity | 34f3510d735fe652e46be39e8789ebf1 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_checkboxes} function plugin
  10. *
  11. * File: function.html_checkboxes.php<br>
  12. * Type: function<br>
  13. * Name: html_checkboxes<br>
  14. * Date: 24.Feb.2003<br>
  15. * Purpose: Prints out a list of checkbox input types<br>
  16. * Examples:
  17. * <pre>
  18. * {html_checkboxes values=$ids output=$names}
  19. * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
  20. * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
  21. * </pre>
  22. * Params:
  23. * <pre>
  24. * - name (optional) - string default "checkbox"
  25. * - values (required) - array
  26. * - options (optional) - associative array
  27. * - checked (optional) - array default not set
  28. * - separator (optional) - ie <br> or &nbsp;
  29. * - output (optional) - the output next to each checkbox
  30. * - assign (optional) - assign the output as an array to this variable
  31. * - escape (optional) - escape the content (not value), defaults to true
  32. * </pre>
  33. *
  34. * @link http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
  35. * (Smarty online manual)
  36. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  37. * @author credits to Monte Ohrt <monte at ohrt dot com>
  38. * @version 1.0
  39. * @param array $params parameters
  40. * @param object $template template object
  41. * @return string
  42. * @uses smarty_function_escape_special_chars()
  43. */
  44. function smarty_function_html_checkboxes($params, $template)
  45. {
  46. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  47. $name = 'checkbox';
  48. $values = null;
  49. $options = null;
  50. $selected = array();
  51. $separator = '';
  52. $escape = true;
  53. $labels = true;
  54. $label_ids = false;
  55. $output = null;
  56. $extra = '';
  57. foreach($params as $_key => $_val) {
  58. switch($_key) {
  59. case 'name':
  60. case 'separator':
  61. $$_key = (string) $_val;
  62. break;
  63. case 'escape':
  64. case 'labels':
  65. case 'label_ids':
  66. $$_key = (bool) $_val;
  67. break;
  68. case 'options':
  69. $$_key = (array) $_val;
  70. break;
  71. case 'values':
  72. case 'output':
  73. $$_key = array_values((array) $_val);
  74. break;
  75. case 'checked':
  76. case 'selected':
  77. if (is_array($_val)) {
  78. $selected = array();
  79. foreach ($_val as $_sel) {
  80. if (is_object($_sel)) {
  81. if (method_exists($_sel, "__toString")) {
  82. $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
  83. } else {
  84. trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
  85. continue;
  86. }
  87. } else {
  88. $_sel = smarty_function_escape_special_chars((string) $_sel);
  89. }
  90. $selected[$_sel] = true;
  91. }
  92. } elseif (is_object($_val)) {
  93. if (method_exists($_val, "__toString")) {
  94. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  95. } else {
  96. trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
  97. }
  98. } else {
  99. $selected = smarty_function_escape_special_chars((string) $_val);
  100. }
  101. break;
  102. case 'checkboxes':
  103. trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
  104. $options = (array) $_val;
  105. break;
  106. case 'assign':
  107. break;
  108. case 'strict': break;
  109. case 'disabled':
  110. case 'readonly':
  111. if (!empty($params['strict'])) {
  112. if (!is_scalar($_val)) {
  113. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
  114. }
  115. if ($_val === true || $_val === $_key) {
  116. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  117. }
  118. break;
  119. }
  120. // omit break; to fall through!
  121. default:
  122. if(!is_array($_val)) {
  123. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  124. } else {
  125. trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  126. }
  127. break;
  128. }
  129. }
  130. if (!isset($options) && !isset($values))
  131. return ''; /* raise error here? */
  132. $_html_result = array();
  133. if (isset($options)) {
  134. foreach ($options as $_key=>$_val) {
  135. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  136. }
  137. } else {
  138. foreach ($values as $_i=>$_key) {
  139. $_val = isset($output[$_i]) ? $output[$_i] : '';
  140. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  141. }
  142. }
  143. if(!empty($params['assign'])) {
  144. $template->assign($params['assign'], $_html_result);
  145. } else {
  146. return implode("\n", $_html_result);
  147. }
  148. }
  149. function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) {
  150. $_output = '';
  151. if (is_object($value)) {
  152. if (method_exists($value, "__toString")) {
  153. $value = (string) $value->__toString();
  154. } else {
  155. trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
  156. return '';
  157. }
  158. } else {
  159. $value = (string) $value;
  160. }
  161. if (is_object($output)) {
  162. if (method_exists($output, "__toString")) {
  163. $output = (string) $output->__toString();
  164. } else {
  165. trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
  166. return '';
  167. }
  168. } else {
  169. $output = (string) $output;
  170. }
  171. if ($labels) {
  172. if ($label_ids) {
  173. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
  174. $_output .= '<label for="' . $_id . '">';
  175. } else {
  176. $_output .= '<label>';
  177. }
  178. }
  179. $name = smarty_function_escape_special_chars($name);
  180. $value = smarty_function_escape_special_chars($value);
  181. if ($escape) {
  182. $output = smarty_function_escape_special_chars($output);
  183. }
  184. $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
  185. if ($labels && $label_ids) {
  186. $_output .= ' id="' . $_id . '"';
  187. }
  188. if (is_array($selected)) {
  189. if (isset($selected[$value])) {
  190. $_output .= ' checked="checked"';
  191. }
  192. } elseif ($value === $selected) {
  193. $_output .= ' checked="checked"';
  194. }
  195. $_output .= $extra . ' />' . $output;
  196. if ($labels) {
  197. $_output .= '</label>';
  198. }
  199. $_output .= $separator;
  200. return $_output;
  201. }
  202. ?>