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