Folder Selection changes integrated from sizzle-ui stream..
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
13
14 /**********************************************/
15 /* Define constants used in the options code. */
16 /**********************************************/
17
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);
27 define('SMOPT_TYPE_FLDRLIST', 8);
28
29 /* Define constants for the options refresh levels. */
30 define('SMOPT_REFRESH_NONE', 0);
31 define('SMOPT_REFRESH_FOLDERLIST', 1);
32 define('SMOPT_REFRESH_ALL', 2);
33
34 /* Define constants for the options size. */
35 define('SMOPT_SIZE_TINY', 0);
36 define('SMOPT_SIZE_SMALL', 1);
37 define('SMOPT_SIZE_MEDIUM', 2);
38 define('SMOPT_SIZE_LARGE', 3);
39 define('SMOPT_SIZE_HUGE', 4);
40 define('SMOPT_SIZE_NORMAL', 5);
41
42 define('SMOPT_SAVE_DEFAULT', 'save_option');
43 define('SMOPT_SAVE_NOOP', 'save_option_noop');
44
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 */
56 class SquirrelOption {
57 /* The basic stuff. */
58 var $name;
59 var $caption;
60 var $type;
61 var $refresh_level;
62 var $size;
63 var $comment;
64 var $script;
65
66 /* The name of the Save Function for this option. */
67 var $save_function;
68
69 /* The various 'values' for this options. */
70 var $value;
71 var $new_value;
72 var $possible_values;
73
74 function SquirrelOption
75 ($name, $caption, $type, $refresh_level, $possible_values = '') {
76 /* Set the basic stuff. */
77 $this->name = $name;
78 $this->caption = $caption;
79 $this->type = $type;
80 $this->refresh_level = $refresh_level;
81 $this->possible_values = $possible_values;
82 $this->size = SMOPT_SIZE_MEDIUM;
83 $this->comment = '';
84 $this->script = '';
85
86 /* Check for a current value. */
87 if (isset($GLOBALS[$name])) {
88 $this->value = $GLOBALS[$name];
89 } else {
90 $this->value = '';
91 }
92
93 /* Check for a new value. */
94 if (isset($_POST["new_$name"])) {
95 $this->new_value = $_POST["new_$name"];
96 } else {
97 $this->new_value = '';
98 }
99
100 /* Set the default save function. */
101 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
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;
116 }
117
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
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
138 function createHTMLWidget() {
139 global $javascript_on;
140
141 /* Get the widget for this option type. */
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;
149 case SMOPT_TYPE_TEXTAREA:
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;
161 case SMOPT_TYPE_HIDDEN:
162 $result = $this->createWidget_Hidden();
163 break;
164 case SMOPT_TYPE_COMMENT:
165 $result = $this->createWidget_Comment();
166 break;
167 case SMOPT_TYPE_FLDRLIST:
168 $result = $this->createWidget_FolderList();
169 break;
170 default:
171 $result = '<font color="' . $color[2] . '">'
172 . sprintf(_("Option Type '%s' Not Found"), $this->type)
173 . '</font>';
174 }
175
176 /* Add the script for this option. */
177 $result .= $this->script;
178
179 /* Now, return the created widget. */
180 return ($result);
181 }
182
183 function createWidget_String() {
184 switch ($this->size) {
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;
197 case SMOPT_SIZE_NORMAL:
198 default:
199 $width = 25;
200 }
201
202 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\">";
203 return ($result);
204 }
205
206 function createWidget_StrList() {
207 /* Begin the select tag. */
208 $result = "<select name=\"new_$this->name\">";
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. */
213 $new_option = "<option value=\"$real_value\"";
214
215 /* If this value is the current value, select it. */
216 if ($real_value == $this->value) {
217 $new_option .= ' selected';
218 }
219
220 /* Add the display value to our option string. */
221 $new_option .= ">$disp_value</option>";
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. */
228 $result .= '</select>';
229 return ($result);
230 }
231
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
264 function createWidget_TextArea() {
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 }
273 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
274 . "cols=\"$cols\">$this->value</textarea>";
275 return ($result);
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() {
287 /* Do the whole current value thing. */
288 if ($this->value != SMPREF_NO) {
289 $yes_chk = ' checked';
290 $no_chk = '';
291 } else {
292 $yes_chk = '';
293 $no_chk = ' checked';
294 }
295
296 /* Build the yes choice. */
297 $yes_option = '<input type="radio" name="new_' . $this->name
298 . '" value="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
299 . _("Yes");
300
301 /* Build the no choice. */
302 $no_option = '<input type="radio" name="new_' . $this->name
303 . '" value="' . SMPREF_NO . "\"$no_chk>&nbsp;"
304 . _("No");
305
306 /* Build and return the combined "boolean widget". */
307 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
308 return ($result);
309 }
310
311 function createWidget_Hidden() {
312 $result = '<input type="hidden" name="new_' . $this->name
313 . '" value="' . $this->value . '">';
314 return ($result);
315 }
316
317 function createWidget_Comment() {
318 $result = $this->comment;
319 return ($result);
320 }
321
322 function save() {
323 $function = $this->save_function;
324 $function($this);
325 }
326
327 function changed() {
328 return ($this->value != $this->new_value);
329 }
330 }
331
332 function save_option($option) {
333 if ( (float)substr(PHP_VERSION,0,3) < 4.1 ) {
334 global $_SESSION;
335 }
336 global $data_dir;
337 $username = $_SESSION['username'];
338
339 setPref($data_dir, $username, $option->name, $option->new_value);
340 }
341
342 function save_option_noop($option) {
343 /* Do nothing here... */
344 }
345
346 function create_optpage_element($optpage) {
347 return create_hidden_element('optpage', $optpage);
348 }
349
350 function create_optmode_element($optmode) {
351 return create_hidden_element('optmode', $optmode);
352 }
353
354 function create_hidden_element($name, $value) {
355 $result = '<input type="hidden" '
356 . 'name="' . $name . '" '
357 . 'value="' . $value . '">';
358 return ($result);
359 }
360
361 function create_option_groups($optgrps, $optvals) {
362 /* Build a simple array with which to start. */
363 $result = array();
364
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
372 /* Create a new SquirrelOption for each set of option values. */
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
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
414 /* Add this option to the option array. */
415 $result[$grpkey]['options'][] = $next_option;
416 }
417 }
418
419 /* Return our resulting array. */
420 return ($result);
421 }
422
423 function print_option_groups($option_groups) {
424 /* Print each option group. */
425 foreach ($option_groups as $next_optgrp) {
426 /* If it is not blank, print the name for this option group. */
427 if ($next_optgrp['name'] != '') {
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";
433 }
434
435 /* Print each option in this option group. */
436 foreach ($next_optgrp['options'] as $option) {
437 if ($option->type != SMOPT_TYPE_HIDDEN) {
438 echo html_tag( 'tr', "\n".
439 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
440 html_tag( 'td', $option->createHTMLWidget(), 'left' )
441 ) ."\n";
442 } else {
443 echo $option->createHTMLWidget();
444 }
445 }
446
447 /* Print an empty row after this option group. */
448 echo html_tag( 'tr',
449 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
450 ) . "\n";
451 }
452 }
453
454 function OptionSubmit( $name ) {
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";
459 }
460
461 ?>