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