/www/lib/smarty/plugins/function.html_checkboxes.php

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