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