More options updated changes.
[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 /* Do the whole current value thing. */
162 if ($this->value != SMPREF_NO) {
163 $yes_chk = ' CHECKED';
164 $no_chk = '';
165 } else {
166 $yes_chk = '';
167 $no_chk = ' CHECKED';
168 }
169
170 /* Build the yes choice. */
171 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
172 . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
173 . _("Yes");
174
175 /* Build the no choice. */
176 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
177 . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
178 . _("No");
179
180 /* Build and return the combined "boolean widget". */
181 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
182 return ($result);
183 }
184
185 function createWidget_Hidden() {
186 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
187 . '" VALUE="' . $this->value . '">';
188 return ($result);
189 }
190
191 function hasChanged() {
192 return ($this->changed);
193 }
194 }
195
196 function createOptionArray($optvals) {
197 /* Build a simple array with which to start. */
198 $result = array();
199
200 /* Create a new SquirrelOption for each set of option values. */
201 foreach ($optvals as $optset) {
202 if (isset($optset['posvals'])) {
203 /* Create a new option with all values given. */
204 $result[] = new SquirrelOption(
205 $optset['name'],
206 $optset['caption'],
207 $optset['type'],
208 $optset['refresh'],
209 $optset['posvals']
210 );
211 } else {
212 /* Create a new option with all but possible values given. */
213 $result[] = new SquirrelOption(
214 $optset['name'],
215 $optset['caption'],
216 $optset['type'],
217 $optset['refresh']
218 );
219 }
220 }
221
222 /* Return our resulting array. */
223 return ($result);
224 }
225
226 function OptionSelect( $title, $name, $data, $default, $show = '', $store = '' ) {
227
228 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
229 "<select name=\"$name\">";
230 foreach( $data as $key => $opt ) {
231 if ( $store == '' ) {
232 $vl = $key;
233 } else{
234 $vl = $opt[$store];
235 }
236 if ( $show == '' ) {
237 $nm = $opt;
238 } else{
239 $nm = $opt[$show];
240 }
241 if ( $nm <> '') {
242 echo "<option value=\"$vl\"";
243 if( $vl == $default ) {
244 echo ' selected';
245 }
246 echo ">$nm</option>\n";
247 }
248 }
249 echo "</select></td></tr>\n";
250 }
251
252 function OptionRadio( $title, $name, $data, $default, $show = '', $store = '', $sep = '&nbsp; &nbsp;' ) {
253 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>";
254 foreach( $data as $key => $opt ) {
255 if ( $store == '' ) {
256 $vl = $key;
257 } else{
258 $vl = $opt[$store];
259 }
260 if ( $show == '' ) {
261 $nm = $opt;
262 } else{
263 $nm = $opt[$show];
264 }
265 if ( $nm <> '') {
266 echo "<input type=\"radio\" name=\"$name\" value=\"$vl\"";
267 if( $vl == $default ) {
268 echo ' checked';
269 }
270 echo ">$nm $sep\n";
271 }
272 }
273 echo "</td></tr>\n";
274 }
275
276 function OptionText( $title, $name, $value, $size ) {
277 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
278 "<input name=\"$name\" value=\"$value\" size=\"$size\">" .
279 "</td></tr>\n";
280 }
281
282 function OptionHidden( $name, $value ) {
283 echo "<INPUT TYPE=HIDDEN NAME=\"$name\" VALUE=\"$value\">\n";
284 }
285
286 function OptionCheck( $title, $name, $value, $comment ) {
287 if ( $value )
288 $chk = 'checked';
289 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
290 "<input type=\"checkbox\" name=\"$name\" $chk> $comment" .
291 "</td></tr>\n";
292 }
293
294 function OptionTitle( $title ) {
295 echo "<tr><td colspan=2 align=left valign=middle nowrap><b>$title</b></td></tr>\n";
296 }
297
298 function OptionSubmit( $name ) {
299 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
300 '</td></tr>';
301 }
302
303 ?>