added flag related functions for handling sets of messages instead of using
[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);
7e6d5ea3 21define('SMOPT_TYPE_TEXTAREA', 2);
a3ec3c91 22define('SMOPT_TYPE_INTEGER', 3);
23define('SMOPT_TYPE_FLOAT', 4);
24define('SMOPT_TYPE_BOOLEAN', 5);
2a50fbd7 25define('SMOPT_TYPE_HIDDEN', 6);
bbcafebd 26define('SMOPT_TYPE_COMMENT', 7);
be2d5495 27define('SMOPT_TYPE_FLDRLIST', 8);
a3ec3c91 28
29/* Define constants for the options refresh levels. */
30define('SMOPT_REFRESH_NONE', 0);
31define('SMOPT_REFRESH_FOLDERLIST', 1);
32define('SMOPT_REFRESH_ALL', 2);
33
bbcafebd 34/* Define constants for the options size. */
35define('SMOPT_SIZE_TINY', 0);
36define('SMOPT_SIZE_SMALL', 1);
37define('SMOPT_SIZE_MEDIUM', 2);
38define('SMOPT_SIZE_LARGE', 3);
39define('SMOPT_SIZE_HUGE', 4);
88cb1b4d 40define('SMOPT_SIZE_NORMAL', 5);
bbcafebd 41
cbe5423b 42define('SMOPT_SAVE_DEFAULT', 'save_option');
43define('SMOPT_SAVE_NOOP', 'save_option_noop');
44
9962527a 45/**
46 * SquirrelOption: An option for Squirrelmail.
47 *
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.
51 *
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.
55 */
56class SquirrelOption {
57 /* The basic stuff. */
58 var $name;
59 var $caption;
9962527a 60 var $type;
a3ec3c91 61 var $refresh_level;
bbcafebd 62 var $size;
63 var $comment;
cbe5423b 64 var $script;
65
66 /* The name of the Save Function for this option. */
67 var $save_function;
9962527a 68
69 /* The various 'values' for this options. */
70 var $value;
71 var $new_value;
a3ec3c91 72 var $possible_values;
9962527a 73
9962527a 74 function SquirrelOption
a3ec3c91 75 ($name, $caption, $type, $refresh_level, $possible_values = '') {
9962527a 76 /* Set the basic stuff. */
77 $this->name = $name;
78 $this->caption = $caption;
9962527a 79 $this->type = $type;
a3ec3c91 80 $this->refresh_level = $refresh_level;
81 $this->possible_values = $possible_values;
bbcafebd 82 $this->size = SMOPT_SIZE_MEDIUM;
83 $this->comment = '';
cbe5423b 84 $this->script = '';
a3ec3c91 85
86 /* Check for a current value. */
87 if (isset($GLOBALS[$name])) {
88 $this->value = $GLOBALS[$name];
89 } else {
90 $this->value = '';
91 }
9962527a 92
a3ec3c91 93 /* Check for a new value. */
0b97a708 94 if (isset($_POST["new_$name"])) {
95 $this->new_value = $_POST["new_$name"];
a3ec3c91 96 } else {
97 $this->new_value = '';
44ef0f47 98 }
cbe5423b 99
100 /* Set the default save function. */
2a50fbd7 101 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 102 $this->save_function = SMOPT_SAVE_DEFAULT;
103 } else {
104 $this->save_function = SMOPT_SAVE_NOOP;
105 }
106 }
107
108 /* Set the value for this option. */
109 function setValue($value) {
110 $this->value = $value;
111 }
112
113 /* Set the new value for this option. */
114 function setNewValue($new_value) {
115 $this->new_value = $new_value;
9962527a 116 }
44ef0f47 117
bbcafebd 118 /* Set the size for this option. */
119 function setSize($size) {
120 $this->size = $size;
121 }
122
123 /* Set the comment for this option. */
124 function setComment($comment) {
125 $this->comment = $comment;
126 }
127
cbe5423b 128 /* Set the script for this option. */
129 function setScript($script) {
130 $this->script = $script;
131 }
132
133 /* Set the save function for this option. */
134 function setSaveFunction($save_function) {
135 $this->save_function = $save_function;
136 }
137
a3ec3c91 138 function createHTMLWidget() {
cbe5423b 139 global $javascript_on;
140
141 /* Get the widget for this option type. */
a3ec3c91 142 switch ($this->type) {
143 case SMOPT_TYPE_STRING:
144 $result = $this->createWidget_String();
145 break;
146 case SMOPT_TYPE_STRLIST:
147 $result = $this->createWidget_StrList();
148 break;
7e6d5ea3 149 case SMOPT_TYPE_TEXTAREA:
a3ec3c91 150 $result = $this->createWidget_TextArea();
151 break;
152 case SMOPT_TYPE_INTEGER:
153 $result = $this->createWidget_Integer();
154 break;
155 case SMOPT_TYPE_FLOAT:
156 $result = $this->createWidget_Float();
157 break;
158 case SMOPT_TYPE_BOOLEAN:
159 $result = $this->createWidget_Boolean();
160 break;
2a50fbd7 161 case SMOPT_TYPE_HIDDEN:
a3ec3c91 162 $result = $this->createWidget_Hidden();
163 break;
bbcafebd 164 case SMOPT_TYPE_COMMENT:
165 $result = $this->createWidget_Comment();
166 break;
be2d5495 167 case SMOPT_TYPE_FLDRLIST:
168 $result = $this->createWidget_FolderList();
169 break;
a3ec3c91 170 default:
6b4bd11f 171 $result = '<font color="' . $color[2] . '">'
a3ec3c91 172 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 173 . '</font>';
a3ec3c91 174 }
175
cbe5423b 176 /* Add the script for this option. */
177 $result .= $this->script;
178
a3ec3c91 179 /* Now, return the created widget. */
180 return ($result);
181 }
182
183 function createWidget_String() {
bbcafebd 184 switch ($this->size) {
88cb1b4d 185 case SMOPT_SIZE_TINY:
186 $width = 5;
187 break;
188 case SMOPT_SIZE_SMALL:
189 $width = 12;
190 break;
191 case SMOPT_SIZE_LARGE:
192 $width = 38;
193 break;
194 case SMOPT_SIZE_HUGE:
195 $width = 50;
196 break;
bbcafebd 197 case SMOPT_SIZE_NORMAL:
88cb1b4d 198 default:
199 $width = 25;
bbcafebd 200 }
201
6b4bd11f 202 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\">";
a3ec3c91 203 return ($result);
204 }
205
206 function createWidget_StrList() {
207 /* Begin the select tag. */
6b4bd11f 208 $result = "<select name=\"new_$this->name\">";
a3ec3c91 209
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. */
6b4bd11f 213 $new_option = "<option value=\"$real_value\"";
a3ec3c91 214
215 /* If this value is the current value, select it. */
216 if ($real_value == $this->value) {
6b4bd11f 217 $new_option .= ' selected';
a3ec3c91 218 }
219
220 /* Add the display value to our option string. */
6b4bd11f 221 $new_option .= ">$disp_value</option>";
a3ec3c91 222
223 /* And add the new option string to our select tag. */
224 $result .= $new_option;
225 }
226
227 /* Close the select tag and return our happy result. */
6b4bd11f 228 $result .= '</select>';
a3ec3c91 229 return ($result);
230 }
231
be2d5495 232 function createWidget_FolderList() {
233 $selected = array(strtolower($this->value));
234
235 /* Begin the select tag. */
236 $result = "<select name=\"new_$this->name\">";
237
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);
243 } else {
244 /* Start the next new option string. */
245 $new_option = "<option value=\"$real_value\"";
246
247 /* If this value is the current value, select it. */
248 if ($real_value == $this->value) {
249 $new_option .= ' selected';
250 }
251
252 /* Add the display value to our option string. */
253 $new_option .= ">$disp_value</option>";
254 }
255 /* And add the new option string to our select tag. */
256 $result .= $new_option;
257 }
258 /* Close the select tag and return our happy result. */
259 $result .= '</select>';
260 return ($result);
261 }
262
263
a3ec3c91 264 function createWidget_TextArea() {
bbcafebd 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;
272 }
6b4bd11f 273 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
274 . "cols=\"$cols\">$this->value</textarea>";
bbcafebd 275 return ($result);
a3ec3c91 276 }
277
278 function createWidget_Integer() {
279 return ($this->createWidget_String());
280 }
281
282 function createWidget_Float() {
283 return ($this->createWidget_String());
284 }
285
286 function createWidget_Boolean() {
fd87494d 287 /* Do the whole current value thing. */
288 if ($this->value != SMPREF_NO) {
6b4bd11f 289 $yes_chk = ' checked';
fd87494d 290 $no_chk = '';
291 } else {
292 $yes_chk = '';
6b4bd11f 293 $no_chk = ' checked';
fd87494d 294 }
295
296 /* Build the yes choice. */
6b4bd11f 297 $yes_option = '<input type="radio" name="new_' . $this->name
298 . '" value="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
fd87494d 299 . _("Yes");
300
301 /* Build the no choice. */
6b4bd11f 302 $no_option = '<input type="radio" name="new_' . $this->name
303 . '" value="' . SMPREF_NO . "\"$no_chk>&nbsp;"
fd87494d 304 . _("No");
305
306 /* Build and return the combined "boolean widget". */
307 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
308 return ($result);
a3ec3c91 309 }
310
311 function createWidget_Hidden() {
6b4bd11f 312 $result = '<input type="hidden" name="new_' . $this->name
313 . '" value="' . $this->value . '">';
a3ec3c91 314 return ($result);
315 }
316
bbcafebd 317 function createWidget_Comment() {
318 $result = $this->comment;
319 return ($result);
320 }
321
cbe5423b 322 function save() {
323 $function = $this->save_function;
324 $function($this);
44ef0f47 325 }
cbe5423b 326
327 function changed() {
6206f6c4 328 return ($this->value != $this->new_value);
cbe5423b 329 }
330}
331
332function save_option($option) {
fb694681 333 if ( !check_php_version(4,1) ) {
0b97a708 334 global $_SESSION;
335 }
336 global $data_dir;
337 $username = $_SESSION['username'];
cbe5423b 338
0b97a708 339 setPref($data_dir, $username, $option->name, $option->new_value);
cbe5423b 340}
341
342function save_option_noop($option) {
343 /* Do nothing here... */
9962527a 344}
44ef0f47 345
cbe5423b 346function create_optpage_element($optpage) {
347 return create_hidden_element('optpage', $optpage);
348}
349
350function create_optmode_element($optmode) {
351 return create_hidden_element('optmode', $optmode);
352}
353
354function create_hidden_element($name, $value) {
6b4bd11f 355 $result = '<input type="hidden" '
356 . 'name="' . $name . '" '
357 . 'value="' . $value . '">';
cbe5423b 358 return ($result);
359}
360
cbe5423b 361function create_option_groups($optgrps, $optvals) {
a3ec3c91 362 /* Build a simple array with which to start. */
363 $result = array();
364
bbcafebd 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();
370 }
371
a3ec3c91 372 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 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(
378 $optset['name'],
379 $optset['caption'],
380 $optset['type'],
381 $optset['refresh'],
382 $optset['posvals']
383 );
384 } else {
385 /* Create a new option with all but possible values given. */
386 $next_option = new SquirrelOption(
387 $optset['name'],
388 $optset['caption'],
389 $optset['type'],
390 $optset['refresh']
391 );
392 }
393
394 /* If provided, set the size for this option. */
395 if (isset($optset['size'])) {
396 $next_option->setSize($optset['size']);
397 }
398
399 /* If provided, set the comment for this option. */
400 if (isset($optset['comment'])) {
401 $next_option->setComment($optset['comment']);
402 }
403
cbe5423b 404 /* If provided, set the save function for this option. */
405 if (isset($optset['save'])) {
406 $next_option->setSaveFunction($optset['save']);
407 }
408
409 /* If provided, set the script for this option. */
410 if (isset($optset['script'])) {
411 $next_option->setScript($optset['script']);
412 }
413
bbcafebd 414 /* Add this option to the option array. */
415 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 416 }
417 }
418
419 /* Return our resulting array. */
420 return ($result);
421}
422
cbe5423b 423function print_option_groups($option_groups) {
2fad95fa 424 /* Print each option group. */
bbcafebd 425 foreach ($option_groups as $next_optgrp) {
2fad95fa 426 /* If it is not blank, print the name for this option group. */
427 if ($next_optgrp['name'] != '') {
6b4bd11f 428 echo html_tag( 'tr', "\n".
429 html_tag( 'td',
430 '<b>' . $next_optgrp['name'] . '</b>' ,
431 'center' ,'', 'valign="middle" colspan="2" nowrap' )
432 ) ."\n";
7e235a1a 433 }
434
435 /* Print each option in this option group. */
bbcafebd 436 foreach ($next_optgrp['options'] as $option) {
2a50fbd7 437 if ($option->type != SMOPT_TYPE_HIDDEN) {
6b4bd11f 438 echo html_tag( 'tr', "\n".
439 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
440 html_tag( 'td', $option->createHTMLWidget(), 'left' )
441 ) ."\n";
bbcafebd 442 } else {
443 echo $option->createHTMLWidget();
444 }
445 }
7e235a1a 446
447 /* Print an empty row after this option group. */
6b4bd11f 448 echo html_tag( 'tr',
449 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
450 ) . "\n";
bbcafebd 451 }
452}
453
9962527a 454function OptionSubmit( $name ) {
6b4bd11f 455 echo html_tag( 'tr',
456 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' ) .
457 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )
458 ) . "\n";
9962527a 459}
02ddcd00 460
23d6bd09 461?>