function.html_checkboxes.php 8.3 KB

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