67c3c132ac378c7562aaf716aa5043fb693b4100
[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
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);
32
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);
39 define('SMOPT_SIZE_NORMAL', 5);
40
41 define('SMOPT_SAVE_DEFAULT', 'save_option');
42 define('SMOPT_SAVE_NOOP', 'save_option_noop');
43
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 */
55 class SquirrelOption {
56 /* The basic stuff. */
57 var $name;
58 var $caption;
59 var $type;
60 var $refresh_level;
61 var $size;
62 var $comment;
63 var $script;
64
65 /* The name of the Save Function for this option. */
66 var $save_function;
67
68 /* The various 'values' for this options. */
69 var $value;
70 var $new_value;
71 var $possible_values;
72
73 function SquirrelOption
74 ($name, $caption, $type, $refresh_level, $possible_values = '') {
75 /* Set the basic stuff. */
76 $this->name = $name;
77 $this->caption = $caption;
78 $this->type = $type;
79 $this->refresh_level = $refresh_level;
80 $this->possible_values = $possible_values;
81 $this->size = SMOPT_SIZE_MEDIUM;
82 $this->comment = '';
83 $this->script = '';
84
85 /* Check for a current value. */
86 if (isset($GLOBALS[$name])) {
87 $this->value = $GLOBALS[$name];
88 } else {
89 $this->value = '';
90 }
91
92 /* Check for a new value. */
93 if (isset($_POST["new_$name"])) {
94 $this->new_value = $_POST["new_$name"];
95 } else {
96 $this->new_value = '';
97 }
98
99 /* Set the default save function. */
100 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
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;
115 }
116
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
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
137 function createHTMLWidget() {
138 global $javascript_on;
139
140 /* Get the widget for this option type. */
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;
163 case SMOPT_TYPE_COMMENT:
164 $result = $this->createWidget_Comment();
165 break;
166 default:
167 $result = '<font color="' . $color[2] . '">'
168 . sprintf(_("Option Type '%s' Not Found"), $this->type)
169 . '</font>';
170 }
171
172 /* Add the script for this option. */
173 $result .= $this->script;
174
175 /* Now, return the created widget. */
176 return ($result);
177 }
178
179 function createWidget_String() {
180 switch ($this->size) {
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;
193 case SMOPT_SIZE_NORMAL:
194 default:
195 $width = 25;
196 }
197
198 $result = "<input name=\"new_$this->name\" value=\"$this->value\" size=\"$width\">";
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() {
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);
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() {
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);
273 }
274
275 function createWidget_Hidden() {
276 $result = '<input type="hidden" name="new_' . $this->name
277 . '" value="' . $this->value . '">';
278 return ($result);
279 }
280
281 function createWidget_Comment() {
282 $result = $this->comment;
283 return ($result);
284 }
285
286 function save() {
287 $function = $this->save_function;
288 $function($this);
289 }
290
291 function changed() {
292 return ($this->value != $this->new_value);
293 }
294 }
295
296 function save_option($option) {
297 if ( (float)substr(PHP_VERSION,0,3) < 4.1 ) {
298 global $_SESSION;
299 }
300 global $data_dir;
301 $username = $_SESSION['username'];
302
303 setPref($data_dir, $username, $option->name, $option->new_value);
304 }
305
306 function save_option_noop($option) {
307 /* Do nothing here... */
308 }
309
310 function create_optpage_element($optpage) {
311 return create_hidden_element('optpage', $optpage);
312 }
313
314 function create_optmode_element($optmode) {
315 return create_hidden_element('optmode', $optmode);
316 }
317
318 function create_hidden_element($name, $value) {
319 $result = '<input type="hidden" '
320 . 'name="' . $name . '" '
321 . 'value="' . $value . '">';
322 return ($result);
323 }
324
325 function create_option_groups($optgrps, $optvals) {
326 /* Build a simple array with which to start. */
327 $result = array();
328
329 /* Create option group for each option group name. */
330 foreach ($optgrps as $grpkey => $grpname) {
331 $result[$grpkey] = array();
332 $result[$grpkey]['name'] = $grpname;
333 $result[$grpkey]['options'] = array();
334 }
335
336 /* Create a new SquirrelOption for each set of option values. */
337 foreach ($optvals as $grpkey => $grpopts) {
338 foreach ($grpopts as $optset) {
339 if (isset($optset['posvals'])) {
340 /* Create a new option with all values given. */
341 $next_option = new SquirrelOption(
342 $optset['name'],
343 $optset['caption'],
344 $optset['type'],
345 $optset['refresh'],
346 $optset['posvals']
347 );
348 } else {
349 /* Create a new option with all but possible values given. */
350 $next_option = new SquirrelOption(
351 $optset['name'],
352 $optset['caption'],
353 $optset['type'],
354 $optset['refresh']
355 );
356 }
357
358 /* If provided, set the size for this option. */
359 if (isset($optset['size'])) {
360 $next_option->setSize($optset['size']);
361 }
362
363 /* If provided, set the comment for this option. */
364 if (isset($optset['comment'])) {
365 $next_option->setComment($optset['comment']);
366 }
367
368 /* If provided, set the save function for this option. */
369 if (isset($optset['save'])) {
370 $next_option->setSaveFunction($optset['save']);
371 }
372
373 /* If provided, set the script for this option. */
374 if (isset($optset['script'])) {
375 $next_option->setScript($optset['script']);
376 }
377
378 /* Add this option to the option array. */
379 $result[$grpkey]['options'][] = $next_option;
380 }
381 }
382
383 /* Return our resulting array. */
384 return ($result);
385 }
386
387 function print_option_groups($option_groups) {
388 /* Print each option group. */
389 foreach ($option_groups as $next_optgrp) {
390 /* If it is not blank, print the name for this option group. */
391 if ($next_optgrp['name'] != '') {
392 echo html_tag( 'tr', "\n".
393 html_tag( 'td',
394 '<b>' . $next_optgrp['name'] . '</b>' ,
395 'center' ,'', 'valign="middle" colspan="2" nowrap' )
396 ) ."\n";
397 }
398
399 /* Print each option in this option group. */
400 foreach ($next_optgrp['options'] as $option) {
401 if ($option->type != SMOPT_TYPE_HIDDEN) {
402 echo html_tag( 'tr', "\n".
403 html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
404 html_tag( 'td', $option->createHTMLWidget(), 'left' )
405 ) ."\n";
406 } else {
407 echo $option->createHTMLWidget();
408 }
409 }
410
411 /* Print an empty row after this option group. */
412 echo html_tag( 'tr',
413 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
414 ) . "\n";
415 }
416 }
417
418 function OptionSubmit( $name ) {
419 echo html_tag( 'tr',
420 html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' ) .
421 html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )
422 ) . "\n";
423 }
424
425 ?>