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.
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 name=\"new_$this->name\" value=\"$this->value\" size=\"$width\" $this->script>";
215 function createWidget_StrList() {
216 /* Begin the select tag. */
217 $result = "<select name=\"new_$this->name\" $this->script>";
219 /* Add each possible value to the select list. */
220 foreach ($this->possible_values
as $real_value => $disp_value) {
221 /* Start the next new option string. */
222 $new_option = "<option value=\"$real_value\"";
224 /* If this value is the current value, select it. */
225 if ($real_value == $this->value
) {
226 $new_option .= ' selected';
229 /* Add the display value to our option string. */
230 $new_option .= ">$disp_value</option>";
232 /* And add the new option string to our select tag. */
233 $result .= $new_option;
236 /* Close the select tag and return our happy result. */
237 $result .= '</select>';
241 function createWidget_FolderList() {
242 $selected = array(strtolower($this->value
));
244 /* Begin the select tag. */
245 $result = "<select name=\"new_$this->name\" $this->script>";
247 /* Add each possible value to the select list. */
248 foreach ($this->possible_values
as $real_value => $disp_value) {
249 if ( is_array($disp_value) ) {
250 /* For folder list, we passed in the array of boxes.. */
251 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
253 /* Start the next new option string. */
254 $new_option = "<option value=\"$real_value\"";
256 /* If this value is the current value, select it. */
257 if ($real_value == $this->value
) {
258 $new_option .= ' selected';
261 /* Add the display value to our option string. */
262 $new_option .= ">$disp_value</option>";
264 /* And add the new option string to our select tag. */
265 $result .= $new_option;
267 /* Close the select tag and return our happy result. */
268 $result .= '</select>';
273 function createWidget_TextArea() {
274 switch ($this->size
) {
275 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
276 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
277 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
278 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
279 case SMOPT_SIZE_NORMAL
:
280 default: $rows = 5; $cols = 50;
282 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
283 . "cols=\"$cols\" $this->script>$this->value</textarea>";
287 function createWidget_Integer() {
289 global $javascript_on;
291 // add onChange javascript handler to a regular string widget
292 // which will strip out all non-numeric chars
294 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
295 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
296 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
297 . 'this.value=newVal;">', $this->createWidget_String());
299 return $this->createWidget_String();
302 function createWidget_Float() {
304 global $javascript_on;
306 // add onChange javascript handler to a regular string widget
307 // which will strip out all non-numeric (period also OK) chars
309 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
310 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
311 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
312 . 'newVal += origVal.charAt(i); } this.value=newVal;">'
313 , $this->createWidget_String());
315 return $this->createWidget_String();
318 function createWidget_Boolean() {
319 /* Do the whole current value thing. */
320 if ($this->value
!= SMPREF_NO
) {
321 $yes_chk = ' checked';
325 $no_chk = ' checked';
328 /* Build the yes choice. */
329 $yes_option = '<input type="radio" name="new_' . $this->name
330 . '" value="' . SMPREF_YES
. "\"$yes_chk $this->script> "
333 /* Build the no choice. */
334 $no_option = '<input type="radio" name="new_' . $this->name
335 . '" value="' . SMPREF_NO
. "\"$no_chk $this->script> "
338 /* Build and return the combined "boolean widget". */
339 $result = "$yes_option $no_option";
343 function createWidget_Hidden() {
344 $result = '<input type="hidden" name="new_' . $this->name
345 . '" value="' . $this->value
. '" ' . $this->script
. '>';
349 function createWidget_Comment() {
350 $result = $this->comment
;
355 $function = $this->save_function
;
360 return ($this->value
!= $this->new_value
);
364 function save_option($option) {
365 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
366 /* Can't save the pref if we don't have the username */
370 setPref($data_dir, $username, $option->name
, $option->new_value
);
373 function save_option_noop($option) {
374 /* Do nothing here... */
377 function create_optpage_element($optpage) {
378 return create_hidden_element('optpage', $optpage);
381 function create_optmode_element($optmode) {
382 return create_hidden_element('optmode', $optmode);
385 function create_hidden_element($name, $value) {
386 $result = '<input type="hidden" '
387 . 'name="' . $name . '" '
388 . 'value="' . $value . '">';
392 function create_option_groups($optgrps, $optvals) {
393 /* Build a simple array with which to start. */
396 /* Create option group for each option group name. */
397 foreach ($optgrps as $grpkey => $grpname) {
398 $result[$grpkey] = array();
399 $result[$grpkey]['name'] = $grpname;
400 $result[$grpkey]['options'] = array();
403 /* Create a new SquirrelOption for each set of option values. */
404 foreach ($optvals as $grpkey => $grpopts) {
405 foreach ($grpopts as $optset) {
406 if (isset($optset['posvals'])) {
407 /* Create a new option with all values given. */
408 $next_option = new SquirrelOption(
413 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
417 /* Create a new option with all but possible values given. */
418 $next_option = new SquirrelOption(
423 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
427 /* If provided, set the size for this option. */
428 if (isset($optset['size'])) {
429 $next_option->setSize($optset['size']);
432 /* If provided, set the comment for this option. */
433 if (isset($optset['comment'])) {
434 $next_option->setComment($optset['comment']);
437 /* If provided, set the save function for this option. */
438 if (isset($optset['save'])) {
439 $next_option->setSaveFunction($optset['save']);
442 /* If provided, set the script for this option. */
443 if (isset($optset['script'])) {
444 $next_option->setScript($optset['script']);
447 /* If provided, set the "post script" for this option. */
448 if (isset($optset['post_script'])) {
449 $next_option->setPostScript($optset['post_script']);
452 /* Add this option to the option array. */
453 $result[$grpkey]['options'][] = $next_option;
457 /* Return our resulting array. */
461 function print_option_groups($option_groups) {
462 /* Print each option group. */
463 foreach ($option_groups as $next_optgrp) {
464 /* If it is not blank, print the name for this option group. */
465 if ($next_optgrp['name'] != '') {
466 echo html_tag( 'tr', "\n".
468 '<b>' . $next_optgrp['name'] . '</b>' ,
469 'center' ,'', 'valign="middle" colspan="2" nowrap' )
473 /* Print each option in this option group. */
474 foreach ($next_optgrp['options'] as $option) {
475 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
476 echo html_tag( 'tr', "\n".
477 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
478 html_tag( 'td', $option->createHTMLWidget(), 'left' )
481 echo $option->createHTMLWidget();
485 /* Print an empty row after this option group. */
487 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
492 function OptionSubmit( $name ) {
494 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '"> ', 'right', '', 'colspan="2"' )