56f81d2057a865220350d1ef7cede067240fa3ae
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. */
66 /* The name of the Save Function for this option. */
69 /* The various 'values' for this options. */
74 function SquirrelOption
75 ($name, $caption, $type, $refresh_level, $possible_values = '') {
76 /* Set the basic stuff. */
78 $this->caption
= $caption;
80 $this->refresh_level
= $refresh_level;
81 $this->possible_values
= $possible_values;
82 $this->size
= SMOPT_SIZE_MEDIUM
;
86 /* Check for a current value. */
87 if (isset($GLOBALS[$name])) {
88 $this->value
= $GLOBALS[$name];
93 /* Check for a new value. */
94 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
95 $this->new_value
= '';
98 /* Set the default save function. */
99 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
100 $this->save_function
= SMOPT_SAVE_DEFAULT
;
102 $this->save_function
= SMOPT_SAVE_NOOP
;
106 /* Set the value for this option. */
107 function setValue($value) {
108 $this->value
= $value;
111 /* Set the new value for this option. */
112 function setNewValue($new_value) {
113 $this->new_value
= $new_value;
116 /* Set the size for this option. */
117 function setSize($size) {
121 /* Set the comment for this option. */
122 function setComment($comment) {
123 $this->comment
= $comment;
126 /* Set the script for this option. */
127 function setScript($script) {
128 $this->script
= $script;
131 /* Set the save function for this option. */
132 function setSaveFunction($save_function) {
133 $this->save_function
= $save_function;
136 function createHTMLWidget() {
137 global $javascript_on;
139 /* Get the widget for this option type. */
140 switch ($this->type
) {
141 case SMOPT_TYPE_STRING
:
142 $result = $this->createWidget_String();
144 case SMOPT_TYPE_STRLIST
:
145 $result = $this->createWidget_StrList();
147 case SMOPT_TYPE_TEXTAREA
:
148 $result = $this->createWidget_TextArea();
150 case SMOPT_TYPE_INTEGER
:
151 $result = $this->createWidget_Integer();
153 case SMOPT_TYPE_FLOAT
:
154 $result = $this->createWidget_Float();
156 case SMOPT_TYPE_BOOLEAN
:
157 $result = $this->createWidget_Boolean();
159 case SMOPT_TYPE_HIDDEN
:
160 $result = $this->createWidget_Hidden();
162 case SMOPT_TYPE_COMMENT
:
163 $result = $this->createWidget_Comment();
165 case SMOPT_TYPE_FLDRLIST
:
166 $result = $this->createWidget_FolderList();
169 $result = '<font color="' . $color[2] . '">'
170 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
174 /* Now, return the created widget. */
178 function createWidget_String() {
179 switch ($this->size
) {
180 case SMOPT_SIZE_TINY
:
183 case SMOPT_SIZE_SMALL
:
186 case SMOPT_SIZE_LARGE
:
189 case SMOPT_SIZE_HUGE
:
192 case SMOPT_SIZE_NORMAL
:
197 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\" $this->script>";
201 function createWidget_StrList() {
202 /* Begin the select tag. */
203 $result = "<select name=\"new_$this->name\" $this->script>";
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\"";
210 /* If this value is the current value, select it. */
211 if ($real_value == $this->value
) {
212 $new_option .= ' selected';
215 /* Add the display value to our option string. */
216 $new_option .= ">$disp_value</option>";
218 /* And add the new option string to our select tag. */
219 $result .= $new_option;
222 /* Close the select tag and return our happy result. */
223 $result .= '</select>';
227 function createWidget_FolderList() {
228 $selected = array(strtolower($this->value
));
230 /* Begin the select tag. */
231 $result = "<select name=\"new_$this->name\" $this->script>";
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);
239 /* Start the next new option string. */
240 $new_option = "<option value=\"$real_value\"";
242 /* If this value is the current value, select it. */
243 if ($real_value == $this->value
) {
244 $new_option .= ' selected';
247 /* Add the display value to our option string. */
248 $new_option .= ">$disp_value</option>";
250 /* And add the new option string to our select tag. */
251 $result .= $new_option;
253 /* Close the select tag and return our happy result. */
254 $result .= '</select>';
259 function createWidget_TextArea() {
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;
268 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
269 . "cols=\"$cols\" $this->script>$this->value</textarea>";
273 function createWidget_Integer() {
275 global $javascript_on;
277 // add onChange javascript handler to a regular string widget
278 // which will strip out all non-numeric chars
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());
285 return $this->createWidget_String();
288 function createWidget_Float() {
290 global $javascript_on;
292 // add onChange javascript handler to a regular string widget
293 // which will strip out all non-numeric (period also OK) chars
295 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
296 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
297 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
298 . 'newVal += origVal.charAt(i); } this.value=newVal;">'
299 , $this->createWidget_String());
301 return $this->createWidget_String();
304 function createWidget_Boolean() {
305 /* Do the whole current value thing. */
306 if ($this->value
!= SMPREF_NO
) {
307 $yes_chk = ' checked';
311 $no_chk = ' checked';
314 /* Build the yes choice. */
315 $yes_option = '<input type="radio" name="new_' . $this->name
316 . '" value="' . SMPREF_YES
. "\"$yes_chk $this->script> "
319 /* Build the no choice. */
320 $no_option = '<input type="radio" name="new_' . $this->name
321 . '" value="' . SMPREF_NO
. "\"$no_chk $this->script> "
324 /* Build and return the combined "boolean widget". */
325 $result = "$yes_option $no_option";
329 function createWidget_Hidden() {
330 $result = '<input type="hidden" name="new_' . $this->name
331 . '" value="' . $this->value
. ' ' . $this->script
. '">';
335 function createWidget_Comment() {
336 $result = $this->comment
;
341 $function = $this->save_function
;
346 return ($this->value
!= $this->new_value
);
350 function save_option($option) {
351 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
352 /* Can't save the pref if we don't have the username */
356 setPref($data_dir, $username, $option->name
, $option->new_value
);
359 function save_option_noop($option) {
360 /* Do nothing here... */
363 function create_optpage_element($optpage) {
364 return create_hidden_element('optpage', $optpage);
367 function create_optmode_element($optmode) {
368 return create_hidden_element('optmode', $optmode);
371 function create_hidden_element($name, $value) {
372 $result = '<input type="hidden" '
373 . 'name="' . $name . '" '
374 . 'value="' . $value . '">';
378 function create_option_groups($optgrps, $optvals) {
379 /* Build a simple array with which to start. */
382 /* Create option group for each option group name. */
383 foreach ($optgrps as $grpkey => $grpname) {
384 $result[$grpkey] = array();
385 $result[$grpkey]['name'] = $grpname;
386 $result[$grpkey]['options'] = array();
389 /* Create a new SquirrelOption for each set of option values. */
390 foreach ($optvals as $grpkey => $grpopts) {
391 foreach ($grpopts as $optset) {
392 if (isset($optset['posvals'])) {
393 /* Create a new option with all values given. */
394 $next_option = new SquirrelOption(
402 /* Create a new option with all but possible values given. */
403 $next_option = new SquirrelOption(
411 /* If provided, set the size for this option. */
412 if (isset($optset['size'])) {
413 $next_option->setSize($optset['size']);
416 /* If provided, set the comment for this option. */
417 if (isset($optset['comment'])) {
418 $next_option->setComment($optset['comment']);
421 /* If provided, set the save function for this option. */
422 if (isset($optset['save'])) {
423 $next_option->setSaveFunction($optset['save']);
426 /* If provided, set the script for this option. */
427 if (isset($optset['script'])) {
428 $next_option->setScript($optset['script']);
431 /* Add this option to the option array. */
432 $result[$grpkey]['options'][] = $next_option;
436 /* Return our resulting array. */
440 function print_option_groups($option_groups) {
441 /* Print each option group. */
442 foreach ($option_groups as $next_optgrp) {
443 /* If it is not blank, print the name for this option group. */
444 if ($next_optgrp['name'] != '') {
445 echo html_tag( 'tr', "\n".
447 '<b>' . $next_optgrp['name'] . '</b>' ,
448 'center' ,'', 'valign="middle" colspan="2" nowrap' )
452 /* Print each option in this option group. */
453 foreach ($next_optgrp['options'] as $option) {
454 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
455 echo html_tag( 'tr', "\n".
456 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
457 html_tag( 'td', $option->createHTMLWidget(), 'left' )
460 echo $option->createHTMLWidget();
464 /* Print an empty row after this option group. */
466 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
471 function OptionSubmit( $name ) {
473 html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) .
474 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )