Oops, text inputs never had a type attribute!
[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;
65 var $comment;
cbe5423b 66 var $script;
6ae9e729 67 var $post_script;
cbe5423b 68
69 /* The name of the Save Function for this option. */
70 var $save_function;
9962527a 71
72 /* The various 'values' for this options. */
73 var $value;
74 var $new_value;
a3ec3c91 75 var $possible_values;
9962527a 76
9962527a 77 function SquirrelOption
6ae9e729 78 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
9962527a 79 /* Set the basic stuff. */
80 $this->name = $name;
81 $this->caption = $caption;
9962527a 82 $this->type = $type;
a3ec3c91 83 $this->refresh_level = $refresh_level;
84 $this->possible_values = $possible_values;
bbcafebd 85 $this->size = SMOPT_SIZE_MEDIUM;
86 $this->comment = '';
cbe5423b 87 $this->script = '';
6ae9e729 88 $this->post_script = '';
a3ec3c91 89
90 /* Check for a current value. */
6ae9e729 91 if (!empty($initial_value)) {
92 $this->value = $initial_value;
93 } else if (isset($GLOBALS[$name])) {
a3ec3c91 94 $this->value = $GLOBALS[$name];
95 } else {
96 $this->value = '';
97 }
9962527a 98
a3ec3c91 99 /* Check for a new value. */
d6150d69 100 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
a3ec3c91 101 $this->new_value = '';
44ef0f47 102 }
cbe5423b 103
104 /* Set the default save function. */
2a50fbd7 105 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 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;
9962527a 120 }
44ef0f47 121
bbcafebd 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
cbe5423b 132 /* Set the script for this option. */
133 function setScript($script) {
134 $this->script = $script;
135 }
136
6ae9e729 137 /* Set the "post script" for this option. */
138 function setPostScript($post_script) {
139 $this->post_script = $post_script;
140 }
141
cbe5423b 142 /* Set the save function for this option. */
143 function setSaveFunction($save_function) {
144 $this->save_function = $save_function;
145 }
146
a3ec3c91 147 function createHTMLWidget() {
cbe5423b 148 global $javascript_on;
149
150 /* Get the widget for this option type. */
a3ec3c91 151 switch ($this->type) {
152 case SMOPT_TYPE_STRING:
37a3ed17 153 $result = $this->createWidget_String();
a3ec3c91 154 break;
155 case SMOPT_TYPE_STRLIST:
37a3ed17 156 $result = $this->createWidget_StrList();
a3ec3c91 157 break;
7e6d5ea3 158 case SMOPT_TYPE_TEXTAREA:
37a3ed17 159 $result = $this->createWidget_TextArea();
a3ec3c91 160 break;
161 case SMOPT_TYPE_INTEGER:
37a3ed17 162 $result = $this->createWidget_Integer();
a3ec3c91 163 break;
164 case SMOPT_TYPE_FLOAT:
37a3ed17 165 $result = $this->createWidget_Float();
a3ec3c91 166 break;
167 case SMOPT_TYPE_BOOLEAN:
37a3ed17 168 $result = $this->createWidget_Boolean();
a3ec3c91 169 break;
2a50fbd7 170 case SMOPT_TYPE_HIDDEN:
37a3ed17 171 $result = $this->createWidget_Hidden();
a3ec3c91 172 break;
bbcafebd 173 case SMOPT_TYPE_COMMENT:
37a3ed17 174 $result = $this->createWidget_Comment();
bbcafebd 175 break;
be2d5495 176 case SMOPT_TYPE_FLDRLIST:
37a3ed17 177 $result = $this->createWidget_FolderList();
be2d5495 178 break;
a3ec3c91 179 default:
6b4bd11f 180 $result = '<font color="' . $color[2] . '">'
a3ec3c91 181 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 182 . '</font>';
a3ec3c91 183 }
184
6ae9e729 185 /* Add the "post script" for this option. */
186 $result .= $this->post_script;
187
a3ec3c91 188 /* Now, return the created widget. */
189 return ($result);
190 }
191
37a3ed17 192 function createWidget_String() {
bbcafebd 193 switch ($this->size) {
88cb1b4d 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;
bbcafebd 206 case SMOPT_SIZE_NORMAL:
88cb1b4d 207 default:
208 $width = 25;
bbcafebd 209 }
210
88440172 211 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
d6150d69 212 htmlspecialchars($this->value) .
213 "\" size=\"$width\" $this->script />\n";
a3ec3c91 214 return ($result);
215 }
216
37a3ed17 217 function createWidget_StrList() {
a3ec3c91 218 /* Begin the select tag. */
d6150d69 219 $result = "<select name=\"new_$this->name\" $this->script>\n";
a3ec3c91 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. */
d6150d69 224 $new_option = '<option value="' .
225 htmlspecialchars($real_value) . '"';
a3ec3c91 226
227 /* If this value is the current value, select it. */
228 if ($real_value == $this->value) {
d6150d69 229 $new_option .= ' selected="selected"';
a3ec3c91 230 }
231
232 /* Add the display value to our option string. */
d6150d69 233 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
a3ec3c91 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. */
d6150d69 240 $result .= "</select>\n";
a3ec3c91 241 return ($result);
242 }
243
37a3ed17 244 function createWidget_FolderList() {
be2d5495 245 $selected = array(strtolower($this->value));
246
247 /* Begin the select tag. */
d6150d69 248 $result = "<select name=\"new_$this->name\" $this->script>\n";
be2d5495 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. */
d6150d69 257 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
be2d5495 258
259 /* If this value is the current value, select it. */
260 if ($real_value == $this->value) {
d6150d69 261 $new_option .= ' selected="selected"';
be2d5495 262 }
263
264 /* Add the display value to our option string. */
d6150d69 265 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
be2d5495 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. */
d6150d69 271 $result .= "</select>\n";
be2d5495 272 return ($result);
273 }
274
275
37a3ed17 276 function createWidget_TextArea() {
bbcafebd 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 }
6b4bd11f 285 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
d6150d69 286 . "cols=\"$cols\" $this->script>"
287 . htmlspecialchars($this->value) . "</textarea>\n";
bbcafebd 288 return ($result);
a3ec3c91 289 }
290
37a3ed17 291 function createWidget_Integer() {
8bde22ae 292
b65d1a08 293 global $javascript_on;
0d08ea5a 294
b65d1a08 295 // add onChange javascript handler to a regular string widget
296 // which will strip out all non-numeric chars
297 if ($javascript_on)
d6150d69 298 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 299 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
300 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
d6150d69 301 . 'this.value=newVal;" />', $this->createWidget_String());
b65d1a08 302 else
37a3ed17 303 return $this->createWidget_String();
a3ec3c91 304 }
305
37a3ed17 306 function createWidget_Float() {
0d08ea5a 307
37a3ed17 308 global $javascript_on;
309
b65d1a08 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)
d6150d69 313 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
b65d1a08 314 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
315 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
d6150d69 316 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
37a3ed17 317 , $this->createWidget_String());
b65d1a08 318 else
37a3ed17 319 return $this->createWidget_String();
a3ec3c91 320 }
321
37a3ed17 322 function createWidget_Boolean() {
fd87494d 323 /* Do the whole current value thing. */
324 if ($this->value != SMPREF_NO) {
f4c37e3c 325 $yes_chk = ' checked=""';
fd87494d 326 $no_chk = '';
327 } else {
328 $yes_chk = '';
f4c37e3c 329 $no_chk = ' checked=""';
fd87494d 330 }
331
332 /* Build the yes choice. */
f4c37e3c 333 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
334 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
d6150d69 335 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 336 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
fd87494d 337
338 /* Build the no choice. */
f4c37e3c 339 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
340 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
d6150d69 341 . $no_chk . ' ' . $this->script . ' />&nbsp;'
f4c37e3c 342 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
fd87494d 343
344 /* Build and return the combined "boolean widget". */
345 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
346 return ($result);
a3ec3c91 347 }
348
37a3ed17 349 function createWidget_Hidden() {
6b4bd11f 350 $result = '<input type="hidden" name="new_' . $this->name
d6150d69 351 . '" value="' . htmlspecialchars($this->value)
352 . '" ' . $this->script . ' />';
a3ec3c91 353 return ($result);
354 }
355
37a3ed17 356 function createWidget_Comment() {
bbcafebd 357 $result = $this->comment;
358 return ($result);
359 }
360
cbe5423b 361 function save() {
362 $function = $this->save_function;
363 $function($this);
44ef0f47 364 }
cbe5423b 365
366 function changed() {
6206f6c4 367 return ($this->value != $this->new_value);
cbe5423b 368 }
369}
370
371function save_option($option) {
dac16606 372 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
373 /* Can't save the pref if we don't have the username */
374 return;
0b97a708 375 }
376 global $data_dir;
0b97a708 377 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 378}
379
380function save_option_noop($option) {
381 /* Do nothing here... */
9962527a 382}
44ef0f47 383
cbe5423b 384function create_optpage_element($optpage) {
385 return create_hidden_element('optpage', $optpage);
386}
387
388function create_optmode_element($optmode) {
389 return create_hidden_element('optmode', $optmode);
390}
391
392function create_hidden_element($name, $value) {
6b4bd11f 393 $result = '<input type="hidden" '
394 . 'name="' . $name . '" '
d6150d69 395 . 'value="' . htmlspecialchars($value) . '" />';
cbe5423b 396 return ($result);
397}
398
cbe5423b 399function create_option_groups($optgrps, $optvals) {
a3ec3c91 400 /* Build a simple array with which to start. */
401 $result = array();
402
bbcafebd 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
a3ec3c91 410 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 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'],
4986e440 419 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
6ae9e729 420 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
bbcafebd 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'],
4986e440 429 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
6ae9e729 430 (isset($optset['initial_value']) ? $optset['initial_value'] : '')
bbcafebd 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
cbe5423b 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
6ae9e729 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
bbcafebd 459 /* Add this option to the option array. */
460 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 461 }
462 }
463
464 /* Return our resulting array. */
465 return ($result);
466}
467
cbe5423b 468function print_option_groups($option_groups) {
2fad95fa 469 /* Print each option group. */
bbcafebd 470 foreach ($option_groups as $next_optgrp) {
2fad95fa 471 /* If it is not blank, print the name for this option group. */
472 if ($next_optgrp['name'] != '') {
6b4bd11f 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";
7e235a1a 478 }
479
480 /* Print each option in this option group. */
bbcafebd 481 foreach ($next_optgrp['options'] as $option) {
2a50fbd7 482 if ($option->type != SMOPT_TYPE_HIDDEN) {
6b4bd11f 483 echo html_tag( 'tr', "\n".
484 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
485 html_tag( 'td', $option->createHTMLWidget(), 'left' )
486 ) ."\n";
bbcafebd 487 } else {
488 echo $option->createHTMLWidget();
489 }
490 }
7e235a1a 491
492 /* Print an empty row after this option group. */
6b4bd11f 493 echo html_tag( 'tr',
494 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
495 ) . "\n";
bbcafebd 496 }
497}
498
9962527a 499function OptionSubmit( $name ) {
6b4bd11f 500 echo html_tag( 'tr',
1aefbee0 501 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
6b4bd11f 502 ) . "\n";
9962527a 503}
02ddcd00 504
d6150d69 505// vim: et ts=4
23d6bd09 506?>