Minor cleanups
[squirrelmail.git] / functions / options.php
... / ...
CommitLineData
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. */
20define('SMOPT_TYPE_STRING', 0);
21define('SMOPT_TYPE_STRLIST', 1);
22define('SMOPT_TYPE_TEXTAREA', 2);
23define('SMOPT_TYPE_INTEGER', 3);
24define('SMOPT_TYPE_FLOAT', 4);
25define('SMOPT_TYPE_BOOLEAN', 5);
26define('SMOPT_TYPE_HIDDEN', 6);
27define('SMOPT_TYPE_COMMENT', 7);
28define('SMOPT_TYPE_FLDRLIST', 8);
29
30/* Define constants for the options refresh levels. */
31define('SMOPT_REFRESH_NONE', 0);
32define('SMOPT_REFRESH_FOLDERLIST', 1);
33define('SMOPT_REFRESH_ALL', 2);
34
35/* Define constants for the options size. */
36define('SMOPT_SIZE_TINY', 0);
37define('SMOPT_SIZE_SMALL', 1);
38define('SMOPT_SIZE_MEDIUM', 2);
39define('SMOPT_SIZE_LARGE', 3);
40define('SMOPT_SIZE_HUGE', 4);
41define('SMOPT_SIZE_NORMAL', 5);
42
43define('SMOPT_SAVE_DEFAULT', 'save_option');
44define('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 */
58class 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 // Use new value if available
158 if (!empty($this->new_value)) {
159 $tempValue = $this->value;
160 $this->value = $this->new_value;
161 }
162
163 /* Get the widget for this option type. */
164 switch ($this->type) {
165 case SMOPT_TYPE_STRING:
166 $result = $this->createWidget_String();
167 break;
168 case SMOPT_TYPE_STRLIST:
169 $result = $this->createWidget_StrList();
170 break;
171 case SMOPT_TYPE_TEXTAREA:
172 $result = $this->createWidget_TextArea();
173 break;
174 case SMOPT_TYPE_INTEGER:
175 $result = $this->createWidget_Integer();
176 break;
177 case SMOPT_TYPE_FLOAT:
178 $result = $this->createWidget_Float();
179 break;
180 case SMOPT_TYPE_BOOLEAN:
181 $result = $this->createWidget_Boolean();
182 break;
183 case SMOPT_TYPE_HIDDEN:
184 $result = $this->createWidget_Hidden();
185 break;
186 case SMOPT_TYPE_COMMENT:
187 $result = $this->createWidget_Comment();
188 break;
189 case SMOPT_TYPE_FLDRLIST:
190 $result = $this->createWidget_FolderList();
191 break;
192 default:
193 $result = '<font color="' . $color[2] . '">'
194 . sprintf(_("Option Type '%s' Not Found"), $this->type)
195 . '</font>';
196 }
197
198 /* Add the "post script" for this option. */
199 $result .= $this->post_script;
200
201 // put correct value back if need be
202 if (!empty($this->new_value)) {
203 $this->value = $tempValue;
204 }
205
206 /* Now, return the created widget. */
207 return ($result);
208 }
209
210 function createWidget_String() {
211 switch ($this->size) {
212 case SMOPT_SIZE_TINY:
213 $width = 5;
214 break;
215 case SMOPT_SIZE_SMALL:
216 $width = 12;
217 break;
218 case SMOPT_SIZE_LARGE:
219 $width = 38;
220 break;
221 case SMOPT_SIZE_HUGE:
222 $width = 50;
223 break;
224 case SMOPT_SIZE_NORMAL:
225 default:
226 $width = 25;
227 }
228
229 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
230 htmlspecialchars($this->value) .
231 "\" size=\"$width\" $this->script />$this->trailing_text\n";
232 return ($result);
233 }
234
235 function createWidget_StrList() {
236 /* Begin the select tag. */
237 $result = "<select name=\"new_$this->name\" $this->script>\n";
238
239 /* Add each possible value to the select list. */
240 foreach ($this->possible_values as $real_value => $disp_value) {
241 /* Start the next new option string. */
242 $new_option = '<option value="' .
243 htmlspecialchars($real_value) . '"';
244
245 /* If this value is the current value, select it. */
246 if ($real_value == $this->value) {
247 $new_option .= ' selected="selected"';
248 }
249
250 /* Add the display value to our option string. */
251 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
252
253 /* And add the new option string to our select tag. */
254 $result .= $new_option;
255 }
256
257 /* Close the select tag and return our happy result. */
258 $result .= "</select>$this->trailing_text\n";
259 return ($result);
260 }
261
262 function createWidget_FolderList() {
263 $selected = array(strtolower($this->value));
264
265 /* Begin the select tag. */
266 $result = "<select name=\"new_$this->name\" $this->script>\n";
267
268 /* Add each possible value to the select list. */
269 foreach ($this->possible_values as $real_value => $disp_value) {
270 if ( is_array($disp_value) ) {
271 /* For folder list, we passed in the array of boxes.. */
272 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
273 } else {
274 /* Start the next new option string. */
275 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
276
277 /* If this value is the current value, select it. */
278 if ($real_value == $this->value) {
279 $new_option .= ' selected="selected"';
280 }
281
282 /* Add the display value to our option string. */
283 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
284 }
285 /* And add the new option string to our select tag. */
286 $result .= $new_option;
287 }
288 /* Close the select tag and return our happy result. */
289 $result .= "</select>\n";
290 return ($result);
291 }
292
293
294 function createWidget_TextArea() {
295 switch ($this->size) {
296 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
297 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
298 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
299 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
300 case SMOPT_SIZE_NORMAL:
301 default: $rows = 5; $cols = 50;
302 }
303 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
304 . "cols=\"$cols\" $this->script>"
305 . htmlspecialchars($this->value) . "</textarea>\n";
306 return ($result);
307 }
308
309 function createWidget_Integer() {
310
311 global $javascript_on;
312
313 // add onChange javascript handler to a regular string widget
314 // which will strip out all non-numeric chars
315 if ($javascript_on)
316 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
317 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
318 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
319 . 'this.value=newVal;" />', $this->createWidget_String());
320 else
321 return $this->createWidget_String();
322 }
323
324 function createWidget_Float() {
325
326 global $javascript_on;
327
328 // add onChange javascript handler to a regular string widget
329 // which will strip out all non-numeric (period also OK) chars
330 if ($javascript_on)
331 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
332 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
333 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
334 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
335 , $this->createWidget_String());
336 else
337 return $this->createWidget_String();
338 }
339
340 function createWidget_Boolean() {
341 /* Do the whole current value thing. */
342 if ($this->value != SMPREF_NO) {
343 $yes_chk = ' checked="checked"';
344 $no_chk = '';
345 } else {
346 $yes_chk = '';
347 $no_chk = ' checked="checked"';
348 }
349
350 /* Build the yes choice. */
351 $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" '
352 . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
353 . $yes_chk . ' ' . $this->script . ' />&nbsp;'
354 . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
355
356 /* Build the no choice. */
357 $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
358 . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
359 . $no_chk . ' ' . $this->script . ' />&nbsp;'
360 . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
361
362 /* Build and return the combined "boolean widget". */
363 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
364 return ($result);
365 }
366
367 function createWidget_Hidden() {
368 $result = '<input type="hidden" name="new_' . $this->name
369 . '" value="' . htmlspecialchars($this->value)
370 . '" ' . $this->script . ' />';
371 return ($result);
372 }
373
374 function createWidget_Comment() {
375 $result = $this->comment;
376 return ($result);
377 }
378
379 function save() {
380 $function = $this->save_function;
381 $function($this);
382 }
383
384 function changed() {
385 return ($this->value != $this->new_value);
386 }
387}
388
389function save_option($option) {
390 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
391 /* Can't save the pref if we don't have the username */
392 return;
393 }
394 global $data_dir;
395 setPref($data_dir, $username, $option->name, $option->new_value);
396}
397
398function save_option_noop($option) {
399 /* Do nothing here... */
400}
401
402function create_optpage_element($optpage) {
403 return create_hidden_element('optpage', $optpage);
404}
405
406function create_optmode_element($optmode) {
407 return create_hidden_element('optmode', $optmode);
408}
409
410function create_hidden_element($name, $value) {
411 $result = '<input type="hidden" '
412 . 'name="' . $name . '" '
413 . 'value="' . htmlspecialchars($value) . '" />';
414 return ($result);
415}
416
417function create_option_groups($optgrps, $optvals) {
418 /* Build a simple array with which to start. */
419 $result = array();
420
421 /* Create option group for each option group name. */
422 foreach ($optgrps as $grpkey => $grpname) {
423 $result[$grpkey] = array();
424 $result[$grpkey]['name'] = $grpname;
425 $result[$grpkey]['options'] = array();
426 }
427
428 /* Create a new SquirrelOption for each set of option values. */
429 foreach ($optvals as $grpkey => $grpopts) {
430 foreach ($grpopts as $optset) {
431 if (isset($optset['posvals'])) {
432 /* Create a new option with all values given. */
433 $next_option = new SquirrelOption(
434 $optset['name'],
435 $optset['caption'],
436 $optset['type'],
437 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
438 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
439 $optset['posvals']
440 );
441 } else {
442 /* Create a new option with all but possible values given. */
443 $next_option = new SquirrelOption(
444 $optset['name'],
445 $optset['caption'],
446 $optset['type'],
447 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
448 (isset($optset['initial_value']) ? $optset['initial_value'] : '')
449 );
450 }
451
452 /* If provided, set the size for this option. */
453 if (isset($optset['size'])) {
454 $next_option->setSize($optset['size']);
455 }
456
457 /* If provided, set the trailing_text for this option. */
458 if (isset($optset['trailing_text'])) {
459 $next_option->setTrailingText($optset['trailing_text']);
460 }
461
462 /* If provided, set the comment for this option. */
463 if (isset($optset['comment'])) {
464 $next_option->setComment($optset['comment']);
465 }
466
467 /* If provided, set the save function for this option. */
468 if (isset($optset['save'])) {
469 $next_option->setSaveFunction($optset['save']);
470 }
471
472 /* If provided, set the script for this option. */
473 if (isset($optset['script'])) {
474 $next_option->setScript($optset['script']);
475 }
476
477 /* If provided, set the "post script" for this option. */
478 if (isset($optset['post_script'])) {
479 $next_option->setPostScript($optset['post_script']);
480 }
481
482 /* Add this option to the option array. */
483 $result[$grpkey]['options'][] = $next_option;
484 }
485 }
486
487 /* Return our resulting array. */
488 return ($result);
489}
490
491function print_option_groups($option_groups) {
492 /* Print each option group. */
493 foreach ($option_groups as $next_optgrp) {
494 /* If it is not blank, print the name for this option group. */
495 if ($next_optgrp['name'] != '') {
496 echo html_tag( 'tr', "\n".
497 html_tag( 'td',
498 '<b>' . $next_optgrp['name'] . '</b>' ,
499 'center' ,'', 'valign="middle" colspan="2" nowrap' )
500 ) ."\n";
501 }
502
503 /* Print each option in this option group. */
504 foreach ($next_optgrp['options'] as $option) {
505 if ($option->type != SMOPT_TYPE_HIDDEN) {
506 echo html_tag( 'tr', "\n".
507 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
508 html_tag( 'td', $option->createHTMLWidget(), 'left' )
509 ) ."\n";
510 } else {
511 echo $option->createHTMLWidget();
512 }
513 }
514
515 /* Print an empty row after this option group. */
516 echo html_tag( 'tr',
517 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
518 ) . "\n";
519 }
520}
521
522function OptionSubmit( $name ) {
523 echo html_tag( 'tr',
524 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
525 ) . "\n";
526}
527
528// vim: et ts=4
529?>