6 * Copyright (c) 1999-2002 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);
28 /* Define constants for the options refresh levels. */
29 define('SMOPT_REFRESH_NONE', 0);
30 define('SMOPT_REFRESH_FOLDERLIST', 1);
31 define('SMOPT_REFRESH_ALL', 2);
33 /* Define constants for the options size. */
34 define('SMOPT_SIZE_TINY', 0);
35 define('SMOPT_SIZE_SMALL', 1);
36 define('SMOPT_SIZE_MEDIUM', 2);
37 define('SMOPT_SIZE_LARGE', 3);
38 define('SMOPT_SIZE_HUGE', 4);
39 define('SMOPT_SIZE_NORMAL', 5);
41 define('SMOPT_SAVE_DEFAULT', 'save_option');
42 define('SMOPT_SAVE_NOOP', 'save_option_noop');
45 * SquirrelOption: An option for Squirrelmail.
47 * This class is a work in progress. When complete, it will handle
48 * presentation and saving of Squirrelmail user options in a simple,
49 * streamline manner. Stay tuned for more stuff.
51 * Also, I'd like to ask that people leave this alone (mostly :) until
52 * I get it a little further along. That should only be a day or two or
53 * three. I will remove this message when it is ready for primetime usage.
55 class SquirrelOption
{
56 /* The basic stuff. */
65 /* The name of the Save Function for this option. */
68 /* The various 'values' for this options. */
73 function SquirrelOption
74 ($name, $caption, $type, $refresh_level, $possible_values = '') {
75 /* Set the basic stuff. */
77 $this->caption
= $caption;
79 $this->refresh_level
= $refresh_level;
80 $this->possible_values
= $possible_values;
81 $this->size
= SMOPT_SIZE_MEDIUM
;
85 /* Check for a current value. */
86 if (isset($GLOBALS[$name])) {
87 $this->value
= $GLOBALS[$name];
92 /* Check for a new value. */
93 if (isset($GLOBALS["new_$name"])) {
94 $this->new_value
= $GLOBALS["new_$name"];
96 $this->new_value
= '';
99 /* Set the default save function. */
100 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
101 $this->save_function
= SMOPT_SAVE_DEFAULT
;
103 $this->save_function
= SMOPT_SAVE_NOOP
;
107 /* Set the value for this option. */
108 function setValue($value) {
109 $this->value
= $value;
112 /* Set the new value for this option. */
113 function setNewValue($new_value) {
114 $this->new_value
= $new_value;
117 /* Set the size for this option. */
118 function setSize($size) {
122 /* Set the comment for this option. */
123 function setComment($comment) {
124 $this->comment
= $comment;
127 /* Set the script for this option. */
128 function setScript($script) {
129 $this->script
= $script;
132 /* Set the save function for this option. */
133 function setSaveFunction($save_function) {
134 $this->save_function
= $save_function;
137 function createHTMLWidget() {
138 global $javascript_on;
140 /* Get the widget for this option type. */
141 switch ($this->type
) {
142 case SMOPT_TYPE_STRING
:
143 $result = $this->createWidget_String();
145 case SMOPT_TYPE_STRLIST
:
146 $result = $this->createWidget_StrList();
148 case SMOPT_TYPE_TEXTAREA
:
149 $result = $this->createWidget_TextArea();
151 case SMOPT_TYPE_INTEGER
:
152 $result = $this->createWidget_Integer();
154 case SMOPT_TYPE_FLOAT
:
155 $result = $this->createWidget_Float();
157 case SMOPT_TYPE_BOOLEAN
:
158 $result = $this->createWidget_Boolean();
160 case SMOPT_TYPE_HIDDEN
:
161 $result = $this->createWidget_Hidden();
163 case SMOPT_TYPE_COMMENT
:
164 $result = $this->createWidget_Comment();
167 $result = '<FONT COLOR="' . $color[2] . '">'
168 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
172 /* Add the script for this option. */
173 $result .= $this->script
;
175 /* Now, return the created widget. */
179 function createWidget_String() {
180 switch ($this->size
) {
181 case SMOPT_SIZE_TINY
:
184 case SMOPT_SIZE_SMALL
:
187 case SMOPT_SIZE_LARGE
:
190 case SMOPT_SIZE_HUGE
:
193 case SMOPT_SIZE_NORMAL
:
198 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
202 function createWidget_StrList() {
203 /* Begin the select tag. */
204 $result = "<SELECT NAME=\"new_$this->name\">";
206 /* Add each possible value to the select list. */
207 foreach ($this->possible_values
as $real_value => $disp_value) {
208 /* Start the next new option string. */
209 $new_option = "<OPTION VALUE=\"$real_value\"";
211 /* If this value is the current value, select it. */
212 if ($real_value == $this->value
) {
213 $new_option .= ' SELECTED';
216 /* Add the display value to our option string. */
217 $new_option .= ">$disp_value</OPTION>";
219 /* And add the new option string to our select tag. */
220 $result .= $new_option;
223 /* Close the select tag and return our happy result. */
224 $result .= '</SELECT>';
228 function createWidget_TextArea() {
229 switch ($this->size
) {
230 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
231 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
232 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
233 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
234 case SMOPT_SIZE_NORMAL
:
235 default: $rows = 5; $cols = 50;
237 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
238 . "COLS=\"$cols\">$this->value</TEXTAREA>";
242 function createWidget_Integer() {
243 return ($this->createWidget_String());
246 function createWidget_Float() {
247 return ($this->createWidget_String());
250 function createWidget_Boolean() {
251 /* Do the whole current value thing. */
252 if ($this->value
!= SMPREF_NO
) {
253 $yes_chk = ' CHECKED';
257 $no_chk = ' CHECKED';
260 /* Build the yes choice. */
261 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
262 . '" VALUE="' . SMPREF_YES
. "\"$yes_chk> "
265 /* Build the no choice. */
266 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
267 . '" VALUE="' . SMPREF_NO
. "\"$no_chk> "
270 /* Build and return the combined "boolean widget". */
271 $result = "$yes_option $no_option";
275 function createWidget_Hidden() {
276 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
277 . '" VALUE="' . $this->value
. '">';
281 function createWidget_Comment() {
282 $result = $this->comment
;
287 $function = $this->save_function
;
292 return ($this->value
!== $this->new_value
);
296 function save_option($option) {
297 global $data_dir, $username;
298 setPref($data_dir, $username, $option->name
, $option->new_value
);
300 /* I do not know if this next line does any good. */
301 $GLOBALS[$option->name
] = $option->new_value
;
304 function save_option_noop($option) {
305 /* Do nothing here... */
308 function create_optpage_element($optpage) {
309 return create_hidden_element('optpage', $optpage);
312 function create_optmode_element($optmode) {
313 return create_hidden_element('optmode', $optmode);
316 function create_hidden_element($name, $value) {
317 $result = '<INPUT TYPE="HIDDEN" '
318 . 'NAME="' . $name . '" '
319 . 'VALUE="' . $value . '">';
323 function create_option_groups($optgrps, $optvals) {
324 /* Build a simple array with which to start. */
327 /* Create option group for each option group name. */
328 foreach ($optgrps as $grpkey => $grpname) {
329 $result[$grpkey] = array();
330 $result[$grpkey]['name'] = $grpname;
331 $result[$grpkey]['options'] = array();
334 /* Create a new SquirrelOption for each set of option values. */
335 foreach ($optvals as $grpkey => $grpopts) {
336 foreach ($grpopts as $optset) {
337 if (isset($optset['posvals'])) {
338 /* Create a new option with all values given. */
339 $next_option = new SquirrelOption(
347 /* Create a new option with all but possible values given. */
348 $next_option = new SquirrelOption(
356 /* If provided, set the size for this option. */
357 if (isset($optset['size'])) {
358 $next_option->setSize($optset['size']);
361 /* If provided, set the comment for this option. */
362 if (isset($optset['comment'])) {
363 $next_option->setComment($optset['comment']);
366 /* If provided, set the save function for this option. */
367 if (isset($optset['save'])) {
368 $next_option->setSaveFunction($optset['save']);
371 /* If provided, set the script for this option. */
372 if (isset($optset['script'])) {
373 $next_option->setScript($optset['script']);
376 /* Add this option to the option array. */
377 $result[$grpkey]['options'][] = $next_option;
381 /* Return our resulting array. */
385 function print_option_groups($option_groups) {
386 /* Print each option group. */
387 foreach ($option_groups as $next_optgrp) {
388 /* If it is not blank, print the name for this option group. */
389 if ($next_optgrp['name'] != '') {
390 echo '<TR><TD ALIGN=CENTER VALIGN=MIDDLE COLSPAN=2 NOWRAP><B>'
391 . $next_optgrp['name']
392 . "</B></TD></TR>\n";
395 /* Print each option in this option group. */
396 foreach ($next_optgrp['options'] as $option) {
397 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
399 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
400 . $option->caption
. ":</TD>\n";
401 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
404 echo $option->createHTMLWidget();
408 /* Print an empty row after this option group. */
409 echo "<TR><TD COLSPAN=\"2\"> </TD></TR>\n";
413 function OptionSubmit( $name ) {
414 echo '<tr><td> </td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .