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.
14 /**********************************************/
15 /* Define constants used in the options code. */
16 /**********************************************/
18 /* Define constants for the various option types. */
19 define('SMOPT_TYPE_STRING', 0);
20 define('SMOPT_TYPE_STRLIST', 1);
21 define('SMOPT_TYPE_TEXTAREA', 2);
22 define('SMOPT_TYPE_INTEGER', 3);
23 define('SMOPT_TYPE_FLOAT', 4);
24 define('SMOPT_TYPE_BOOLEAN', 5);
25 define('SMOPT_TYPE_HIDDEN', 6);
26 define('SMOPT_TYPE_COMMENT', 7);
27 define('SMOPT_TYPE_FLDRLIST', 8);
29 /* Define constants for the options refresh levels. */
30 define('SMOPT_REFRESH_NONE', 0);
31 define('SMOPT_REFRESH_FOLDERLIST', 1);
32 define('SMOPT_REFRESH_ALL', 2);
34 /* Define constants for the options size. */
35 define('SMOPT_SIZE_TINY', 0);
36 define('SMOPT_SIZE_SMALL', 1);
37 define('SMOPT_SIZE_MEDIUM', 2);
38 define('SMOPT_SIZE_LARGE', 3);
39 define('SMOPT_SIZE_HUGE', 4);
40 define('SMOPT_SIZE_NORMAL', 5);
42 define('SMOPT_SAVE_DEFAULT', 'save_option');
43 define('SMOPT_SAVE_NOOP', 'save_option_noop');
46 * SquirrelOption: An option for Squirrelmail.
48 * This class is a work in progress. When complete, it will handle
49 * presentation and saving of Squirrelmail user options in a simple,
50 * streamline manner. Stay tuned for more stuff.
52 * Also, I'd like to ask that people leave this alone (mostly :) until
53 * I get it a little further along. That should only be a day or two or
54 * three. I will remove this message when it is ready for primetime usage.
56 class SquirrelOption
{
57 /* The basic stuff. */
66 /* The name of the Save Function for this option. */
69 /* The various 'values' for this options. */
74 function SquirrelOption
75 ($name, $caption, $type, $refresh_level, $possible_values = '') {
76 /* Set the basic stuff. */
78 $this->caption
= $caption;
80 $this->refresh_level
= $refresh_level;
81 $this->possible_values
= $possible_values;
82 $this->size
= SMOPT_SIZE_MEDIUM
;
86 /* Check for a current value. */
87 if (isset($GLOBALS[$name])) {
88 $this->value
= $GLOBALS[$name];
93 /* Check for a new value. */
94 if (isset($_POST["new_$name"])) {
95 $this->new_value
= $_POST["new_$name"];
97 $this->new_value
= '';
100 /* Set the default save function. */
101 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
102 $this->save_function
= SMOPT_SAVE_DEFAULT
;
104 $this->save_function
= SMOPT_SAVE_NOOP
;
108 /* Set the value for this option. */
109 function setValue($value) {
110 $this->value
= $value;
113 /* Set the new value for this option. */
114 function setNewValue($new_value) {
115 $this->new_value
= $new_value;
118 /* Set the size for this option. */
119 function setSize($size) {
123 /* Set the comment for this option. */
124 function setComment($comment) {
125 $this->comment
= $comment;
128 /* Set the script for this option. */
129 function setScript($script) {
130 $this->script
= $script;
133 /* Set the save function for this option. */
134 function setSaveFunction($save_function) {
135 $this->save_function
= $save_function;
138 function createHTMLWidget() {
139 global $javascript_on;
141 /* Get the widget for this option type. */
142 switch ($this->type
) {
143 case SMOPT_TYPE_STRING
:
144 $result = $this->createWidget_String();
146 case SMOPT_TYPE_STRLIST
:
147 $result = $this->createWidget_StrList();
149 case SMOPT_TYPE_TEXTAREA
:
150 $result = $this->createWidget_TextArea();
152 case SMOPT_TYPE_INTEGER
:
153 $result = $this->createWidget_Integer();
155 case SMOPT_TYPE_FLOAT
:
156 $result = $this->createWidget_Float();
158 case SMOPT_TYPE_BOOLEAN
:
159 $result = $this->createWidget_Boolean();
161 case SMOPT_TYPE_HIDDEN
:
162 $result = $this->createWidget_Hidden();
164 case SMOPT_TYPE_COMMENT
:
165 $result = $this->createWidget_Comment();
167 case SMOPT_TYPE_FLDRLIST
:
168 $result = $this->createWidget_FolderList();
171 $result = '<font color="' . $color[2] . '">'
172 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
176 /* Add the script for this option. */
177 $result .= $this->script
;
179 /* Now, return the created widget. */
183 function createWidget_String() {
184 switch ($this->size
) {
185 case SMOPT_SIZE_TINY
:
188 case SMOPT_SIZE_SMALL
:
191 case SMOPT_SIZE_LARGE
:
194 case SMOPT_SIZE_HUGE
:
197 case SMOPT_SIZE_NORMAL
:
202 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\">";
206 function createWidget_StrList() {
207 /* Begin the select tag. */
208 $result = "<select name=\"new_$this->name\">";
210 /* Add each possible value to the select list. */
211 foreach ($this->possible_values
as $real_value => $disp_value) {
212 /* Start the next new option string. */
213 $new_option = "<option value=\"$real_value\"";
215 /* If this value is the current value, select it. */
216 if ($real_value == $this->value
) {
217 $new_option .= ' selected';
220 /* Add the display value to our option string. */
221 $new_option .= ">$disp_value</option>";
223 /* And add the new option string to our select tag. */
224 $result .= $new_option;
227 /* Close the select tag and return our happy result. */
228 $result .= '</select>';
232 function createWidget_FolderList() {
233 $selected = array(strtolower($this->value
));
235 /* Begin the select tag. */
236 $result = "<select name=\"new_$this->name\">";
238 /* Add each possible value to the select list. */
239 foreach ($this->possible_values
as $real_value => $disp_value) {
240 if ( is_array($disp_value) ) {
241 /* For folder list, we passed in the array of boxes.. */
242 $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
244 /* Start the next new option string. */
245 $new_option = "<option value=\"$real_value\"";
247 /* If this value is the current value, select it. */
248 if ($real_value == $this->value
) {
249 $new_option .= ' selected';
252 /* Add the display value to our option string. */
253 $new_option .= ">$disp_value</option>";
255 /* And add the new option string to our select tag. */
256 $result .= $new_option;
258 /* Close the select tag and return our happy result. */
259 $result .= '</select>';
264 function createWidget_TextArea() {
265 switch ($this->size
) {
266 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
267 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
268 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
269 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
270 case SMOPT_SIZE_NORMAL
:
271 default: $rows = 5; $cols = 50;
273 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
274 . "cols=\"$cols\">$this->value</textarea>";
278 function createWidget_Integer() {
279 return ($this->createWidget_String());
282 function createWidget_Float() {
283 return ($this->createWidget_String());
286 function createWidget_Boolean() {
287 /* Do the whole current value thing. */
288 if ($this->value
!= SMPREF_NO
) {
289 $yes_chk = ' checked';
293 $no_chk = ' checked';
296 /* Build the yes choice. */
297 $yes_option = '<input type="radio" name="new_' . $this->name
298 . '" value="' . SMPREF_YES
. "\"$yes_chk> "
301 /* Build the no choice. */
302 $no_option = '<input type="radio" name="new_' . $this->name
303 . '" value="' . SMPREF_NO
. "\"$no_chk> "
306 /* Build and return the combined "boolean widget". */
307 $result = "$yes_option $no_option";
311 function createWidget_Hidden() {
312 $result = '<input type="hidden" name="new_' . $this->name
313 . '" value="' . $this->value
. '">';
317 function createWidget_Comment() {
318 $result = $this->comment
;
323 $function = $this->save_function
;
328 return ($this->value
!= $this->new_value
);
332 function save_option($option) {
333 if ( !check_php_version(4,1) ) {
337 $username = $_SESSION['username'];
339 setPref($data_dir, $username, $option->name
, $option->new_value
);
342 function save_option_noop($option) {
343 /* Do nothing here... */
346 function create_optpage_element($optpage) {
347 return create_hidden_element('optpage', $optpage);
350 function create_optmode_element($optmode) {
351 return create_hidden_element('optmode', $optmode);
354 function create_hidden_element($name, $value) {
355 $result = '<input type="hidden" '
356 . 'name="' . $name . '" '
357 . 'value="' . $value . '">';
361 function create_option_groups($optgrps, $optvals) {
362 /* Build a simple array with which to start. */
365 /* Create option group for each option group name. */
366 foreach ($optgrps as $grpkey => $grpname) {
367 $result[$grpkey] = array();
368 $result[$grpkey]['name'] = $grpname;
369 $result[$grpkey]['options'] = array();
372 /* Create a new SquirrelOption for each set of option values. */
373 foreach ($optvals as $grpkey => $grpopts) {
374 foreach ($grpopts as $optset) {
375 if (isset($optset['posvals'])) {
376 /* Create a new option with all values given. */
377 $next_option = new SquirrelOption(
385 /* Create a new option with all but possible values given. */
386 $next_option = new SquirrelOption(
394 /* If provided, set the size for this option. */
395 if (isset($optset['size'])) {
396 $next_option->setSize($optset['size']);
399 /* If provided, set the comment for this option. */
400 if (isset($optset['comment'])) {
401 $next_option->setComment($optset['comment']);
404 /* If provided, set the save function for this option. */
405 if (isset($optset['save'])) {
406 $next_option->setSaveFunction($optset['save']);
409 /* If provided, set the script for this option. */
410 if (isset($optset['script'])) {
411 $next_option->setScript($optset['script']);
414 /* Add this option to the option array. */
415 $result[$grpkey]['options'][] = $next_option;
419 /* Return our resulting array. */
423 function print_option_groups($option_groups) {
424 /* Print each option group. */
425 foreach ($option_groups as $next_optgrp) {
426 /* If it is not blank, print the name for this option group. */
427 if ($next_optgrp['name'] != '') {
428 echo html_tag( 'tr', "\n".
430 '<b>' . $next_optgrp['name'] . '</b>' ,
431 'center' ,'', 'valign="middle" colspan="2" nowrap' )
435 /* Print each option in this option group. */
436 foreach ($next_optgrp['options'] as $option) {
437 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
438 echo html_tag( 'tr', "\n".
439 html_tag( 'td', $option->caption
. ':', 'right' ,'', 'valign="middle"' ) .
440 html_tag( 'td', $option->createHTMLWidget(), 'left' )
443 echo $option->createHTMLWidget();
447 /* Print an empty row after this option group. */
449 html_tag( 'td', ' ', 'left', '', 'colspan="2"' )
454 function OptionSubmit( $name ) {
456 html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) .
457 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )