forgot to move global $message
[squirrelmail.git] / functions / options.php
CommitLineData
44ef0f47 1<?php
2ba13803 2
35586184 3/**
4 * options.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Functions needed to display the options pages.
10 *
11 * $Id$
12 */
a3ec3c91 13
14/**********************************************/
15/* Define constants used in the options code. */
16/**********************************************/
17
18/* Define constants for the various option types. */
19define('SMOPT_TYPE_STRING', 0);
20define('SMOPT_TYPE_STRLIST', 1);
21define('SMOPT_TYPE_TEXTAREA', 2);
22define('SMOPT_TYPE_INTEGER', 3);
23define('SMOPT_TYPE_FLOAT', 4);
24define('SMOPT_TYPE_BOOLEAN', 5);
25define('SMOPT_TYPE_HIDDEN', 6);
bbcafebd 26define('SMOPT_TYPE_COMMENT', 7);
a3ec3c91 27
28/* Define constants for the options refresh levels. */
29define('SMOPT_REFRESH_NONE', 0);
30define('SMOPT_REFRESH_FOLDERLIST', 1);
31define('SMOPT_REFRESH_ALL', 2);
32
bbcafebd 33/* Define constants for the options size. */
34define('SMOPT_SIZE_TINY', 0);
35define('SMOPT_SIZE_SMALL', 1);
36define('SMOPT_SIZE_MEDIUM', 2);
37define('SMOPT_SIZE_LARGE', 3);
38define('SMOPT_SIZE_HUGE', 4);
88cb1b4d 39define('SMOPT_SIZE_NORMAL', 5);
bbcafebd 40
cbe5423b 41define('SMOPT_SAVE_DEFAULT', 'save_option');
42define('SMOPT_SAVE_NOOP', 'save_option_noop');
43
9962527a 44/**
45 * SquirrelOption: An option for Squirrelmail.
46 *
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.
50 *
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.
54 */
55class SquirrelOption {
56 /* The basic stuff. */
57 var $name;
58 var $caption;
9962527a 59 var $type;
a3ec3c91 60 var $refresh_level;
bbcafebd 61 var $size;
62 var $comment;
cbe5423b 63 var $script;
64
65 /* The name of the Save Function for this option. */
66 var $save_function;
9962527a 67
68 /* The various 'values' for this options. */
69 var $value;
70 var $new_value;
a3ec3c91 71 var $possible_values;
9962527a 72
9962527a 73 function SquirrelOption
a3ec3c91 74 ($name, $caption, $type, $refresh_level, $possible_values = '') {
9962527a 75 /* Set the basic stuff. */
76 $this->name = $name;
77 $this->caption = $caption;
9962527a 78 $this->type = $type;
a3ec3c91 79 $this->refresh_level = $refresh_level;
80 $this->possible_values = $possible_values;
bbcafebd 81 $this->size = SMOPT_SIZE_MEDIUM;
82 $this->comment = '';
cbe5423b 83 $this->script = '';
a3ec3c91 84
85 /* Check for a current value. */
86 if (isset($GLOBALS[$name])) {
87 $this->value = $GLOBALS[$name];
88 } else {
89 $this->value = '';
90 }
9962527a 91
a3ec3c91 92 /* Check for a new value. */
9962527a 93 if (isset($GLOBALS["new_$name"])) {
94 $this->new_value = $GLOBALS["new_$name"];
a3ec3c91 95 } else {
96 $this->new_value = '';
44ef0f47 97 }
cbe5423b 98
99 /* Set the default save function. */
03192051 100 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 101 $this->save_function = SMOPT_SAVE_DEFAULT;
102 } else {
103 $this->save_function = SMOPT_SAVE_NOOP;
104 }
105 }
106
107 /* Set the value for this option. */
108 function setValue($value) {
109 $this->value = $value;
110 }
111
112 /* Set the new value for this option. */
113 function setNewValue($new_value) {
114 $this->new_value = $new_value;
9962527a 115 }
44ef0f47 116
bbcafebd 117 /* Set the size for this option. */
118 function setSize($size) {
119 $this->size = $size;
120 }
121
122 /* Set the comment for this option. */
123 function setComment($comment) {
124 $this->comment = $comment;
125 }
126
cbe5423b 127 /* Set the script for this option. */
128 function setScript($script) {
129 $this->script = $script;
130 }
131
132 /* Set the save function for this option. */
133 function setSaveFunction($save_function) {
134 $this->save_function = $save_function;
135 }
136
a3ec3c91 137 function createHTMLWidget() {
cbe5423b 138 global $javascript_on;
139
140 /* Get the widget for this option type. */
a3ec3c91 141 switch ($this->type) {
142 case SMOPT_TYPE_STRING:
143 $result = $this->createWidget_String();
144 break;
145 case SMOPT_TYPE_STRLIST:
146 $result = $this->createWidget_StrList();
147 break;
148 case SMOPT_TYPE_TEXTAREA:
149 $result = $this->createWidget_TextArea();
150 break;
151 case SMOPT_TYPE_INTEGER:
152 $result = $this->createWidget_Integer();
153 break;
154 case SMOPT_TYPE_FLOAT:
155 $result = $this->createWidget_Float();
156 break;
157 case SMOPT_TYPE_BOOLEAN:
158 $result = $this->createWidget_Boolean();
159 break;
160 case SMOPT_TYPE_HIDDEN:
161 $result = $this->createWidget_Hidden();
162 break;
bbcafebd 163 case SMOPT_TYPE_COMMENT:
164 $result = $this->createWidget_Comment();
165 break;
a3ec3c91 166 default:
26f277a6 167 $result = '<FONT COLOR="' . $color[2] . '">'
a3ec3c91 168 . sprintf(_("Option Type '%s' Not Found"), $this->type)
169 . '</FONT>';
170 }
171
cbe5423b 172 /* Add the script for this option. */
173 $result .= $this->script;
174
a3ec3c91 175 /* Now, return the created widget. */
176 return ($result);
177 }
178
179 function createWidget_String() {
bbcafebd 180 switch ($this->size) {
88cb1b4d 181 case SMOPT_SIZE_TINY:
182 $width = 5;
183 break;
184 case SMOPT_SIZE_SMALL:
185 $width = 12;
186 break;
187 case SMOPT_SIZE_LARGE:
188 $width = 38;
189 break;
190 case SMOPT_SIZE_HUGE:
191 $width = 50;
192 break;
bbcafebd 193 case SMOPT_SIZE_NORMAL:
88cb1b4d 194 default:
195 $width = 25;
bbcafebd 196 }
197
198 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
a3ec3c91 199 return ($result);
200 }
201
202 function createWidget_StrList() {
203 /* Begin the select tag. */
204 $result = "<SELECT NAME=\"new_$this->name\">";
205
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\"";
210
211 /* If this value is the current value, select it. */
212 if ($real_value == $this->value) {
213 $new_option .= ' SELECTED';
214 }
215
216 /* Add the display value to our option string. */
217 $new_option .= ">$disp_value</OPTION>";
218
219 /* And add the new option string to our select tag. */
220 $result .= $new_option;
221 }
222
223 /* Close the select tag and return our happy result. */
224 $result .= '</SELECT>';
225 return ($result);
226 }
227
228 function createWidget_TextArea() {
bbcafebd 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;
236 }
237 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
238 . "COLS=\"$cols\">$this->value</TEXTAREA>";
239 return ($result);
a3ec3c91 240 }
241
242 function createWidget_Integer() {
243 return ($this->createWidget_String());
244 }
245
246 function createWidget_Float() {
247 return ($this->createWidget_String());
248 }
249
250 function createWidget_Boolean() {
fd87494d 251 /* Do the whole current value thing. */
252 if ($this->value != SMPREF_NO) {
253 $yes_chk = ' CHECKED';
254 $no_chk = '';
255 } else {
256 $yes_chk = '';
257 $no_chk = ' CHECKED';
258 }
259
260 /* Build the yes choice. */
261 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
262 . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
263 . _("Yes");
264
265 /* Build the no choice. */
266 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
267 . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
268 . _("No");
269
270 /* Build and return the combined "boolean widget". */
271 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
272 return ($result);
a3ec3c91 273 }
274
275 function createWidget_Hidden() {
276 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
277 . '" VALUE="' . $this->value . '">';
278 return ($result);
279 }
280
bbcafebd 281 function createWidget_Comment() {
282 $result = $this->comment;
283 return ($result);
284 }
285
cbe5423b 286 function save() {
287 $function = $this->save_function;
288 $function($this);
44ef0f47 289 }
cbe5423b 290
291 function changed() {
292 return ($this->value !== $this->new_value);
293 }
294}
295
296function save_option($option) {
297 global $data_dir, $username;
298 setPref($data_dir, $username, $option->name, $option->new_value);
299
300 /* I do not know if this next line does any good. */
03192051 301 $GLOBALS[$option->name] = $option->new_value;
cbe5423b 302}
303
304function save_option_noop($option) {
305 /* Do nothing here... */
9962527a 306}
44ef0f47 307
cbe5423b 308function create_optpage_element($optpage) {
309 return create_hidden_element('optpage', $optpage);
310}
311
312function create_optmode_element($optmode) {
313 return create_hidden_element('optmode', $optmode);
314}
315
316function create_hidden_element($name, $value) {
317 $result = '<INPUT TYPE="HIDDEN" '
318 . 'NAME="' . $name . '" '
319 . 'VALUE="' . $value . '">';
320 return ($result);
321}
322
cbe5423b 323function create_option_groups($optgrps, $optvals) {
a3ec3c91 324 /* Build a simple array with which to start. */
325 $result = array();
326
bbcafebd 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();
332 }
333
a3ec3c91 334 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 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(
340 $optset['name'],
341 $optset['caption'],
342 $optset['type'],
343 $optset['refresh'],
344 $optset['posvals']
345 );
346 } else {
347 /* Create a new option with all but possible values given. */
348 $next_option = new SquirrelOption(
349 $optset['name'],
350 $optset['caption'],
351 $optset['type'],
352 $optset['refresh']
353 );
354 }
355
356 /* If provided, set the size for this option. */
357 if (isset($optset['size'])) {
358 $next_option->setSize($optset['size']);
359 }
360
361 /* If provided, set the comment for this option. */
362 if (isset($optset['comment'])) {
363 $next_option->setComment($optset['comment']);
364 }
365
cbe5423b 366 /* If provided, set the save function for this option. */
367 if (isset($optset['save'])) {
368 $next_option->setSaveFunction($optset['save']);
369 }
370
371 /* If provided, set the script for this option. */
372 if (isset($optset['script'])) {
373 $next_option->setScript($optset['script']);
374 }
375
bbcafebd 376 /* Add this option to the option array. */
377 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 378 }
379 }
380
381 /* Return our resulting array. */
382 return ($result);
383}
384
cbe5423b 385function print_option_groups($option_groups) {
2fad95fa 386 /* Print each option group. */
bbcafebd 387 foreach ($option_groups as $next_optgrp) {
2fad95fa 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']
7e235a1a 392 . "</B></TD></TR>\n";
393 }
394
395 /* Print each option in this option group. */
bbcafebd 396 foreach ($next_optgrp['options'] as $option) {
397 if ($option->type != SMOPT_TYPE_HIDDEN) {
398 echo "<TR>\n";
399 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
400 . $option->caption . ":</TD>\n";
401 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
402 echo "</TR>\n";
403 } else {
404 echo $option->createHTMLWidget();
405 }
406 }
7e235a1a 407
408 /* Print an empty row after this option group. */
bbcafebd 409 echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>\n";
410 }
411}
412
9962527a 413function OptionSubmit( $name ) {
414 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
415 '</td></tr>';
416}
02ddcd00 417
23d6bd09 418?>