Skip to content

Commit 1c5b42b

Browse files
committed
Added a new smarty function for html - selects html_ncoptions
The original one adds the class $class . " option" to the individual options which messes up the default behavior of selects.
1 parent 48adf7c commit 1c5b42b

File tree

4 files changed

+233
-9
lines changed

4 files changed

+233
-9
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
/**
3+
* Smarty plugin
4+
*
5+
* @package Smarty
6+
* @subpackage PluginsFunction
7+
*/
8+
/**
9+
* Smarty {html_ncoptions} function plugin
10+
* Type: function
11+
* Name: html_ncoptions
12+
* Purpose: Prints the list of <option> tags generated from
13+
* the passed parameters - doesn't apply any class to the options
14+
* Params:
15+
*
16+
* - name (optional) - string default "select"
17+
* - values (required) - if no options supplied) - array
18+
* - options (required) - if no values supplied) - associative array
19+
* - selected (optional) - string default not set
20+
* - output (required) - if not options supplied) - array
21+
* - id (optional) - string default not set
22+
* - class (optional) - string default not set
23+
*
24+
* @link https://www.smarty.net/manual/en/language.function.html.options.php {html_image}
25+
* (Smarty online manual)
26+
* @author Monte Ohrt <monte at ohrt dot com> - changed by Schnoog
27+
* @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
28+
*
29+
* @param array $params parameters
30+
*
31+
* @param \Smarty_Internal_Template $template
32+
*
33+
* @return string
34+
* @uses smarty_function_escape_special_chars()
35+
* @throws \SmartyException
36+
*/
37+
function smarty_function_html_ncoptions($params, Smarty_Internal_Template $template)
38+
{
39+
$template->_checkPlugins(
40+
array(
41+
array(
42+
'function' => 'smarty_function_escape_special_chars',
43+
'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
44+
)
45+
)
46+
);
47+
$name = null;
48+
$values = null;
49+
$options = null;
50+
$selected = null;
51+
$output = null;
52+
$id = null;
53+
$class = null;
54+
$extra = '';
55+
foreach ($params as $_key => $_val) {
56+
switch ($_key) {
57+
case 'name':
58+
case 'class':
59+
case 'id':
60+
$$_key = (string)$_val;
61+
break;
62+
case 'options':
63+
$options = (array)$_val;
64+
break;
65+
case 'values':
66+
case 'output':
67+
$$_key = array_values((array)$_val);
68+
break;
69+
case 'selected':
70+
if (is_array($_val)) {
71+
$selected = array();
72+
foreach ($_val as $_sel) {
73+
if (is_object($_sel)) {
74+
if (method_exists($_sel, '__toString')) {
75+
$_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
76+
} else {
77+
trigger_error(
78+
'html_ncoptions: selected attribute contains an object of class \'' .
79+
get_class($_sel) . '\' without __toString() method',
80+
E_USER_NOTICE
81+
);
82+
continue;
83+
}
84+
} else {
85+
$_sel = smarty_function_escape_special_chars((string)$_sel);
86+
}
87+
$selected[ $_sel ] = true;
88+
}
89+
} elseif (is_object($_val)) {
90+
if (method_exists($_val, '__toString')) {
91+
$selected = smarty_function_escape_special_chars((string)$_val->__toString());
92+
} else {
93+
trigger_error(
94+
'html_ncoptions: selected attribute is an object of class \'' . get_class($_val) .
95+
'\' without __toString() method',
96+
E_USER_NOTICE
97+
);
98+
}
99+
} else {
100+
$selected = smarty_function_escape_special_chars((string)$_val);
101+
}
102+
break;
103+
case 'strict':
104+
break;
105+
case 'disabled':
106+
case 'readonly':
107+
if (!empty($params[ 'strict' ])) {
108+
if (!is_scalar($_val)) {
109+
trigger_error(
110+
"html_ncoptions: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
111+
E_USER_NOTICE
112+
);
113+
}
114+
if ($_val === true || $_val === $_key) {
115+
$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
116+
}
117+
break;
118+
}
119+
// omit break; to fall through!
120+
// no break
121+
default:
122+
if (!is_array($_val)) {
123+
$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
124+
} else {
125+
trigger_error("html_ncoptions: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
126+
}
127+
break;
128+
}
129+
}
130+
if (!isset($options) && !isset($values)) {
131+
/* raise error here? */
132+
return '';
133+
}
134+
$_html_result = '';
135+
$_idx = 0;
136+
if (isset($options)) {
137+
foreach ($options as $_key => $_val) {
138+
$_html_result .= smarty_function_html_ncoptions_optoutput($_key, $_val, $selected, $id, $class, $_idx);
139+
}
140+
} else {
141+
foreach ($values as $_i => $_key) {
142+
$_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
143+
$_html_result .= smarty_function_html_ncoptions_optoutput($_key, $_val, $selected, $id, $class, $_idx);
144+
}
145+
}
146+
if (!empty($name)) {
147+
$_html_class = !empty($class) ? ' class="' . $class . '"' : '';
148+
$_html_id = !empty($id) ? ' id="' . $id . '"' : '';
149+
$_html_result =
150+
'<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result .
151+
'</select>' . "\n";
152+
}
153+
return $_html_result;
154+
}
155+
156+
/**
157+
* @param $key
158+
* @param $value
159+
* @param $selected
160+
* @param $id
161+
* @param $class
162+
* @param $idx
163+
*
164+
* @return string
165+
*/
166+
function smarty_function_html_ncoptions_optoutput($key, $value, $selected, $id, $class, &$idx)
167+
{
168+
if (!is_array($value)) {
169+
$_key = smarty_function_escape_special_chars($key);
170+
$_html_result = '<option value="' . $_key . '"';
171+
if (is_array($selected)) {
172+
if (isset($selected[ $_key ])) {
173+
$_html_result .= ' selected="selected"';
174+
}
175+
} elseif ($_key === $selected) {
176+
$_html_result .= ' selected="selected"';
177+
}
178+
$_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
179+
$_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
180+
if (is_object($value)) {
181+
if (method_exists($value, '__toString')) {
182+
$value = smarty_function_escape_special_chars((string)$value->__toString());
183+
} else {
184+
trigger_error(
185+
'html_ncoptions: value is an object of class \'' . get_class($value) .
186+
'\' without __toString() method',
187+
E_USER_NOTICE
188+
);
189+
return '';
190+
}
191+
} else {
192+
$value = smarty_function_escape_special_chars((string)$value);
193+
}
194+
$_html_result .= $_html_id . '>' . $value . '</option>' . "\n";
195+
$idx++;
196+
} else {
197+
$_idx = 0;
198+
$_html_result =
199+
smarty_function_html_ncoptions_optgroup(
200+
$key,
201+
$value,
202+
$selected,
203+
!empty($id) ? ($id . '-' . $idx) : null,
204+
$class,
205+
$_idx
206+
);
207+
$idx++;
208+
}
209+
return $_html_result;
210+
}
211+
212+
/**
213+
* @param $key
214+
* @param $values
215+
* @param $selected
216+
* @param $id
217+
* @param $class
218+
* @param $idx
219+
*
220+
* @return string
221+
*/
222+
function smarty_function_html_ncoptions_optgroup($key, $values, $selected, $id, $class, &$idx)
223+
{
224+
$optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
225+
foreach ($values as $key => $value) {
226+
$optgroup_html .= smarty_function_html_ncoptions_optoutput($key, $value, $selected, $id, $class, $idx);
227+
}
228+
$optgroup_html .= "</optgroup>\n";
229+
return $optgroup_html;
230+
}

app/templates_ajax/admin_sys_naveditbox.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{html_options name="nav_parentid" id="nav_parentid" options=$navitems selected=$nav.nav_parentid class="form-control"}
1212

1313
<label for="nav_allowedmask">{'Access control'|gettext}<br /><small>{'(visibility in menu)'|gettext}</small></label>
14-
{html_options name="roles" size="{$roles|@count}" options=$roles selected=$nav.nav_allowedmask_array class="form-control" multiple="" size="10" id="nav_allowedmask" }
14+
{html_ncoptions name="roles" size="{$roles|@count}" options=$roles selected=$nav.nav_allowedmask_array class="form-control" multiple="" size="10" id="nav_allowedmask" }
1515
<input type="hidden" id="nav_allowedmask_sum" value="{$nav.nav_allowedmask}" class="form-control">
1616
<label for="nav_target">{'Target'|gettext}<br /><small>{'Full-url for extern targets, internal only pagename'|gettext}</small></label>
1717
<input type="text" id="nav_target" value="{$nav.nav_target}" class="form-control" required>

app/templates_ajax/admin_sys_navnewbox.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{html_options name="nav_parentid" id="nav_parentid" options=$navitems class="form-control"}
1212

1313
<label for="nav_allowedmask">{'Access control'|gettext}<br /><small>{'(visibility in menu)'|gettext}</small></label>
14-
{html_options name="roles" size="{$roles|@count}" options=$roles class="form-control" multiple="" id="nav_allowedmask"}
14+
{html_ncoptions name="roles" size="{$roles|@count}" options=$roles class="form-control" multiple="" id="nav_allowedmask"}
1515
<input type="hidden" id="nav_allowedmask_sum" class="form-control">
1616
<label for="nav_target">{'Target'|gettext}<br /><small>{'Full-url for extern targets, internal only pagename'|gettext}</small></label>
1717
<input type="text" id="nav_target" class="form-control" required>

app/templates_ajax/admin_sys_tools.tpl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
<label for="usermask">{'Access control to the page'|gettext}<br /><small>{'If no group is selected, access is garanted to everyone'|gettext}</small></label>
7-
{html_options name=roles options=$roles class="form-control" multiple="" id="usermask" size="10"}
7+
{html_ncoptions name=roles options=$roles class="form-control" multiple="" id="usermask" size="10"}
88
<input type="text" class="form-control" id="masksum" />
99
<button type="button" class="form-control btn btn-success" onclick="calculate();">{'Get the number'|gettext}</button><br />
1010

@@ -38,12 +38,6 @@ $(document).ready(function() {
3838
$('option').mousedown(function(e) {
3939
e.preventDefault();
4040
$(this).prop('selected', !$(this).prop('selected'));
41-
if($(this).prop('selected')){
42-
$(this).css('backgroundColor','green');
43-
44-
}else{
45-
$(this).css('backgroundColor','white');
46-
}
4741
calculate();
4842
return false;
4943
} );

0 commit comments

Comments
 (0)