r2l by Yoav
[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);
6b4bd11f 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);
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. */
2a50fbd7 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;
6b4bd11f 148 case SMOPT_TYPE_textarea:
a3ec3c91 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;
2a50fbd7 160 case SMOPT_TYPE_HIDDEN:
a3ec3c91 161 $result = $this->createWidget_Hidden();
162 break;
bbcafebd 163 case SMOPT_TYPE_COMMENT:
164 $result = $this->createWidget_Comment();
165 break;
a3ec3c91 166 default:
6b4bd11f 167 $result = '<font color="' . $color[2] . '">'
a3ec3c91 168 . sprintf(_("Option Type '%s' Not Found"), $this->type)
6b4bd11f 169 . '</font>';
a3ec3c91 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
6b4bd11f 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. */
6b4bd11f 204 $result = "<select name=\"new_$this->name\">";
a3ec3c91 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. */
6b4bd11f 209 $new_option = "<option value=\"$real_value\"";
a3ec3c91 210
211 /* If this value is the current value, select it. */
212 if ($real_value == $this->value) {
6b4bd11f 213 $new_option .= ' selected';
a3ec3c91 214 }
215
216 /* Add the display value to our option string. */
6b4bd11f 217 $new_option .= ">$disp_value</option>";
a3ec3c91 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. */
6b4bd11f 224 $result .= '</select>';
a3ec3c91 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 }
6b4bd11f 237 $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
238 . "cols=\"$cols\">$this->value</textarea>";
bbcafebd 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) {
6b4bd11f 253 $yes_chk = ' checked';
fd87494d 254 $no_chk = '';
255 } else {
256 $yes_chk = '';
6b4bd11f 257 $no_chk = ' checked';
fd87494d 258 }
259
260 /* Build the yes choice. */
6b4bd11f 261 $yes_option = '<input type="radio" name="new_' . $this->name
262 . '" value="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
fd87494d 263 . _("Yes");
264
265 /* Build the no choice. */
6b4bd11f 266 $no_option = '<input type="radio" name="new_' . $this->name
267 . '" value="' . SMPREF_NO . "\"$no_chk>&nbsp;"
fd87494d 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() {
6b4bd11f 276 $result = '<input type="hidden" name="new_' . $this->name
277 . '" value="' . $this->value . '">';
a3ec3c91 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) {
6b4bd11f 317 $result = '<input type="hidden" '
318 . 'name="' . $name . '" '
319 . 'value="' . $value . '">';
cbe5423b 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'] != '') {
6b4bd11f 390 echo html_tag( 'tr', "\n".
391 html_tag( 'td',
392 '<b>' . $next_optgrp['name'] . '</b>' ,
393 'center' ,'', 'valign="middle" colspan="2" nowrap' )
394 ) ."\n";
7e235a1a 395 }
396
397 /* Print each option in this option group. */
bbcafebd 398 foreach ($next_optgrp['options'] as $option) {
2a50fbd7 399 if ($option->type != SMOPT_TYPE_HIDDEN) {
6b4bd11f 400 echo html_tag( 'tr', "\n".
401 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
402 html_tag( 'td', $option->createHTMLWidget(), 'left' )
403 ) ."\n";
bbcafebd 404 } else {
405 echo $option->createHTMLWidget();
406 }
407 }
7e235a1a 408
409 /* Print an empty row after this option group. */
6b4bd11f 410 echo html_tag( 'tr',
411 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
412 ) . "\n";
bbcafebd 413 }
414}
415
9962527a 416function OptionSubmit( $name ) {
6b4bd11f 417 echo html_tag( 'tr',
418 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' ) .
419 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )
420 ) . "\n";
9962527a 421}
02ddcd00 422
23d6bd09 423?>