6 * Copyright (c) 1999-2004 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.
12 * @package squirrelmail
15 /**********************************************/
16 /* Define constants used in the options code. */
17 /**********************************************/
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);
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);
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);
43 define('SMOPT_SAVE_DEFAULT', 'save_option');
44 define('SMOPT_SAVE_NOOP', 'save_option_noop');
47 * SquirrelOption: An option for Squirrelmail.
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.
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
58 class SquirrelOption
{
59 /* The basic stuff. */
69 /* The name of the Save Function for this option. */
72 /* The various 'values' for this options. */
77 function SquirrelOption
78 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
79 /* Set the basic stuff. */
81 $this->caption
= $caption;
83 $this->refresh_level
= $refresh_level;
84 $this->possible_values
= $possible_values;
85 $this->size
= SMOPT_SIZE_MEDIUM
;
88 $this->post_script
= '';
90 /* Check for a current value. */
91 if (!empty($initial_value)) {
92 $this->value
= $initial_value;
93 } else if (isset($GLOBALS[$name])) {
94 $this->value
= $GLOBALS[$name];
99 /* Check for a new value. */
100 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
101 $this->new_value
= '';
104 /* Set the default save function. */
105 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
106 $this->save_function
= SMOPT_SAVE_DEFAULT
;
108 $this->save_function
= SMOPT_SAVE_NOOP
;
112 /* Set the value for this option. */
113 function setValue($value) {
114 $this->value
= $value;
117 /* Set the new value for this option. */
118 function setNewValue($new_value) {
119 $this->new_value
= $new_value;
122 /* Set the size for this option. */
123 function setSize($size) {
127 /* Set the comment for this option. */
128 function setComment($comment) {
129 $this->comment
= $comment;
132 /* Set the script for this option. */
133 function setScript($script) {
134 $this->script
= $script;
137 /* Set the "post script" for this option. */
138 function setPostScript($post_script) {
139 $this->post_script
= $post_script;
142 /* Set the save function for this option. */
143 function setSaveFunction($save_function) {
144 $this->save_function
= $save_function;
147 function createHTMLWidget() {
148 global $javascript_on;
150 /* Get the widget for this option type. */
151 switch ($this->type
) {
152 case SMOPT_TYPE_STRING
:
153 $result = $this->createWidget_String();
155 case SMOPT_TYPE_STRLIST
:
156 $result = $this->createWidget_StrList();
158 case SMOPT_TYPE_TEXTAREA
:
159 $result = $this->createWidget_TextArea();
161 case SMOPT_TYPE_INTEGER
:
162 $result = $this->createWidget_Integer();
164 case SMOPT_TYPE_FLOAT
:
165 $result = $this->createWidget_Float();
167 case SMOPT_TYPE_BOOLEAN
:
168 $result = $this->createWidget_Boolean();
170 case SMOPT_TYPE_HIDDEN
:
171 $result = $this->createWidget_Hidden();
173 case SMOPT_TYPE_COMMENT
:
174 $result = $this->createWidget_Comment();
176 case SMOPT_TYPE_FLDRLIST
:
177 $result = $this->createWidget_FolderList();
180 $result = '<font color="' . $color[2] . '">'
181 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
185 /* Add the "post script" for this option. */
186 $result .= $this->post_script
;
188 /* Now, return the created widget. */
192 function createWidget_String() {
193 switch ($this->size
) {
194 case SMOPT_SIZE_TINY
:
197 case SMOPT_SIZE_SMALL
:
200 case SMOPT_SIZE_LARGE
:
203 case SMOPT_SIZE_HUGE
:
206 case SMOPT_SIZE_NORMAL
:
211 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
212 htmlspecialchars($this->value
) .
213 "\" size=\"$width\" $this->script />\n";
217 function createWidget_StrList() {
218 /* Begin the select tag. */
219 $result = "<select name=\"new_$this->name\" $this->script>\n";
221 /* Add each possible value to the select list. */
222 foreach ($this->possible_values
as $real_value => $disp_value) {
223 /* Start the next new option string. */
224 $new_option = '<option value="' .
225 htmlspecialchars($real_value) . '"';
227 /* If this value is the current value, select it. */
228 if ($real_value == $this->value
) {
229 $new_option .= ' selected="selected"';
232 /* Add the display value to our option string. */
233 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
235 /* And add the new option string to our select tag. */
236 $result .= $new_option;
239 /* Close the select tag and return our happy result. */
240 $result .= "</select>\n";
244 function createWidget_FolderList() {
245 $selected = array(strtolower($this->value
));
247 /* Begin the select tag. */
248 $result = "<select name=\"new_$this->name\" $this->script>\n";
250 /* Add each possible value to the select list. */
251 foreach ($this->possible_values
as $real_value => $disp_value) {
252 if ( is_array($disp_value) ) {
253 /* For folder list, we passed in the array of boxes.. */
254 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
256 /* Start the next new option string. */
257 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
259 /* If this value is the current value, select it. */
260 if ($real_value == $this->value
) {
261 $new_option .= ' selected="selected"';
264 /* Add the display value to our option string. */
265 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
267 /* And add the new option string to our select tag. */
268 $result .= $new_option;
270 /* Close the select tag and return our happy result. */
271 $result .= "</select>\n";
276 function createWidget_TextArea() {
277 switch ($this->size
) {
278 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
279 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
280 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
281 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
282 case SMOPT_SIZE_NORMAL
:
283 default: $rows = 5; $cols = 50;
285 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
286 . "cols=\"$cols\" $this->script>"
287 . htmlspecialchars($this->value
) . "</textarea>\n";
291 function createWidget_Integer() {
293 global $javascript_on;
295 // add onChange javascript handler to a regular string widget
296 // which will strip out all non-numeric chars
298 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
299 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
300 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
301 . 'this.value=newVal;" />', $this->createWidget_String());
303 return $this->createWidget_String();
306 function createWidget_Float() {
308 global $javascript_on;
310 // add onChange javascript handler to a regular string widget
311 // which will strip out all non-numeric (period also OK) chars
313 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
314 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
315 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
316 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
317 , $this->createWidget_String());
319 return $this->createWidget_String();
322 function createWidget_Boolean() {
323 /* Do the whole current value thing. */
324 if ($this->value
!= SMPREF_NO
) {
325 $yes_chk = ' checked=""';
329 $no_chk = ' checked=""';
332 /* Build the yes choice. */
333 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
334 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
335 . $yes_chk . ' ' . $this->script
. ' /> '
336 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
338 /* Build the no choice. */
339 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
340 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
341 . $no_chk . ' ' . $this->script
. ' /> '
342 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
344 /* Build and return the combined "boolean widget". */
345 $result = "$yes_option $no_option";
349 function createWidget_Hidden() {
350 $result = '<input type="hidden" name="new_' . $this->name
351 . '" value="' . htmlspecialchars($this->value
)
352 . '" ' . $this->script
. ' />';
356 function createWidget_Comment() {
357 $result = $this->comment
;
362 $function = $this->save_function
;
367 return ($this->value
!= $this->new_value
);
371 function save_option($option) {
372 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
373 /* Can't save the pref if we don't have the username */
377 setPref($data_dir, $username, $option->name
, $option->new_value
);
380 function save_option_noop($option) {
381 /* Do nothing here... */
384 function create_optpage_element($optpage) {
385 return create_hidden_element('optpage', $optpage);
388 function create_optmode_element($optmode) {
389 return create_hidden_element('optmode', $optmode);
392 function create_hidden_element($name, $value) {
393 $result = '<input type="hidden" '
394 . 'name="' . $name . '" '
395 . 'value="' . htmlspecialchars($value) . '" />';
399 function create_option_groups($optgrps, $optvals) {
400 /* Build a simple array with which to start. */
403 /* Create option group for each option group name. */
404 foreach ($optgrps as $grpkey => $grpname) {
405 $result[$grpkey] = array();
406 $result[$grpkey]['name'] = $grpname;
407 $result[$grpkey]['options'] = array();
410 /* Create a new SquirrelOption for each set of option values. */
411 foreach ($optvals as $grpkey => $grpopts) {
412 foreach ($grpopts as $optset) {
413 if (isset($optset['posvals'])) {
414 /* Create a new option with all values given. */
415 $next_option = new SquirrelOption(
419 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
420 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
424 /* Create a new option with all but possible values given. */
425 $next_option = new SquirrelOption(
429 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
430 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
434 /* If provided, set the size for this option. */
435 if (isset($optset['size'])) {
436 $next_option->setSize($optset['size']);
439 /* If provided, set the comment for this option. */
440 if (isset($optset['comment'])) {
441 $next_option->setComment($optset['comment']);
444 /* If provided, set the save function for this option. */
445 if (isset($optset['save'])) {
446 $next_option->setSaveFunction($optset['save']);
449 /* If provided, set the script for this option. */
450 if (isset($optset['script'])) {
451 $next_option->setScript($optset['script']);
454 /* If provided, set the "post script" for this option. */
455 if (isset($optset['post_script'])) {
456 $next_option->setPostScript($optset['post_script']);
459 /* Add this option to the option array. */
460 $result[$grpkey]['options'][] = $next_option;
464 /* Return our resulting array. */
468 function print_option_groups($option_groups) {
469 /* Print each option group. */
470 foreach ($option_groups as $next_optgrp) {
471 /* If it is not blank, print the name for this option group. */
472 if ($next_optgrp['name'] != '') {
473 echo html_tag( 'tr', "\n".
475 '<b>' . $next_optgrp['name'] . '</b>' ,
476 'center' ,'', 'valign="middle" colspan="2" nowrap' )
480 /* Print each option in this option group. */
481 foreach ($next_optgrp['options'] as $option) {
482 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
483 echo html_tag( 'tr', "\n".
484 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
485 html_tag( 'td', $option->createHTMLWidget(), 'left' )
488 echo $option->createHTMLWidget();
492 /* Print an empty row after this option group. */
494 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
499 function OptionSubmit( $name ) {
501 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '"> ', 'right', '', 'colspan="2"' )