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 | |
f2e12923 |
178 | /** |
179 | * Check for a current value. If the $GLOBALS[] value exists, we also |
180 | * need to make sure it is the same data type as the initial value to |
181 | * make sure we are looking at the correct preference. |
182 | */ |
183 | $var_type = NULL; |
184 | if (!empty($initial_value)) { |
185 | $var_type = gettype($initial_value); |
186 | } elseif (is_array($possible_values)) { |
187 | list($index, $x) = each ($possible_values); |
188 | $var_type = gettype($index); |
189 | } |
190 | |
191 | if (isset($GLOBALS[$name]) && (is_null($var_type) ? true : $var_type == gettype($GLOBALS[$name]))) { |
a3ec3c91 |
192 | $this->value = $GLOBALS[$name]; |
17f3d242 |
193 | } else if (!empty($initial_value)) { |
194 | $this->value = $initial_value; |
a3ec3c91 |
195 | } else { |
196 | $this->value = ''; |
197 | } |
9962527a |
198 | |
a3ec3c91 |
199 | /* Check for a new value. */ |
b4856b14 |
200 | if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) { |
a3ec3c91 |
201 | $this->new_value = ''; |
44ef0f47 |
202 | } |
cbe5423b |
203 | |
204 | /* Set the default save function. */ |
2a50fbd7 |
205 | if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) { |
cbe5423b |
206 | $this->save_function = SMOPT_SAVE_DEFAULT; |
207 | } else { |
208 | $this->save_function = SMOPT_SAVE_NOOP; |
209 | } |
210 | } |
211 | |
b4856b14 |
212 | /** |
213 | * Set the value for this option. |
214 | * @param mixed $value |
215 | */ |
cbe5423b |
216 | function setValue($value) { |
217 | $this->value = $value; |
218 | } |
219 | |
b4856b14 |
220 | /** |
221 | * Set the new value for this option. |
222 | * @param mixed $new_value |
223 | */ |
cbe5423b |
224 | function setNewValue($new_value) { |
225 | $this->new_value = $new_value; |
9962527a |
226 | } |
44ef0f47 |
227 | |
b4856b14 |
228 | /** |
229 | * Set the size for this option. |
230 | * @param integer $size |
231 | */ |
bbcafebd |
232 | function setSize($size) { |
233 | $this->size = $size; |
234 | } |
235 | |
b4856b14 |
236 | /** |
237 | * Set the trailing_text for this option. |
238 | * @param string $trailing_text |
239 | */ |
361d6e1b |
240 | function setTrailingText($trailing_text) { |
241 | $this->trailing_text = $trailing_text; |
242 | } |
243 | |
b4856b14 |
244 | /** |
245 | * Set the comment for this option. |
246 | * @param string $comment |
247 | */ |
bbcafebd |
248 | function setComment($comment) { |
249 | $this->comment = $comment; |
250 | } |
251 | |
b4856b14 |
252 | /** |
253 | * Set the script for this option. |
598294a7 |
254 | * @param string $script |
b4856b14 |
255 | */ |
cbe5423b |
256 | function setScript($script) { |
257 | $this->script = $script; |
258 | } |
259 | |
b4856b14 |
260 | /** |
261 | * Set the "post script" for this option. |
262 | * @param string $post_script |
263 | */ |
6ae9e729 |
264 | function setPostScript($post_script) { |
265 | $this->post_script = $post_script; |
266 | } |
267 | |
b4856b14 |
268 | /** |
269 | * Set the save function for this option. |
270 | * @param string $save_function |
271 | */ |
cbe5423b |
272 | function setSaveFunction($save_function) { |
273 | $this->save_function = $save_function; |
274 | } |
275 | |
99ecf044 |
276 | /** |
277 | * Set the trailing_text for this option. |
278 | * @param string $folder_filter |
279 | * @since 1.5.1 |
280 | */ |
281 | function setFolderFilter($folder_filter) { |
282 | $this->folder_filter = $folder_filter; |
283 | } |
284 | |
b4856b14 |
285 | /** |
286 | * Creates fields on option pages according to option type |
287 | * |
288 | * Function that calls other createWidget* functions. |
598294a7 |
289 | * @return string html formated option field |
b4856b14 |
290 | */ |
a3ec3c91 |
291 | function createHTMLWidget() { |
ce68b76b |
292 | global $color; |
cbe5423b |
293 | |
62f7daa5 |
294 | // Use new value if available |
74e44765 |
295 | if (!empty($this->new_value)) { |
296 | $tempValue = $this->value; |
297 | $this->value = $this->new_value; |
298 | } |
299 | |
cbe5423b |
300 | /* Get the widget for this option type. */ |
a3ec3c91 |
301 | switch ($this->type) { |
302 | case SMOPT_TYPE_STRING: |
37a3ed17 |
303 | $result = $this->createWidget_String(); |
a3ec3c91 |
304 | break; |
305 | case SMOPT_TYPE_STRLIST: |
37a3ed17 |
306 | $result = $this->createWidget_StrList(); |
a3ec3c91 |
307 | break; |
7e6d5ea3 |
308 | case SMOPT_TYPE_TEXTAREA: |
37a3ed17 |
309 | $result = $this->createWidget_TextArea(); |
a3ec3c91 |
310 | break; |
311 | case SMOPT_TYPE_INTEGER: |
37a3ed17 |
312 | $result = $this->createWidget_Integer(); |
a3ec3c91 |
313 | break; |
314 | case SMOPT_TYPE_FLOAT: |
37a3ed17 |
315 | $result = $this->createWidget_Float(); |
a3ec3c91 |
316 | break; |
317 | case SMOPT_TYPE_BOOLEAN: |
37a3ed17 |
318 | $result = $this->createWidget_Boolean(); |
a3ec3c91 |
319 | break; |
2a50fbd7 |
320 | case SMOPT_TYPE_HIDDEN: |
37a3ed17 |
321 | $result = $this->createWidget_Hidden(); |
a3ec3c91 |
322 | break; |
bbcafebd |
323 | case SMOPT_TYPE_COMMENT: |
37a3ed17 |
324 | $result = $this->createWidget_Comment(); |
bbcafebd |
325 | break; |
be2d5495 |
326 | case SMOPT_TYPE_FLDRLIST: |
37a3ed17 |
327 | $result = $this->createWidget_FolderList(); |
be2d5495 |
328 | break; |
a3ec3c91 |
329 | default: |
6b4bd11f |
330 | $result = '<font color="' . $color[2] . '">' |
a3ec3c91 |
331 | . sprintf(_("Option Type '%s' Not Found"), $this->type) |
6b4bd11f |
332 | . '</font>'; |
a3ec3c91 |
333 | } |
334 | |
6ae9e729 |
335 | /* Add the "post script" for this option. */ |
336 | $result .= $this->post_script; |
62f7daa5 |
337 | |
74e44765 |
338 | // put correct value back if need be |
339 | if (!empty($this->new_value)) { |
340 | $this->value = $tempValue; |
341 | } |
342 | |
a3ec3c91 |
343 | /* Now, return the created widget. */ |
344 | return ($result); |
345 | } |
346 | |
b4856b14 |
347 | /** |
348 | * Create string field |
349 | * @return string html formated option field |
350 | */ |
37a3ed17 |
351 | function createWidget_String() { |
bbcafebd |
352 | switch ($this->size) { |
88cb1b4d |
353 | case SMOPT_SIZE_TINY: |
354 | $width = 5; |
355 | break; |
356 | case SMOPT_SIZE_SMALL: |
357 | $width = 12; |
358 | break; |
359 | case SMOPT_SIZE_LARGE: |
360 | $width = 38; |
361 | break; |
362 | case SMOPT_SIZE_HUGE: |
363 | $width = 50; |
364 | break; |
bbcafebd |
365 | case SMOPT_SIZE_NORMAL: |
88cb1b4d |
366 | default: |
367 | $width = 25; |
bbcafebd |
368 | } |
369 | |
88440172 |
370 | $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" . |
62f7daa5 |
371 | htmlspecialchars($this->value) . |
361d6e1b |
372 | "\" size=\"$width\" $this->script />$this->trailing_text\n"; |
a3ec3c91 |
373 | return ($result); |
374 | } |
375 | |
b4856b14 |
376 | /** |
377 | * Create selection box |
378 | * @return string html formated selection box |
379 | */ |
37a3ed17 |
380 | function createWidget_StrList() { |
a3ec3c91 |
381 | /* Begin the select tag. */ |
d6150d69 |
382 | $result = "<select name=\"new_$this->name\" $this->script>\n"; |
a3ec3c91 |
383 | |
384 | /* Add each possible value to the select list. */ |
385 | foreach ($this->possible_values as $real_value => $disp_value) { |
386 | /* Start the next new option string. */ |
62f7daa5 |
387 | $new_option = '<option value="' . |
28520c87 |
388 | ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"'; |
a3ec3c91 |
389 | |
390 | /* If this value is the current value, select it. */ |
391 | if ($real_value == $this->value) { |
d6150d69 |
392 | $new_option .= ' selected="selected"'; |
a3ec3c91 |
393 | } |
394 | |
395 | /* Add the display value to our option string. */ |
28520c87 |
396 | $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n"; |
a3ec3c91 |
397 | |
398 | /* And add the new option string to our select tag. */ |
399 | $result .= $new_option; |
400 | } |
401 | |
402 | /* Close the select tag and return our happy result. */ |
361d6e1b |
403 | $result .= "</select>$this->trailing_text\n"; |
a3ec3c91 |
404 | return ($result); |
405 | } |
406 | |
b4856b14 |
407 | /** |
408 | * Create folder selection box |
409 | * @return string html formated selection box |
410 | */ |
37a3ed17 |
411 | function createWidget_FolderList() { |
be2d5495 |
412 | $selected = array(strtolower($this->value)); |
413 | |
99ecf044 |
414 | /* set initial value */ |
415 | $result = ''; |
be2d5495 |
416 | |
417 | /* Add each possible value to the select list. */ |
418 | foreach ($this->possible_values as $real_value => $disp_value) { |
62f7daa5 |
419 | if ( is_array($disp_value) ) { |
be2d5495 |
420 | /* For folder list, we passed in the array of boxes.. */ |
99ecf044 |
421 | $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value, $this->folder_filter); |
be2d5495 |
422 | } else { |
423 | /* Start the next new option string. */ |
d6150d69 |
424 | $new_option = '<option value="' . htmlspecialchars($real_value) . '"'; |
62f7daa5 |
425 | |
be2d5495 |
426 | /* If this value is the current value, select it. */ |
427 | if ($real_value == $this->value) { |
d6150d69 |
428 | $new_option .= ' selected="selected"'; |
be2d5495 |
429 | } |
62f7daa5 |
430 | |
be2d5495 |
431 | /* Add the display value to our option string. */ |
d6150d69 |
432 | $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n"; |
be2d5495 |
433 | } |
434 | /* And add the new option string to our select tag. */ |
435 | $result .= $new_option; |
62f7daa5 |
436 | } |
99ecf044 |
437 | |
438 | |
439 | if (empty($result)) { |
440 | // string is displayed when interface can't build folder selection box |
441 | return _("unavailable"); |
442 | } else { |
443 | /* Begin the select tag. */ |
444 | $ret = "<select name=\"new_$this->name\" $this->script>\n"; |
445 | $ret.= $result; |
446 | /* Close the select tag and return our happy result. */ |
447 | $ret.= "</select>\n"; |
448 | return ($ret); |
449 | } |
be2d5495 |
450 | } |
451 | |
b4856b14 |
452 | /** |
453 | * Creates textarea |
454 | * @return string html formated textarea field |
455 | */ |
37a3ed17 |
456 | function createWidget_TextArea() { |
bbcafebd |
457 | switch ($this->size) { |
458 | case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break; |
459 | case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break; |
460 | case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break; |
461 | case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break; |
462 | case SMOPT_SIZE_NORMAL: |
463 | default: $rows = 5; $cols = 50; |
464 | } |
6b4bd11f |
465 | $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" " |
d6150d69 |
466 | . "cols=\"$cols\" $this->script>" |
467 | . htmlspecialchars($this->value) . "</textarea>\n"; |
bbcafebd |
468 | return ($result); |
a3ec3c91 |
469 | } |
470 | |
b4856b14 |
471 | /** |
472 | * Creates field for integer |
473 | * |
474 | * Difference from createWidget_String is visible only when javascript is enabled |
475 | * @return string html formated option field |
476 | */ |
37a3ed17 |
477 | function createWidget_Integer() { |
b65d1a08 |
478 | global $javascript_on; |
0d08ea5a |
479 | |
b65d1a08 |
480 | // add onChange javascript handler to a regular string widget |
481 | // which will strip out all non-numeric chars |
482 | if ($javascript_on) |
d6150d69 |
483 | return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
484 | . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' ' |
485 | . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } ' |
d6150d69 |
486 | . 'this.value=newVal;" />', $this->createWidget_String()); |
b65d1a08 |
487 | else |
37a3ed17 |
488 | return $this->createWidget_String(); |
a3ec3c91 |
489 | } |
490 | |
b4856b14 |
491 | /** |
492 | * Creates field for floating number |
493 | * Difference from createWidget_String is visible only when javascript is enabled |
494 | * @return string html formated option field |
495 | */ |
37a3ed17 |
496 | function createWidget_Float() { |
37a3ed17 |
497 | global $javascript_on; |
498 | |
b65d1a08 |
499 | // add onChange javascript handler to a regular string widget |
62f7daa5 |
500 | // which will strip out all non-numeric (period also OK) chars |
b65d1a08 |
501 | if ($javascript_on) |
d6150d69 |
502 | return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
503 | . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' ' |
504 | . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') ' |
d6150d69 |
505 | . 'newVal += origVal.charAt(i); } this.value=newVal;" />' |
37a3ed17 |
506 | , $this->createWidget_String()); |
b65d1a08 |
507 | else |
37a3ed17 |
508 | return $this->createWidget_String(); |
a3ec3c91 |
509 | } |
510 | |
b4856b14 |
511 | /** |
512 | * Creates radio field (yes/no) |
513 | * @return string html formated radio field |
514 | */ |
37a3ed17 |
515 | function createWidget_Boolean() { |
fd87494d |
516 | /* Do the whole current value thing. */ |
517 | if ($this->value != SMPREF_NO) { |
62f7daa5 |
518 | $yes_chk = ' checked="checked"'; |
fd87494d |
519 | $no_chk = ''; |
520 | } else { |
521 | $yes_chk = ''; |
62f7daa5 |
522 | $no_chk = ' checked="checked"'; |
fd87494d |
523 | } |
524 | |
525 | /* Build the yes choice. */ |
f4c37e3c |
526 | $yes_option = '<input type="radio" id="new_' . $this->name . '_yes" ' |
527 | . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"' |
d6150d69 |
528 | . $yes_chk . ' ' . $this->script . ' /> ' |
f4c37e3c |
529 | . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>'; |
fd87494d |
530 | |
531 | /* Build the no choice. */ |
f4c37e3c |
532 | $no_option = '<input type="radio" id="new_' . $this->name . '_no" ' |
533 | . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"' |
d6150d69 |
534 | . $no_chk . ' ' . $this->script . ' /> ' |
f4c37e3c |
535 | . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>'; |
fd87494d |
536 | |
537 | /* Build and return the combined "boolean widget". */ |
538 | $result = "$yes_option $no_option"; |
539 | return ($result); |
a3ec3c91 |
540 | } |
541 | |
b4856b14 |
542 | /** |
543 | * Creates hidden field |
544 | * @return string html formated hidden input field |
545 | */ |
37a3ed17 |
546 | function createWidget_Hidden() { |
6b4bd11f |
547 | $result = '<input type="hidden" name="new_' . $this->name |
d6150d69 |
548 | . '" value="' . htmlspecialchars($this->value) |
549 | . '" ' . $this->script . ' />'; |
a3ec3c91 |
550 | return ($result); |
551 | } |
552 | |
b4856b14 |
553 | /** |
554 | * Creates comment |
555 | * @return string comment |
556 | */ |
37a3ed17 |
557 | function createWidget_Comment() { |
bbcafebd |
558 | $result = $this->comment; |
559 | return ($result); |
560 | } |
561 | |
b4856b14 |
562 | /** |
563 | * |
564 | */ |
cbe5423b |
565 | function save() { |
566 | $function = $this->save_function; |
567 | $function($this); |
44ef0f47 |
568 | } |
cbe5423b |
569 | |
b4856b14 |
570 | /** |
571 | * |
572 | */ |
cbe5423b |
573 | function changed() { |
6206f6c4 |
574 | return ($this->value != $this->new_value); |
cbe5423b |
575 | } |
b4856b14 |
576 | } /* End of SquirrelOption class*/ |
cbe5423b |
577 | |
b4856b14 |
578 | /** |
579 | * Saves option |
580 | * @param object $option object that holds option name and new_value |
581 | */ |
cbe5423b |
582 | function save_option($option) { |
dac16606 |
583 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
584 | /* Can't save the pref if we don't have the username */ |
585 | return; |
0b97a708 |
586 | } |
587 | global $data_dir; |
0b97a708 |
588 | setPref($data_dir, $username, $option->name, $option->new_value); |
cbe5423b |
589 | } |
590 | |
b4856b14 |
591 | /** |
592 | * save function that does not save |
593 | * @param object $option |
594 | */ |
cbe5423b |
595 | function save_option_noop($option) { |
596 | /* Do nothing here... */ |
9962527a |
597 | } |
44ef0f47 |
598 | |
b4856b14 |
599 | /** |
600 | * Create hidden 'optpage' input field with value set by argument |
601 | * @param string $optpage identification of option page |
602 | * @return string html formated hidden input field |
603 | */ |
cbe5423b |
604 | function create_optpage_element($optpage) { |
605 | return create_hidden_element('optpage', $optpage); |
606 | } |
607 | |
b4856b14 |
608 | /** |
609 | * Create hidden 'optmode' input field with value set by argument |
610 | * @param string $optmode |
611 | * @return string html formated hidden input field |
612 | */ |
cbe5423b |
613 | function create_optmode_element($optmode) { |
614 | return create_hidden_element('optmode', $optmode); |
615 | } |
616 | |
b4856b14 |
617 | /** |
618 | * Create hidden field. |
619 | * @param string $name field name |
620 | * @param string $value field value |
621 | * @return string html formated hidden input field |
622 | */ |
cbe5423b |
623 | function create_hidden_element($name, $value) { |
6b4bd11f |
624 | $result = '<input type="hidden" ' |
625 | . 'name="' . $name . '" ' |
d6150d69 |
626 | . 'value="' . htmlspecialchars($value) . '" />'; |
cbe5423b |
627 | return ($result); |
628 | } |
629 | |
b4856b14 |
630 | /** |
631 | * @param array $optgrps |
632 | * @param array $optvals |
633 | * @return array |
634 | */ |
cbe5423b |
635 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
636 | /* Build a simple array with which to start. */ |
637 | $result = array(); |
638 | |
bbcafebd |
639 | /* Create option group for each option group name. */ |
640 | foreach ($optgrps as $grpkey => $grpname) { |
641 | $result[$grpkey] = array(); |
642 | $result[$grpkey]['name'] = $grpname; |
643 | $result[$grpkey]['options'] = array(); |
644 | } |
645 | |
a3ec3c91 |
646 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
647 | foreach ($optvals as $grpkey => $grpopts) { |
648 | foreach ($grpopts as $optset) { |
28520c87 |
649 | /* Create a new option with all values given. */ |
650 | $next_option = new SquirrelOption( |
7390e240 |
651 | $optset['name'], |
652 | $optset['caption'], |
653 | $optset['type'], |
654 | (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE), |
655 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
656 | (isset($optset['posvals']) ? $optset['posvals'] : ''), |
657 | (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false) |
658 | ); |
bbcafebd |
659 | |
660 | /* If provided, set the size for this option. */ |
661 | if (isset($optset['size'])) { |
662 | $next_option->setSize($optset['size']); |
663 | } |
664 | |
361d6e1b |
665 | /* If provided, set the trailing_text for this option. */ |
666 | if (isset($optset['trailing_text'])) { |
667 | $next_option->setTrailingText($optset['trailing_text']); |
668 | } |
669 | |
bbcafebd |
670 | /* If provided, set the comment for this option. */ |
671 | if (isset($optset['comment'])) { |
672 | $next_option->setComment($optset['comment']); |
673 | } |
674 | |
cbe5423b |
675 | /* If provided, set the save function for this option. */ |
676 | if (isset($optset['save'])) { |
677 | $next_option->setSaveFunction($optset['save']); |
678 | } |
679 | |
680 | /* If provided, set the script for this option. */ |
681 | if (isset($optset['script'])) { |
682 | $next_option->setScript($optset['script']); |
683 | } |
684 | |
6ae9e729 |
685 | /* If provided, set the "post script" for this option. */ |
686 | if (isset($optset['post_script'])) { |
687 | $next_option->setPostScript($optset['post_script']); |
688 | } |
689 | |
99ecf044 |
690 | /* If provided, set the folder_filter for this option. */ |
691 | if (isset($optset['folder_filter'])) { |
692 | $next_option->setFolderFilter($optset['folder_filter']); |
693 | } |
694 | |
bbcafebd |
695 | /* Add this option to the option array. */ |
696 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
697 | } |
698 | } |
699 | |
700 | /* Return our resulting array. */ |
701 | return ($result); |
702 | } |
703 | |
b4856b14 |
704 | /** |
705 | * @param array $option_groups |
706 | */ |
cbe5423b |
707 | function print_option_groups($option_groups) { |
2fad95fa |
708 | /* Print each option group. */ |
bbcafebd |
709 | foreach ($option_groups as $next_optgrp) { |
2fad95fa |
710 | /* If it is not blank, print the name for this option group. */ |
711 | if ($next_optgrp['name'] != '') { |
6b4bd11f |
712 | echo html_tag( 'tr', "\n". |
713 | html_tag( 'td', |
714 | '<b>' . $next_optgrp['name'] . '</b>' , |
c435f076 |
715 | 'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' ) |
6b4bd11f |
716 | ) ."\n"; |
7e235a1a |
717 | } |
718 | |
719 | /* Print each option in this option group. */ |
bbcafebd |
720 | foreach ($next_optgrp['options'] as $option) { |
2a50fbd7 |
721 | if ($option->type != SMOPT_TYPE_HIDDEN) { |
6b4bd11f |
722 | echo html_tag( 'tr', "\n". |
723 | html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) . |
724 | html_tag( 'td', $option->createHTMLWidget(), 'left' ) |
725 | ) ."\n"; |
bbcafebd |
726 | } else { |
727 | echo $option->createHTMLWidget(); |
728 | } |
729 | } |
7e235a1a |
730 | |
731 | /* Print an empty row after this option group. */ |
6b4bd11f |
732 | echo html_tag( 'tr', |
733 | html_tag( 'td', ' ', 'left', '', 'colspan="2"' ) |
734 | ) . "\n"; |
bbcafebd |
735 | } |
736 | } |
737 | |
b4856b14 |
738 | /** |
739 | * Create submit button inside table row. |
740 | * @param string $name |
741 | */ |
9962527a |
742 | function OptionSubmit( $name ) { |
6b4bd11f |
743 | echo html_tag( 'tr', |
6fd95361 |
744 | html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" /> ', 'right', '', 'colspan="2"' ) |
6b4bd11f |
745 | ) . "\n"; |
9962527a |
746 | } |
02ddcd00 |
747 | |
d6150d69 |
748 | // vim: et ts=4 |