3 * Class that builds and maintains a select list.
4 * Applications utilizing this file: Vanilla;
6 * Copyright 2003 Mark O'Sullivan
7 * This file is part of Lussumo's Software Library.
8 * Lussumo's Software Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9 * Lussumo's Software Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10 * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11 * The latest source code is available at www.lussumo.com
12 * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
14 * @author Mark O'Sullivan
15 * @copyright 2003 Mark O'Sullivan
16 * @license http://lussumo.com/community/gpl.txt GPL 2
23 * Builds and maintains a select list.
27 var $Name; // Name of the select list
28 var $SelectedValue; // The value to be selected in the list (you can pass an array of ids for multiselects)
29 var $CssClass; // Stylesheet class name
30 var $Attributes; // Additional attributes for select element
31 var $aOptions; // Array for holding select options
33 function AddOption($IdValue, $DisplayValue, $Attributes = '') {
34 $this->aOptions
[] = array('IdValue' => $IdValue, 'DisplayValue' => $DisplayValue, 'Attributes' => $Attributes);
37 function AddOptionsFromAssociativeArray($Array, $KeyPrefix) {
38 while (list($key, $val) = each($Array)) {
39 $this->AddOption($KeyPrefix.$key, $val);
43 function AddOptionsFromDataSet(&$Database, $DataSet, $IdField, $DisplayField, $AttrsInfo = false) {
44 while ($rows = $Database->GetRow($DataSet)) {
45 if ($AttrsInfo === false)
46 $this->AddOption($rows[$IdField], $rows[$DisplayField]);
47 else if (is_callable($AttrsInfo)) {
48 if (is_array($AttrsInfo) && count($AttrsInfo) == 2) {
52 call_user_func($AttrsInfo, $rows)
63 $this->AddOption($rows[$IdField], $rows[$DisplayField], $rows[$AttrsInfo]);
69 $this->CssClass
= 'LargeSelect';
70 $this->Attributes
= '';
71 $this->aOptions
= array();
74 function ClearOptions() {
75 $this->aOptions
= array();
79 return count($this->aOptions
);
83 $sReturn = '<select name="'.$this->Name
.'" class="'.$this->CssClass
.'" '.$this->Attributes
.'>
85 $OptionCount = count($this->aOptions
);
87 for ($i = 0 ; $i < $OptionCount; $i++
) {
88 $sReturn .= '<option value="'.FormatStringForDisplay($this->aOptions
[$i]['IdValue']).'" ';
90 if (is_array($this->SelectedValue
)) {
91 $numrows = count($this->SelectedValue
);
92 for ($j = 0; $j < $numrows; $j++
) {
93 if ($this->aOptions
[$i]['IdValue'] == $this->SelectedValue
[$j]) {
94 $sReturn .= ' selected="selected"';
95 $j = $numrows; // If you've found a match, don't bother looping anymore
99 if ($this->aOptions
[$i]['IdValue'] == $this->SelectedValue
) $sReturn .= ' selected="selected"';
101 if ($this->aOptions
[$i]['Attributes'] != '') $sReturn .= $this->aOptions
[$i]['Attributes'];
102 $sReturn .= '>'.FormatStringForDisplay($this->aOptions
[$i]['DisplayValue']).'</option>
105 $sReturn .= '</select>
110 function RemoveOption($IdValue) {
111 if ($IdValue == $this->SelectedValue
) $this->SelectedValue
= '';
112 $OptionCount = count($this->aOptions
);
114 for($i = 0; $i < $OptionCount; $i++
) {
115 if ($this->aOptions
[$i]['IdValue'] == $IdValue) {
116 array_splice($this->aOptions
, $i, 1);