Adding FIXME to comment
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions needed to display the options pages.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**********************************************/
16 /* Define constants used in the options code. */
17 /**********************************************/
18
19 /* Define constants for the various option types. */
20 define('SMOPT_TYPE_STRING', 0);
21 define('SMOPT_TYPE_STRLIST', 1);
22 define('SMOPT_TYPE_TEXTAREA', 2);
23 define('SMOPT_TYPE_INTEGER', 3);
24 define('SMOPT_TYPE_FLOAT', 4);
25 define('SMOPT_TYPE_BOOLEAN', 5);
26 define('SMOPT_TYPE_HIDDEN', 6);
27 define('SMOPT_TYPE_COMMENT', 7);
28 define('SMOPT_TYPE_FLDRLIST', 8);
29
30 /* Define constants for the options refresh levels. */
31 define('SMOPT_REFRESH_NONE', 0);
32 define('SMOPT_REFRESH_FOLDERLIST', 1);
33 define('SMOPT_REFRESH_ALL', 2);
34
35 /* Define constants for the options size. */
36 define('SMOPT_SIZE_TINY', 0);
37 define('SMOPT_SIZE_SMALL', 1);
38 define('SMOPT_SIZE_MEDIUM', 2);
39 define('SMOPT_SIZE_LARGE', 3);
40 define('SMOPT_SIZE_HUGE', 4);
41 define('SMOPT_SIZE_NORMAL', 5);
42
43 define('SMOPT_SAVE_DEFAULT', 'save_option');
44 define('SMOPT_SAVE_NOOP', 'save_option_noop');
45
46 /**
47 * SquirrelOption: An option for Squirrelmail.
48 *
49 * This class is a work in progress. When complete, it will handle
50 * presentation and saving of Squirrelmail user options in a simple,
51 * streamline manner. Stay tuned for more stuff.
52 *
53 * Also, I'd like to ask that people leave this alone (mostly :) until
54 * I get it a little further along. That should only be a day or two or
55 * three. I will remove this message when it is ready for primetime usage.
56 * @package squirrelmail
57 */
58 class SquirrelOption {
59 /* The basic stuff. */
60 var $name;
61 var $caption;
62 var $type;
63 var $refresh_level;
64 var $size;
65 var $comment;
66 var $script;
67 var $post_script;
68
69 /* The name of the Save Function for this option. */
70 var $save_function;
71
72 /* The various 'values' for this options. */
73 var $value;
74 var $new_value;
75 var $possible_values;
76
77 function SquirrelOption
78 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
79 /* Set the basic stuff. */
80 $this->name = $name;
81 $this->caption = $caption;
82 $this->type = $type;
83 $this->refresh_level = $refresh_level;
84 $this->possible_values = $possible_values;
85 $this->size = SMOPT_SIZE_MEDIUM;
86 $this->comment = '';
87 $this->script = '';
88 $this->post_script = '';
89
90 /* Check for a current value. */
91 if (!empty($initial_value)) {
92 $this->value = $initial_value;
93 } else if (isset($GLOBALS[$name])) {
94 $this->value = $GLOBALS[$name];
95 } else {
96 $this->value = '';
97 }
98
99 /* Check for a new value. */
100 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
101 $this->new_value = '';
102 }
103
104 /* Set the default save function. */
105 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
106 $this->save_function = SMOPT_SAVE_DEFAULT;
107 } else {
108 $this->save_function = SMOPT_SAVE_NOOP;
109 }
110 }
111
112 /* Set the value for this option. */
113 function setValue($value) {
114 $this->value = $value;
115 }
116
117 /* Set the new value for this option. */
118 function setNewValue($new_value) {
119 $this->new_value = $new_value;
120 }
121
122 /* Set the size for this option. */
123 function setSize($size) {
124 $this->size = $size;
125 }
126
127 /* Set the comment for this option. */
128 function setComment($comment) {
129 $this->comment = $comment;
130 }
131
132 /* Set the script for this option. */
133 function setScript($script) {
134 $this->script = $script;
135 }
136
137 /* Set the "post script" for this option. */
138 function setPostScript($post_script) {
139 $this->post_script = $post_script;
140 }
141
142 /* Set the save function for this option. */
143 function setSaveFunction($save_function) {
144 $this->save_function = $save_function;
145 }
146
147 function createHTMLWidget() {
148 global $javascript_on;
149
150 /* Get the widget for this option type. */
151 switch ($this->type) {
152 case SMOPT_TYPE_STRING:
153 $result = $this->createWidget_String();
154 break;
155 case SMOPT_TYPE_STRLIST:
156 $result = $this->createWidget_StrList();
157 break;
158 case SMOPT_TYPE_TEXTAREA:
159 $result = $this->createWidget_TextArea();
160 break;
161 case SMOPT_TYPE_INTEGER:
162 $result = $this->createWidget_Integer();
163 break;
164 case SMOPT_TYPE_FLOAT:
165 $result = $this->createWidget_Float();
166 break;
167 case SMOPT_TYPE_BOOLEAN:
168 $result = $this->createWidget_Boolean();
169 break;
170 case SMOPT_TYPE_HIDDEN:
171 $result = $this->createWidget_Hidden();
172 break;
173 case SMOPT_TYPE_COMMENT:
174 $result = $this->createWidget_Comment();
175 break;
176 case SMOPT_TYPE_FLDRLIST:
177 $result = $this->createWidget_FolderList();
178 break;
179 default:
180 $result = '<font color="' . $color[2] . '">'
181 . sprintf(_("Option Type '%s' Not Found"), $this->type)
182 . '</font>';
183 }
184
185 /* Add the "post script" for this option. */
186 $result .= $this->post_script;
187
188 /* Now, return the created widget. */
189 return ($result);
190 }
191
192 function createWidget_String() {
193 switch ($this->size) {
194 case SMOPT_SIZE_TINY:
195 $width = 5;
196 break;
197 case SMOPT_SIZE_SMALL:
198 $width = 12;
199 break;
200 case SMOPT_SIZE_LARGE:
201 $width = 38;
202 break;
203 case SMOPT_SIZE_HUGE:
204 $width = 50;
205 break;
206 case SMOPT_SIZE_NORMAL:
207 default:
208 $width = 25;
209 }
210
211 $result = "<input name=\"new_$this->name\" value=\"" .
212 htmlspecialchars($this->value) .
213 "\" size=\"$width\" $this->script />\n";
214 return ($result);
215 }
216
217 function createWidget_StrList() {
218 /* Begin the select tag. */
219 $result = "<select name=\"new_$this->name\" $this->script>\n";
220
221 /* Add each possible value to the select list. */
222 foreach ($this->possible_values as $real_value => $disp_value) {
223 /* Start the next new option string. */
224 $new_option = '<option value="' .
225 htmlspecialchars($real_value) . '"';
226
227 /* If this value is the current value, select it. */
228 if ($real_value == $this->value) {
229 $new_option .= ' selected="selected"';
230 }
231
232 /* Add the display value to our option string. */
233 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
234
235 /* And add the new option string to our select tag. */
236 $result .= $new_option;
237 }
238
239 /* Close the select tag and return our happy result. */
240 $result .= "</select>\n";
241 return ($result);
242 }
243
244 function createWidget_FolderList() {
245 $selected = array(strtolower($this->value));
246
247 /* Begin the select tag. */
248 $result = "<select name=\"new_$this->name\" $this->script>\n";
249
250 /* Add each possible value to the select list. */
251 foreach ($this->possible_values as $real_value => $disp_value) {
252 if ( is_array($disp_value) ) {
253 /* For folder list, we passed in the array of boxes.. */
254 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
255 } else {
256 /* Start the next new option string. */
257 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
258
259 /* If this value is the current value, select it. */
260 if ($real_value == $this->value) {
261 $new_option .= ' selected="selected"';
262 }
263
264 /* Add the display value to our option string. */
265 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
266 }
267 /* And add the new option string to our select tag. */
268 $result .= $new_option;
269 }
270 /* Close the select tag and return our happy result. */
271 $result .= "</select>\n";
272 return ($result);
273 }
274
275
276 function createWidget_TextArea() {
277 switch ($this->size) {
278 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
279 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
280 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
281 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
282 case SMOPT_SIZE_NORMAL:
283 default: $rows = 5; $cols = 50;
284 }
285 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
286 . "cols=\"$cols\" $this->script>"
287 . htmlspecialchars($this->value) . "</textarea>\n";
288 return ($result);
289 }
290
291 function createWidget_Integer() {
292
293 global $javascript_on;
294
295 // add onChange javascript handler to a regular string widget
296 // which will strip out all non-numeric chars
297 if ($javascript_on)
298 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
299 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
300 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
301 . 'this.value=newVal;" />', $this->createWidget_String());
302 else
303 return $this->createWidget_String();
304 }
305
306 function createWidget_Float() {
307
308 global $javascript_on;
309
310 // add onChange javascript handler to a regular string widget
311 // which will strip out all non-numeric (period also OK) chars
312 if ($javascript_on)
313 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
314 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
315 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
316 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
317 , $this->createWidget_String());
318 else
319 return $this->createWidget_String();
320 }
321
322 function createWidget_Boolean() {
323 /* Do the whole current value thing. */
324 if ($this->value != SMPREF_NO) {
325 $yes_chk = ' checked=""';
326 $no_chk = '';
327 } else {
328 $yes_chk = '';
329 $no_chk = ' checked=""';
330 }
331
332 /* Build the yes choice. */
333 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
334 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
335 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
336 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
337
338 /* Build the no choice. */
339 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
340 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
341 . $no_chk . ' ' . $this->script . ' />&nbsp;'
342 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
343
344 /* Build and return the combined "boolean widget". */
345 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
346 return ($result);
347 }
348
349 function createWidget_Hidden() {
350 $result = '<input type="hidden" name="new_' . $this->name
351 . '" value="' . htmlspecialchars($this->value)
352 . '" ' . $this->script . ' />';
353 return ($result);
354 }
355
356 function createWidget_Comment() {
357 $result = $this->comment;
358 return ($result);
359 }
360
361 function save() {
362 $function = $this->save_function;
363 $function($this);
364 }
365
366 function changed() {
367 return ($this->value != $this->new_value);
368 }
369 }
370
371 function save_option($option) {
372 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
373 /* Can't save the pref if we don't have the username */
374 return;
375 }
376 global $data_dir;
377 setPref($data_dir, $username, $option->name, $option->new_value);
378 }
379
380 function save_option_noop($option) {
381 /* Do nothing here... */
382 }
383
384 function create_optpage_element($optpage) {
385 return create_hidden_element('optpage', $optpage);
386 }
387
388 function create_optmode_element($optmode) {
389 return create_hidden_element('optmode', $optmode);
390 }
391
392 function create_hidden_element($name, $value) {
393 $result = '<input type="hidden" '
394 . 'name="' . $name . '" '
395 . 'value="' . htmlspecialchars($value) . '" />';
396 return ($result);
397 }
398
399 function create_option_groups($optgrps, $optvals) {
400 /* Build a simple array with which to start. */
401 $result = array();
402
403 /* Create option group for each option group name. */
404 foreach ($optgrps as $grpkey => $grpname) {
405 $result[$grpkey] = array();
406 $result[$grpkey]['name'] = $grpname;
407 $result[$grpkey]['options'] = array();
408 }
409
410 /* Create a new SquirrelOption for each set of option values. */
411 foreach ($optvals as $grpkey => $grpopts) {
412 foreach ($grpopts as $optset) {
413 if (isset($optset['posvals'])) {
414 /* Create a new option with all values given. */
415 $next_option = new SquirrelOption(
416 $optset['name'],
417 $optset['caption'],
418 $optset['type'],
419 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
420 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
421 $optset['posvals']
422 );
423 } else {
424 /* Create a new option with all but possible values given. */
425 $next_option = new SquirrelOption(
426 $optset['name'],
427 $optset['caption'],
428 $optset['type'],
429 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
430 (isset($optset['initial_value']) ? $optset['initial_value'] : '')
431 );
432 }
433
434 /* If provided, set the size for this option. */
435 if (isset($optset['size'])) {
436 $next_option->setSize($optset['size']);
437 }
438
439 /* If provided, set the comment for this option. */
440 if (isset($optset['comment'])) {
441 $next_option->setComment($optset['comment']);
442 }
443
444 /* If provided, set the save function for this option. */
445 if (isset($optset['save'])) {
446 $next_option->setSaveFunction($optset['save']);
447 }
448
449 /* If provided, set the script for this option. */
450 if (isset($optset['script'])) {
451 $next_option->setScript($optset['script']);
452 }
453
454 /* If provided, set the "post script" for this option. */
455 if (isset($optset['post_script'])) {
456 $next_option->setPostScript($optset['post_script']);
457 }
458
459 /* Add this option to the option array. */
460 $result[$grpkey]['options'][] = $next_option;
461 }
462 }
463
464 /* Return our resulting array. */
465 return ($result);
466 }
467
468 function print_option_groups($option_groups) {
469 /* Print each option group. */
470 foreach ($option_groups as $next_optgrp) {
471 /* If it is not blank, print the name for this option group. */
472 if ($next_optgrp['name'] != '') {
473 echo html_tag( 'tr', "\n".
474 html_tag( 'td',
475 '<b>' . $next_optgrp['name'] . '</b>' ,
476 'center' ,'', 'valign="middle" colspan="2" nowrap' )
477 ) ."\n";
478 }
479
480 /* Print each option in this option group. */
481 foreach ($next_optgrp['options'] as $option) {
482 if ($option->type != SMOPT_TYPE_HIDDEN) {
483 echo html_tag( 'tr', "\n".
484 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
485 html_tag( 'td', $option->createHTMLWidget(), 'left' )
486 ) ."\n";
487 } else {
488 echo $option->createHTMLWidget();
489 }
490 }
491
492 /* Print an empty row after this option group. */
493 echo html_tag( 'tr',
494 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
495 ) . "\n";
496 }
497 }
498
499 function OptionSubmit( $name ) {
500 echo html_tag( 'tr',
501 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
502 ) . "\n";
503 }
504
505 // vim: et ts=4
506 ?>