function.html_checkboxes.php 8.5 KB

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