PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/smarty/plugins/function.html_checkboxes.php

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