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