您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

function.html_checkboxes.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. *
  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 Smarty_Internal_Template $template template object
  41. *
  42. * @return string
  43. * @uses smarty_function_escape_special_chars()
  44. * @throws \SmartyException
  45. */
  46. function smarty_function_html_checkboxes($params, Smarty_Internal_Template $template)
  47. {
  48. $template->_checkPlugins(array(array('function' => 'smarty_function_escape_special_chars',
  49. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php')));
  50. $name = 'checkbox';
  51. $values = null;
  52. $options = null;
  53. $selected = array();
  54. $separator = '';
  55. $escape = true;
  56. $labels = true;
  57. $label_ids = false;
  58. $output = null;
  59. $extra = '';
  60. foreach ($params as $_key => $_val) {
  61. switch ($_key) {
  62. case 'name':
  63. case 'separator':
  64. $$_key = (string) $_val;
  65. break;
  66. case 'escape':
  67. case 'labels':
  68. case 'label_ids':
  69. $$_key = (bool) $_val;
  70. break;
  71. case 'options':
  72. $$_key = (array) $_val;
  73. break;
  74. case 'values':
  75. case 'output':
  76. $$_key = array_values((array) $_val);
  77. break;
  78. case 'checked':
  79. case 'selected':
  80. if (is_array($_val)) {
  81. $selected = array();
  82. foreach ($_val as $_sel) {
  83. if (is_object($_sel)) {
  84. if (method_exists($_sel, '__toString')) {
  85. $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
  86. } else {
  87. trigger_error('html_checkboxes: selected attribute contains an object of class \'' .
  88. get_class($_sel) . '\' without __toString() method', E_USER_NOTICE);
  89. continue;
  90. }
  91. } else {
  92. $_sel = smarty_function_escape_special_chars((string) $_sel);
  93. }
  94. $selected[ $_sel ] = true;
  95. }
  96. } elseif (is_object($_val)) {
  97. if (method_exists($_val, '__toString')) {
  98. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  99. } else {
  100. trigger_error('html_checkboxes: selected attribute is an object of class \'' . get_class($_val) .
  101. '\' without __toString() method', E_USER_NOTICE);
  102. }
  103. } else {
  104. $selected = smarty_function_escape_special_chars((string) $_val);
  105. }
  106. break;
  107. case 'checkboxes':
  108. trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead',
  109. E_USER_WARNING);
  110. $options = (array) $_val;
  111. break;
  112. case 'assign':
  113. break;
  114. case 'strict':
  115. break;
  116. case 'disabled':
  117. case 'readonly':
  118. if (!empty($params[ 'strict' ])) {
  119. if (!is_scalar($_val)) {
  120. trigger_error("html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
  121. E_USER_NOTICE);
  122. }
  123. if ($_val === true || $_val === $_key) {
  124. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  125. }
  126. break;
  127. }
  128. // omit break; to fall through!
  129. default:
  130. if (!is_array($_val)) {
  131. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  132. } else {
  133. trigger_error("html_checkboxes: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  134. }
  135. break;
  136. }
  137. }
  138. if (!isset($options) && !isset($values)) {
  139. return '';
  140. } /* raise error here? */
  141. $_html_result = array();
  142. if (isset($options)) {
  143. foreach ($options as $_key => $_val) {
  144. $_html_result[] =
  145. smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  146. $label_ids, $escape);
  147. }
  148. } else {
  149. foreach ($values as $_i => $_key) {
  150. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  151. $_html_result[] =
  152. smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  153. $label_ids, $escape);
  154. }
  155. }
  156. if (!empty($params[ 'assign' ])) {
  157. $template->assign($params[ 'assign' ], $_html_result);
  158. } else {
  159. return implode("\n", $_html_result);
  160. }
  161. }
  162. /**
  163. * @param $name
  164. * @param $value
  165. * @param $output
  166. * @param $selected
  167. * @param $extra
  168. * @param $separator
  169. * @param $labels
  170. * @param $label_ids
  171. * @param bool $escape
  172. *
  173. * @return string
  174. */
  175. function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels,
  176. $label_ids, $escape = true)
  177. {
  178. $_output = '';
  179. if (is_object($value)) {
  180. if (method_exists($value, '__toString')) {
  181. $value = (string) $value->__toString();
  182. } else {
  183. trigger_error('html_options: value is an object of class \'' . get_class($value) .
  184. '\' without __toString() method', E_USER_NOTICE);
  185. return '';
  186. }
  187. } else {
  188. $value = (string) $value;
  189. }
  190. if (is_object($output)) {
  191. if (method_exists($output, '__toString')) {
  192. $output = (string) $output->__toString();
  193. } else {
  194. trigger_error('html_options: output is an object of class \'' . get_class($output) .
  195. '\' without __toString() method', E_USER_NOTICE);
  196. return '';
  197. }
  198. } else {
  199. $output = (string) $output;
  200. }
  201. if ($labels) {
  202. if ($label_ids) {
  203. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
  204. $name . '_' . $value));
  205. $_output .= '<label for="' . $_id . '">';
  206. } else {
  207. $_output .= '<label>';
  208. }
  209. }
  210. $name = smarty_function_escape_special_chars($name);
  211. $value = smarty_function_escape_special_chars($value);
  212. if ($escape) {
  213. $output = smarty_function_escape_special_chars($output);
  214. }
  215. $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
  216. if ($labels && $label_ids) {
  217. $_output .= ' id="' . $_id . '"';
  218. }
  219. if (is_array($selected)) {
  220. if (isset($selected[ $value ])) {
  221. $_output .= ' checked="checked"';
  222. }
  223. } elseif ($value === $selected) {
  224. $_output .= ' checked="checked"';
  225. }
  226. $_output .= $extra . ' />' . $output;
  227. if ($labels) {
  228. $_output .= '</label>';
  229. }
  230. $_output .= $separator;
  231. return $_output;
  232. }