b516e45d81b113ae2fe17ef4b83b754ad2edfccf
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.
57 class SquirrelOption
{
58 /* The basic stuff. */
68 /* The name of the Save Function for this option. */
71 /* The various 'values' for this options. */
76 function SquirrelOption
77 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '') {
78 /* Set the basic stuff. */
80 $this->caption
= $caption;
82 $this->refresh_level
= $refresh_level;
83 $this->possible_values
= $possible_values;
84 $this->size
= SMOPT_SIZE_MEDIUM
;
87 $this->post_script
= '';
89 /* Check for a current value. */
90 if (!empty($initial_value)) {
91 $this->value
= $initial_value;
92 } else if (isset($GLOBALS[$name])) {
93 $this->value
= $GLOBALS[$name];
98 /* Check for a new value. */
99 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
100 $this->new_value
= '';
103 /* Set the default save function. */
104 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
105 $this->save_function
= SMOPT_SAVE_DEFAULT
;
107 $this->save_function
= SMOPT_SAVE_NOOP
;
111 /* Set the value for this option. */
112 function setValue($value) {
113 $this->value
= $value;
116 /* Set the new value for this option. */
117 function setNewValue($new_value) {
118 $this->new_value
= $new_value;
121 /* Set the size for this option. */
122 function setSize($size) {
126 /* Set the comment for this option. */
127 function setComment($comment) {
128 $this->comment
= $comment;
131 /* Set the script for this option. */
132 function setScript($script) {
133 $this->script
= $script;
136 /* Set the "post script" for this option. */
137 function setPostScript($post_script) {
138 $this->post_script
= $post_script;
141 /* Set the save function for this option. */
142 function setSaveFunction($save_function) {
143 $this->save_function
= $save_function;
146 function createHTMLWidget() {
147 global $javascript_on;
149 /* Get the widget for this option type. */
150 switch ($this->type
) {
151 case SMOPT_TYPE_STRING
:
152 $result = $this->createWidget_String();
154 case SMOPT_TYPE_STRLIST
:
155 $result = $this->createWidget_StrList();
157 case SMOPT_TYPE_TEXTAREA
:
158 $result = $this->createWidget_TextArea();
160 case SMOPT_TYPE_INTEGER
:
161 $result = $this->createWidget_Integer();
163 case SMOPT_TYPE_FLOAT
:
164 $result = $this->createWidget_Float();
166 case SMOPT_TYPE_BOOLEAN
:
167 $result = $this->createWidget_Boolean();
169 case SMOPT_TYPE_HIDDEN
:
170 $result = $this->createWidget_Hidden();
172 case SMOPT_TYPE_COMMENT
:
173 $result = $this->createWidget_Comment();
175 case SMOPT_TYPE_FLDRLIST
:
176 $result = $this->createWidget_FolderList();
179 $result = '<font color="' . $color[2] . '">'
180 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
184 /* Add the "post script" for this option. */
185 $result .= $this->post_script
;
187 /* Now, return the created widget. */
191 function createWidget_String() {
192 switch ($this->size
) {
193 case SMOPT_SIZE_TINY
:
196 case SMOPT_SIZE_SMALL
:
199 case SMOPT_SIZE_LARGE
:
202 case SMOPT_SIZE_HUGE
:
205 case SMOPT_SIZE_NORMAL
:
210 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\" $this->script>";
214 function createWidget_StrList() {
215 /* Begin the select tag. */
216 $result = "<select name=\"new_$this->name\" $this->script>";
218 /* Add each possible value to the select list. */
219 foreach ($this->possible_values
as $real_value => $disp_value) {
220 /* Start the next new option string. */
221 $new_option = "<option value=\"$real_value\"";
223 /* If this value is the current value, select it. */
224 if ($real_value == $this->value
) {
225 $new_option .= ' selected';
228 /* Add the display value to our option string. */
229 $new_option .= ">$disp_value</option>";
231 /* And add the new option string to our select tag. */
232 $result .= $new_option;
235 /* Close the select tag and return our happy result. */
236 $result .= '</select>';
240 function createWidget_FolderList() {
241 $selected = array(strtolower($this->value
));
243 /* Begin the select tag. */
244 $result = "<select name=\"new_$this->name\" $this->script>";
246 /* Add each possible value to the select list. */
247 foreach ($this->possible_values
as $real_value => $disp_value) {
248 if ( is_array($disp_value) ) {
249 /* For folder list, we passed in the array of boxes.. */
250 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
252 /* Start the next new option string. */
253 $new_option = "<option value=\"$real_value\"";
255 /* If this value is the current value, select it. */
256 if ($real_value == $this->value
) {
257 $new_option .= ' selected';
260 /* Add the display value to our option string. */
261 $new_option .= ">$disp_value</option>";
263 /* And add the new option string to our select tag. */
264 $result .= $new_option;
266 /* Close the select tag and return our happy result. */
267 $result .= '</select>';
272 function createWidget_TextArea() {
273 switch ($this->size
) {
274 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
275 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
276 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
277 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
278 case SMOPT_SIZE_NORMAL
:
279 default: $rows = 5; $cols = 50;
281 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
282 . "cols=\"$cols\" $this->script>$this->value</textarea>";
286 function createWidget_Integer() {
288 global $javascript_on;
290 // add onChange javascript handler to a regular string widget
291 // which will strip out all non-numeric chars
293 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
294 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
295 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
296 . 'this.value=newVal;">', $this->createWidget_String());
298 return $this->createWidget_String();
301 function createWidget_Float() {
303 global $javascript_on;
305 // add onChange javascript handler to a regular string widget
306 // which will strip out all non-numeric (period also OK) chars
308 return preg_replace('/>/', ' onChange="origVal=this.value; newVal=\'\'; '
309 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
310 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
311 . 'newVal += origVal.charAt(i); } this.value=newVal;">'
312 , $this->createWidget_String());
314 return $this->createWidget_String();
317 function createWidget_Boolean() {
318 /* Do the whole current value thing. */
319 if ($this->value
!= SMPREF_NO
) {
320 $yes_chk = ' checked';
324 $no_chk = ' checked';
327 /* Build the yes choice. */
328 $yes_option = '<input type="radio" name="new_' . $this->name
329 . '" value="' . SMPREF_YES
. "\"$yes_chk $this->script> "
332 /* Build the no choice. */
333 $no_option = '<input type="radio" name="new_' . $this->name
334 . '" value="' . SMPREF_NO
. "\"$no_chk $this->script> "
337 /* Build and return the combined "boolean widget". */
338 $result = "$yes_option $no_option";
342 function createWidget_Hidden() {
343 $result = '<input type="hidden" name="new_' . $this->name
344 . '" value="' . $this->value
. '" ' . $this->script
. '>';
348 function createWidget_Comment() {
349 $result = $this->comment
;
354 $function = $this->save_function
;
359 return ($this->value
!= $this->new_value
);
363 function save_option($option) {
364 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
365 /* Can't save the pref if we don't have the username */
369 setPref($data_dir, $username, $option->name
, $option->new_value
);
372 function save_option_noop($option) {
373 /* Do nothing here... */
376 function create_optpage_element($optpage) {
377 return create_hidden_element('optpage', $optpage);
380 function create_optmode_element($optmode) {
381 return create_hidden_element('optmode', $optmode);
384 function create_hidden_element($name, $value) {
385 $result = '<input type="hidden" '
386 . 'name="' . $name . '" '
387 . 'value="' . $value . '">';
391 function create_option_groups($optgrps, $optvals) {
392 /* Build a simple array with which to start. */
395 /* Create option group for each option group name. */
396 foreach ($optgrps as $grpkey => $grpname) {
397 $result[$grpkey] = array();
398 $result[$grpkey]['name'] = $grpname;
399 $result[$grpkey]['options'] = array();
402 /* Create a new SquirrelOption for each set of option values. */
403 foreach ($optvals as $grpkey => $grpopts) {
404 foreach ($grpopts as $optset) {
405 if (isset($optset['posvals'])) {
406 /* Create a new option with all values given. */
407 $next_option = new SquirrelOption(
412 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
416 /* Create a new option with all but possible values given. */
417 $next_option = new SquirrelOption(
422 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
426 /* If provided, set the size for this option. */
427 if (isset($optset['size'])) {
428 $next_option->setSize($optset['size']);
431 /* If provided, set the comment for this option. */
432 if (isset($optset['comment'])) {
433 $next_option->setComment($optset['comment']);
436 /* If provided, set the save function for this option. */
437 if (isset($optset['save'])) {
438 $next_option->setSaveFunction($optset['save']);
441 /* If provided, set the script for this option. */
442 if (isset($optset['script'])) {
443 $next_option->setScript($optset['script']);
446 /* If provided, set the "post script" for this option. */
447 if (isset($optset['post_script'])) {
448 $next_option->setPostScript($optset['post_script']);
451 /* Add this option to the option array. */
452 $result[$grpkey]['options'][] = $next_option;
456 /* Return our resulting array. */
460 function print_option_groups($option_groups) {
461 /* Print each option group. */
462 foreach ($option_groups as $next_optgrp) {
463 /* If it is not blank, print the name for this option group. */
464 if ($next_optgrp['name'] != '') {
465 echo html_tag( 'tr', "\n".
467 '<b>' . $next_optgrp['name'] . '</b>' ,
468 'center' ,'', 'valign="middle" colspan="2" nowrap' )
472 /* Print each option in this option group. */
473 foreach ($next_optgrp['options'] as $option) {
474 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
475 echo html_tag( 'tr', "\n".
476 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
477 html_tag( 'td', $option->createHTMLWidget(), 'left' )
480 echo $option->createHTMLWidget();
484 /* Print an empty row after this option group. */
486 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
491 function OptionSubmit( $name ) {
493 html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) .
494 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )