Please ignore the red for now :)
[squirrelmail.git] / functions / options.php
1 <?php
2 /**
3 * options.php
4 *
5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Functions needed to display the options pages.
9 *
10 * $Id$
11 */
12
13 /**********************************************/
14 /* Define constants used in the options code. */
15 /**********************************************/
16
17 /* Define constants for the various option types. */
18 define('SMOPT_TYPE_STRING', 0);
19 define('SMOPT_TYPE_STRLIST', 1);
20 define('SMOPT_TYPE_TEXTAREA', 2);
21 define('SMOPT_TYPE_INTEGER', 3);
22 define('SMOPT_TYPE_FLOAT', 4);
23 define('SMOPT_TYPE_BOOLEAN', 5);
24 define('SMOPT_TYPE_HIDDEN', 6);
25
26 /* Define constants for the options refresh levels. */
27 define('SMOPT_REFRESH_NONE', 0);
28 define('SMOPT_REFRESH_FOLDERLIST', 1);
29 define('SMOPT_REFRESH_ALL', 2);
30
31 /**
32 * SquirrelOption: An option for Squirrelmail.
33 *
34 * This class is a work in progress. When complete, it will handle
35 * presentation and saving of Squirrelmail user options in a simple,
36 * streamline manner. Stay tuned for more stuff.
37 *
38 * Also, I'd like to ask that people leave this alone (mostly :) until
39 * I get it a little further along. That should only be a day or two or
40 * three. I will remove this message when it is ready for primetime usage.
41 */
42 class SquirrelOption {
43 /* The basic stuff. */
44 var $name;
45 var $caption;
46 var $type;
47 var $refresh_level;
48
49 /* The various 'values' for this options. */
50 var $value;
51 var $new_value;
52 var $possible_values;
53
54 /* This variable needs to be made private so it can not be messed with. */
55 /* I just don't remember how to do it right now and think it would be */
56 /* better to keep coding. Someone can fix it, if they want. Or I will. */
57 var $changed;
58
59 function SquirrelOption
60 ($name, $caption, $type, $refresh_level, $possible_values = '') {
61 /* Set the basic stuff. */
62 $this->name = $name;
63 $this->caption = $caption;
64 $this->type = $type;
65 $this->refresh_level = $refresh_level;
66 $this->possible_values = $possible_values;
67
68 /* Check for a current value. */
69 if (isset($GLOBALS[$name])) {
70 $this->value = $GLOBALS[$name];
71 } else {
72 $this->value = '';
73 }
74
75 /* Check for a new value. */
76 if (isset($GLOBALS["new_$name"])) {
77 $this->new_value = $GLOBALS["new_$name"];
78 $this->changed = ($this->value !== $this->new_value);
79 } else {
80 $this->new_value = '';
81 $this->changed = false;
82 }
83 }
84
85 function createHTMLWidget() {
86 switch ($this->type) {
87 case SMOPT_TYPE_STRING:
88 $result = $this->createWidget_String();
89 break;
90 case SMOPT_TYPE_STRLIST:
91 $result = $this->createWidget_StrList();
92 break;
93 case SMOPT_TYPE_TEXTAREA:
94 $result = $this->createWidget_TextArea();
95 break;
96 case SMOPT_TYPE_INTEGER:
97 $result = $this->createWidget_Integer();
98 break;
99 case SMOPT_TYPE_FLOAT:
100 $result = $this->createWidget_Float();
101 break;
102 case SMOPT_TYPE_BOOLEAN:
103 $result = $this->createWidget_Boolean();
104 break;
105 case SMOPT_TYPE_HIDDEN:
106 $result = $this->createWidget_Hidden();
107 break;
108 default:
109 $result = '<FONT COLOR=RED>'
110 . sprintf(_("Option Type '%s' Not Found"), $this->type)
111 . '</FONT>';
112 }
113
114 /* Now, return the created widget. */
115 return ($result);
116 }
117
118 function createWidget_String() {
119 $result = "<INPUT NAME=\"new_$this->name\" value=\"$this->value\" size=\"5\">";
120 return ($result);
121 }
122
123 function createWidget_StrList() {
124 /* Begin the select tag. */
125 $result = "<SELECT NAME=\"new_$this->name\">";
126
127 /* Add each possible value to the select list. */
128 foreach ($this->possible_values as $real_value => $disp_value) {
129 /* Start the next new option string. */
130 $new_option = "<OPTION VALUE=\"$real_value\"";
131
132 /* If this value is the current value, select it. */
133 if ($real_value == $this->value) {
134 $new_option .= ' SELECTED';
135 }
136
137 /* Add the display value to our option string. */
138 $new_option .= ">$disp_value</OPTION>";
139
140 /* And add the new option string to our select tag. */
141 $result .= $new_option;
142 }
143
144 /* Close the select tag and return our happy result. */
145 $result .= '</SELECT>';
146 return ($result);
147 }
148
149 function createWidget_TextArea() {
150 }
151
152 function createWidget_Integer() {
153 return ($this->createWidget_String());
154 }
155
156 function createWidget_Float() {
157 return ($this->createWidget_String());
158 }
159
160 function createWidget_Boolean() {
161 }
162
163 function createWidget_Hidden() {
164 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
165 . '" VALUE="' . $this->value . '">';
166 return ($result);
167 }
168
169 function hasChanged() {
170 return ($this->changed);
171 }
172 }
173
174 function createOptionArray($optvals) {
175 /* Build a simple array with which to start. */
176 $result = array();
177
178 /* Create a new SquirrelOption for each set of option values. */
179 foreach ($optvals as $optset) {
180 if (isset($optset['posvals'])) {
181 /* Create a new option with all values given. */
182 $result[] = new SquirrelOption(
183 $optset['name'],
184 $optset['caption'],
185 $optset['type'],
186 $optset['refresh'],
187 $optset['posvals']
188 );
189 } else {
190 /* Create a new option with all but possible values given. */
191 $result[] = new SquirrelOption(
192 $optset['name'],
193 $optset['caption'],
194 $optset['type'],
195 $optset['refresh']
196 );
197 }
198 }
199
200 /* Return our resulting array. */
201 return ($result);
202 }
203
204 function OptionSelect( $title, $name, $data, $default, $show = '', $store = '' ) {
205
206 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
207 "<select name=\"$name\">";
208 foreach( $data as $key => $opt ) {
209 if ( $store == '' ) {
210 $vl = $key;
211 } else{
212 $vl = $opt[$store];
213 }
214 if ( $show == '' ) {
215 $nm = $opt;
216 } else{
217 $nm = $opt[$show];
218 }
219 if ( $nm <> '') {
220 echo "<option value=\"$vl\"";
221 if( $vl == $default ) {
222 echo ' selected';
223 }
224 echo ">$nm</option>\n";
225 }
226 }
227 echo "</select></td></tr>\n";
228 }
229
230 function OptionRadio( $title, $name, $data, $default, $show = '', $store = '', $sep = '&nbsp; &nbsp;' ) {
231 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>";
232 foreach( $data as $key => $opt ) {
233 if ( $store == '' ) {
234 $vl = $key;
235 } else{
236 $vl = $opt[$store];
237 }
238 if ( $show == '' ) {
239 $nm = $opt;
240 } else{
241 $nm = $opt[$show];
242 }
243 if ( $nm <> '') {
244 echo "<input type=\"radio\" name=\"$name\" value=\"$vl\"";
245 if( $vl == $default ) {
246 echo ' checked';
247 }
248 echo ">$nm $sep\n";
249 }
250 }
251 echo "</td></tr>\n";
252 }
253
254 function OptionText( $title, $name, $value, $size ) {
255 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
256 "<input name=\"$name\" value=\"$value\" size=\"$size\">" .
257 "</td></tr>\n";
258 }
259
260 function OptionHidden( $name, $value ) {
261 echo "<INPUT TYPE=HIDDEN NAME=\"$name\" VALUE=\"$value\">\n";
262 }
263
264 function OptionCheck( $title, $name, $value, $comment ) {
265 if ( $value )
266 $chk = 'checked';
267 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
268 "<input type=\"checkbox\" name=\"$name\" $chk> $comment" .
269 "</td></tr>\n";
270 }
271
272 function OptionTitle( $title ) {
273 echo "<tr><td colspan=2 align=left valign=middle nowrap><b>$title</b></td></tr>\n";
274 }
275
276 function OptionSubmit( $name ) {
277 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
278 '</td></tr>';
279 }
280
281 ?>