Added 'trailing text' for options that SM builds, which is placed after a text input...
[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
157 /* Get the widget for this option type. */
a3ec3c91 158 switch ($this->type) {
159 case SMOPT_TYPE_STRING:
37a3ed17 160 $result = $this->createWidget_String();
a3ec3c91 161 break;
162 case SMOPT_TYPE_STRLIST:
37a3ed17 163 $result = $this->createWidget_StrList();
a3ec3c91 164 break;
7e6d5ea3 165 case SMOPT_TYPE_TEXTAREA:
37a3ed17 166 $result = $this->createWidget_TextArea();
a3ec3c91 167 break;
168 case SMOPT_TYPE_INTEGER:
37a3ed17 169 $result = $this->createWidget_Integer();
a3ec3c91 170 break;
171 case SMOPT_TYPE_FLOAT:
37a3ed17 172 $result = $this->createWidget_Float();
a3ec3c91 173 break;
174 case SMOPT_TYPE_BOOLEAN:
37a3ed17 175 $result = $this->createWidget_Boolean();
a3ec3c91 176 break;
2a50fbd7 177 case SMOPT_TYPE_HIDDEN:
37a3ed17 178 $result = $this->createWidget_Hidden();
a3ec3c91 179 break;
bbcafebd 180 case SMOPT_TYPE_COMMENT:
37a3ed17 181 $result = $this->createWidget_Comment();
bbcafebd 182 break;
be2d5495 183 case SMOPT_TYPE_FLDRLIST:
37a3ed17 184 $result = $this->createWidget_FolderList();
be2d5495 185 break;
a3ec3c91 186 default:
6b4bd11f 187 $result = '<font color="' . $color[2] . '">'
a3ec3c91 188 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 189 . '</font>';
a3ec3c91 190 }
191
6ae9e729 192 /* Add the "post script" for this option. */
193 $result .= $this->post_script;
194
a3ec3c91 195 /* Now, return the created widget. */
196 return ($result);
197 }
198
37a3ed17 199 function createWidget_String() {
bbcafebd 200 switch ($this->size) {
88cb1b4d 201 case SMOPT_SIZE_TINY:
202 $width = 5;
203 break;
204 case SMOPT_SIZE_SMALL:
205 $width = 12;
206 break;
207 case SMOPT_SIZE_LARGE:
208 $width = 38;
209 break;
210 case SMOPT_SIZE_HUGE:
211 $width = 50;
212 break;
bbcafebd 213 case SMOPT_SIZE_NORMAL:
88cb1b4d 214 default:
215 $width = 25;
bbcafebd 216 }
217
88440172 218 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
d6150d69 219 htmlspecialchars($this->value) .
361d6e1b 220 "\" size=\"$width\" $this->script />$this->trailing_text\n";
a3ec3c91 221 return ($result);
222 }
223
37a3ed17 224 function createWidget_StrList() {
a3ec3c91 225 /* Begin the select tag. */
d6150d69 226 $result = "<select name=\"new_$this->name\" $this->script>\n";
a3ec3c91 227
228 /* Add each possible value to the select list. */
229 foreach ($this->possible_values as $real_value => $disp_value) {
230 /* Start the next new option string. */
d6150d69 231 $new_option = '<option value="' .
232 htmlspecialchars($real_value) . '"';
a3ec3c91 233
234 /* If this value is the current value, select it. */
235 if ($real_value == $this->value) {
d6150d69 236 $new_option .= ' selected="selected"';
a3ec3c91 237 }
238
239 /* Add the display value to our option string. */
d6150d69 240 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
a3ec3c91 241
242 /* And add the new option string to our select tag. */
243 $result .= $new_option;
244 }
245
246 /* Close the select tag and return our happy result. */
361d6e1b 247 $result .= "</select>$this->trailing_text\n";
a3ec3c91 248 return ($result);
249 }
250
37a3ed17 251 function createWidget_FolderList() {
be2d5495 252 $selected = array(strtolower($this->value));
253
254 /* Begin the select tag. */
d6150d69 255 $result = "<select name=\"new_$this->name\" $this->script>\n";
be2d5495 256
257 /* Add each possible value to the select list. */
258 foreach ($this->possible_values as $real_value => $disp_value) {
259 if ( is_array($disp_value) ) {
260 /* For folder list, we passed in the array of boxes.. */
261 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
262 } else {
263 /* Start the next new option string. */
d6150d69 264 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
be2d5495 265
266 /* If this value is the current value, select it. */
267 if ($real_value == $this->value) {
d6150d69 268 $new_option .= ' selected="selected"';
be2d5495 269 }
270
271 /* Add the display value to our option string. */
d6150d69 272 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
be2d5495 273 }
274 /* And add the new option string to our select tag. */
275 $result .= $new_option;
276 }
277 /* Close the select tag and return our happy result. */
d6150d69 278 $result .= "</select>\n";
be2d5495 279 return ($result);
280 }
281
282
37a3ed17 283 function createWidget_TextArea() {
bbcafebd 284 switch ($this->size) {
285 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
286 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
287 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
288 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
289 case SMOPT_SIZE_NORMAL:
290 default: $rows = 5; $cols = 50;
291 }
6b4bd11f 292 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
d6150d69 293 . "cols=\"$cols\" $this->script>"
294 . htmlspecialchars($this->value) . "</textarea>\n";
bbcafebd 295 return ($result);
a3ec3c91 296 }
297
37a3ed17 298 function createWidget_Integer() {
8bde22ae 299
b65d1a08 300 global $javascript_on;
0d08ea5a 301
b65d1a08 302 // add onChange javascript handler to a regular string widget
303 // which will strip out all non-numeric chars
304 if ($javascript_on)
d6150d69 305 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 306 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
307 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
d6150d69 308 . 'this.value=newVal;" />', $this->createWidget_String());
b65d1a08 309 else
37a3ed17 310 return $this->createWidget_String();
a3ec3c91 311 }
312
37a3ed17 313 function createWidget_Float() {
0d08ea5a 314
37a3ed17 315 global $javascript_on;
316
b65d1a08 317 // add onChange javascript handler to a regular string widget
318 // which will strip out all non-numeric (period also OK) chars
319 if ($javascript_on)
d6150d69 320 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 321 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
322 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
d6150d69 323 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
37a3ed17 324 , $this->createWidget_String());
b65d1a08 325 else
37a3ed17 326 return $this->createWidget_String();
a3ec3c91 327 }
328
37a3ed17 329 function createWidget_Boolean() {
fd87494d 330 /* Do the whole current value thing. */
331 if ($this->value != SMPREF_NO) {
f4c37e3c 332 $yes_chk = ' checked=""';
fd87494d 333 $no_chk = '';
334 } else {
335 $yes_chk = '';
f4c37e3c 336 $no_chk = ' checked=""';
fd87494d 337 }
338
339 /* Build the yes choice. */
f4c37e3c 340 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
341 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
d6150d69 342 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 343 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
fd87494d 344
345 /* Build the no choice. */
f4c37e3c 346 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
347 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
d6150d69 348 . $no_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 349 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
fd87494d 350
351 /* Build and return the combined "boolean widget". */
352 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
353 return ($result);
a3ec3c91 354 }
355
37a3ed17 356 function createWidget_Hidden() {
6b4bd11f 357 $result = '<input type="hidden" name="new_' . $this->name
d6150d69 358 . '" value="' . htmlspecialchars($this->value)
359 . '" ' . $this->script . ' />';
a3ec3c91 360 return ($result);
361 }
362
37a3ed17 363 function createWidget_Comment() {
bbcafebd 364 $result = $this->comment;
365 return ($result);
366 }
367
cbe5423b 368 function save() {
369 $function = $this->save_function;
370 $function($this);
44ef0f47 371 }
cbe5423b 372
373 function changed() {
6206f6c4 374 return ($this->value != $this->new_value);
cbe5423b 375 }
376}
377
378function save_option($option) {
dac16606 379 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
380 /* Can't save the pref if we don't have the username */
381 return;
0b97a708 382 }
383 global $data_dir;
0b97a708 384 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 385}
386
387function save_option_noop($option) {
388 /* Do nothing here... */
9962527a 389}
44ef0f47 390
cbe5423b 391function create_optpage_element($optpage) {
392 return create_hidden_element('optpage', $optpage);
393}
394
395function create_optmode_element($optmode) {
396 return create_hidden_element('optmode', $optmode);
397}
398
399function create_hidden_element($name, $value) {
6b4bd11f 400 $result = '<input type="hidden" '
401 . 'name="' . $name . '" '
d6150d69 402 . 'value="' . htmlspecialchars($value) . '" />';
cbe5423b 403 return ($result);
404}
405
cbe5423b 406function create_option_groups($optgrps, $optvals) {
a3ec3c91 407 /* Build a simple array with which to start. */
408 $result = array();
409
bbcafebd 410 /* Create option group for each option group name. */
411 foreach ($optgrps as $grpkey => $grpname) {
412 $result[$grpkey] = array();
413 $result[$grpkey]['name'] = $grpname;
414 $result[$grpkey]['options'] = array();
415 }
416
a3ec3c91 417 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 418 foreach ($optvals as $grpkey => $grpopts) {
419 foreach ($grpopts as $optset) {
420 if (isset($optset['posvals'])) {
421 /* Create a new option with all values given. */
422 $next_option = new SquirrelOption(
423 $optset['name'],
424 $optset['caption'],
425 $optset['type'],
4986e440 426 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
6ae9e729 427 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
bbcafebd 428 $optset['posvals']
429 );
430 } else {
431 /* Create a new option with all but possible values given. */
432 $next_option = new SquirrelOption(
433 $optset['name'],
434 $optset['caption'],
435 $optset['type'],
4986e440 436 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
6ae9e729 437 (isset($optset['initial_value']) ? $optset['initial_value'] : '')
bbcafebd 438 );
439 }
440
441 /* If provided, set the size for this option. */
442 if (isset($optset['size'])) {
443 $next_option->setSize($optset['size']);
444 }
445
361d6e1b 446 /* If provided, set the trailing_text for this option. */
447 if (isset($optset['trailing_text'])) {
448 $next_option->setTrailingText($optset['trailing_text']);
449 }
450
bbcafebd 451 /* If provided, set the comment for this option. */
452 if (isset($optset['comment'])) {
453 $next_option->setComment($optset['comment']);
454 }
455
cbe5423b 456 /* If provided, set the save function for this option. */
457 if (isset($optset['save'])) {
458 $next_option->setSaveFunction($optset['save']);
459 }
460
461 /* If provided, set the script for this option. */
462 if (isset($optset['script'])) {
463 $next_option->setScript($optset['script']);
464 }
465
6ae9e729 466 /* If provided, set the "post script" for this option. */
467 if (isset($optset['post_script'])) {
468 $next_option->setPostScript($optset['post_script']);
469 }
470
bbcafebd 471 /* Add this option to the option array. */
472 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 473 }
474 }
475
476 /* Return our resulting array. */
477 return ($result);
478}
479
cbe5423b 480function print_option_groups($option_groups) {
2fad95fa 481 /* Print each option group. */
bbcafebd 482 foreach ($option_groups as $next_optgrp) {
2fad95fa 483 /* If it is not blank, print the name for this option group. */
484 if ($next_optgrp['name'] != '') {
6b4bd11f 485 echo html_tag( 'tr', "\n".
486 html_tag( 'td',
487 '<b>' . $next_optgrp['name'] . '</b>' ,
488 'center' ,'', 'valign="middle" colspan="2" nowrap' )
489 ) ."\n";
7e235a1a 490 }
491
492 /* Print each option in this option group. */
bbcafebd 493 foreach ($next_optgrp['options'] as $option) {
2a50fbd7 494 if ($option->type != SMOPT_TYPE_HIDDEN) {
6b4bd11f 495 echo html_tag( 'tr', "\n".
496 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
497 html_tag( 'td', $option->createHTMLWidget(), 'left' )
498 ) ."\n";
bbcafebd 499 } else {
500 echo $option->createHTMLWidget();
501 }
502 }
7e235a1a 503
504 /* Print an empty row after this option group. */
6b4bd11f 505 echo html_tag( 'tr',
506 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
507 ) . "\n";
bbcafebd 508 }
509}
510
9962527a 511function OptionSubmit( $name ) {
6b4bd11f 512 echo html_tag( 'tr',
1aefbee0 513 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
6b4bd11f 514 ) . "\n";
9962527a 515}
02ddcd00 516
d6150d69 517// vim: et ts=4
23d6bd09 518?>