5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Functions needed to display the options pages.
13 /**********************************************/
14 /* Define constants used in the options code. */
15 /**********************************************/
17 /* Define constants for the various option types. */
18 define('SMOPT_TYPE_STRING', 0);
19 define('SMOPT_TYPE_STRLIST', 1);
20 define('SMOPT_TYPE_TEXTAREA', 2);
21 define('SMOPT_TYPE_INTEGER', 3);
22 define('SMOPT_TYPE_FLOAT', 4);
23 define('SMOPT_TYPE_BOOLEAN', 5);
24 define('SMOPT_TYPE_HIDDEN', 6);
25 define('SMOPT_TYPE_COMMENT', 7);
27 /* Define constants for the options refresh levels. */
28 define('SMOPT_REFRESH_NONE', 0);
29 define('SMOPT_REFRESH_FOLDERLIST', 1);
30 define('SMOPT_REFRESH_ALL', 2);
32 /* Define constants for the options size. */
33 define('SMOPT_SIZE_TINY', 0);
34 define('SMOPT_SIZE_SMALL', 1);
35 define('SMOPT_SIZE_MEDIUM', 2);
36 define('SMOPT_SIZE_LARGE', 3);
37 define('SMOPT_SIZE_HUGE', 4);
39 define('SMOPT_SAVE_DEFAULT', 'save_option');
40 define('SMOPT_SAVE_NOOP', 'save_option_noop');
43 * SquirrelOption: An option for Squirrelmail.
45 * This class is a work in progress. When complete, it will handle
46 * presentation and saving of Squirrelmail user options in a simple,
47 * streamline manner. Stay tuned for more stuff.
49 * Also, I'd like to ask that people leave this alone (mostly :) until
50 * I get it a little further along. That should only be a day or two or
51 * three. I will remove this message when it is ready for primetime usage.
53 class SquirrelOption
{
54 /* The basic stuff. */
63 /* The name of the Save Function for this option. */
66 /* The various 'values' for this options. */
71 function SquirrelOption
72 ($name, $caption, $type, $refresh_level, $possible_values = '') {
73 /* Set the basic stuff. */
75 $this->caption
= $caption;
77 $this->refresh_level
= $refresh_level;
78 $this->possible_values
= $possible_values;
79 $this->size
= SMOPT_SIZE_MEDIUM
;
83 /* Check for a current value. */
84 if (isset($GLOBALS[$name])) {
85 $this->value
= $GLOBALS[$name];
90 /* Check for a new value. */
91 if (isset($GLOBALS["new_$name"])) {
92 $this->new_value
= $GLOBALS["new_$name"];
94 $this->new_value
= '';
97 /* Set the default save function. */
98 if ((type
!= SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
99 $this->save_function
= SMOPT_SAVE_DEFAULT
;
101 $this->save_function
= SMOPT_SAVE_NOOP
;
105 /* Set the value for this option. */
106 function setValue($value) {
107 $this->value
= $value;
110 /* Set the new value for this option. */
111 function setNewValue($new_value) {
112 $this->new_value
= $new_value;
115 /* Set the size for this option. */
116 function setSize($size) {
120 /* Set the comment for this option. */
121 function setComment($comment) {
122 $this->comment
= $comment;
125 /* Set the script for this option. */
126 function setScript($script) {
127 $this->script
= $script;
130 /* Set the save function for this option. */
131 function setSaveFunction($save_function) {
132 $this->save_function
= $save_function;
135 function createHTMLWidget() {
136 global $javascript_on;
138 /* Get the widget for this option type. */
139 switch ($this->type
) {
140 case SMOPT_TYPE_STRING
:
141 $result = $this->createWidget_String();
143 case SMOPT_TYPE_STRLIST
:
144 $result = $this->createWidget_StrList();
146 case SMOPT_TYPE_TEXTAREA
:
147 $result = $this->createWidget_TextArea();
149 case SMOPT_TYPE_INTEGER
:
150 $result = $this->createWidget_Integer();
152 case SMOPT_TYPE_FLOAT
:
153 $result = $this->createWidget_Float();
155 case SMOPT_TYPE_BOOLEAN
:
156 $result = $this->createWidget_Boolean();
158 case SMOPT_TYPE_HIDDEN
:
159 $result = $this->createWidget_Hidden();
161 case SMOPT_TYPE_COMMENT
:
162 $result = $this->createWidget_Comment();
165 $result = '<FONT COLOR=RED>'
166 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
170 /* Add the script for this option. */
171 $result .= $this->script
;
173 /* Now, return the created widget. */
177 function createWidget_String() {
178 switch ($this->size
) {
179 case SMOPT_SIZE_TINY
: $width = 5; break;
180 case SMOPT_SIZE_SMALL
: $width = 12; break;
181 case SMOPT_SIZE_LARGE
: $width = 38; break;
182 case SMOPT_SIZE_HUGE
: $width = 50; break;
183 case SMOPT_SIZE_NORMAL
:
184 default: $width = 25;
187 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
191 function createWidget_StrList() {
192 /* Begin the select tag. */
193 $result = "<SELECT NAME=\"new_$this->name\">";
195 /* Add each possible value to the select list. */
196 foreach ($this->possible_values
as $real_value => $disp_value) {
197 /* Start the next new option string. */
198 $new_option = "<OPTION VALUE=\"$real_value\"";
200 /* If this value is the current value, select it. */
201 if ($real_value == $this->value
) {
202 $new_option .= ' SELECTED';
205 /* Add the display value to our option string. */
206 $new_option .= ">$disp_value</OPTION>";
208 /* And add the new option string to our select tag. */
209 $result .= $new_option;
212 /* Close the select tag and return our happy result. */
213 $result .= '</SELECT>';
217 function createWidget_TextArea() {
218 switch ($this->size
) {
219 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
220 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
221 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
222 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
223 case SMOPT_SIZE_NORMAL
:
224 default: $rows = 5; $cols = 50;
226 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
227 . "COLS=\"$cols\">$this->value</TEXTAREA>";
231 function createWidget_Integer() {
232 return ($this->createWidget_String());
235 function createWidget_Float() {
236 return ($this->createWidget_String());
239 function createWidget_Boolean() {
240 /* Do the whole current value thing. */
241 if ($this->value
!= SMPREF_NO
) {
242 $yes_chk = ' CHECKED';
246 $no_chk = ' CHECKED';
249 /* Build the yes choice. */
250 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
251 . '" VALUE="' . SMPREF_YES
. "\"$yes_chk> "
254 /* Build the no choice. */
255 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
256 . '" VALUE="' . SMPREF_NO
. "\"$no_chk> "
259 /* Build and return the combined "boolean widget". */
260 $result = "$yes_option $no_option";
264 function createWidget_Hidden() {
265 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
266 . '" VALUE="' . $this->value
. '">';
270 function createWidget_Comment() {
271 $result = $this->comment
;
276 $function = $this->save_function
;
281 return ($this->value
!== $this->new_value
);
285 function save_option($option) {
286 global $data_dir, $username;
287 setPref($data_dir, $username, $option->name
, $option->new_value
);
289 /* I do not know if this next line does any good. */
290 $GLOBALS[$name] = $option->new_value
;
293 function save_option_noop($option) {
294 /* Do nothing here... */
297 function create_optpage_element($optpage) {
298 return create_hidden_element('optpage', $optpage);
301 function create_optmode_element($optmode) {
302 return create_hidden_element('optmode', $optmode);
305 function create_hidden_element($name, $value) {
306 $result = '<INPUT TYPE="HIDDEN" '
307 . 'NAME="' . $name . '" '
308 . 'VALUE="' . $value . '">';
313 function createOptionGroups($optgrps, $optvals) {
314 return create_option_groups($optgrps, $optvals);
317 function create_option_groups($optgrps, $optvals) {
318 /* Build a simple array with which to start. */
321 /* Create option group for each option group name. */
322 foreach ($optgrps as $grpkey => $grpname) {
323 $result[$grpkey] = array();
324 $result[$grpkey]['name'] = $grpname;
325 $result[$grpkey]['options'] = array();
328 /* Create a new SquirrelOption for each set of option values. */
329 foreach ($optvals as $grpkey => $grpopts) {
330 foreach ($grpopts as $optset) {
331 if (isset($optset['posvals'])) {
332 /* Create a new option with all values given. */
333 $next_option = new SquirrelOption(
341 /* Create a new option with all but possible values given. */
342 $next_option = new SquirrelOption(
350 /* If provided, set the size for this option. */
351 if (isset($optset['size'])) {
352 $next_option->setSize($optset['size']);
355 /* If provided, set the comment for this option. */
356 if (isset($optset['comment'])) {
357 $next_option->setComment($optset['comment']);
360 /* If provided, set the save function for this option. */
361 if (isset($optset['save'])) {
362 $next_option->setSaveFunction($optset['save']);
365 /* If provided, set the script for this option. */
366 if (isset($optset['script'])) {
367 $next_option->setScript($optset['script']);
370 /* Add this option to the option array. */
371 $result[$grpkey]['options'][] = $next_option;
375 /* Return our resulting array. */
379 function printOptionGroups($option_groups) {
380 print_option_groups($option_groups);
383 function print_option_groups($option_groups) {
384 foreach ($option_groups as $next_optgrp) {
385 echo '<TR><TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2" NOWRAP><B>'
386 . $next_optgrp['name'] . "</B></TD></TR>\n";
387 foreach ($next_optgrp['options'] as $option) {
388 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
390 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
391 . $option->caption
. ":</TD>\n";
392 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
395 echo $option->createHTMLWidget();
398 echo "<TR><TD COLSPAN=\"2\"> </TD></TR>\n";
402 function OptionSubmit( $name ) {
403 echo '<tr><td> </td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .