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. */
70 /* The name of the Save Function for this option. */
73 /* The various 'values' for this options. */
78 function SquirrelOption
79 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
80 /* Set the basic stuff. */
82 $this->caption
= $caption;
84 $this->refresh_level
= $refresh_level;
85 $this->possible_values
= $possible_values;
86 $this->size
= SMOPT_SIZE_MEDIUM
;
87 $this->trailing_text
= '';
90 $this->post_script
= '';
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];
101 /* Check for a new value. */
102 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
103 $this->new_value
= '';
106 /* Set the default save function. */
107 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
108 $this->save_function
= SMOPT_SAVE_DEFAULT
;
110 $this->save_function
= SMOPT_SAVE_NOOP
;
114 /* Set the value for this option. */
115 function setValue($value) {
116 $this->value
= $value;
119 /* Set the new value for this option. */
120 function setNewValue($new_value) {
121 $this->new_value
= $new_value;
124 /* Set the size for this option. */
125 function setSize($size) {
129 /* Set the trailing_text for this option. */
130 function setTrailingText($trailing_text) {
131 $this->trailing_text
= $trailing_text;
134 /* Set the comment for this option. */
135 function setComment($comment) {
136 $this->comment
= $comment;
139 /* Set the script for this option. */
140 function setScript($script) {
141 $this->script
= $script;
144 /* Set the "post script" for this option. */
145 function setPostScript($post_script) {
146 $this->post_script
= $post_script;
149 /* Set the save function for this option. */
150 function setSaveFunction($save_function) {
151 $this->save_function
= $save_function;
154 function createHTMLWidget() {
155 global $javascript_on;
157 // Use new value if available
158 if (!empty($this->new_value
)) {
159 $tempValue = $this->value
;
160 $this->value
= $this->new_value
;
163 /* Get the widget for this option type. */
164 switch ($this->type
) {
165 case SMOPT_TYPE_STRING
:
166 $result = $this->createWidget_String();
168 case SMOPT_TYPE_STRLIST
:
169 $result = $this->createWidget_StrList();
171 case SMOPT_TYPE_TEXTAREA
:
172 $result = $this->createWidget_TextArea();
174 case SMOPT_TYPE_INTEGER
:
175 $result = $this->createWidget_Integer();
177 case SMOPT_TYPE_FLOAT
:
178 $result = $this->createWidget_Float();
180 case SMOPT_TYPE_BOOLEAN
:
181 $result = $this->createWidget_Boolean();
183 case SMOPT_TYPE_HIDDEN
:
184 $result = $this->createWidget_Hidden();
186 case SMOPT_TYPE_COMMENT
:
187 $result = $this->createWidget_Comment();
189 case SMOPT_TYPE_FLDRLIST
:
190 $result = $this->createWidget_FolderList();
193 $result = '<font color="' . $color[2] . '">'
194 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
198 /* Add the "post script" for this option. */
199 $result .= $this->post_script
;
201 // put correct value back if need be
202 if (!empty($this->new_value
)) {
203 $this->value
= $tempValue;
206 /* Now, return the created widget. */
210 function createWidget_String() {
211 switch ($this->size
) {
212 case SMOPT_SIZE_TINY
:
215 case SMOPT_SIZE_SMALL
:
218 case SMOPT_SIZE_LARGE
:
221 case SMOPT_SIZE_HUGE
:
224 case SMOPT_SIZE_NORMAL
:
229 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
230 htmlspecialchars($this->value
) .
231 "\" size=\"$width\" $this->script />$this->trailing_text\n";
235 function createWidget_StrList() {
236 /* Begin the select tag. */
237 $result = "<select name=\"new_$this->name\" $this->script>\n";
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) . '"';
245 /* If this value is the current value, select it. */
246 if ($real_value == $this->value
) {
247 $new_option .= ' selected="selected"';
250 /* Add the display value to our option string. */
251 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
253 /* And add the new option string to our select tag. */
254 $result .= $new_option;
257 /* Close the select tag and return our happy result. */
258 $result .= "</select>$this->trailing_text\n";
262 function createWidget_FolderList() {
263 $selected = array(strtolower($this->value
));
265 /* Begin the select tag. */
266 $result = "<select name=\"new_$this->name\" $this->script>\n";
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);
274 /* Start the next new option string. */
275 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
277 /* If this value is the current value, select it. */
278 if ($real_value == $this->value
) {
279 $new_option .= ' selected="selected"';
282 /* Add the display value to our option string. */
283 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
285 /* And add the new option string to our select tag. */
286 $result .= $new_option;
288 /* Close the select tag and return our happy result. */
289 $result .= "</select>\n";
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;
303 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
304 . "cols=\"$cols\" $this->script>"
305 . htmlspecialchars($this->value
) . "</textarea>\n";
309 function createWidget_Integer() {
311 global $javascript_on;
313 // add onChange javascript handler to a regular string widget
314 // which will strip out all non-numeric chars
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());
321 return $this->createWidget_String();
324 function createWidget_Float() {
326 global $javascript_on;
328 // add onChange javascript handler to a regular string widget
329 // which will strip out all non-numeric (period also OK) chars
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());
337 return $this->createWidget_String();
340 function createWidget_Boolean() {
341 /* Do the whole current value thing. */
342 if ($this->value
!= SMPREF_NO
) {
343 $yes_chk = ' checked=""';
347 $no_chk = ' checked=""';
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
. ' /> '
354 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
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
. ' /> '
360 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
362 /* Build and return the combined "boolean widget". */
363 $result = "$yes_option $no_option";
367 function createWidget_Hidden() {
368 $result = '<input type="hidden" name="new_' . $this->name
369 . '" value="' . htmlspecialchars($this->value
)
370 . '" ' . $this->script
. ' />';
374 function createWidget_Comment() {
375 $result = $this->comment
;
380 $function = $this->save_function
;
385 return ($this->value
!= $this->new_value
);
389 function save_option($option) {
390 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
391 /* Can't save the pref if we don't have the username */
395 setPref($data_dir, $username, $option->name
, $option->new_value
);
398 function save_option_noop($option) {
399 /* Do nothing here... */
402 function create_optpage_element($optpage) {
403 return create_hidden_element('optpage', $optpage);
406 function create_optmode_element($optmode) {
407 return create_hidden_element('optmode', $optmode);
410 function create_hidden_element($name, $value) {
411 $result = '<input type="hidden" '
412 . 'name="' . $name . '" '
413 . 'value="' . htmlspecialchars($value) . '" />';
417 function create_option_groups($optgrps, $optvals) {
418 /* Build a simple array with which to start. */
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();
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(
437 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
438 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
442 /* Create a new option with all but possible values given. */
443 $next_option = new SquirrelOption(
447 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
448 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
452 /* If provided, set the size for this option. */
453 if (isset($optset['size'])) {
454 $next_option->setSize($optset['size']);
457 /* If provided, set the trailing_text for this option. */
458 if (isset($optset['trailing_text'])) {
459 $next_option->setTrailingText($optset['trailing_text']);
462 /* If provided, set the comment for this option. */
463 if (isset($optset['comment'])) {
464 $next_option->setComment($optset['comment']);
467 /* If provided, set the save function for this option. */
468 if (isset($optset['save'])) {
469 $next_option->setSaveFunction($optset['save']);
472 /* If provided, set the script for this option. */
473 if (isset($optset['script'])) {
474 $next_option->setScript($optset['script']);
477 /* If provided, set the "post script" for this option. */
478 if (isset($optset['post_script'])) {
479 $next_option->setPostScript($optset['post_script']);
482 /* Add this option to the option array. */
483 $result[$grpkey]['options'][] = $next_option;
487 /* Return our resulting array. */
491 function 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".
498 '<b>' . $next_optgrp['name'] . '</b>' ,
499 'center' ,'', 'valign="middle" colspan="2" nowrap' )
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' )
511 echo $option->createHTMLWidget();
515 /* Print an empty row after this option group. */
517 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
522 function OptionSubmit( $name ) {
524 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '"> ', 'right', '', 'colspan="2"' )