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. */ |
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); |
bbcafebd |
26 | define('SMOPT_TYPE_COMMENT', 7); |
a3ec3c91 |
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 | |
bbcafebd |
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 | |
cbe5423b |
40 | define('SMOPT_SAVE_DEFAULT', 'save_option'); |
41 | define('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 | */ |
54 | class 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. */ |
03192051 |
99 | if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) { |
cbe5423b |
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> " |
253 | . _("Yes"); |
254 | |
255 | /* Build the no choice. */ |
256 | $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name |
257 | . '" VALUE="' . SMPREF_NO . "\"$no_chk> " |
258 | . _("No"); |
259 | |
260 | /* Build and return the combined "boolean widget". */ |
261 | $result = "$yes_option $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 | |
286 | function 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. */ |
03192051 |
291 | $GLOBALS[$option->name] = $option->new_value; |
cbe5423b |
292 | } |
293 | |
294 | function save_option_noop($option) { |
295 | /* Do nothing here... */ |
9962527a |
296 | } |
44ef0f47 |
297 | |
cbe5423b |
298 | function create_optpage_element($optpage) { |
299 | return create_hidden_element('optpage', $optpage); |
300 | } |
301 | |
302 | function create_optmode_element($optmode) { |
303 | return create_hidden_element('optmode', $optmode); |
304 | } |
305 | |
306 | function create_hidden_element($name, $value) { |
307 | $result = '<INPUT TYPE="HIDDEN" ' |
308 | . 'NAME="' . $name . '" ' |
309 | . 'VALUE="' . $value . '">'; |
310 | return ($result); |
311 | } |
312 | |
cbe5423b |
313 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
314 | /* Build a simple array with which to start. */ |
315 | $result = array(); |
316 | |
bbcafebd |
317 | /* Create option group for each option group name. */ |
318 | foreach ($optgrps as $grpkey => $grpname) { |
319 | $result[$grpkey] = array(); |
320 | $result[$grpkey]['name'] = $grpname; |
321 | $result[$grpkey]['options'] = array(); |
322 | } |
323 | |
a3ec3c91 |
324 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
325 | foreach ($optvals as $grpkey => $grpopts) { |
326 | foreach ($grpopts as $optset) { |
327 | if (isset($optset['posvals'])) { |
328 | /* Create a new option with all values given. */ |
329 | $next_option = new SquirrelOption( |
330 | $optset['name'], |
331 | $optset['caption'], |
332 | $optset['type'], |
333 | $optset['refresh'], |
334 | $optset['posvals'] |
335 | ); |
336 | } else { |
337 | /* Create a new option with all but possible values given. */ |
338 | $next_option = new SquirrelOption( |
339 | $optset['name'], |
340 | $optset['caption'], |
341 | $optset['type'], |
342 | $optset['refresh'] |
343 | ); |
344 | } |
345 | |
346 | /* If provided, set the size for this option. */ |
347 | if (isset($optset['size'])) { |
348 | $next_option->setSize($optset['size']); |
349 | } |
350 | |
351 | /* If provided, set the comment for this option. */ |
352 | if (isset($optset['comment'])) { |
353 | $next_option->setComment($optset['comment']); |
354 | } |
355 | |
cbe5423b |
356 | /* If provided, set the save function for this option. */ |
357 | if (isset($optset['save'])) { |
358 | $next_option->setSaveFunction($optset['save']); |
359 | } |
360 | |
361 | /* If provided, set the script for this option. */ |
362 | if (isset($optset['script'])) { |
363 | $next_option->setScript($optset['script']); |
364 | } |
365 | |
bbcafebd |
366 | /* Add this option to the option array. */ |
367 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
368 | } |
369 | } |
370 | |
371 | /* Return our resulting array. */ |
372 | return ($result); |
373 | } |
374 | |
cbe5423b |
375 | function print_option_groups($option_groups) { |
2fad95fa |
376 | /* Print each option group. */ |
bbcafebd |
377 | foreach ($option_groups as $next_optgrp) { |
2fad95fa |
378 | /* If it is not blank, print the name for this option group. */ |
379 | if ($next_optgrp['name'] != '') { |
380 | echo '<TR><TD ALIGN=CENTER VALIGN=MIDDLE COLSPAN=2 NOWRAP><B>' |
381 | . $next_optgrp['name'] |
382 | . "</B></TD></TR>\n"; |
383 | } |
384 | |
385 | /* Print each option in this option group. */ |
bbcafebd |
386 | foreach ($next_optgrp['options'] as $option) { |
387 | if ($option->type != SMOPT_TYPE_HIDDEN) { |
388 | echo "<TR>\n"; |
389 | echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">' |
390 | . $option->caption . ":</TD>\n"; |
391 | echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n"; |
392 | echo "</TR>\n"; |
393 | } else { |
394 | echo $option->createHTMLWidget(); |
395 | } |
396 | } |
2fad95fa |
397 | |
398 | /* Print an empty row after this option group. */ |
bbcafebd |
399 | echo "<TR><TD COLSPAN=\"2\"> </TD></TR>\n"; |
400 | } |
401 | } |
402 | |
9962527a |
403 | function OptionSubmit( $name ) { |
404 | echo '<tr><td> </td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' . |
405 | '</td></tr>'; |
406 | } |
02ddcd00 |
407 | |
23d6bd09 |
408 | ?> |