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