8416b5f6e7e935c465c01faee70bfbb6855618d8
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. */
77 var $htmlencoded=false;
79 function SquirrelOption
80 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
81 /* Set the basic stuff. */
83 $this->caption
= $caption;
85 $this->refresh_level
= $refresh_level;
86 $this->possible_values
= $possible_values;
87 $this->htmlencoded
= $htmlencoded;
88 $this->size
= SMOPT_SIZE_MEDIUM
;
89 $this->trailing_text
= '';
92 $this->post_script
= '';
94 /* Check for a current value. */
95 if (!empty($initial_value)) {
96 $this->value
= $initial_value;
97 } else if (isset($GLOBALS[$name])) {
98 $this->value
= $GLOBALS[$name];
103 /* Check for a new value. */
104 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
105 $this->new_value
= '';
108 /* Set the default save function. */
109 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
110 $this->save_function
= SMOPT_SAVE_DEFAULT
;
112 $this->save_function
= SMOPT_SAVE_NOOP
;
116 /* Set the value for this option. */
117 function setValue($value) {
118 $this->value
= $value;
121 /* Set the new value for this option. */
122 function setNewValue($new_value) {
123 $this->new_value
= $new_value;
126 /* Set the size for this option. */
127 function setSize($size) {
131 /* Set the trailing_text for this option. */
132 function setTrailingText($trailing_text) {
133 $this->trailing_text
= $trailing_text;
136 /* Set the comment for this option. */
137 function setComment($comment) {
138 $this->comment
= $comment;
141 /* Set the script for this option. */
142 function setScript($script) {
143 $this->script
= $script;
146 /* Set the "post script" for this option. */
147 function setPostScript($post_script) {
148 $this->post_script
= $post_script;
151 /* Set the save function for this option. */
152 function setSaveFunction($save_function) {
153 $this->save_function
= $save_function;
156 function createHTMLWidget() {
159 // Use new value if available
160 if (!empty($this->new_value
)) {
161 $tempValue = $this->value
;
162 $this->value
= $this->new_value
;
165 /* Get the widget for this option type. */
166 switch ($this->type
) {
167 case SMOPT_TYPE_STRING
:
168 $result = $this->createWidget_String();
170 case SMOPT_TYPE_STRLIST
:
171 $result = $this->createWidget_StrList();
173 case SMOPT_TYPE_TEXTAREA
:
174 $result = $this->createWidget_TextArea();
176 case SMOPT_TYPE_INTEGER
:
177 $result = $this->createWidget_Integer();
179 case SMOPT_TYPE_FLOAT
:
180 $result = $this->createWidget_Float();
182 case SMOPT_TYPE_BOOLEAN
:
183 $result = $this->createWidget_Boolean();
185 case SMOPT_TYPE_HIDDEN
:
186 $result = $this->createWidget_Hidden();
188 case SMOPT_TYPE_COMMENT
:
189 $result = $this->createWidget_Comment();
191 case SMOPT_TYPE_FLDRLIST
:
192 $result = $this->createWidget_FolderList();
195 $result = '<font color="' . $color[2] . '">'
196 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
200 /* Add the "post script" for this option. */
201 $result .= $this->post_script
;
203 // put correct value back if need be
204 if (!empty($this->new_value
)) {
205 $this->value
= $tempValue;
208 /* Now, return the created widget. */
212 function createWidget_String() {
213 switch ($this->size
) {
214 case SMOPT_SIZE_TINY
:
217 case SMOPT_SIZE_SMALL
:
220 case SMOPT_SIZE_LARGE
:
223 case SMOPT_SIZE_HUGE
:
226 case SMOPT_SIZE_NORMAL
:
231 $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
232 htmlspecialchars($this->value
) .
233 "\" size=\"$width\" $this->script />$this->trailing_text\n";
237 function createWidget_StrList() {
238 /* Begin the select tag. */
239 $result = "<select name=\"new_$this->name\" $this->script>\n";
241 /* Add each possible value to the select list. */
242 foreach ($this->possible_values
as $real_value => $disp_value) {
243 /* Start the next new option string. */
244 $new_option = '<option value="' .
245 ($this->htmlencoded ?
$real_value : htmlspecialchars($real_value)) . '"';
247 /* If this value is the current value, select it. */
248 if ($real_value == $this->value
) {
249 $new_option .= ' selected="selected"';
252 /* Add the display value to our option string. */
253 $new_option .= '>' . ($this->htmlencoded ?
$disp_value : htmlspecialchars($disp_value)) . "</option>\n";
255 /* And add the new option string to our select tag. */
256 $result .= $new_option;
259 /* Close the select tag and return our happy result. */
260 $result .= "</select>$this->trailing_text\n";
264 function createWidget_FolderList() {
265 $selected = array(strtolower($this->value
));
267 /* Begin the select tag. */
268 $result = "<select name=\"new_$this->name\" $this->script>\n";
270 /* Add each possible value to the select list. */
271 foreach ($this->possible_values
as $real_value => $disp_value) {
272 if ( is_array($disp_value) ) {
273 /* For folder list, we passed in the array of boxes.. */
274 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
276 /* Start the next new option string. */
277 $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
279 /* If this value is the current value, select it. */
280 if ($real_value == $this->value
) {
281 $new_option .= ' selected="selected"';
284 /* Add the display value to our option string. */
285 $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
287 /* And add the new option string to our select tag. */
288 $result .= $new_option;
290 /* Close the select tag and return our happy result. */
291 $result .= "</select>\n";
296 function createWidget_TextArea() {
297 switch ($this->size
) {
298 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
299 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
300 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
301 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
302 case SMOPT_SIZE_NORMAL
:
303 default: $rows = 5; $cols = 50;
305 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
306 . "cols=\"$cols\" $this->script>"
307 . htmlspecialchars($this->value
) . "</textarea>\n";
311 function createWidget_Integer() {
313 global $javascript_on;
315 // add onChange javascript handler to a regular string widget
316 // which will strip out all non-numeric chars
318 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
319 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
320 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
321 . 'this.value=newVal;" />', $this->createWidget_String());
323 return $this->createWidget_String();
326 function createWidget_Float() {
328 global $javascript_on;
330 // add onChange javascript handler to a regular string widget
331 // which will strip out all non-numeric (period also OK) chars
333 return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
334 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
335 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
336 . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
337 , $this->createWidget_String());
339 return $this->createWidget_String();
342 function createWidget_Boolean() {
343 /* Do the whole current value thing. */
344 if ($this->value
!= SMPREF_NO
) {
345 $yes_chk = ' checked="checked"';
349 $no_chk = ' checked="checked"';
352 /* Build the yes choice. */
353 $yes_option = '<input type="radio" id="new_' . $this->name
. '_yes" '
354 . 'name="new_' . $this->name
. '" value="' . SMPREF_YES
. '"'
355 . $yes_chk . ' ' . $this->script
. ' /> '
356 . '<label for="new_'.$this->name
.'_yes">' . _("Yes") . '</label>';
358 /* Build the no choice. */
359 $no_option = '<input type="radio" id="new_' . $this->name
. '_no" '
360 . 'name="new_' . $this->name
. '" value="' . SMPREF_NO
. '"'
361 . $no_chk . ' ' . $this->script
. ' /> '
362 . '<label for="new_'.$this->name
.'_no">' . _("No") . '</label>';
364 /* Build and return the combined "boolean widget". */
365 $result = "$yes_option $no_option";
369 function createWidget_Hidden() {
370 $result = '<input type="hidden" name="new_' . $this->name
371 . '" value="' . htmlspecialchars($this->value
)
372 . '" ' . $this->script
. ' />';
376 function createWidget_Comment() {
377 $result = $this->comment
;
382 $function = $this->save_function
;
387 return ($this->value
!= $this->new_value
);
391 function save_option($option) {
392 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
393 /* Can't save the pref if we don't have the username */
397 setPref($data_dir, $username, $option->name
, $option->new_value
);
400 function save_option_noop($option) {
401 /* Do nothing here... */
404 function create_optpage_element($optpage) {
405 return create_hidden_element('optpage', $optpage);
408 function create_optmode_element($optmode) {
409 return create_hidden_element('optmode', $optmode);
412 function create_hidden_element($name, $value) {
413 $result = '<input type="hidden" '
414 . 'name="' . $name . '" '
415 . 'value="' . htmlspecialchars($value) . '" />';
419 function create_option_groups($optgrps, $optvals) {
420 /* Build a simple array with which to start. */
423 /* Create option group for each option group name. */
424 foreach ($optgrps as $grpkey => $grpname) {
425 $result[$grpkey] = array();
426 $result[$grpkey]['name'] = $grpname;
427 $result[$grpkey]['options'] = array();
430 /* Create a new SquirrelOption for each set of option values. */
431 foreach ($optvals as $grpkey => $grpopts) {
432 foreach ($grpopts as $optset) {
433 /* Create a new option with all values given. */
434 $next_option = new SquirrelOption(
438 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
439 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
440 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
441 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
444 /* If provided, set the size for this option. */
445 if (isset($optset['size'])) {
446 $next_option->setSize($optset['size']);
449 /* If provided, set the trailing_text for this option. */
450 if (isset($optset['trailing_text'])) {
451 $next_option->setTrailingText($optset['trailing_text']);
454 /* If provided, set the comment for this option. */
455 if (isset($optset['comment'])) {
456 $next_option->setComment($optset['comment']);
459 /* If provided, set the save function for this option. */
460 if (isset($optset['save'])) {
461 $next_option->setSaveFunction($optset['save']);
464 /* If provided, set the script for this option. */
465 if (isset($optset['script'])) {
466 $next_option->setScript($optset['script']);
469 /* If provided, set the "post script" for this option. */
470 if (isset($optset['post_script'])) {
471 $next_option->setPostScript($optset['post_script']);
474 /* Add this option to the option array. */
475 $result[$grpkey]['options'][] = $next_option;
479 /* Return our resulting array. */
483 function print_option_groups($option_groups) {
484 /* Print each option group. */
485 foreach ($option_groups as $next_optgrp) {
486 /* If it is not blank, print the name for this option group. */
487 if ($next_optgrp['name'] != '') {
488 echo html_tag( 'tr', "\n".
490 '<b>' . $next_optgrp['name'] . '</b>' ,
491 'center' ,'', 'valign="middle" colspan="2" nowrap' )
495 /* Print each option in this option group. */
496 foreach ($next_optgrp['options'] as $option) {
497 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
498 echo html_tag( 'tr', "\n".
499 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
500 html_tag( 'td', $option->createHTMLWidget(), 'left' )
503 echo $option->createHTMLWidget();
507 /* Print an empty row after this option group. */
509 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
514 function OptionSubmit( $name ) {
516 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' )