Made quite a few changes so that the code works in the server
[vanilla-miry.git] / library / Framework / Framework.Class.Select.php
blob659842d004696314a7b63d842e3e33d6f868bea4
1 <?php
2 /**
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
17 * @package Framework
18 * @version 1.1.8
22 /**
23 * Builds and maintains a select list.
24 * @package Framework
26 class Select {
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) {
49 $this->AddOption(
50 $rows[$IdField],
51 $rows[$DisplayField],
52 call_user_func($AttrsInfo, $rows)
54 } else {
55 $this->AddOption(
56 $rows[$IdField],
57 $rows[$DisplayField],
58 $AttrsInfo($rows)
62 else
63 $this->AddOption($rows[$IdField], $rows[$DisplayField], $rows[$AttrsInfo]);
67 function Clear() {
68 $this->Name = '';
69 $this->CssClass = 'LargeSelect';
70 $this->Attributes = '';
71 $this->aOptions = array();
74 function ClearOptions() {
75 $this->aOptions = array();
78 function Count() {
79 return count($this->aOptions);
82 function Get() {
83 $sReturn = '<select name="'.$this->Name.'" class="'.$this->CssClass.'" '.$this->Attributes.'>
85 $OptionCount = count($this->aOptions);
86 $i = 0;
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
98 } else {
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>
107 return $sReturn;
110 function RemoveOption($IdValue) {
111 if ($IdValue == $this->SelectedValue) $this->SelectedValue = '';
112 $OptionCount = count($this->aOptions);
113 $i = 0;
114 for($i = 0; $i < $OptionCount; $i++) {
115 if ($this->aOptions[$i]['IdValue'] == $IdValue) {
116 array_splice($this->aOptions, $i, 1);
117 break;
122 function Select() {
123 $this->Clear();
126 function Write() {
127 echo($this->Get());