Cleaned up options main for IE and Netscape compatibility. Did major work on the...
[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 define('SMOPT_TYPE_COMMENT', 7);
26
27 /* Define constants for the options refresh levels. */
28 define('SMOPT_REFRESH_NONE', 0);
29 define('SMOPT_REFRESH_FOLDERLIST', 1);
30 define('SMOPT_REFRESH_ALL', 2);
31
32 /* Define constants for the options size. */
33 define('SMOPT_SIZE_TINY', 0);
34 define('SMOPT_SIZE_SMALL', 1);
35 define('SMOPT_SIZE_MEDIUM', 2);
36 define('SMOPT_SIZE_LARGE', 3);
37 define('SMOPT_SIZE_HUGE', 4);
38
39 define('SMOPT_SAVE_DEFAULT', 'save_option');
40 define('SMOPT_SAVE_NOOP', 'save_option_noop');
41
42 /**
43 * SquirrelOption: An option for Squirrelmail.
44 *
45 * This class is a work in progress. When complete, it will handle
46 * presentation and saving of Squirrelmail user options in a simple,
47 * streamline manner. Stay tuned for more stuff.
48 *
49 * Also, I'd like to ask that people leave this alone (mostly :) until
50 * I get it a little further along. That should only be a day or two or
51 * three. I will remove this message when it is ready for primetime usage.
52 */
53 class SquirrelOption {
54 /* The basic stuff. */
55 var $name;
56 var $caption;
57 var $type;
58 var $refresh_level;
59 var $size;
60 var $comment;
61 var $script;
62
63 /* The name of the Save Function for this option. */
64 var $save_function;
65
66 /* The various 'values' for this options. */
67 var $value;
68 var $new_value;
69 var $possible_values;
70
71 function SquirrelOption
72 ($name, $caption, $type, $refresh_level, $possible_values = '') {
73 /* Set the basic stuff. */
74 $this->name = $name;
75 $this->caption = $caption;
76 $this->type = $type;
77 $this->refresh_level = $refresh_level;
78 $this->possible_values = $possible_values;
79 $this->size = SMOPT_SIZE_MEDIUM;
80 $this->comment = '';
81 $this->script = '';
82
83 /* Check for a current value. */
84 if (isset($GLOBALS[$name])) {
85 $this->value = $GLOBALS[$name];
86 } else {
87 $this->value = '';
88 }
89
90 /* Check for a new value. */
91 if (isset($GLOBALS["new_$name"])) {
92 $this->new_value = $GLOBALS["new_$name"];
93 } else {
94 $this->new_value = '';
95 }
96
97 /* Set the default save function. */
98 if ((type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
99 $this->save_function = SMOPT_SAVE_DEFAULT;
100 } else {
101 $this->save_function = SMOPT_SAVE_NOOP;
102 }
103 }
104
105 /* Set the value for this option. */
106 function setValue($value) {
107 $this->value = $value;
108 }
109
110 /* Set the new value for this option. */
111 function setNewValue($new_value) {
112 $this->new_value = $new_value;
113 }
114
115 /* Set the size for this option. */
116 function setSize($size) {
117 $this->size = $size;
118 }
119
120 /* Set the comment for this option. */
121 function setComment($comment) {
122 $this->comment = $comment;
123 }
124
125 /* Set the script for this option. */
126 function setScript($script) {
127 $this->script = $script;
128 }
129
130 /* Set the save function for this option. */
131 function setSaveFunction($save_function) {
132 $this->save_function = $save_function;
133 }
134
135 function createHTMLWidget() {
136 global $javascript_on;
137
138 /* Get the widget for this option type. */
139 switch ($this->type) {
140 case SMOPT_TYPE_STRING:
141 $result = $this->createWidget_String();
142 break;
143 case SMOPT_TYPE_STRLIST:
144 $result = $this->createWidget_StrList();
145 break;
146 case SMOPT_TYPE_TEXTAREA:
147 $result = $this->createWidget_TextArea();
148 break;
149 case SMOPT_TYPE_INTEGER:
150 $result = $this->createWidget_Integer();
151 break;
152 case SMOPT_TYPE_FLOAT:
153 $result = $this->createWidget_Float();
154 break;
155 case SMOPT_TYPE_BOOLEAN:
156 $result = $this->createWidget_Boolean();
157 break;
158 case SMOPT_TYPE_HIDDEN:
159 $result = $this->createWidget_Hidden();
160 break;
161 case SMOPT_TYPE_COMMENT:
162 $result = $this->createWidget_Comment();
163 break;
164 default:
165 $result = '<FONT COLOR=RED>'
166 . sprintf(_("Option Type '%s' Not Found"), $this->type)
167 . '</FONT>';
168 }
169
170 /* Add the script for this option. */
171 $result .= $this->script;
172
173 /* Now, return the created widget. */
174 return ($result);
175 }
176
177 function createWidget_String() {
178 switch ($this->size) {
179 case SMOPT_SIZE_TINY: $width = 5; break;
180 case SMOPT_SIZE_SMALL: $width = 12; break;
181 case SMOPT_SIZE_LARGE: $width = 38; break;
182 case SMOPT_SIZE_HUGE: $width = 50; break;
183 case SMOPT_SIZE_NORMAL:
184 default: $width = 25;
185 }
186
187 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
188 return ($result);
189 }
190
191 function createWidget_StrList() {
192 /* Begin the select tag. */
193 $result = "<SELECT NAME=\"new_$this->name\">";
194
195 /* Add each possible value to the select list. */
196 foreach ($this->possible_values as $real_value => $disp_value) {
197 /* Start the next new option string. */
198 $new_option = "<OPTION VALUE=\"$real_value\"";
199
200 /* If this value is the current value, select it. */
201 if ($real_value == $this->value) {
202 $new_option .= ' SELECTED';
203 }
204
205 /* Add the display value to our option string. */
206 $new_option .= ">$disp_value</OPTION>";
207
208 /* And add the new option string to our select tag. */
209 $result .= $new_option;
210 }
211
212 /* Close the select tag and return our happy result. */
213 $result .= '</SELECT>';
214 return ($result);
215 }
216
217 function createWidget_TextArea() {
218 switch ($this->size) {
219 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
220 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
221 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
222 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
223 case SMOPT_SIZE_NORMAL:
224 default: $rows = 5; $cols = 50;
225 }
226 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
227 . "COLS=\"$cols\">$this->value</TEXTAREA>";
228 return ($result);
229 }
230
231 function createWidget_Integer() {
232 return ($this->createWidget_String());
233 }
234
235 function createWidget_Float() {
236 return ($this->createWidget_String());
237 }
238
239 function createWidget_Boolean() {
240 /* Do the whole current value thing. */
241 if ($this->value != SMPREF_NO) {
242 $yes_chk = ' CHECKED';
243 $no_chk = '';
244 } else {
245 $yes_chk = '';
246 $no_chk = ' CHECKED';
247 }
248
249 /* Build the yes choice. */
250 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
251 . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
252 . _("Yes");
253
254 /* Build the no choice. */
255 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
256 . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
257 . _("No");
258
259 /* Build and return the combined "boolean widget". */
260 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
261 return ($result);
262 }
263
264 function createWidget_Hidden() {
265 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
266 . '" VALUE="' . $this->value . '">';
267 return ($result);
268 }
269
270 function createWidget_Comment() {
271 $result = $this->comment;
272 return ($result);
273 }
274
275 function save() {
276 $function = $this->save_function;
277 $function($this);
278 }
279
280 function changed() {
281 return ($this->value !== $this->new_value);
282 }
283 }
284
285 function save_option($option) {
286 global $data_dir, $username;
287 setPref($data_dir, $username, $option->name, $option->new_value);
288
289 /* I do not know if this next line does any good. */
290 $GLOBALS[$name] = $option->new_value;
291 }
292
293 function save_option_noop($option) {
294 /* Do nothing here... */
295 }
296
297 function create_optpage_element($optpage) {
298 return create_hidden_element('optpage', $optpage);
299 }
300
301 function create_optmode_element($optmode) {
302 return create_hidden_element('optmode', $optmode);
303 }
304
305 function create_hidden_element($name, $value) {
306 $result = '<INPUT TYPE="HIDDEN" '
307 . 'NAME="' . $name . '" '
308 . 'VALUE="' . $value . '">';
309 return ($result);
310 }
311
312
313 function createOptionGroups($optgrps, $optvals) {
314 return create_option_groups($optgrps, $optvals);
315 }
316
317 function create_option_groups($optgrps, $optvals) {
318 /* Build a simple array with which to start. */
319 $result = array();
320
321 /* Create option group for each option group name. */
322 foreach ($optgrps as $grpkey => $grpname) {
323 $result[$grpkey] = array();
324 $result[$grpkey]['name'] = $grpname;
325 $result[$grpkey]['options'] = array();
326 }
327
328 /* Create a new SquirrelOption for each set of option values. */
329 foreach ($optvals as $grpkey => $grpopts) {
330 foreach ($grpopts as $optset) {
331 if (isset($optset['posvals'])) {
332 /* Create a new option with all values given. */
333 $next_option = new SquirrelOption(
334 $optset['name'],
335 $optset['caption'],
336 $optset['type'],
337 $optset['refresh'],
338 $optset['posvals']
339 );
340 } else {
341 /* Create a new option with all but possible values given. */
342 $next_option = new SquirrelOption(
343 $optset['name'],
344 $optset['caption'],
345 $optset['type'],
346 $optset['refresh']
347 );
348 }
349
350 /* If provided, set the size for this option. */
351 if (isset($optset['size'])) {
352 $next_option->setSize($optset['size']);
353 }
354
355 /* If provided, set the comment for this option. */
356 if (isset($optset['comment'])) {
357 $next_option->setComment($optset['comment']);
358 }
359
360 /* If provided, set the save function for this option. */
361 if (isset($optset['save'])) {
362 $next_option->setSaveFunction($optset['save']);
363 }
364
365 /* If provided, set the script for this option. */
366 if (isset($optset['script'])) {
367 $next_option->setScript($optset['script']);
368 }
369
370 /* Add this option to the option array. */
371 $result[$grpkey]['options'][] = $next_option;
372 }
373 }
374
375 /* Return our resulting array. */
376 return ($result);
377 }
378
379 function printOptionGroups($option_groups) {
380 print_option_groups($option_groups);
381 }
382
383 function print_option_groups($option_groups) {
384 foreach ($option_groups as $next_optgrp) {
385 echo '<TR><TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2" NOWRAP><B>'
386 . $next_optgrp['name'] . "</B></TD></TR>\n";
387 foreach ($next_optgrp['options'] as $option) {
388 if ($option->type != SMOPT_TYPE_HIDDEN) {
389 echo "<TR>\n";
390 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
391 . $option->caption . ":</TD>\n";
392 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
393 echo "</TR>\n";
394 } else {
395 echo $option->createHTMLWidget();
396 }
397 }
398 echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>\n";
399 }
400 }
401
402 function OptionSubmit( $name ) {
403 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
404 '</td></tr>';
405 }
406
407 ?>