cb4fe820b66a53e1df7a772c30c9bddec0b353f4
[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
66 /* The name of the Save Function for this option. */
67 var $save_function;
68
69 /* The various 'values' for this options. */
70 var $value;
71 var $new_value;
72 var $possible_values;
73
74 function SquirrelOption
75 ($name, $caption, $type, $refresh_level, $possible_values = '') {
76 /* Set the basic stuff. */
77 $this->name = $name;
78 $this->caption = $caption;
79 $this->type = $type;
80 $this->refresh_level = $refresh_level;
81 $this->possible_values = $possible_values;
82 $this->size = SMOPT_SIZE_MEDIUM;
83 $this->comment = '';
84 $this->script = '';
85
86 /* Check for a current value. */
87 if (isset($GLOBALS[$name])) {
88 $this->value = $GLOBALS[$name];
89 } else {
90 $this->value = '';
91 }
92
93 /* Check for a new value. */
94 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
95 $this->new_value = '';
96 }
97
98 /* Set the default save function. */
99 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
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;
114 }
115
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
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
136 function createHTMLWidget() {
137 global $javascript_on;
138
139 /* Get the widget for this option type. */
140 switch ($this->type) {
141 case SMOPT_TYPE_STRING:
142 $result = $this->createWidget_String($this->script);
143 break;
144 case SMOPT_TYPE_STRLIST:
145 $result = $this->createWidget_StrList($this->script);
146 break;
147 case SMOPT_TYPE_TEXTAREA:
148 $result = $this->createWidget_TextArea($this->script);
149 break;
150 case SMOPT_TYPE_INTEGER:
151 $result = $this->createWidget_Integer($this->script);
152 break;
153 case SMOPT_TYPE_FLOAT:
154 $result = $this->createWidget_Float($this->script);
155 break;
156 case SMOPT_TYPE_BOOLEAN:
157 $result = $this->createWidget_Boolean($this->script);
158 break;
159 case SMOPT_TYPE_HIDDEN:
160 $result = $this->createWidget_Hidden($this->script);
161 break;
162 case SMOPT_TYPE_COMMENT:
163 $result = $this->createWidget_Comment($this->script);
164 break;
165 case SMOPT_TYPE_FLDRLIST:
166 $result = $this->createWidget_FolderList($this->script);
167 break;
168 default:
169 $result = '<font color="' . $color[2] . '">'
170 . sprintf(_("Option Type '%s' Not Found"), $this->type)
171 . '</font>';
172 }
173
174 /* Now, return the created widget. */
175 return ($result);
176 }
177
178 function createWidget_String($script) {
179 switch ($this->size) {
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;
192 case SMOPT_SIZE_NORMAL:
193 default:
194 $width = 25;
195 }
196
197 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\" $script>";
198 return ($result);
199 }
200
201 function createWidget_StrList($script) {
202 /* Begin the select tag. */
203 $result = "<select name=\"new_$this->name\" $script>";
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. */
208 $new_option = "<option value=\"$real_value\"";
209
210 /* If this value is the current value, select it. */
211 if ($real_value == $this->value) {
212 $new_option .= ' selected';
213 }
214
215 /* Add the display value to our option string. */
216 $new_option .= ">$disp_value</option>";
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. */
223 $result .= '</select>';
224 return ($result);
225 }
226
227 function createWidget_FolderList($script) {
228 $selected = array(strtolower($this->value));
229
230 /* Begin the select tag. */
231 $result = "<select name=\"new_$this->name\" $script>";
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
259 function createWidget_TextArea($script) {
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 }
268 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
269 . "cols=\"$cols\" $script>$this->value</textarea>";
270 return ($result);
271 }
272
273 function createWidget_Integer($script) {
274
275 global $javascript_on;
276
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); } '
283 . 'this.value=newVal;">', $this->createWidget_String($script));
284 else
285 return $this->createWidget_String($script);
286 }
287
288 function createWidget_Float($script) {
289
290 // add onChange javascript handler to a regular string widget
291 // which will strip out all non-numeric (period also OK) chars
292 if ($javascript_on)
293 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
294 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
295 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
296 . 'newVal += origVal.charAt(i); } this.value=newVal;">'
297 , $this->createWidget_String($script));
298 else
299 return $this->createWidget_String($script);
300 }
301
302 function createWidget_Boolean($script) {
303 /* Do the whole current value thing. */
304 if ($this->value != SMPREF_NO) {
305 $yes_chk = ' checked';
306 $no_chk = '';
307 } else {
308 $yes_chk = '';
309 $no_chk = ' checked';
310 }
311
312 /* Build the yes choice. */
313 $yes_option = '<input type="radio" name="new_' . $this->name
314 . '" value="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
315 . _("Yes");
316
317 /* Build the no choice. */
318 $no_option = '<input type="radio" name="new_' . $this->name
319 . '" value="' . SMPREF_NO . "\"$no_chk>&nbsp;"
320 . _("No");
321
322 /* Build and return the combined "boolean widget". */
323 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
324 return ($result);
325 }
326
327 function createWidget_Hidden($script) {
328 $result = '<input type="hidden" name="new_' . $this->name
329 . '" value="' . $this->value . '">';
330 return ($result);
331 }
332
333 function createWidget_Comment($script) {
334 $result = $this->comment;
335 return ($result);
336 }
337
338 function save() {
339 $function = $this->save_function;
340 $function($this);
341 }
342
343 function changed() {
344 return ($this->value != $this->new_value);
345 }
346 }
347
348 function save_option($option) {
349 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
350 /* Can't save the pref if we don't have the username */
351 return;
352 }
353 global $data_dir;
354 setPref($data_dir, $username, $option->name, $option->new_value);
355 }
356
357 function save_option_noop($option) {
358 /* Do nothing here... */
359 }
360
361 function create_optpage_element($optpage) {
362 return create_hidden_element('optpage', $optpage);
363 }
364
365 function create_optmode_element($optmode) {
366 return create_hidden_element('optmode', $optmode);
367 }
368
369 function create_hidden_element($name, $value) {
370 $result = '<input type="hidden" '
371 . 'name="' . $name . '" '
372 . 'value="' . $value . '">';
373 return ($result);
374 }
375
376 function create_option_groups($optgrps, $optvals) {
377 /* Build a simple array with which to start. */
378 $result = array();
379
380 /* Create option group for each option group name. */
381 foreach ($optgrps as $grpkey => $grpname) {
382 $result[$grpkey] = array();
383 $result[$grpkey]['name'] = $grpname;
384 $result[$grpkey]['options'] = array();
385 }
386
387 /* Create a new SquirrelOption for each set of option values. */
388 foreach ($optvals as $grpkey => $grpopts) {
389 foreach ($grpopts as $optset) {
390 if (isset($optset['posvals'])) {
391 /* Create a new option with all values given. */
392 $next_option = new SquirrelOption(
393 $optset['name'],
394 $optset['caption'],
395 $optset['type'],
396 $optset['refresh'],
397 $optset['posvals']
398 );
399 } else {
400 /* Create a new option with all but possible values given. */
401 $next_option = new SquirrelOption(
402 $optset['name'],
403 $optset['caption'],
404 $optset['type'],
405 $optset['refresh']
406 );
407 }
408
409 /* If provided, set the size for this option. */
410 if (isset($optset['size'])) {
411 $next_option->setSize($optset['size']);
412 }
413
414 /* If provided, set the comment for this option. */
415 if (isset($optset['comment'])) {
416 $next_option->setComment($optset['comment']);
417 }
418
419 /* If provided, set the save function for this option. */
420 if (isset($optset['save'])) {
421 $next_option->setSaveFunction($optset['save']);
422 }
423
424 /* If provided, set the script for this option. */
425 if (isset($optset['script'])) {
426 $next_option->setScript($optset['script']);
427 }
428
429 /* Add this option to the option array. */
430 $result[$grpkey]['options'][] = $next_option;
431 }
432 }
433
434 /* Return our resulting array. */
435 return ($result);
436 }
437
438 function print_option_groups($option_groups) {
439 /* Print each option group. */
440 foreach ($option_groups as $next_optgrp) {
441 /* If it is not blank, print the name for this option group. */
442 if ($next_optgrp['name'] != '') {
443 echo html_tag( 'tr', "\n".
444 html_tag( 'td',
445 '<b>' . $next_optgrp['name'] . '</b>' ,
446 'center' ,'', 'valign="middle" colspan="2" nowrap' )
447 ) ."\n";
448 }
449
450 /* Print each option in this option group. */
451 foreach ($next_optgrp['options'] as $option) {
452 if ($option->type != SMOPT_TYPE_HIDDEN) {
453 echo html_tag( 'tr', "\n".
454 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
455 html_tag( 'td', $option->createHTMLWidget(), 'left' )
456 ) ."\n";
457 } else {
458 echo $option->createHTMLWidget();
459 }
460 }
461
462 /* Print an empty row after this option group. */
463 echo html_tag( 'tr',
464 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
465 ) . "\n";
466 }
467 }
468
469 function OptionSubmit( $name ) {
470 echo html_tag( 'tr',
471 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' ) .
472 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )
473 ) . "\n";
474 }
475
476 ?>