init
[garradin.git] / include / libs / template_lite / plugins / function.html_options.php
1 <?php
2 /*
3 * template_lite plugin
4 * -------------------------------------------------------------
5 * Type: function
6 * Name: html_options
7 * Purpose: prints out the options for an html_select item
8 * Credit: Taken from the original Smarty
9 * http://smarty.php.net
10 * -------------------------------------------------------------
11 */
12 function tpl_function_html_options($params, &$tpl)
13 {
14 require_once("shared.escape_chars.php");
15 $name = null;
16 $options = null;
17 $selected = array();
18 $extra = '';
19
20 foreach($params as $_key => $_val)
21 {
22 switch($_key)
23 {
24 case 'name':
25 $$_key = (string)$_val;
26 break;
27 case 'options':
28 $$_key = (array)$_val;
29 break;
30 case 'values':
31 case 'output':
32 $$_key = array_values((array)$_val);
33 break;
34 case 'selected':
35 $$_key = array_values((array)$_val);
36 break;
37 default:
38 if(!is_array($_key))
39 {
40 $extra .= ' ' . $_key . '="' . tpl_escape_chars($_val) . '"';
41 }
42 else
43 {
44 throw new Template_Exception("html_select: attribute '$_key' cannot be an array", $tpl);
45 }
46 break;
47 }
48 }
49
50 $_html_result = '';
51 if (is_array($options))
52 {
53 foreach ($options as $_key=>$_val)
54 {
55 $_html_result .= tpl_function_html_options_optoutput($tpl, $_key, $_val, $selected);
56 }
57 }
58 else
59 {
60 foreach ((array)$values as $_i=>$_key)
61 {
62 $_val = isset($output[$_i]) ? $output[$_i] : '';
63 $_html_result .= tpl_function_html_options_optoutput($tpl, $_key, $_val, $selected);
64 }
65 }
66
67 if(!empty($name))
68 {
69 $_html_result = '<select name="' . tpl_escape_chars($name) . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
70 }
71
72 return $_html_result;
73 }
74
75 function tpl_function_html_options_optoutput(&$tpl, $key, $value, $selected)
76 {
77 if(!is_array($value))
78 {
79 $_html_result = '<option label="' . tpl_escape_chars($value) . '" value="' . tpl_escape_chars($key) . '"';
80 if (in_array($key, $selected))
81 {
82 $_html_result .= ' selected="selected"';
83 }
84 $_html_result .= '>' . tpl_escape_chars($value) . '</option>' . "\n";
85 }
86 else
87 {
88 $_html_result = tpl_function_html_options_optgroup($tpl, $key, $value, $selected);
89 }
90 return $_html_result;
91 }
92
93 function tpl_function_html_options_optgroup(&$tpl, $key, $values, $selected)
94 {
95 $optgroup_html = '<optgroup label="' . tpl_escape_chars($key) . '">' . "\n";
96 foreach ($values as $key => $value)
97 {
98 $optgroup_html .= tpl_function_html_options_optoutput($tpl, $key, $value, $selected);
99 }
100 $optgroup_html .= "</optgroup>\n";
101 return $optgroup_html;
102 }
103
104 ?>