function.html_checkboxes.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage PluginsFunction
  6. */
  7. /**
  8. * Smarty {html_checkboxes} function plugin
  9. *
  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. * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
  22. * (Smarty online manual)
  23. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  24. * @author credits to Monte Ohrt <monte at ohrt dot com>
  25. * @version 1.0
  26. * @param array $params parameters
  27. * Input:<br>
  28. * - name (optional) - string default "checkbox"
  29. * - values (required) - array
  30. * - options (optional) - associative array
  31. * - checked (optional) - array default not set
  32. * - separator (optional) - ie <br> or &nbsp;
  33. * - output (optional) - the output next to each checkbox
  34. * - assign (optional) - assign the output as an array to this variable
  35. * @param object $smarty Smarty object
  36. * @param object $template template object
  37. * @return string
  38. * @uses smarty_function_escape_special_chars()
  39. */
  40. function smarty_function_html_checkboxes($params, $smarty, $template)
  41. {
  42. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  43. //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
  44. $name = 'checkbox';
  45. $values = null;
  46. $options = null;
  47. $selected = null;
  48. $separator = '';
  49. $labels = true;
  50. $output = null;
  51. $extra = '';
  52. foreach($params as $_key => $_val) {
  53. switch($_key) {
  54. case 'name':
  55. case 'separator':
  56. $$_key = $_val;
  57. break;
  58. case 'labels':
  59. $$_key = (bool)$_val;
  60. break;
  61. case 'options':
  62. $$_key = (array)$_val;
  63. break;
  64. case 'values':
  65. case 'output':
  66. $$_key = array_values((array)$_val);
  67. break;
  68. case 'checked':
  69. case 'selected':
  70. $selected = array_map('strval', array_values((array)$_val));
  71. break;
  72. case 'checkboxes':
  73. trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
  74. $options = (array)$_val;
  75. break;
  76. case 'assign':
  77. break;
  78. default:
  79. if(!is_array($_val)) {
  80. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  81. } else {
  82. trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  83. }
  84. break;
  85. }
  86. }
  87. if (!isset($options) && !isset($values))
  88. return ''; /* raise error here? */
  89. settype($selected, 'array');
  90. $_html_result = array();
  91. if (isset($options)) {
  92. foreach ($options as $_key=>$_val)
  93. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
  94. } else {
  95. foreach ($values as $_i=>$_key) {
  96. $_val = isset($output[$_i]) ? $output[$_i] : '';
  97. $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
  98. }
  99. }
  100. if(!empty($params['assign'])) {
  101. $template->assign($params['assign'], $_html_result);
  102. } else {
  103. return implode("\n",$_html_result);
  104. }
  105. }
  106. function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) {
  107. $_output = '';
  108. if ($labels) $_output .= '<label>';
  109. $_output .= '<input type="checkbox" name="'
  110. . smarty_function_escape_special_chars($name) . '[]" value="'
  111. . smarty_function_escape_special_chars($value) . '"';
  112. if (in_array((string)$value, $selected)) {
  113. $_output .= ' checked="checked"';
  114. }
  115. $_output .= $extra . ' />' . $output;
  116. if ($labels) $_output .= '</label>';
  117. $_output .= $separator;
  118. return $_output;
  119. }
  120. ?>