959b92f1e581ddbfe9046dfc70bb208cb63719b3
[squirrelmail.git] / functions / options.php
1 <?php
2 /**
3 * options.php
4 *
5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Functions needed to display the options pages.
9 *
10 * $Id$
11 */
12
13 /**********************************************/
14 /* Define constants used in the options code. */
15 /**********************************************/
16
17 /* Define constants for the various option types. */
18 define('SMOPT_TYPE_STRING', 0);
19 define('SMOPT_TYPE_STRLIST', 1);
20 define('SMOPT_TYPE_TEXTAREA', 2);
21 define('SMOPT_TYPE_INTEGER', 3);
22 define('SMOPT_TYPE_FLOAT', 4);
23 define('SMOPT_TYPE_BOOLEAN', 5);
24 define('SMOPT_TYPE_HIDDEN', 6);
25 define('SMOPT_TYPE_COMMENT', 7);
26
27 /* Define constants for the options refresh levels. */
28 define('SMOPT_REFRESH_NONE', 0);
29 define('SMOPT_REFRESH_FOLDERLIST', 1);
30 define('SMOPT_REFRESH_ALL', 2);
31
32 /* Define constants for the options size. */
33 define('SMOPT_SIZE_TINY', 0);
34 define('SMOPT_SIZE_SMALL', 1);
35 define('SMOPT_SIZE_MEDIUM', 2);
36 define('SMOPT_SIZE_LARGE', 3);
37 define('SMOPT_SIZE_HUGE', 4);
38
39 /**
40 * SquirrelOption: An option for Squirrelmail.
41 *
42 * This class is a work in progress. When complete, it will handle
43 * presentation and saving of Squirrelmail user options in a simple,
44 * streamline manner. Stay tuned for more stuff.
45 *
46 * Also, I'd like to ask that people leave this alone (mostly :) until
47 * I get it a little further along. That should only be a day or two or
48 * three. I will remove this message when it is ready for primetime usage.
49 */
50 class SquirrelOption {
51 /* The basic stuff. */
52 var $name;
53 var $caption;
54 var $type;
55 var $refresh_level;
56 var $size;
57 var $comment;
58
59 /* The various 'values' for this options. */
60 var $value;
61 var $new_value;
62 var $possible_values;
63
64 /* This variable needs to be made private so it can not be messed with. */
65 /* I just don't remember how to do it right now and think it would be */
66 /* better to keep coding. Someone can fix it, if they want. Or I will. */
67 var $changed;
68
69 function SquirrelOption
70 ($name, $caption, $type, $refresh_level, $possible_values = '') {
71 /* Set the basic stuff. */
72 $this->name = $name;
73 $this->caption = $caption;
74 $this->type = $type;
75 $this->refresh_level = $refresh_level;
76 $this->possible_values = $possible_values;
77 $this->size = SMOPT_SIZE_MEDIUM;
78 $this->comment = '';
79
80 /* Check for a current value. */
81 if (isset($GLOBALS[$name])) {
82 $this->value = $GLOBALS[$name];
83 } else {
84 $this->value = '';
85 }
86
87 /* Check for a new value. */
88 if (isset($GLOBALS["new_$name"])) {
89 $this->new_value = $GLOBALS["new_$name"];
90 $this->changed = ($this->value !== $this->new_value);
91 } else {
92 $this->new_value = '';
93 $this->changed = false;
94 }
95 }
96
97 /* Set the size for this option. */
98 function setSize($size) {
99 $this->size = $size;
100 }
101
102 /* Set the comment for this option. */
103 function setComment($comment) {
104 $this->comment = $comment;
105 }
106
107 function createHTMLWidget() {
108 switch ($this->type) {
109 case SMOPT_TYPE_STRING:
110 $result = $this->createWidget_String();
111 break;
112 case SMOPT_TYPE_STRLIST:
113 $result = $this->createWidget_StrList();
114 break;
115 case SMOPT_TYPE_TEXTAREA:
116 $result = $this->createWidget_TextArea();
117 break;
118 case SMOPT_TYPE_INTEGER:
119 $result = $this->createWidget_Integer();
120 break;
121 case SMOPT_TYPE_FLOAT:
122 $result = $this->createWidget_Float();
123 break;
124 case SMOPT_TYPE_BOOLEAN:
125 $result = $this->createWidget_Boolean();
126 break;
127 case SMOPT_TYPE_HIDDEN:
128 $result = $this->createWidget_Hidden();
129 break;
130 case SMOPT_TYPE_COMMENT:
131 $result = $this->createWidget_Comment();
132 break;
133 default:
134 $result = '<FONT COLOR=RED>'
135 . sprintf(_("Option Type '%s' Not Found"), $this->type)
136 . '</FONT>';
137 }
138
139 /* Now, return the created widget. */
140 return ($result);
141 }
142
143 function createWidget_String() {
144 switch ($this->size) {
145 case SMOPT_SIZE_TINY: $width = 5; break;
146 case SMOPT_SIZE_SMALL: $width = 12; break;
147 case SMOPT_SIZE_LARGE: $width = 38; break;
148 case SMOPT_SIZE_HUGE: $width = 50; break;
149 case SMOPT_SIZE_NORMAL:
150 default: $width = 25;
151 }
152
153 $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
154 return ($result);
155 }
156
157 function createWidget_StrList() {
158 /* Begin the select tag. */
159 $result = "<SELECT NAME=\"new_$this->name\">";
160
161 /* Add each possible value to the select list. */
162 foreach ($this->possible_values as $real_value => $disp_value) {
163 /* Start the next new option string. */
164 $new_option = "<OPTION VALUE=\"$real_value\"";
165
166 /* If this value is the current value, select it. */
167 if ($real_value == $this->value) {
168 $new_option .= ' SELECTED';
169 }
170
171 /* Add the display value to our option string. */
172 $new_option .= ">$disp_value</OPTION>";
173
174 /* And add the new option string to our select tag. */
175 $result .= $new_option;
176 }
177
178 /* Close the select tag and return our happy result. */
179 $result .= '</SELECT>';
180 return ($result);
181 }
182
183 function createWidget_TextArea() {
184 switch ($this->size) {
185 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
186 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
187 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
188 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
189 case SMOPT_SIZE_NORMAL:
190 default: $rows = 5; $cols = 50;
191 }
192 $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
193 . "COLS=\"$cols\">$this->value</TEXTAREA>";
194 return ($result);
195 }
196
197 function createWidget_Integer() {
198 return ($this->createWidget_String());
199 }
200
201 function createWidget_Float() {
202 return ($this->createWidget_String());
203 }
204
205 function createWidget_Boolean() {
206 /* Do the whole current value thing. */
207 if ($this->value != SMPREF_NO) {
208 $yes_chk = ' CHECKED';
209 $no_chk = '';
210 } else {
211 $yes_chk = '';
212 $no_chk = ' CHECKED';
213 }
214
215 /* Build the yes choice. */
216 $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
217 . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
218 . _("Yes");
219
220 /* Build the no choice. */
221 $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
222 . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
223 . _("No");
224
225 /* Build and return the combined "boolean widget". */
226 $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
227 return ($result);
228 }
229
230 function createWidget_Hidden() {
231 $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
232 . '" VALUE="' . $this->value . '">';
233 return ($result);
234 }
235
236 function createWidget_Comment() {
237 $result = $this->comment;
238 return ($result);
239 }
240
241 function hasChanged() {
242 return ($this->changed);
243 }
244 }
245
246 function createOptionGroups($optgrps, $optvals) {
247 /* Build a simple array with which to start. */
248 $result = array();
249
250 /* Create option group for each option group name. */
251 foreach ($optgrps as $grpkey => $grpname) {
252 $result[$grpkey] = array();
253 $result[$grpkey]['name'] = $grpname;
254 $result[$grpkey]['options'] = array();
255 }
256
257 /* Create a new SquirrelOption for each set of option values. */
258 foreach ($optvals as $grpkey => $grpopts) {
259 foreach ($grpopts as $optset) {
260 if (isset($optset['posvals'])) {
261 /* Create a new option with all values given. */
262 $next_option = new SquirrelOption(
263 $optset['name'],
264 $optset['caption'],
265 $optset['type'],
266 $optset['refresh'],
267 $optset['posvals']
268 );
269 } else {
270 /* Create a new option with all but possible values given. */
271 $next_option = new SquirrelOption(
272 $optset['name'],
273 $optset['caption'],
274 $optset['type'],
275 $optset['refresh']
276 );
277 }
278
279 /* If provided, set the size for this option. */
280 if (isset($optset['size'])) {
281 $next_option->setSize($optset['size']);
282 }
283
284 /* If provided, set the comment for this option. */
285 if (isset($optset['comment'])) {
286 $next_option->setComment($optset['comment']);
287 }
288
289 /* Add this option to the option array. */
290 $result[$grpkey]['options'][] = $next_option;
291 }
292 }
293
294 /* Return our resulting array. */
295 return ($result);
296 }
297
298 function printOptionGroups($option_groups) {
299 foreach ($option_groups as $next_optgrp) {
300 echo '<TR><TD ALIGN="CENTER" VALIGN="MIDDLE" COLSPAN="2" NOWRAP><B>'
301 . $next_optgrp['name'] . "</B></TD></TR>\n";
302 foreach ($next_optgrp['options'] as $option) {
303 if ($option->type != SMOPT_TYPE_HIDDEN) {
304 echo "<TR>\n";
305 echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
306 . $option->caption . ":</TD>\n";
307 echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
308 echo "</TR>\n";
309 } else {
310 echo $option->createHTMLWidget();
311 }
312 }
313 echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>\n";
314 }
315 }
316
317 function OptionSelect( $title, $name, $data, $default, $show = '', $store = '' ) {
318
319 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
320 "<select name=\"$name\">";
321 foreach( $data as $key => $opt ) {
322 if ( $store == '' ) {
323 $vl = $key;
324 } else{
325 $vl = $opt[$store];
326 }
327 if ( $show == '' ) {
328 $nm = $opt;
329 } else{
330 $nm = $opt[$show];
331 }
332 if ( $nm <> '') {
333 echo "<option value=\"$vl\"";
334 if( $vl == $default ) {
335 echo ' selected';
336 }
337 echo ">$nm</option>\n";
338 }
339 }
340 echo "</select></td></tr>\n";
341 }
342
343 function OptionRadio( $title, $name, $data, $default, $show = '', $store = '', $sep = '&nbsp; &nbsp;' ) {
344 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>";
345 foreach( $data as $key => $opt ) {
346 if ( $store == '' ) {
347 $vl = $key;
348 } else{
349 $vl = $opt[$store];
350 }
351 if ( $show == '' ) {
352 $nm = $opt;
353 } else{
354 $nm = $opt[$show];
355 }
356 if ( $nm <> '') {
357 echo "<input type=\"radio\" name=\"$name\" value=\"$vl\"";
358 if( $vl == $default ) {
359 echo ' checked';
360 }
361 echo ">$nm $sep\n";
362 }
363 }
364 echo "</td></tr>\n";
365 }
366
367 function OptionText( $title, $name, $value, $size ) {
368 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
369 "<input name=\"$name\" value=\"$value\" size=\"$size\">" .
370 "</td></tr>\n";
371 }
372
373 function OptionHidden( $name, $value ) {
374 echo "<INPUT TYPE=HIDDEN NAME=\"$name\" VALUE=\"$value\">\n";
375 }
376
377 function OptionCheck( $title, $name, $value, $comment ) {
378 if ( $value )
379 $chk = 'checked';
380 echo "<tr><td align=right valign=middle nowrap>$title: </td><td>" .
381 "<input type=\"checkbox\" name=\"$name\" $chk> $comment" .
382 "</td></tr>\n";
383 }
384
385 function OptionTitle( $title ) {
386 echo "<tr><td colspan=2 align=left valign=middle nowrap><b>$title</b></td></tr>\n";
387 }
388
389 function OptionSubmit( $name ) {
390 echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
391 '</td></tr>';
392 }
393
394 ?>