44ef0f47 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * options.php |
5 | * |
35586184 |
6 | * Functions needed to display the options pages. |
7 | * |
47ccfad4 |
8 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
10 | * @version $Id$ |
d6c32258 |
11 | * @package squirrelmail |
ca479ad1 |
12 | * @subpackage prefs |
35586184 |
13 | */ |
a3ec3c91 |
14 | |
15 | /**********************************************/ |
16 | /* Define constants used in the options code. */ |
17 | /**********************************************/ |
18 | |
19 | /* Define constants for the various option types. */ |
20 | define('SMOPT_TYPE_STRING', 0); |
21 | define('SMOPT_TYPE_STRLIST', 1); |
7e6d5ea3 |
22 | define('SMOPT_TYPE_TEXTAREA', 2); |
a3ec3c91 |
23 | define('SMOPT_TYPE_INTEGER', 3); |
24 | define('SMOPT_TYPE_FLOAT', 4); |
25 | define('SMOPT_TYPE_BOOLEAN', 5); |
2a50fbd7 |
26 | define('SMOPT_TYPE_HIDDEN', 6); |
bbcafebd |
27 | define('SMOPT_TYPE_COMMENT', 7); |
be2d5495 |
28 | define('SMOPT_TYPE_FLDRLIST', 8); |
a3ec3c91 |
29 | |
30 | /* Define constants for the options refresh levels. */ |
31 | define('SMOPT_REFRESH_NONE', 0); |
32 | define('SMOPT_REFRESH_FOLDERLIST', 1); |
33 | define('SMOPT_REFRESH_ALL', 2); |
34 | |
bbcafebd |
35 | /* Define constants for the options size. */ |
36 | define('SMOPT_SIZE_TINY', 0); |
37 | define('SMOPT_SIZE_SMALL', 1); |
38 | define('SMOPT_SIZE_MEDIUM', 2); |
39 | define('SMOPT_SIZE_LARGE', 3); |
40 | define('SMOPT_SIZE_HUGE', 4); |
88cb1b4d |
41 | define('SMOPT_SIZE_NORMAL', 5); |
bbcafebd |
42 | |
cbe5423b |
43 | define('SMOPT_SAVE_DEFAULT', 'save_option'); |
44 | define('SMOPT_SAVE_NOOP', 'save_option_noop'); |
45 | |
9962527a |
46 | /** |
598294a7 |
47 | * SquirrelOption: An option for SquirrelMail. |
9962527a |
48 | * |
8f6f9ba5 |
49 | * @package squirrelmail |
b4856b14 |
50 | * @subpackage prefs |
9962527a |
51 | */ |
52 | class SquirrelOption { |
b4856b14 |
53 | /** |
54 | * The name of this setting |
55 | * @var string |
56 | */ |
9962527a |
57 | var $name; |
b4856b14 |
58 | /** |
59 | * The text that prefaces setting on the preferences page |
60 | * @var string |
61 | */ |
9962527a |
62 | var $caption; |
b4856b14 |
63 | /** |
64 | * The type of INPUT element |
65 | * |
66 | * See SMOPT_TYPE_* defines |
67 | * @var integer |
68 | */ |
9962527a |
69 | var $type; |
b4856b14 |
70 | /** |
598294a7 |
71 | * Indicates if a link should be shown to refresh part |
b4856b14 |
72 | * or all of the window |
73 | * |
74 | * See SMOPT_REFRESH_* defines |
75 | * @var integer |
76 | */ |
a3ec3c91 |
77 | var $refresh_level; |
b4856b14 |
78 | /** |
79 | * Specifies the size of certain input items |
80 | * |
81 | * See SMOPT_SIZE_* defines |
82 | * @var integer |
83 | */ |
bbcafebd |
84 | var $size; |
b4856b14 |
85 | /** |
598294a7 |
86 | * Text that follows a text input or |
b4856b14 |
87 | * select list input on the preferences page |
598294a7 |
88 | * |
b4856b14 |
89 | * useful for indicating units, meanings of special values, etc. |
90 | * @var string |
91 | */ |
361d6e1b |
92 | var $trailing_text; |
b4856b14 |
93 | /** |
94 | * text displayed to the user |
95 | * |
96 | * Used with SMOPT_TYPE_COMMENT options |
97 | * @var string |
98 | */ |
bbcafebd |
99 | var $comment; |
b4856b14 |
100 | /** |
101 | * additional javascript or other code added to the user input |
102 | * @var string |
103 | */ |
cbe5423b |
104 | var $script; |
b4856b14 |
105 | /** |
598294a7 |
106 | * script (usually Javascript) that will be placed after (outside of) |
b4856b14 |
107 | * the INPUT tag |
108 | * @var string |
109 | */ |
6ae9e729 |
110 | var $post_script; |
cbe5423b |
111 | |
b4856b14 |
112 | /** |
113 | * The name of the Save Function for this option. |
114 | * @var string |
115 | */ |
cbe5423b |
116 | var $save_function; |
9962527a |
117 | |
118 | /* The various 'values' for this options. */ |
b4856b14 |
119 | /** |
120 | * default/preselected value for this option |
121 | * @var mixed |
122 | */ |
9962527a |
123 | var $value; |
b4856b14 |
124 | /** |
125 | * new option value |
126 | * @var mixed |
127 | */ |
9962527a |
128 | var $new_value; |
b4856b14 |
129 | /** |
598294a7 |
130 | * associative array, where each key is an actual input value |
b4856b14 |
131 | * and the corresponding value is what is displayed to the user |
132 | * for that list item in the drop-down list |
133 | * @var array |
134 | */ |
a3ec3c91 |
135 | var $possible_values; |
b4856b14 |
136 | /** |
137 | * disables html sanitizing. |
598294a7 |
138 | * |
139 | * WARNING - don't use it, if user input is possible in option |
140 | * or use own sanitizing functions. Currently works only with |
b4856b14 |
141 | * SMOPT_TYPE_STRLIST. |
142 | * @var bool |
143 | */ |
28520c87 |
144 | var $htmlencoded=false; |
99ecf044 |
145 | /** |
146 | * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget. |
147 | * See $flag argument in sqimap_mailbox_option_list() function. |
148 | * @var string |
149 | * @since 1.5.1 |
150 | */ |
151 | var $folder_filter='noselect'; |
9962527a |
152 | |
b4856b14 |
153 | /** |
154 | * Constructor function |
155 | * @param string $name |
156 | * @param string $caption |
157 | * @param integer $type |
158 | * @param integer $refresh_level |
159 | * @param mixed $initial_value |
160 | * @param array $possible_values |
161 | * @param bool $htmlencoded |
162 | */ |
9962527a |
163 | function SquirrelOption |
28520c87 |
164 | ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) { |
9962527a |
165 | /* Set the basic stuff. */ |
166 | $this->name = $name; |
167 | $this->caption = $caption; |
9962527a |
168 | $this->type = $type; |
a3ec3c91 |
169 | $this->refresh_level = $refresh_level; |
170 | $this->possible_values = $possible_values; |
28520c87 |
171 | $this->htmlencoded = $htmlencoded; |
bbcafebd |
172 | $this->size = SMOPT_SIZE_MEDIUM; |
361d6e1b |
173 | $this->trailing_text = ''; |
bbcafebd |
174 | $this->comment = ''; |
cbe5423b |
175 | $this->script = ''; |
6ae9e729 |
176 | $this->post_script = ''; |
a3ec3c91 |
177 | |
991c88e7 |
178 | //Check for a current value. |
179 | if (isset($GLOBALS[$name])) { |
a3ec3c91 |
180 | $this->value = $GLOBALS[$name]; |
17f3d242 |
181 | } else if (!empty($initial_value)) { |
182 | $this->value = $initial_value; |
a3ec3c91 |
183 | } else { |
184 | $this->value = ''; |
185 | } |
9962527a |
186 | |
a3ec3c91 |
187 | /* Check for a new value. */ |
b4856b14 |
188 | if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) { |
a3ec3c91 |
189 | $this->new_value = ''; |
44ef0f47 |
190 | } |
cbe5423b |
191 | |
192 | /* Set the default save function. */ |
2a50fbd7 |
193 | if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) { |
cbe5423b |
194 | $this->save_function = SMOPT_SAVE_DEFAULT; |
195 | } else { |
196 | $this->save_function = SMOPT_SAVE_NOOP; |
197 | } |
198 | } |
199 | |
b4856b14 |
200 | /** |
201 | * Set the value for this option. |
202 | * @param mixed $value |
203 | */ |
cbe5423b |
204 | function setValue($value) { |
205 | $this->value = $value; |
206 | } |
207 | |
b4856b14 |
208 | /** |
209 | * Set the new value for this option. |
210 | * @param mixed $new_value |
211 | */ |
cbe5423b |
212 | function setNewValue($new_value) { |
213 | $this->new_value = $new_value; |
9962527a |
214 | } |
44ef0f47 |
215 | |
b4856b14 |
216 | /** |
217 | * Set the size for this option. |
218 | * @param integer $size |
219 | */ |
bbcafebd |
220 | function setSize($size) { |
221 | $this->size = $size; |
222 | } |
223 | |
b4856b14 |
224 | /** |
225 | * Set the trailing_text for this option. |
226 | * @param string $trailing_text |
227 | */ |
361d6e1b |
228 | function setTrailingText($trailing_text) { |
229 | $this->trailing_text = $trailing_text; |
230 | } |
231 | |
b4856b14 |
232 | /** |
233 | * Set the comment for this option. |
234 | * @param string $comment |
235 | */ |
bbcafebd |
236 | function setComment($comment) { |
237 | $this->comment = $comment; |
238 | } |
239 | |
b4856b14 |
240 | /** |
241 | * Set the script for this option. |
598294a7 |
242 | * @param string $script |
b4856b14 |
243 | */ |
cbe5423b |
244 | function setScript($script) { |
245 | $this->script = $script; |
246 | } |
247 | |
b4856b14 |
248 | /** |
249 | * Set the "post script" for this option. |
250 | * @param string $post_script |
251 | */ |
6ae9e729 |
252 | function setPostScript($post_script) { |
253 | $this->post_script = $post_script; |
254 | } |
255 | |
b4856b14 |
256 | /** |
257 | * Set the save function for this option. |
258 | * @param string $save_function |
259 | */ |
cbe5423b |
260 | function setSaveFunction($save_function) { |
261 | $this->save_function = $save_function; |
262 | } |
263 | |
99ecf044 |
264 | /** |
265 | * Set the trailing_text for this option. |
266 | * @param string $folder_filter |
267 | * @since 1.5.1 |
268 | */ |
269 | function setFolderFilter($folder_filter) { |
270 | $this->folder_filter = $folder_filter; |
271 | } |
272 | |
b4856b14 |
273 | /** |
274 | * Creates fields on option pages according to option type |
275 | * |
276 | * Function that calls other createWidget* functions. |
598294a7 |
277 | * @return string html formated option field |
b4856b14 |
278 | */ |
a3ec3c91 |
279 | function createHTMLWidget() { |
ce68b76b |
280 | global $color; |
cbe5423b |
281 | |
62f7daa5 |
282 | // Use new value if available |
74e44765 |
283 | if (!empty($this->new_value)) { |
284 | $tempValue = $this->value; |
285 | $this->value = $this->new_value; |
286 | } |
287 | |
cbe5423b |
288 | /* Get the widget for this option type. */ |
a3ec3c91 |
289 | switch ($this->type) { |
290 | case SMOPT_TYPE_STRING: |
37a3ed17 |
291 | $result = $this->createWidget_String(); |
a3ec3c91 |
292 | break; |
293 | case SMOPT_TYPE_STRLIST: |
37a3ed17 |
294 | $result = $this->createWidget_StrList(); |
a3ec3c91 |
295 | break; |
7e6d5ea3 |
296 | case SMOPT_TYPE_TEXTAREA: |
37a3ed17 |
297 | $result = $this->createWidget_TextArea(); |
a3ec3c91 |
298 | break; |
299 | case SMOPT_TYPE_INTEGER: |
37a3ed17 |
300 | $result = $this->createWidget_Integer(); |
a3ec3c91 |
301 | break; |
302 | case SMOPT_TYPE_FLOAT: |
37a3ed17 |
303 | $result = $this->createWidget_Float(); |
a3ec3c91 |
304 | break; |
305 | case SMOPT_TYPE_BOOLEAN: |
37a3ed17 |
306 | $result = $this->createWidget_Boolean(); |
a3ec3c91 |
307 | break; |
2a50fbd7 |
308 | case SMOPT_TYPE_HIDDEN: |
37a3ed17 |
309 | $result = $this->createWidget_Hidden(); |
a3ec3c91 |
310 | break; |
bbcafebd |
311 | case SMOPT_TYPE_COMMENT: |
37a3ed17 |
312 | $result = $this->createWidget_Comment(); |
bbcafebd |
313 | break; |
be2d5495 |
314 | case SMOPT_TYPE_FLDRLIST: |
37a3ed17 |
315 | $result = $this->createWidget_FolderList(); |
be2d5495 |
316 | break; |
a3ec3c91 |
317 | default: |
6b4bd11f |
318 | $result = '<font color="' . $color[2] . '">' |
a3ec3c91 |
319 | . sprintf(_("Option Type '%s' Not Found"), $this->type) |
6b4bd11f |
320 | . '</font>'; |
a3ec3c91 |
321 | } |
322 | |
6ae9e729 |
323 | /* Add the "post script" for this option. */ |
324 | $result .= $this->post_script; |
62f7daa5 |
325 | |
74e44765 |
326 | // put correct value back if need be |
327 | if (!empty($this->new_value)) { |
328 | $this->value = $tempValue; |
329 | } |
330 | |
a3ec3c91 |
331 | /* Now, return the created widget. */ |
332 | return ($result); |
333 | } |
334 | |
b4856b14 |
335 | /** |
336 | * Create string field |
337 | * @return string html formated option field |
338 | */ |
37a3ed17 |
339 | function createWidget_String() { |
bbcafebd |
340 | switch ($this->size) { |
88cb1b4d |
341 | case SMOPT_SIZE_TINY: |
342 | $width = 5; |
343 | break; |
344 | case SMOPT_SIZE_SMALL: |
345 | $width = 12; |
346 | break; |
347 | case SMOPT_SIZE_LARGE: |
348 | $width = 38; |
349 | break; |
350 | case SMOPT_SIZE_HUGE: |
351 | $width = 50; |
352 | break; |
bbcafebd |
353 | case SMOPT_SIZE_NORMAL: |
88cb1b4d |
354 | default: |
355 | $width = 25; |
bbcafebd |
356 | } |
357 | |
88440172 |
358 | $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" . |
62f7daa5 |
359 | htmlspecialchars($this->value) . |
361d6e1b |
360 | "\" size=\"$width\" $this->script />$this->trailing_text\n"; |
a3ec3c91 |
361 | return ($result); |
362 | } |
363 | |
b4856b14 |
364 | /** |
365 | * Create selection box |
366 | * @return string html formated selection box |
367 | */ |
37a3ed17 |
368 | function createWidget_StrList() { |
a3ec3c91 |
369 | /* Begin the select tag. */ |
d6150d69 |
370 | $result = "<select name=\"new_$this->name\" $this->script>\n"; |
a3ec3c91 |
371 | |
372 | /* Add each possible value to the select list. */ |
373 | foreach ($this->possible_values as $real_value => $disp_value) { |
374 | /* Start the next new option string. */ |
62f7daa5 |
375 | $new_option = '<option value="' . |
28520c87 |
376 | ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"'; |
a3ec3c91 |
377 | |
378 | /* If this value is the current value, select it. */ |
379 | if ($real_value == $this->value) { |
d6150d69 |
380 | $new_option .= ' selected="selected"'; |
a3ec3c91 |
381 | } |
382 | |
383 | /* Add the display value to our option string. */ |
28520c87 |
384 | $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n"; |
a3ec3c91 |
385 | |
386 | /* And add the new option string to our select tag. */ |
387 | $result .= $new_option; |
388 | } |
389 | |
390 | /* Close the select tag and return our happy result. */ |
361d6e1b |
391 | $result .= "</select>$this->trailing_text\n"; |
a3ec3c91 |
392 | return ($result); |
393 | } |
394 | |
b4856b14 |
395 | /** |
396 | * Create folder selection box |
397 | * @return string html formated selection box |
398 | */ |
37a3ed17 |
399 | function createWidget_FolderList() { |
be2d5495 |
400 | $selected = array(strtolower($this->value)); |
401 | |
99ecf044 |
402 | /* set initial value */ |
403 | $result = ''; |
be2d5495 |
404 | |
405 | /* Add each possible value to the select list. */ |
406 | foreach ($this->possible_values as $real_value => $disp_value) { |
62f7daa5 |
407 | if ( is_array($disp_value) ) { |
be2d5495 |
408 | /* For folder list, we passed in the array of boxes.. */ |
99ecf044 |
409 | $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value, $this->folder_filter); |
be2d5495 |
410 | } else { |
411 | /* Start the next new option string. */ |
d6150d69 |
412 | $new_option = '<option value="' . htmlspecialchars($real_value) . '"'; |
62f7daa5 |
413 | |
be2d5495 |
414 | /* If this value is the current value, select it. */ |
415 | if ($real_value == $this->value) { |
d6150d69 |
416 | $new_option .= ' selected="selected"'; |
be2d5495 |
417 | } |
62f7daa5 |
418 | |
be2d5495 |
419 | /* Add the display value to our option string. */ |
d6150d69 |
420 | $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n"; |
be2d5495 |
421 | } |
422 | /* And add the new option string to our select tag. */ |
423 | $result .= $new_option; |
62f7daa5 |
424 | } |
99ecf044 |
425 | |
426 | |
427 | if (empty($result)) { |
428 | // string is displayed when interface can't build folder selection box |
429 | return _("unavailable"); |
430 | } else { |
431 | /* Begin the select tag. */ |
432 | $ret = "<select name=\"new_$this->name\" $this->script>\n"; |
433 | $ret.= $result; |
434 | /* Close the select tag and return our happy result. */ |
435 | $ret.= "</select>\n"; |
436 | return ($ret); |
437 | } |
be2d5495 |
438 | } |
439 | |
b4856b14 |
440 | /** |
441 | * Creates textarea |
442 | * @return string html formated textarea field |
443 | */ |
37a3ed17 |
444 | function createWidget_TextArea() { |
bbcafebd |
445 | switch ($this->size) { |
446 | case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break; |
447 | case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break; |
448 | case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break; |
449 | case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break; |
450 | case SMOPT_SIZE_NORMAL: |
451 | default: $rows = 5; $cols = 50; |
452 | } |
6b4bd11f |
453 | $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" " |
d6150d69 |
454 | . "cols=\"$cols\" $this->script>" |
455 | . htmlspecialchars($this->value) . "</textarea>\n"; |
bbcafebd |
456 | return ($result); |
a3ec3c91 |
457 | } |
458 | |
b4856b14 |
459 | /** |
460 | * Creates field for integer |
461 | * |
462 | * Difference from createWidget_String is visible only when javascript is enabled |
463 | * @return string html formated option field |
464 | */ |
37a3ed17 |
465 | function createWidget_Integer() { |
0d08ea5a |
466 | |
b65d1a08 |
467 | // add onChange javascript handler to a regular string widget |
468 | // which will strip out all non-numeric chars |
83aff890 |
469 | if (checkForJavascript()) |
d6150d69 |
470 | return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
471 | . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' ' |
472 | . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } ' |
d6150d69 |
473 | . 'this.value=newVal;" />', $this->createWidget_String()); |
b65d1a08 |
474 | else |
37a3ed17 |
475 | return $this->createWidget_String(); |
a3ec3c91 |
476 | } |
477 | |
b4856b14 |
478 | /** |
479 | * Creates field for floating number |
480 | * Difference from createWidget_String is visible only when javascript is enabled |
481 | * @return string html formated option field |
482 | */ |
37a3ed17 |
483 | function createWidget_Float() { |
37a3ed17 |
484 | |
b65d1a08 |
485 | // add onChange javascript handler to a regular string widget |
62f7daa5 |
486 | // which will strip out all non-numeric (period also OK) chars |
83aff890 |
487 | if (checkForJavascript()) |
d6150d69 |
488 | return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
489 | . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' ' |
490 | . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') ' |
d6150d69 |
491 | . 'newVal += origVal.charAt(i); } this.value=newVal;" />' |
37a3ed17 |
492 | , $this->createWidget_String()); |
b65d1a08 |
493 | else |
37a3ed17 |
494 | return $this->createWidget_String(); |
a3ec3c91 |
495 | } |
496 | |
b4856b14 |
497 | /** |
498 | * Creates radio field (yes/no) |
499 | * @return string html formated radio field |
500 | */ |
37a3ed17 |
501 | function createWidget_Boolean() { |
fd87494d |
502 | /* Do the whole current value thing. */ |
503 | if ($this->value != SMPREF_NO) { |
62f7daa5 |
504 | $yes_chk = ' checked="checked"'; |
fd87494d |
505 | $no_chk = ''; |
506 | } else { |
507 | $yes_chk = ''; |
62f7daa5 |
508 | $no_chk = ' checked="checked"'; |
fd87494d |
509 | } |
510 | |
511 | /* Build the yes choice. */ |
f4c37e3c |
512 | $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" ' |
513 | . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"' |
d6150d69 |
514 | . $yes_chk . ' ' . $this->script . ' /> ' |
f4c37e3c |
515 | . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>'; |
fd87494d |
516 | |
517 | /* Build the no choice. */ |
f4c37e3c |
518 | $no_option = '<input type="radio" id="new_' . $this->name . '_no" ' |
519 | . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"' |
d6150d69 |
520 | . $no_chk . ' ' . $this->script . ' /> ' |
f4c37e3c |
521 | . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>'; |
fd87494d |
522 | |
523 | /* Build and return the combined "boolean widget". */ |
524 | $result = "$yes_option $no_option"; |
525 | return ($result); |
a3ec3c91 |
526 | } |
527 | |
b4856b14 |
528 | /** |
529 | * Creates hidden field |
530 | * @return string html formated hidden input field |
531 | */ |
37a3ed17 |
532 | function createWidget_Hidden() { |
6b4bd11f |
533 | $result = '<input type="hidden" name="new_' . $this->name |
d6150d69 |
534 | . '" value="' . htmlspecialchars($this->value) |
535 | . '" ' . $this->script . ' />'; |
a3ec3c91 |
536 | return ($result); |
537 | } |
538 | |
b4856b14 |
539 | /** |
540 | * Creates comment |
541 | * @return string comment |
542 | */ |
37a3ed17 |
543 | function createWidget_Comment() { |
bbcafebd |
544 | $result = $this->comment; |
545 | return ($result); |
546 | } |
547 | |
b4856b14 |
548 | /** |
549 | * |
550 | */ |
cbe5423b |
551 | function save() { |
552 | $function = $this->save_function; |
553 | $function($this); |
44ef0f47 |
554 | } |
cbe5423b |
555 | |
b4856b14 |
556 | /** |
557 | * |
558 | */ |
cbe5423b |
559 | function changed() { |
6206f6c4 |
560 | return ($this->value != $this->new_value); |
cbe5423b |
561 | } |
b4856b14 |
562 | } /* End of SquirrelOption class*/ |
cbe5423b |
563 | |
b4856b14 |
564 | /** |
565 | * Saves option |
566 | * @param object $option object that holds option name and new_value |
567 | */ |
cbe5423b |
568 | function save_option($option) { |
dac16606 |
569 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
570 | /* Can't save the pref if we don't have the username */ |
571 | return; |
0b97a708 |
572 | } |
573 | global $data_dir; |
0b97a708 |
574 | setPref($data_dir, $username, $option->name, $option->new_value); |
cbe5423b |
575 | } |
576 | |
b4856b14 |
577 | /** |
578 | * save function that does not save |
579 | * @param object $option |
580 | */ |
cbe5423b |
581 | function save_option_noop($option) { |
582 | /* Do nothing here... */ |
9962527a |
583 | } |
44ef0f47 |
584 | |
b4856b14 |
585 | /** |
586 | * Create hidden 'optpage' input field with value set by argument |
587 | * @param string $optpage identification of option page |
588 | * @return string html formated hidden input field |
589 | */ |
cbe5423b |
590 | function create_optpage_element($optpage) { |
591 | return create_hidden_element('optpage', $optpage); |
592 | } |
593 | |
b4856b14 |
594 | /** |
595 | * Create hidden 'optmode' input field with value set by argument |
596 | * @param string $optmode |
597 | * @return string html formated hidden input field |
598 | */ |
cbe5423b |
599 | function create_optmode_element($optmode) { |
600 | return create_hidden_element('optmode', $optmode); |
601 | } |
602 | |
b4856b14 |
603 | /** |
604 | * Create hidden field. |
605 | * @param string $name field name |
606 | * @param string $value field value |
607 | * @return string html formated hidden input field |
608 | */ |
cbe5423b |
609 | function create_hidden_element($name, $value) { |
6b4bd11f |
610 | $result = '<input type="hidden" ' |
611 | . 'name="' . $name . '" ' |
d6150d69 |
612 | . 'value="' . htmlspecialchars($value) . '" />'; |
cbe5423b |
613 | return ($result); |
614 | } |
615 | |
b4856b14 |
616 | /** |
617 | * @param array $optgrps |
618 | * @param array $optvals |
619 | * @return array |
620 | */ |
cbe5423b |
621 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
622 | /* Build a simple array with which to start. */ |
623 | $result = array(); |
624 | |
bbcafebd |
625 | /* Create option group for each option group name. */ |
626 | foreach ($optgrps as $grpkey => $grpname) { |
627 | $result[$grpkey] = array(); |
628 | $result[$grpkey]['name'] = $grpname; |
629 | $result[$grpkey]['options'] = array(); |
630 | } |
631 | |
a3ec3c91 |
632 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
633 | foreach ($optvals as $grpkey => $grpopts) { |
634 | foreach ($grpopts as $optset) { |
28520c87 |
635 | /* Create a new option with all values given. */ |
636 | $next_option = new SquirrelOption( |
7390e240 |
637 | $optset['name'], |
638 | $optset['caption'], |
639 | $optset['type'], |
640 | (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE), |
641 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
642 | (isset($optset['posvals']) ? $optset['posvals'] : ''), |
643 | (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false) |
644 | ); |
bbcafebd |
645 | |
646 | /* If provided, set the size for this option. */ |
647 | if (isset($optset['size'])) { |
648 | $next_option->setSize($optset['size']); |
649 | } |
650 | |
361d6e1b |
651 | /* If provided, set the trailing_text for this option. */ |
652 | if (isset($optset['trailing_text'])) { |
653 | $next_option->setTrailingText($optset['trailing_text']); |
654 | } |
655 | |
bbcafebd |
656 | /* If provided, set the comment for this option. */ |
657 | if (isset($optset['comment'])) { |
658 | $next_option->setComment($optset['comment']); |
659 | } |
660 | |
cbe5423b |
661 | /* If provided, set the save function for this option. */ |
662 | if (isset($optset['save'])) { |
663 | $next_option->setSaveFunction($optset['save']); |
664 | } |
665 | |
666 | /* If provided, set the script for this option. */ |
667 | if (isset($optset['script'])) { |
668 | $next_option->setScript($optset['script']); |
669 | } |
670 | |
6ae9e729 |
671 | /* If provided, set the "post script" for this option. */ |
672 | if (isset($optset['post_script'])) { |
673 | $next_option->setPostScript($optset['post_script']); |
674 | } |
675 | |
99ecf044 |
676 | /* If provided, set the folder_filter for this option. */ |
677 | if (isset($optset['folder_filter'])) { |
678 | $next_option->setFolderFilter($optset['folder_filter']); |
679 | } |
680 | |
bbcafebd |
681 | /* Add this option to the option array. */ |
682 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
683 | } |
684 | } |
685 | |
686 | /* Return our resulting array. */ |
687 | return ($result); |
688 | } |
689 | |
d6150d69 |
690 | // vim: et ts=4 |