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);
40 define('SMOPT_SAVE_DEFAULT', 'save_option');
41 define('SMOPT_SAVE_NOOP', 'save_option_noop');
44 * SquirrelOption: An option for Squirrelmail.
46 * This class is a work in progress. When complete, it will handle
47 * presentation and saving of Squirrelmail user options in a simple,
48 * streamline manner. Stay tuned for more stuff.
50 * Also, I'd like to ask that people leave this alone (mostly :) until
51 * I get it a little further along. That should only be a day or two or
52 * three. I will remove this message when it is ready for primetime usage.
54 class SquirrelOption
{
55 /* The basic stuff. */
64 /* The name of the Save Function for this option. */
67 /* The various 'values' for this options. */
72 function SquirrelOption
73 ($name, $caption, $type, $refresh_level, $possible_values = '') {
74 /* Set the basic stuff. */
76 $this->caption
= $caption;
78 $this->refresh_level
= $refresh_level;
79 $this->possible_values
= $possible_values;
80 $this->size
= SMOPT_SIZE_MEDIUM
;
84 /* Check for a current value. */
85 if (isset($GLOBALS[$name])) {
86 $this->value
= $GLOBALS[$name];
91 /* Check for a new value. */
92 if (isset($GLOBALS["new_$name"])) {
93 $this->new_value
= $GLOBALS["new_$name"];
95 $this->new_value
= '';
98 /* Set the default save function. */
99 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
100 $this->save_function
= SMOPT_SAVE_DEFAULT
;
102 $this->save_function
= SMOPT_SAVE_NOOP
;
106 /* Set the value for this option. */
107 function setValue($value) {
108 $this->value
= $value;
111 /* Set the new value for this option. */
112 function setNewValue($new_value) {
113 $this->new_value
= $new_value;
116 /* Set the size for this option. */
117 function setSize($size) {
121 /* Set the comment for this option. */
122 function setComment($comment) {
123 $this->comment
= $comment;
126 /* Set the script for this option. */
127 function setScript($script) {
128 $this->script
= $script;
131 /* Set the save function for this option. */
132 function setSaveFunction($save_function) {
133 $this->save_function
= $save_function;
136 function createHTMLWidget() {
137 global $javascript_on;
139 /* Get the widget for this option type. */
140 switch ($this->type
) {
141 case SMOPT_TYPE_STRING
:
142 $result = $this->createWidget_String();
144 case SMOPT_TYPE_STRLIST
:
145 $result = $this->createWidget_StrList();
147 case SMOPT_TYPE_TEXTAREA
:
148 $result = $this->createWidget_TextArea();
150 case SMOPT_TYPE_INTEGER
:
151 $result = $this->createWidget_Integer();
153 case SMOPT_TYPE_FLOAT
:
154 $result = $this->createWidget_Float();
156 case SMOPT_TYPE_BOOLEAN
:
157 $result = $this->createWidget_Boolean();
159 case SMOPT_TYPE_HIDDEN
:
160 $result = $this->createWidget_Hidden();
162 case SMOPT_TYPE_COMMENT
:
163 $result = $this->createWidget_Comment();
166 $result = '<FONT COLOR=RED>'
167 . sprintf(_("Option Type '%s' Not Found"), $this->type
)
171 /* Add the script for this option. */
172 $result .= $this->script
;
174 /* Now, return the created widget. */
178 function createWidget_String() {
179 switch ($this->size
) {
180 case SMOPT_SIZE_TINY
: $width = 5; break;
181 case SMOPT_SIZE_SMALL
: $width = 12; break;
182 case SMOPT_SIZE_LARGE
: $width = 38; break;
183 case SMOPT_SIZE_HUGE
: $width = 50; break;
184 case SMOPT_SIZE_NORMAL
:
185 default: $width = 25;
188 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
192 function createWidget_StrList() {
193 /* Begin the select tag. */
194 $result = "<SELECT NAME=\"new_$this->name\">";
196 /* Add each possible value to the select list. */
197 foreach ($this->possible_values
as $real_value => $disp_value) {
198 /* Start the next new option string. */
199 $new_option = "<OPTION VALUE=\"$real_value\"";
201 /* If this value is the current value, select it. */
202 if ($real_value == $this->value
) {
203 $new_option .= ' SELECTED';
206 /* Add the display value to our option string. */
207 $new_option .= ">$disp_value</OPTION>";
209 /* And add the new option string to our select tag. */
210 $result .= $new_option;
213 /* Close the select tag and return our happy result. */
214 $result .= '</SELECT>';
218 function createWidget_TextArea() {
219 switch ($this->size
) {
220 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
221 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
222 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
223 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
224 case SMOPT_SIZE_NORMAL
:
225 default: $rows = 5; $cols = 50;
227 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
228 . "COLS=\"$cols\">$this->value</TEXTAREA>";
232 function createWidget_Integer() {
233 return ($this->createWidget_String());
236 function createWidget_Float() {
237 return ($this->createWidget_String());
240 function createWidget_Boolean() {
241 /* Do the whole current value thing. */
242 if ($this->value
!= SMPREF_NO
) {
243 $yes_chk = ' CHECKED';
247 $no_chk = ' CHECKED';
250 /* Build the yes choice. */
251 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
252 . '" VALUE="' . SMPREF_YES
. "\"$yes_chk> "
255 /* Build the no choice. */
256 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
257 . '" VALUE="' . SMPREF_NO
. "\"$no_chk> "
260 /* Build and return the combined "boolean widget". */
261 $result = "$yes_option $no_option";
265 function createWidget_Hidden() {
266 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
267 . '" VALUE="' . $this->value
. '">';
271 function createWidget_Comment() {
272 $result = $this->comment
;
277 $function = $this->save_function
;
282 return ($this->value
!== $this->new_value
);
286 function save_option($option) {
287 global $data_dir, $username;
288 setPref($data_dir, $username, $option->name
, $option->new_value
);
290 /* I do not know if this next line does any good. */
291 $GLOBALS[$option->name
] = $option->new_value
;
294 function save_option_noop($option) {
295 /* Do nothing here... */
298 function create_optpage_element($optpage) {
299 return create_hidden_element('optpage', $optpage);
302 function create_optmode_element($optmode) {
303 return create_hidden_element('optmode', $optmode);
306 function create_hidden_element($name, $value) {
307 $result = '<INPUT TYPE="HIDDEN" '
308 . 'NAME="' . $name . '" '
309 . 'VALUE="' . $value . '">';
313 function create_option_groups($optgrps, $optvals) {
314 /* Build a simple array with which to start. */
317 /* Create option group for each option group name. */
318 foreach ($optgrps as $grpkey => $grpname) {
319 $result[$grpkey] = array();
320 $result[$grpkey]['name'] = $grpname;
321 $result[$grpkey]['options'] = array();
324 /* Create a new SquirrelOption for each set of option values. */
325 foreach ($optvals as $grpkey => $grpopts) {
326 foreach ($grpopts as $optset) {
327 if (isset($optset['posvals'])) {
328 /* Create a new option with all values given. */
329 $next_option = new SquirrelOption(
337 /* Create a new option with all but possible values given. */
338 $next_option = new SquirrelOption(
346 /* If provided, set the size for this option. */
347 if (isset($optset['size'])) {
348 $next_option->setSize($optset['size']);
351 /* If provided, set the comment for this option. */
352 if (isset($optset['comment'])) {
353 $next_option->setComment($optset['comment']);
356 /* If provided, set the save function for this option. */
357 if (isset($optset['save'])) {
358 $next_option->setSaveFunction($optset['save']);
361 /* If provided, set the script for this option. */
362 if (isset($optset['script'])) {
363 $next_option->setScript($optset['script']);
366 /* Add this option to the option array. */
367 $result[$grpkey]['options'][] = $next_option;
371 /* Return our resulting array. */
375 function print_option_groups($option_groups) {
376 /* Print each option group. */
377 foreach ($option_groups as $next_optgrp) {
378 /* If it is not blank, print the name for this option group. */
379 if ($next_optgrp['name'] != '') {
380 echo '<TR><TD ALIGN=CENTER VALIGN=MIDDLE COLSPAN=2 NOWRAP><B>'
381 . $next_optgrp['name']
382 . "</B></TD></TR>\n";
385 /* Print each option in this option group. */
386 foreach ($next_optgrp['options'] as $option) {
387 if ($option->type
!= SMOPT_TYPE_HIDDEN
) {
389 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
390 . $option->caption
. ":</TD>\n";
391 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
394 echo $option->createHTMLWidget();
398 /* Print an empty row after this option group. */
399 echo "<TR><TD COLSPAN=\"2\"> </TD></TR>\n";
403 function OptionSubmit( $name ) {
404 echo '<tr><td> </td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .