PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/smarty/plugins/function.html_checkboxes.php

https://github.com/jonathanbugs/spr
PHP | 216 lines | 147 code | 27 blank | 42 comment | 33 complexity | dbdbe5c7257e3a5699c664d4a4c880d5 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. default:
  109. if(!is_array($_val)) {
  110. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  111. } else {
  112. trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  113. }
  114. break;
  115. }
  116. }
  117. if (!isset($options) && !isset($values))
  118. return ''; /* raise error here? */
  119. $_html_result = array();
  120. if (isset($options)) {
  121. foreach ($options as $_key=>$_val) {
  122. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  123. }
  124. } else {
  125. foreach ($values as $_i=>$_key) {
  126. $_val = isset($output[$_i]) ? $output[$_i] : '';
  127. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  128. }
  129. }
  130. if(!empty($params['assign'])) {
  131. $template->assign($params['assign'], $_html_result);
  132. } else {
  133. return implode("\n", $_html_result);
  134. }
  135. }
  136. function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape=true) {
  137. $_output = '';
  138. if (is_object($value)) {
  139. if (method_exists($value, "__toString")) {
  140. $value = (string) $value->__toString();
  141. } else {
  142. trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
  143. return '';
  144. }
  145. } else {
  146. $value = (string) $value;
  147. }
  148. if (is_object($output)) {
  149. if (method_exists($output, "__toString")) {
  150. $output = (string) $output->__toString();
  151. } else {
  152. trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
  153. return '';
  154. }
  155. } else {
  156. $output = (string) $output;
  157. }
  158. if ($labels) {
  159. if ($label_ids) {
  160. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
  161. $_output .= '<label for="' . $_id . '">';
  162. } else {
  163. $_output .= '<label>';
  164. }
  165. }
  166. $name = smarty_function_escape_special_chars($name);
  167. $value = smarty_function_escape_special_chars($value);
  168. if ($escape) {
  169. $output = smarty_function_escape_special_chars($output);
  170. }
  171. $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
  172. if ($labels && $label_ids) {
  173. $_output .= ' id="' . $_id . '"';
  174. }
  175. if (is_array($selected)) {
  176. if (isset($selected[$value])) {
  177. $_output .= ' checked="checked"';
  178. }
  179. } elseif ($value === $selected) {
  180. $_output .= ' checked="checked"';
  181. }
  182. $_output .= $extra . ' />' . $output;
  183. if ($labels) {
  184. $_output .= '</label>';
  185. }
  186. $_output .= $separator;
  187. return $_output;
  188. }
  189. ?>