PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/html/lib/plugins/function.html_checkboxes.php

https://bitbucket.org/kuy/ecom
PHP | 143 lines | 81 code | 25 blank | 37 comment | 11 complexity | a05460918e36fd9ef66d955754868a3e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  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. ?>