6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Functions needed to display the options pages.
14 /**********************************************/
15 /* Define constants used in the options code. */
16 /**********************************************/
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);
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);
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);
42 define('SMOPT_SAVE_DEFAULT', 'save_option');
43 define('SMOPT_SAVE_NOOP', 'save_option_noop');
46 * SquirrelOption: An option for Squirrelmail.
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.
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.
56 class SquirrelOption
{
57 /* The basic stuff. */
67 /* The name of the Save Function for this option. */
70 /* The various 'values' for this options. */
75 function SquirrelOption
76 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
77 /* Set the basic stuff. */
79 $this->caption
= $caption;
81 $this->refresh_level
= $refresh_level;
82 $this->possible_values
= $possible_values;
83 $this->size
= SMOPT_SIZE_MEDIUM
;
86 $this->post_script
= '';
88 /* Check for a current value. */
89 if (!empty($initial_value)) {
90 $this->value
= $initial_value;
91 } else if (isset($GLOBALS[$name])) {
92 $this->value
= $GLOBALS[$name];
97 /* Check for a new value. */
98 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
99 $this->new_value
= '';
102 /* Set the default save function. */
103 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
104 $this->save_function
= SMOPT_SAVE_DEFAULT
;
106 $this->save_function
= SMOPT_SAVE_NOOP
;
110 /* Set the value for this option. */
111 function setValue($value) {
112 $this->value
= $value;
115 /* Set the new value for this option. */
116 function setNewValue($new_value) {
117 $this->new_value
= $new_value;
120 /* Set the size for this option. */
121 function setSize($size) {
125 /* Set the comment for this option. */
126 function setComment($comment) {
127 $this->comment
= $comment;
130 /* Set the script for this option. */
131 function setScript($script) {
132 $this->script
= $script;
135 /* Set the "post script" for this option. */
136 function setPostScript($post_script) {
137 $this->post_script
= $post_script;
140 /* Set the save function for this option. */
141 function setSaveFunction($save_function) {
142 $this->save_function
= $save_function;
145 function createHTMLWidget() {
146 global $javascript_on;
148 /* Get the widget for this option type. */
149 switch ($this->type
) {
150 case SMOPT_TYPE_STRING
:
151 $result = $this->createWidget_String();
153 case SMOPT_TYPE_STRLIST
:
154 $result = $this->createWidget_StrList();
156 case SMOPT_TYPE_TEXTAREA
:
157 $result = $this->createWidget_TextArea();
159 case SMOPT_TYPE_INTEGER
:
160 $result = $this->createWidget_Integer();
162 case SMOPT_TYPE_FLOAT
:
163 $result = $this->createWidget_Float();
165 case SMOPT_TYPE_BOOLEAN
:
166 $result = $this->createWidget_Boolean();
168 case SMOPT_TYPE_HIDDEN
:
169 $result = $this->createWidget_Hidden();
171 case SMOPT_TYPE_COMMENT
:
172 $result = $this->createWidget_Comment();
174 case SMOPT_TYPE_FLDRLIST
:
175 $result = $this->createWidget_FolderList();
178 $result = '<font color="' . $color[2] . '">'
179 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
183 /* Add the "post script" for this option. */
184 $result .= $this->post_script
;
186 /* Now, return the created widget. */
190 function createWidget_String() {
191 switch ($this->size
) {
192 case SMOPT_SIZE_TINY
:
195 case SMOPT_SIZE_SMALL
:
198 case SMOPT_SIZE_LARGE
:
201 case SMOPT_SIZE_HUGE
:
204 case SMOPT_SIZE_NORMAL
:
209 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\" $this->script>";
213 function createWidget_StrList() {
214 /* Begin the select tag. */
215 $result = "<select name=\"new_$this->name\" $this->script>";
217 /* Add each possible value to the select list. */
218 foreach ($this->possible_values
as $real_value => $disp_value) {
219 /* Start the next new option string. */
220 $new_option = "<option value=\"$real_value\"";
222 /* If this value is the current value, select it. */
223 if ($real_value == $this->value
) {
224 $new_option .= ' selected';
227 /* Add the display value to our option string. */
228 $new_option .= ">$disp_value</option>";
230 /* And add the new option string to our select tag. */
231 $result .= $new_option;
234 /* Close the select tag and return our happy result. */
235 $result .= '</select>';
239 function createWidget_FolderList() {
240 $selected = array(strtolower($this->value
));
242 /* Begin the select tag. */
243 $result = "<select name=\"new_$this->name\" $this->script>";
245 /* Add each possible value to the select list. */
246 foreach ($this->possible_values
as $real_value => $disp_value) {
247 if ( is_array($disp_value) ) {
248 /* For folder list, we passed in the array of boxes.. */
249 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
251 /* Start the next new option string. */
252 $new_option = "<option value=\"$real_value\"";
254 /* If this value is the current value, select it. */
255 if ($real_value == $this->value
) {
256 $new_option .= ' selected';
259 /* Add the display value to our option string. */
260 $new_option .= ">$disp_value</option>";
262 /* And add the new option string to our select tag. */
263 $result .= $new_option;
265 /* Close the select tag and return our happy result. */
266 $result .= '</select>';
271 function createWidget_TextArea() {
272 switch ($this->size
) {
273 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
274 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
275 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
276 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
277 case SMOPT_SIZE_NORMAL
:
278 default: $rows = 5; $cols = 50;
280 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
281 . "cols=\"$cols\" $this->script>$this->value</textarea>";
285 function createWidget_Integer() {
287 global $javascript_on;
289 // add onChange javascript handler to a regular string widget
290 // which will strip out all non-numeric chars
292 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
293 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
294 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
295 . 'this.value=newVal;">', $this->createWidget_String());
297 return $this->createWidget_String();
300 function createWidget_Float() {
302 global $javascript_on;
304 // add onChange javascript handler to a regular string widget
305 // which will strip out all non-numeric (period also OK) chars
307 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
308 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
309 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
310 . 'newVal += origVal.charAt(i); } this.value=newVal;">'
311 , $this->createWidget_String());
313 return $this->createWidget_String();
316 function createWidget_Boolean() {
317 /* Do the whole current value thing. */
318 if ($this->value
!= SMPREF_NO
) {
319 $yes_chk = ' checked';
323 $no_chk = ' checked';
326 /* Build the yes choice. */
327 $yes_option = '<input type="radio" name="new_' . $this->name
328 . '" value="' . SMPREF_YES
. "\"$yes_chk $this->script> "
331 /* Build the no choice. */
332 $no_option = '<input type="radio" name="new_' . $this->name
333 . '" value="' . SMPREF_NO
. "\"$no_chk $this->script> "
336 /* Build and return the combined "boolean widget". */
337 $result = "$yes_option $no_option";
341 function createWidget_Hidden() {
342 $result = '<input type="hidden" name="new_' . $this->name
343 . '" value="' . $this->value
. '" ' . $this->script
. '>';
347 function createWidget_Comment() {
348 $result = $this->comment
;
353 $function = $this->save_function
;
358 return ($this->value
!= $this->new_value
);
362 function save_option($option) {
363 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
364 /* Can't save the pref if we don't have the username */
368 setPref($data_dir, $username, $option->name
, $option->new_value
);
371 function save_option_noop($option) {
372 /* Do nothing here... */
375 function create_optpage_element($optpage) {
376 return create_hidden_element('optpage', $optpage);
379 function create_optmode_element($optmode) {
380 return create_hidden_element('optmode', $optmode);
383 function create_hidden_element($name, $value) {
384 $result = '<input type="hidden" '
385 . 'name="' . $name . '" '
386 . 'value="' . $value . '">';
390 function create_option_groups($optgrps, $optvals) {
391 /* Build a simple array with which to start. */
394 /* Create option group for each option group name. */
395 foreach ($optgrps as $grpkey => $grpname) {
396 $result[$grpkey] = array();
397 $result[$grpkey]['name'] = $grpname;
398 $result[$grpkey]['options'] = array();
401 /* Create a new SquirrelOption for each set of option values. */
402 foreach ($optvals as $grpkey => $grpopts) {
403 foreach ($grpopts as $optset) {
404 if (isset($optset['posvals'])) {
405 /* Create a new option with all values given. */
406 $next_option = new SquirrelOption(
411 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
415 /* Create a new option with all but possible values given. */
416 $next_option = new SquirrelOption(
421 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
425 /* If provided, set the size for this option. */
426 if (isset($optset['size'])) {
427 $next_option->setSize($optset['size']);
430 /* If provided, set the comment for this option. */
431 if (isset($optset['comment'])) {
432 $next_option->setComment($optset['comment']);
435 /* If provided, set the save function for this option. */
436 if (isset($optset['save'])) {
437 $next_option->setSaveFunction($optset['save']);
440 /* If provided, set the script for this option. */
441 if (isset($optset['script'])) {
442 $next_option->setScript($optset['script']);
445 /* If provided, set the "post script" for this option. */
446 if (isset($optset['post_script'])) {
447 $next_option->setPostScript($optset['post_script']);
450 /* Add this option to the option array. */
451 $result[$grpkey]['options'][] = $next_option;
455 /* Return our resulting array. */
459 function print_option_groups($option_groups) {
460 /* Print each option group. */
461 foreach ($option_groups as $next_optgrp) {
462 /* If it is not blank, print the name for this option group. */
463 if ($next_optgrp['name'] != '') {
464 echo html_tag( 'tr', "\n".
466 '<b>' . $next_optgrp['name'] . '</b>' ,
467 'center' ,'', 'valign="middle" colspan="2" nowrap' )
471 /* Print each option in this option group. */
472 foreach ($next_optgrp['options'] as $option) {
473 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
474 echo html_tag( 'tr', "\n".
475 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
476 html_tag( 'td', $option->createHTMLWidget(), 'left' )
479 echo $option->createHTMLWidget();
483 /* Print an empty row after this option group. */
485 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
490 function OptionSubmit( $name ) {
492 html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) .
493 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )