Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_checkboxes.php 4.3 KiB

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