Added (c) stuff and some formatting.
[squirrelmail.git] / functions / options.php
CommitLineData
44ef0f47 1<?php
2ba13803 2
9962527a 3 /**
4 * options.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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 */
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);
39
cbe5423b 40define('SMOPT_SAVE_DEFAULT', 'save_option');
41define('SMOPT_SAVE_NOOP', 'save_option_noop');
42
9962527a 43/**
44 * SquirrelOption: An option for Squirrelmail.
45 *
46 * This class is a work in progress. When complete, it will handle
47 * presentation and saving of Squirrelmail user options in a simple,
48 * streamline manner. Stay tuned for more stuff.
49 *
50 * Also, I'd like to ask that people leave this alone (mostly :) until
51 * I get it a little further along. That should only be a day or two or
52 * three. I will remove this message when it is ready for primetime usage.
53 */
54class SquirrelOption {
55 /* The basic stuff. */
56 var $name;
57 var $caption;
9962527a 58 var $type;
a3ec3c91 59 var $refresh_level;
bbcafebd 60 var $size;
61 var $comment;
cbe5423b 62 var $script;
63
64 /* The name of the Save Function for this option. */
65 var $save_function;
9962527a 66
67 /* The various 'values' for this options. */
68 var $value;
69 var $new_value;
a3ec3c91 70 var $possible_values;
9962527a 71
9962527a 72 function SquirrelOption
a3ec3c91 73 ($name, $caption, $type, $refresh_level, $possible_values = '') {
9962527a 74 /* Set the basic stuff. */
75 $this->name = $name;
76 $this->caption = $caption;
9962527a 77 $this->type = $type;
a3ec3c91 78 $this->refresh_level = $refresh_level;
79 $this->possible_values = $possible_values;
bbcafebd 80 $this->size = SMOPT_SIZE_MEDIUM;
81 $this->comment = '';
cbe5423b 82 $this->script = '';
a3ec3c91 83
84 /* Check for a current value. */
85 if (isset($GLOBALS[$name])) {
86 $this->value = $GLOBALS[$name];
87 } else {
88 $this->value = '';
89 }
9962527a 90
a3ec3c91 91 /* Check for a new value. */
9962527a 92 if (isset($GLOBALS["new_$name"])) {
93 $this->new_value = $GLOBALS["new_$name"];
a3ec3c91 94 } else {
95 $this->new_value = '';
44ef0f47 96 }
cbe5423b 97
98 /* Set the default save function. */
99 if ((type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
100 $this->save_function = SMOPT_SAVE_DEFAULT;
101 } else {
102 $this->save_function = SMOPT_SAVE_NOOP;
103 }
104 }
105
106 /* Set the value for this option. */
107 function setValue($value) {
108 $this->value = $value;
109 }
110
111 /* Set the new value for this option. */
112 function setNewValue($new_value) {
113 $this->new_value = $new_value;
9962527a 114 }
44ef0f47 115
bbcafebd 116 /* Set the size for this option. */
117 function setSize($size) {
118 $this->size = $size;
119 }
120
121 /* Set the comment for this option. */
122 function setComment($comment) {
123 $this->comment = $comment;
124 }
125
cbe5423b 126 /* Set the script for this option. */
127 function setScript($script) {
128 $this->script = $script;
129 }
130
131 /* Set the save function for this option. */
132 function setSaveFunction($save_function) {
133 $this->save_function = $save_function;
134 }
135
a3ec3c91 136 function createHTMLWidget() {
cbe5423b 137 global $javascript_on;
138
139 /* Get the widget for this option type. */
a3ec3c91 140 switch ($this->type) {
141 case SMOPT_TYPE_STRING:
142 $result = $this->createWidget_String();
143 break;
144 case SMOPT_TYPE_STRLIST:
145 $result = $this->createWidget_StrList();
146 break;
147 case SMOPT_TYPE_TEXTAREA:
148 $result = $this->createWidget_TextArea();
149 break;
150 case SMOPT_TYPE_INTEGER:
151 $result = $this->createWidget_Integer();
152 break;
153 case SMOPT_TYPE_FLOAT:
154 $result = $this->createWidget_Float();
155 break;
156 case SMOPT_TYPE_BOOLEAN:
157 $result = $this->createWidget_Boolean();
158 break;
159 case SMOPT_TYPE_HIDDEN:
160 $result = $this->createWidget_Hidden();
161 break;
bbcafebd 162 case SMOPT_TYPE_COMMENT:
163 $result = $this->createWidget_Comment();
164 break;
a3ec3c91 165 default:
166 $result = '<FONT COLOR=RED>'
167 . sprintf(_("Option Type '%s' Not Found"), $this->type)
168 . '</FONT>';
169 }
170
cbe5423b 171 /* Add the script for this option. */
172 $result .= $this->script;
173
a3ec3c91 174 /* Now, return the created widget. */
175 return ($result);
176 }
177
178 function createWidget_String() {
bbcafebd 179 switch ($this->size) {
180 case SMOPT_SIZE_TINY: $width = 5; break;
181 case SMOPT_SIZE_SMALL: $width = 12; break;
182 case SMOPT_SIZE_LARGE: $width = 38; break;
183 case SMOPT_SIZE_HUGE: $width = 50; break;
184 case SMOPT_SIZE_NORMAL:
185 default: $width = 25;
186 }
187
188 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
a3ec3c91 189 return ($result);
190 }
191
192 function createWidget_StrList() {
193 /* Begin the select tag. */
194 $result = "<SELECT NAME=\"new_$this->name\">";
195
196 /* Add each possible value to the select list. */
197 foreach ($this->possible_values as $real_value => $disp_value) {
198 /* Start the next new option string. */
199 $new_option = "<OPTION VALUE=\"$real_value\"";
200
201 /* If this value is the current value, select it. */
202 if ($real_value == $this->value) {
203 $new_option .= ' SELECTED';
204 }
205
206 /* Add the display value to our option string. */
207 $new_option .= ">$disp_value</OPTION>";
208
209 /* And add the new option string to our select tag. */
210 $result .= $new_option;
211 }
212
213 /* Close the select tag and return our happy result. */
214 $result .= '</SELECT>';
215 return ($result);
216 }
217
218 function createWidget_TextArea() {
bbcafebd 219 switch ($this->size) {
220 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
221 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
222 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
223 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
224 case SMOPT_SIZE_NORMAL:
225 default: $rows = 5; $cols = 50;
226 }
227 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
228 . "COLS=\"$cols\">$this->value</TEXTAREA>";
229 return ($result);
a3ec3c91 230 }
231
232 function createWidget_Integer() {
233 return ($this->createWidget_String());
234 }
235
236 function createWidget_Float() {
237 return ($this->createWidget_String());
238 }
239
240 function createWidget_Boolean() {
fd87494d 241 /* Do the whole current value thing. */
242 if ($this->value != SMPREF_NO) {
243 $yes_chk = ' CHECKED';
244 $no_chk = '';
245 } else {
246 $yes_chk = '';
247 $no_chk = ' CHECKED';
248 }
249
250 /* Build the yes choice. */
251 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
252 . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
253 . _("Yes");
254
255 /* Build the no choice. */
256 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
257 . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
258 . _("No");
259
260 /* Build and return the combined "boolean widget". */
261 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
262 return ($result);
a3ec3c91 263 }
264
265 function createWidget_Hidden() {
266 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
267 . '" VALUE="' . $this->value . '">';
268 return ($result);
269 }
270
bbcafebd 271 function createWidget_Comment() {
272 $result = $this->comment;
273 return ($result);
274 }
275
cbe5423b 276 function save() {
277 $function = $this->save_function;
278 $function($this);
44ef0f47 279 }
cbe5423b 280
281 function changed() {
282 return ($this->value !== $this->new_value);
283 }
284}
285
286function save_option($option) {
287 global $data_dir, $username;
288 setPref($data_dir, $username, $option->name, $option->new_value);
289
290 /* I do not know if this next line does any good. */
291 $GLOBALS[$name] = $option->new_value;
292}
293
294function save_option_noop($option) {
295 /* Do nothing here... */
9962527a 296}
44ef0f47 297
cbe5423b 298function create_optpage_element($optpage) {
299 return create_hidden_element('optpage', $optpage);
300}
301
302function create_optmode_element($optmode) {
303 return create_hidden_element('optmode', $optmode);
304}
305
306function create_hidden_element($name, $value) {
307 $result = '<INPUT TYPE="HIDDEN" '
308 . 'NAME="' . $name . '" '
309 . 'VALUE="' . $value . '">';
310 return ($result);
311}
312
313
bbcafebd 314function createOptionGroups($optgrps, $optvals) {
cbe5423b 315 return create_option_groups($optgrps, $optvals);
316}
317
318function create_option_groups($optgrps, $optvals) {
a3ec3c91 319 /* Build a simple array with which to start. */
320 $result = array();
321
bbcafebd 322 /* Create option group for each option group name. */
323 foreach ($optgrps as $grpkey => $grpname) {
324 $result[$grpkey] = array();
325 $result[$grpkey]['name'] = $grpname;
326 $result[$grpkey]['options'] = array();
327 }
328
a3ec3c91 329 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 330 foreach ($optvals as $grpkey => $grpopts) {
331 foreach ($grpopts as $optset) {
332 if (isset($optset['posvals'])) {
333 /* Create a new option with all values given. */
334 $next_option = new SquirrelOption(
335 $optset['name'],
336 $optset['caption'],
337 $optset['type'],
338 $optset['refresh'],
339 $optset['posvals']
340 );
341 } else {
342 /* Create a new option with all but possible values given. */
343 $next_option = new SquirrelOption(
344 $optset['name'],
345 $optset['caption'],
346 $optset['type'],
347 $optset['refresh']
348 );
349 }
350
351 /* If provided, set the size for this option. */
352 if (isset($optset['size'])) {
353 $next_option->setSize($optset['size']);
354 }
355
356 /* If provided, set the comment for this option. */
357 if (isset($optset['comment'])) {
358 $next_option->setComment($optset['comment']);
359 }
360
cbe5423b 361 /* If provided, set the save function for this option. */
362 if (isset($optset['save'])) {
363 $next_option->setSaveFunction($optset['save']);
364 }
365
366 /* If provided, set the script for this option. */
367 if (isset($optset['script'])) {
368 $next_option->setScript($optset['script']);
369 }
370
bbcafebd 371 /* Add this option to the option array. */
372 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 373 }
374 }
375
376 /* Return our resulting array. */
377 return ($result);
378}
379
bbcafebd 380function printOptionGroups($option_groups) {
cbe5423b 381 print_option_groups($option_groups);
382}
383
384function print_option_groups($option_groups) {
bbcafebd 385 foreach ($option_groups as $next_optgrp) {
386 echo '<TR><TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2" NOWRAP><B>'
387 . $next_optgrp['name'] . "</B></TD></TR>\n";
388 foreach ($next_optgrp['options'] as $option) {
389 if ($option->type != SMOPT_TYPE_HIDDEN) {
390 echo "<TR>\n";
391 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
392 . $option->caption . ":</TD>\n";
393 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
394 echo "</TR>\n";
395 } else {
396 echo $option->createHTMLWidget();
397 }
398 }
399 echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>\n";
400 }
401}
402
9962527a 403function OptionSubmit( $name ) {
404 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
405 '</td></tr>';
406}
02ddcd00 407
23d6bd09 408?>