6
require_once 'Savant2/Plugin.php';
10
* Outputs a series of HTML <option>s.
12
* $Id: Savant2_Plugin_options.php,v 1.2 2005/08/09 22:19:39 pmjones Exp $
14
* @author Paul M. Jones <pmjones@ciaweb.net>
18
* @license LGPL http://www.gnu.org/copyleft/lesser.html
20
* This program is free software; you can redistribute it and/or modify
21
* it under the terms of the GNU Lesser General Public License as
22
* published by the Free Software Foundation; either version 2.1 of the
23
* License, or (at your option) any later version.
25
* This program is distributed in the hope that it will be useful, but
26
* WITHOUT ANY WARRANTY; without even the implied warranty of
27
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28
* Lesser General Public License for more details.
32
class Savant2_Plugin_options extends Savant2_Plugin {
37
* Outputs a series of HTML <option>s.
39
* Outputs a series of HTML <option>s based on an associative array
40
* where the key is the option value and the value is the option
41
* label. You can pass a "selected" value as well to tell the
42
* function which option value(s) should be marked as selected.
46
* @param array $options An associative array of key-value pairs; the
47
* key is the option value, the value is the option label.
49
* @param string|array $selected A string or array that matches one
50
* or more option values, to tell the function what options should be
51
* marked as selected. Defaults to an empty array.
53
* @param string|array $attr Extra attributes to apply to the option
54
* tag. If a string, they are added as-is; if an array, the key is
55
* the attribute name and the value is the attribute value.
57
* @return string A set of HTML <option> tags.
61
function plugin($options, $selected = array(), $attr = null,
62
$labelIsValue = false)
66
// force $selected to be an array. this allows multi-selects to
67
// have multiple selected options.
68
settype($selected, 'array');
69
settype($options, 'array');
71
// loop through the options array
72
foreach ($options as $value => $label) {
74
// is the label being used as the value?
79
// set the value and label in the tag
80
$html .= '<option value="' . htmlspecialchars($value) . '"';
81
$html .= ' label="' . htmlspecialchars($label) . '"';
83
// is the option one of the selected values?
84
if (in_array($value, $selected)) {
85
$html .= ' selected="selected"';
88
// are we adding extra attributes?
89
if (is_array($attr)) {
91
foreach ($attr as $key => $val) {
92
$val = htmlspecialchars($val);
93
$html .= " $key=\"$val\"";
95
} elseif (! is_null($attr)) {
100
// add the label and close the tag
101
$html .= '>' . htmlspecialchars($label) . "</option>\n";
b'\\ No newline at end of file'