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