7fadcbc6165a23f05cbd16d95928765fa1820ed1
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 /* Get the widget for this option type. */
158 switch ($this->type
) {
159 case SMOPT_TYPE_STRING
:
160 $result = $this->createWidget_String();
162 case SMOPT_TYPE_STRLIST
:
163 $result = $this->createWidget_StrList();
165 case SMOPT_TYPE_TEXTAREA
:
166 $result = $this->createWidget_TextArea();
168 case SMOPT_TYPE_INTEGER
:
169 $result = $this->createWidget_Integer();
171 case SMOPT_TYPE_FLOAT
:
172 $result = $this->createWidget_Float();
174 case SMOPT_TYPE_BOOLEAN
:
175 $result = $this->createWidget_Boolean();
177 case SMOPT_TYPE_HIDDEN
:
178 $result = $this->createWidget_Hidden();
180 case SMOPT_TYPE_COMMENT
:
181 $result = $this->createWidget_Comment();
183 case SMOPT_TYPE_FLDRLIST
:
184 $result = $this->createWidget_FolderList();
187 $result = '<font color="' . $color[2] . '">'
188 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
192 /* Add the "post script" for this option. */
193 $result .= $this->post_script
;
195 /* Now, return the created widget. */
199 function createWidget_String() {
200 switch ($this->size
) {
201 case SMOPT_SIZE_TINY
:
204 case SMOPT_SIZE_SMALL
:
207 case SMOPT_SIZE_LARGE
:
210 case SMOPT_SIZE_HUGE
:
213 case SMOPT_SIZE_NORMAL
:
218 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
219 htmlspecialchars($this->value
) .
220 "\" size=\"$width\" $this->script />$this->trailing_text\n";
224 function createWidget_StrList() {
225 /* Begin the select tag. */
226 $result = "<select name=\"new_$this->name\" $this->script>\n";
228 /* Add each possible value to the select list. */
229 foreach ($this->possible_values
as $real_value => $disp_value) {
230 /* Start the next new option string. */
231 $new_option = '<option value="' .
232 htmlspecialchars($real_value) . '"';
234 /* If this value is the current value, select it. */
235 if ($real_value == $this->value
) {
236 $new_option .= ' selected="selected"';
239 /* Add the display value to our option string. */
240 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
242 /* And add the new option string to our select tag. */
243 $result .= $new_option;
246 /* Close the select tag and return our happy result. */
247 $result .= "</select>$this->trailing_text\n";
251 function createWidget_FolderList() {
252 $selected = array(strtolower($this->value
));
254 /* Begin the select tag. */
255 $result = "<select name=\"new_$this->name\" $this->script>\n";
257 /* Add each possible value to the select list. */
258 foreach ($this->possible_values
as $real_value => $disp_value) {
259 if ( is_array($disp_value) ) {
260 /* For folder list, we passed in the array of boxes.. */
261 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
263 /* Start the next new option string. */
264 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
266 /* If this value is the current value, select it. */
267 if ($real_value == $this->value
) {
268 $new_option .= ' selected="selected"';
271 /* Add the display value to our option string. */
272 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
274 /* And add the new option string to our select tag. */
275 $result .= $new_option;
277 /* Close the select tag and return our happy result. */
278 $result .= "</select>\n";
283 function createWidget_TextArea() {
284 switch ($this->size
) {
285 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
286 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
287 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
288 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
289 case SMOPT_SIZE_NORMAL
:
290 default: $rows = 5; $cols = 50;
292 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
293 . "cols=\"$cols\" $this->script>"
294 . htmlspecialchars($this->value
) . "</textarea>\n";
298 function createWidget_Integer() {
300 global $javascript_on;
302 // add onChange javascript handler to a regular string widget
303 // which will strip out all non-numeric chars
305 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
306 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
307 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
308 . 'this.value=newVal;" />', $this->createWidget_String());
310 return $this->createWidget_String();
313 function createWidget_Float() {
315 global $javascript_on;
317 // add onChange javascript handler to a regular string widget
318 // which will strip out all non-numeric (period also OK) chars
320 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
321 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
322 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
323 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
324 , $this->createWidget_String());
326 return $this->createWidget_String();
329 function createWidget_Boolean() {
330 /* Do the whole current value thing. */
331 if ($this->value
!= SMPREF_NO
) {
332 $yes_chk = ' checked=""';
336 $no_chk = ' checked=""';
339 /* Build the yes choice. */
340 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
341 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
342 . $yes_chk . ' ' . $this->script
. ' /> '
343 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
345 /* Build the no choice. */
346 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
347 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
348 . $no_chk . ' ' . $this->script
. ' /> '
349 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
351 /* Build and return the combined "boolean widget". */
352 $result = "$yes_option $no_option";
356 function createWidget_Hidden() {
357 $result = '<input type="hidden" name="new_' . $this->name
358 . '" value="' . htmlspecialchars($this->value
)
359 . '" ' . $this->script
. ' />';
363 function createWidget_Comment() {
364 $result = $this->comment
;
369 $function = $this->save_function
;
374 return ($this->value
!= $this->new_value
);
378 function save_option($option) {
379 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
380 /* Can't save the pref if we don't have the username */
384 setPref($data_dir, $username, $option->name
, $option->new_value
);
387 function save_option_noop($option) {
388 /* Do nothing here... */
391 function create_optpage_element($optpage) {
392 return create_hidden_element('optpage', $optpage);
395 function create_optmode_element($optmode) {
396 return create_hidden_element('optmode', $optmode);
399 function create_hidden_element($name, $value) {
400 $result = '<input type="hidden" '
401 . 'name="' . $name . '" '
402 . 'value="' . htmlspecialchars($value) . '" />';
406 function create_option_groups($optgrps, $optvals) {
407 /* Build a simple array with which to start. */
410 /* Create option group for each option group name. */
411 foreach ($optgrps as $grpkey => $grpname) {
412 $result[$grpkey] = array();
413 $result[$grpkey]['name'] = $grpname;
414 $result[$grpkey]['options'] = array();
417 /* Create a new SquirrelOption for each set of option values. */
418 foreach ($optvals as $grpkey => $grpopts) {
419 foreach ($grpopts as $optset) {
420 if (isset($optset['posvals'])) {
421 /* Create a new option with all values given. */
422 $next_option = new SquirrelOption(
426 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
427 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
431 /* Create a new option with all but possible values given. */
432 $next_option = new SquirrelOption(
436 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
437 (isset($optset['initial_value']) ?
$optset['initial_value'] : '')
441 /* If provided, set the size for this option. */
442 if (isset($optset['size'])) {
443 $next_option->setSize($optset['size']);
446 /* If provided, set the trailing_text for this option. */
447 if (isset($optset['trailing_text'])) {
448 $next_option->setTrailingText($optset['trailing_text']);
451 /* If provided, set the comment for this option. */
452 if (isset($optset['comment'])) {
453 $next_option->setComment($optset['comment']);
456 /* If provided, set the save function for this option. */
457 if (isset($optset['save'])) {
458 $next_option->setSaveFunction($optset['save']);
461 /* If provided, set the script for this option. */
462 if (isset($optset['script'])) {
463 $next_option->setScript($optset['script']);
466 /* If provided, set the "post script" for this option. */
467 if (isset($optset['post_script'])) {
468 $next_option->setPostScript($optset['post_script']);
471 /* Add this option to the option array. */
472 $result[$grpkey]['options'][] = $next_option;
476 /* Return our resulting array. */
480 function print_option_groups($option_groups) {
481 /* Print each option group. */
482 foreach ($option_groups as $next_optgrp) {
483 /* If it is not blank, print the name for this option group. */
484 if ($next_optgrp['name'] != '') {
485 echo html_tag( 'tr', "\n".
487 '<b>' . $next_optgrp['name'] . '</b>' ,
488 'center' ,'', 'valign="middle" colspan="2" nowrap' )
492 /* Print each option in this option group. */
493 foreach ($next_optgrp['options'] as $option) {
494 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
495 echo html_tag( 'tr', "\n".
496 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
497 html_tag( 'td', $option->createHTMLWidget(), 'left' )
500 echo $option->createHTMLWidget();
504 /* Print an empty row after this option group. */
506 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
511 function OptionSubmit( $name ) {
513 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '"> ', 'right', '', 'colspan="2"' )