44ef0f47 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * options.php |
5 | * |
35586184 |
6 | * Functions needed to display the options pages. |
7 | * |
4b5049de |
8 | * @copyright © 1999-2007 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); |
42b7c9d4 |
29 | define('SMOPT_TYPE_FLDRLIST_MULTI', 9); |
38d93650 |
30 | define('SMOPT_TYPE_EDIT_LIST', 10); |
40268626 |
31 | define('SMOPT_TYPE_STRLIST_MULTI', 11); |
1b7db98b |
32 | define('SMOPT_TYPE_BOOLEAN_CHECKBOX', 12); |
8b2726c5 |
33 | define('SMOPT_TYPE_BOOLEAN_RADIO', 13); |
a3ec3c91 |
34 | |
35 | /* Define constants for the options refresh levels. */ |
36 | define('SMOPT_REFRESH_NONE', 0); |
37 | define('SMOPT_REFRESH_FOLDERLIST', 1); |
38 | define('SMOPT_REFRESH_ALL', 2); |
39 | |
bbcafebd |
40 | /* Define constants for the options size. */ |
41 | define('SMOPT_SIZE_TINY', 0); |
42 | define('SMOPT_SIZE_SMALL', 1); |
43 | define('SMOPT_SIZE_MEDIUM', 2); |
44 | define('SMOPT_SIZE_LARGE', 3); |
45 | define('SMOPT_SIZE_HUGE', 4); |
88cb1b4d |
46 | define('SMOPT_SIZE_NORMAL', 5); |
bbcafebd |
47 | |
cbe5423b |
48 | define('SMOPT_SAVE_DEFAULT', 'save_option'); |
49 | define('SMOPT_SAVE_NOOP', 'save_option_noop'); |
50 | |
9962527a |
51 | /** |
598294a7 |
52 | * SquirrelOption: An option for SquirrelMail. |
9962527a |
53 | * |
8f6f9ba5 |
54 | * @package squirrelmail |
b4856b14 |
55 | * @subpackage prefs |
9962527a |
56 | */ |
57 | class SquirrelOption { |
b4856b14 |
58 | /** |
59 | * The name of this setting |
60 | * @var string |
61 | */ |
9962527a |
62 | var $name; |
b4856b14 |
63 | /** |
64 | * The text that prefaces setting on the preferences page |
65 | * @var string |
66 | */ |
9962527a |
67 | var $caption; |
b4856b14 |
68 | /** |
69 | * The type of INPUT element |
70 | * |
71 | * See SMOPT_TYPE_* defines |
72 | * @var integer |
73 | */ |
9962527a |
74 | var $type; |
b4856b14 |
75 | /** |
598294a7 |
76 | * Indicates if a link should be shown to refresh part |
b4856b14 |
77 | * or all of the window |
78 | * |
79 | * See SMOPT_REFRESH_* defines |
80 | * @var integer |
81 | */ |
a3ec3c91 |
82 | var $refresh_level; |
b4856b14 |
83 | /** |
84 | * Specifies the size of certain input items |
85 | * |
86 | * See SMOPT_SIZE_* defines |
87 | * @var integer |
88 | */ |
bbcafebd |
89 | var $size; |
b4856b14 |
90 | /** |
598294a7 |
91 | * Text that follows a text input or |
b4856b14 |
92 | * select list input on the preferences page |
598294a7 |
93 | * |
b4856b14 |
94 | * useful for indicating units, meanings of special values, etc. |
95 | * @var string |
96 | */ |
361d6e1b |
97 | var $trailing_text; |
5b277d00 |
98 | /** |
99 | * Text that overrides the "Yes" label for boolean |
100 | * radio option widgets |
101 | * |
102 | * @var string |
103 | */ |
104 | var $yes_text; |
105 | /** |
106 | * Text that overrides the "No" label for boolean |
107 | * radio option widgets |
108 | * |
109 | * @var string |
110 | */ |
111 | var $no_text; |
b4856b14 |
112 | /** |
113 | * text displayed to the user |
114 | * |
115 | * Used with SMOPT_TYPE_COMMENT options |
116 | * @var string |
117 | */ |
bbcafebd |
118 | var $comment; |
b4856b14 |
119 | /** |
0177059f |
120 | * additional javascript or other widget attributes added to the |
121 | * user input; must be an array where keys are attribute names |
122 | * ("onclick", etc) and values are the attribute values. |
123 | * @var array |
b4856b14 |
124 | */ |
0177059f |
125 | var $aExtraAttribs; |
b4856b14 |
126 | /** |
598294a7 |
127 | * script (usually Javascript) that will be placed after (outside of) |
b4856b14 |
128 | * the INPUT tag |
129 | * @var string |
130 | */ |
6ae9e729 |
131 | var $post_script; |
cbe5423b |
132 | |
b4856b14 |
133 | /** |
134 | * The name of the Save Function for this option. |
135 | * @var string |
136 | */ |
cbe5423b |
137 | var $save_function; |
9962527a |
138 | |
139 | /* The various 'values' for this options. */ |
b4856b14 |
140 | /** |
141 | * default/preselected value for this option |
142 | * @var mixed |
143 | */ |
9962527a |
144 | var $value; |
b4856b14 |
145 | /** |
146 | * new option value |
147 | * @var mixed |
148 | */ |
9962527a |
149 | var $new_value; |
b4856b14 |
150 | /** |
598294a7 |
151 | * associative array, where each key is an actual input value |
b4856b14 |
152 | * and the corresponding value is what is displayed to the user |
153 | * for that list item in the drop-down list |
154 | * @var array |
155 | */ |
a3ec3c91 |
156 | var $possible_values; |
b4856b14 |
157 | /** |
158 | * disables html sanitizing. |
598294a7 |
159 | * |
160 | * WARNING - don't use it, if user input is possible in option |
0177059f |
161 | * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST. |
b4856b14 |
162 | * @var bool |
163 | */ |
28520c87 |
164 | var $htmlencoded=false; |
99ecf044 |
165 | /** |
42b7c9d4 |
166 | * Controls folder list limits in SMOPT_TYPE_FLDRLIST and |
167 | * SMOPT_TYPE_FLDRLIST_MULTI widgets. |
99ecf044 |
168 | * See $flag argument in sqimap_mailbox_option_list() function. |
169 | * @var string |
170 | * @since 1.5.1 |
171 | */ |
172 | var $folder_filter='noselect'; |
9962527a |
173 | |
b4856b14 |
174 | /** |
175 | * Constructor function |
176 | * @param string $name |
177 | * @param string $caption |
178 | * @param integer $type |
179 | * @param integer $refresh_level |
180 | * @param mixed $initial_value |
181 | * @param array $possible_values |
182 | * @param bool $htmlencoded |
183 | */ |
9962527a |
184 | function SquirrelOption |
28520c87 |
185 | ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) { |
9962527a |
186 | /* Set the basic stuff. */ |
187 | $this->name = $name; |
188 | $this->caption = $caption; |
9962527a |
189 | $this->type = $type; |
a3ec3c91 |
190 | $this->refresh_level = $refresh_level; |
191 | $this->possible_values = $possible_values; |
28520c87 |
192 | $this->htmlencoded = $htmlencoded; |
bbcafebd |
193 | $this->size = SMOPT_SIZE_MEDIUM; |
361d6e1b |
194 | $this->trailing_text = ''; |
5b277d00 |
195 | $this->yes_text = ''; |
196 | $this->no_text = ''; |
bbcafebd |
197 | $this->comment = ''; |
0177059f |
198 | $this->aExtraAttribs = array(); |
6ae9e729 |
199 | $this->post_script = ''; |
a3ec3c91 |
200 | |
991c88e7 |
201 | //Check for a current value. |
202 | if (isset($GLOBALS[$name])) { |
a3ec3c91 |
203 | $this->value = $GLOBALS[$name]; |
17f3d242 |
204 | } else if (!empty($initial_value)) { |
205 | $this->value = $initial_value; |
a3ec3c91 |
206 | } else { |
207 | $this->value = ''; |
208 | } |
9962527a |
209 | |
a3ec3c91 |
210 | /* Check for a new value. */ |
b4856b14 |
211 | if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) { |
a3ec3c91 |
212 | $this->new_value = ''; |
44ef0f47 |
213 | } |
cbe5423b |
214 | |
215 | /* Set the default save function. */ |
2a50fbd7 |
216 | if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) { |
cbe5423b |
217 | $this->save_function = SMOPT_SAVE_DEFAULT; |
218 | } else { |
219 | $this->save_function = SMOPT_SAVE_NOOP; |
220 | } |
221 | } |
222 | |
5a42c101 |
223 | /** Convenience function that identifies which types of |
224 | widgets are stored as (serialized) array values. */ |
225 | function is_multiple_valued() { |
226 | return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI |
227 | || $this->type == SMOPT_TYPE_STRLIST_MULTI |
228 | || $this->type == SMOPT_TYPE_EDIT_LIST); |
229 | } |
230 | |
b4856b14 |
231 | /** |
232 | * Set the value for this option. |
233 | * @param mixed $value |
234 | */ |
cbe5423b |
235 | function setValue($value) { |
236 | $this->value = $value; |
237 | } |
238 | |
b4856b14 |
239 | /** |
240 | * Set the new value for this option. |
241 | * @param mixed $new_value |
242 | */ |
cbe5423b |
243 | function setNewValue($new_value) { |
244 | $this->new_value = $new_value; |
9962527a |
245 | } |
44ef0f47 |
246 | |
b4856b14 |
247 | /** |
248 | * Set the size for this option. |
249 | * @param integer $size |
250 | */ |
bbcafebd |
251 | function setSize($size) { |
252 | $this->size = $size; |
253 | } |
254 | |
b4856b14 |
255 | /** |
256 | * Set the trailing_text for this option. |
257 | * @param string $trailing_text |
258 | */ |
361d6e1b |
259 | function setTrailingText($trailing_text) { |
260 | $this->trailing_text = $trailing_text; |
261 | } |
262 | |
5b277d00 |
263 | /** |
264 | * Set the yes_text for this option. |
265 | * @param string $yes_text |
266 | */ |
267 | function setYesText($yes_text) { |
268 | $this->yes_text = $yes_text; |
269 | } |
270 | |
271 | /** |
272 | * Set the no_text for this option. |
273 | * @param string $no_text |
274 | */ |
275 | function setNoText($no_text) { |
276 | $this->no_text = $no_text; |
277 | } |
278 | |
b4856b14 |
279 | /** |
280 | * Set the comment for this option. |
281 | * @param string $comment |
282 | */ |
bbcafebd |
283 | function setComment($comment) { |
284 | $this->comment = $comment; |
285 | } |
286 | |
b4856b14 |
287 | /** |
0177059f |
288 | * Set the extra attributes for this option. |
289 | * @param array $aExtraAttribs |
b4856b14 |
290 | */ |
0177059f |
291 | function setExtraAttributes($aExtraAttribs) { |
292 | $this->aExtraAttribs = $aExtraAttribs; |
cbe5423b |
293 | } |
294 | |
b4856b14 |
295 | /** |
296 | * Set the "post script" for this option. |
297 | * @param string $post_script |
298 | */ |
6ae9e729 |
299 | function setPostScript($post_script) { |
300 | $this->post_script = $post_script; |
301 | } |
302 | |
b4856b14 |
303 | /** |
304 | * Set the save function for this option. |
305 | * @param string $save_function |
306 | */ |
cbe5423b |
307 | function setSaveFunction($save_function) { |
308 | $this->save_function = $save_function; |
309 | } |
310 | |
99ecf044 |
311 | /** |
5b277d00 |
312 | * Set the folder_filter for this option. |
99ecf044 |
313 | * @param string $folder_filter |
314 | * @since 1.5.1 |
315 | */ |
316 | function setFolderFilter($folder_filter) { |
317 | $this->folder_filter = $folder_filter; |
318 | } |
319 | |
b4856b14 |
320 | /** |
321 | * Creates fields on option pages according to option type |
322 | * |
9786ea94 |
323 | * This is the function that calls all other createWidget* functions. |
324 | * |
325 | * @return string The formated option field |
326 | * |
b4856b14 |
327 | */ |
9786ea94 |
328 | function createWidget() { |
ce68b76b |
329 | global $color; |
cbe5423b |
330 | |
62f7daa5 |
331 | // Use new value if available |
74e44765 |
332 | if (!empty($this->new_value)) { |
333 | $tempValue = $this->value; |
334 | $this->value = $this->new_value; |
335 | } |
336 | |
cbe5423b |
337 | /* Get the widget for this option type. */ |
a3ec3c91 |
338 | switch ($this->type) { |
339 | case SMOPT_TYPE_STRING: |
37a3ed17 |
340 | $result = $this->createWidget_String(); |
a3ec3c91 |
341 | break; |
342 | case SMOPT_TYPE_STRLIST: |
37a3ed17 |
343 | $result = $this->createWidget_StrList(); |
a3ec3c91 |
344 | break; |
7e6d5ea3 |
345 | case SMOPT_TYPE_TEXTAREA: |
37a3ed17 |
346 | $result = $this->createWidget_TextArea(); |
a3ec3c91 |
347 | break; |
348 | case SMOPT_TYPE_INTEGER: |
37a3ed17 |
349 | $result = $this->createWidget_Integer(); |
a3ec3c91 |
350 | break; |
351 | case SMOPT_TYPE_FLOAT: |
37a3ed17 |
352 | $result = $this->createWidget_Float(); |
a3ec3c91 |
353 | break; |
354 | case SMOPT_TYPE_BOOLEAN: |
37a3ed17 |
355 | $result = $this->createWidget_Boolean(); |
a3ec3c91 |
356 | break; |
1b7db98b |
357 | case SMOPT_TYPE_BOOLEAN_CHECKBOX: |
358 | $result = $this->createWidget_Boolean(TRUE); |
359 | break; |
8b2726c5 |
360 | case SMOPT_TYPE_BOOLEAN_RADIO: |
361 | $result = $this->createWidget_Boolean(FALSE); |
362 | break; |
2a50fbd7 |
363 | case SMOPT_TYPE_HIDDEN: |
37a3ed17 |
364 | $result = $this->createWidget_Hidden(); |
a3ec3c91 |
365 | break; |
bbcafebd |
366 | case SMOPT_TYPE_COMMENT: |
37a3ed17 |
367 | $result = $this->createWidget_Comment(); |
bbcafebd |
368 | break; |
be2d5495 |
369 | case SMOPT_TYPE_FLDRLIST: |
37a3ed17 |
370 | $result = $this->createWidget_FolderList(); |
be2d5495 |
371 | break; |
42b7c9d4 |
372 | case SMOPT_TYPE_FLDRLIST_MULTI: |
373 | $result = $this->createWidget_FolderList(TRUE); |
374 | break; |
38d93650 |
375 | case SMOPT_TYPE_EDIT_LIST: |
376 | $result = $this->createWidget_EditList(); |
377 | break; |
40268626 |
378 | case SMOPT_TYPE_STRLIST_MULTI: |
379 | $result = $this->createWidget_StrList(TRUE); |
380 | break; |
a3ec3c91 |
381 | default: |
fb8c4296 |
382 | error_box ( |
383 | sprintf(_("Option Type '%s' Not Found"), $this->type) |
384 | ); |
a3ec3c91 |
385 | } |
386 | |
6ae9e729 |
387 | /* Add the "post script" for this option. */ |
388 | $result .= $this->post_script; |
62f7daa5 |
389 | |
74e44765 |
390 | // put correct value back if need be |
391 | if (!empty($this->new_value)) { |
392 | $this->value = $tempValue; |
393 | } |
394 | |
a3ec3c91 |
395 | /* Now, return the created widget. */ |
9786ea94 |
396 | return $result; |
a3ec3c91 |
397 | } |
398 | |
b4856b14 |
399 | /** |
400 | * Create string field |
401 | * @return string html formated option field |
402 | */ |
37a3ed17 |
403 | function createWidget_String() { |
bbcafebd |
404 | switch ($this->size) { |
88cb1b4d |
405 | case SMOPT_SIZE_TINY: |
406 | $width = 5; |
407 | break; |
408 | case SMOPT_SIZE_SMALL: |
409 | $width = 12; |
410 | break; |
411 | case SMOPT_SIZE_LARGE: |
412 | $width = 38; |
413 | break; |
414 | case SMOPT_SIZE_HUGE: |
415 | $width = 50; |
416 | break; |
bbcafebd |
417 | case SMOPT_SIZE_NORMAL: |
88cb1b4d |
418 | default: |
419 | $width = 25; |
bbcafebd |
420 | } |
421 | |
0177059f |
422 | return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text); |
a3ec3c91 |
423 | } |
424 | |
b4856b14 |
425 | /** |
426 | * Create selection box |
0177059f |
427 | * |
428 | * When $this->htmlencoded is TRUE, the keys and values in |
429 | * $this->possible_values are assumed to be display-safe. |
430 | * Use with care! |
431 | * |
40268626 |
432 | * @param boolean $multiple_select When TRUE, the select widget |
433 | * will allow multiple selections |
434 | * (OPTIONAL; default is FALSE |
435 | * (single select list)) |
436 | * |
b4856b14 |
437 | * @return string html formated selection box |
40268626 |
438 | * |
b4856b14 |
439 | */ |
40268626 |
440 | function createWidget_StrList($multiple_select=FALSE) { |
98e88751 |
441 | //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch |
40268626 |
442 | |
443 | switch ($this->size) { |
444 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
445 | case SMOPT_SIZE_TINY: |
446 | $height = 3; |
447 | break; |
448 | case SMOPT_SIZE_SMALL: |
449 | $height = 8; |
450 | break; |
451 | case SMOPT_SIZE_LARGE: |
452 | $height = 15; |
453 | break; |
454 | case SMOPT_SIZE_HUGE: |
455 | $height = 25; |
456 | break; |
457 | case SMOPT_SIZE_NORMAL: |
458 | default: |
459 | $height = 5; |
460 | } |
461 | |
462 | return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text); |
a3ec3c91 |
463 | |
a3ec3c91 |
464 | } |
465 | |
b4856b14 |
466 | /** |
467 | * Create folder selection box |
42b7c9d4 |
468 | * |
469 | * @param boolean $multiple_select When TRUE, the select widget |
470 | * will allow multiple selections |
471 | * (OPTIONAL; default is FALSE |
472 | * (single select list)) |
473 | * |
b4856b14 |
474 | * @return string html formated selection box |
42b7c9d4 |
475 | * |
b4856b14 |
476 | */ |
42b7c9d4 |
477 | function createWidget_FolderList($multiple_select=FALSE) { |
be2d5495 |
478 | |
38d93650 |
479 | switch ($this->size) { |
480 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
481 | case SMOPT_SIZE_TINY: |
482 | $height = 3; |
483 | break; |
484 | case SMOPT_SIZE_SMALL: |
485 | $height = 8; |
486 | break; |
487 | case SMOPT_SIZE_LARGE: |
488 | $height = 15; |
489 | break; |
490 | case SMOPT_SIZE_HUGE: |
491 | $height = 25; |
492 | break; |
493 | case SMOPT_SIZE_NORMAL: |
494 | default: |
495 | $height = 5; |
496 | } |
497 | |
0177059f |
498 | // possible values might include a nested array of |
499 | // possible values (list of folders) |
500 | // |
501 | $option_list = array(); |
502 | foreach ($this->possible_values as $value => $text) { |
62f7daa5 |
503 | |
0177059f |
504 | // list of folders (boxes array) |
505 | // |
506 | if (is_array($text)) { |
42b7c9d4 |
507 | $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter)); |
62f7daa5 |
508 | |
0177059f |
509 | // just one option here |
510 | // |
511 | } else { |
512 | $option_list = array_merge($option_list, array($value => $text)); |
be2d5495 |
513 | } |
0177059f |
514 | |
62f7daa5 |
515 | } |
0177059f |
516 | if (empty($option_list)) |
517 | $option_list = array('ignore' => _("unavailable")); |
99ecf044 |
518 | |
519 | |
38d93650 |
520 | return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text); |
0177059f |
521 | |
be2d5495 |
522 | } |
523 | |
b4856b14 |
524 | /** |
525 | * Creates textarea |
526 | * @return string html formated textarea field |
527 | */ |
37a3ed17 |
528 | function createWidget_TextArea() { |
bbcafebd |
529 | switch ($this->size) { |
530 | case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break; |
531 | case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break; |
532 | case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break; |
533 | case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break; |
534 | case SMOPT_SIZE_NORMAL: |
535 | default: $rows = 5; $cols = 50; |
536 | } |
ba556ce5 |
537 | return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs); |
a3ec3c91 |
538 | } |
539 | |
b4856b14 |
540 | /** |
541 | * Creates field for integer |
542 | * |
543 | * Difference from createWidget_String is visible only when javascript is enabled |
544 | * @return string html formated option field |
545 | */ |
37a3ed17 |
546 | function createWidget_Integer() { |
0d08ea5a |
547 | |
b65d1a08 |
548 | // add onChange javascript handler to a regular string widget |
549 | // which will strip out all non-numeric chars |
83aff890 |
550 | if (checkForJavascript()) |
0177059f |
551 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
552 | . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' ' |
553 | . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } ' |
0177059f |
554 | . 'this.value=newVal;'; |
555 | |
556 | return $this->createWidget_String(); |
a3ec3c91 |
557 | } |
558 | |
b4856b14 |
559 | /** |
560 | * Creates field for floating number |
561 | * Difference from createWidget_String is visible only when javascript is enabled |
562 | * @return string html formated option field |
563 | */ |
37a3ed17 |
564 | function createWidget_Float() { |
37a3ed17 |
565 | |
b65d1a08 |
566 | // add onChange javascript handler to a regular string widget |
62f7daa5 |
567 | // which will strip out all non-numeric (period also OK) chars |
83aff890 |
568 | if (checkForJavascript()) |
0177059f |
569 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
570 | . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' ' |
571 | . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') ' |
0177059f |
572 | . 'newVal += origVal.charAt(i); } this.value=newVal;'; |
573 | |
574 | return $this->createWidget_String(); |
a3ec3c91 |
575 | } |
576 | |
b4856b14 |
577 | /** |
1b7db98b |
578 | * Create boolean widget |
579 | * |
5b277d00 |
580 | * When creating Yes/No radio buttons, the "yes_text" |
581 | * and "no_text" option attributes are used to override |
582 | * the typical "Yes" and "No" text. |
583 | * |
1b7db98b |
584 | * @param boolean $checkbox When TRUE, the widget will be |
585 | * constructed as a checkbox, |
586 | * otherwise it will be a set of |
587 | * Yes/No radio buttons (OPTIONAL; |
3b6a455c |
588 | * default is TRUE (checkbox)). |
1b7db98b |
589 | * |
590 | * @return string html formated boolean widget |
591 | * |
b4856b14 |
592 | */ |
3b6a455c |
593 | function createWidget_Boolean($checkbox=TRUE) { |
0177059f |
594 | |
5f88daeb |
595 | global $oTemplate, $nbsp; |
fd87494d |
596 | |
fd87494d |
597 | |
1b7db98b |
598 | // checkbox... |
599 | // |
600 | if ($checkbox) { |
601 | $result = addCheckbox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name), $this->aExtraAttribs)) . $nbsp . create_label($this->trailing_text, 'new_' . $this->name); |
602 | } |
603 | |
604 | // radio buttons... |
605 | // |
606 | else { |
607 | |
608 | /* Build the yes choice. */ |
5b277d00 |
609 | $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->yes_text) ? $this->yes_text : _("Yes")), 'new_' . $this->name . '_yes'); |
1b7db98b |
610 | |
611 | /* Build the no choice. */ |
5b277d00 |
612 | $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->no_text) ? $this->no_text : _("No")), 'new_' . $this->name . '_no'); |
1b7db98b |
613 | |
614 | /* Build the combined "boolean widget". */ |
615 | $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option"; |
616 | |
617 | } |
fd87494d |
618 | |
fd87494d |
619 | return ($result); |
a3ec3c91 |
620 | } |
621 | |
b4856b14 |
622 | /** |
623 | * Creates hidden field |
624 | * @return string html formated hidden input field |
625 | */ |
37a3ed17 |
626 | function createWidget_Hidden() { |
0177059f |
627 | return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs); |
a3ec3c91 |
628 | } |
629 | |
b4856b14 |
630 | /** |
631 | * Creates comment |
632 | * @return string comment |
633 | */ |
37a3ed17 |
634 | function createWidget_Comment() { |
bbcafebd |
635 | $result = $this->comment; |
636 | return ($result); |
637 | } |
638 | |
38d93650 |
639 | /** |
640 | * Creates an edit list |
641 | * @return string html formated list of edit fields and |
642 | * their associated controls |
643 | */ |
644 | function createWidget_EditList() { |
645 | |
cb606dfd |
646 | global $oTemplate; |
38d93650 |
647 | |
648 | switch ($this->size) { |
649 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
650 | case SMOPT_SIZE_TINY: |
651 | $height = 3; |
652 | break; |
653 | case SMOPT_SIZE_SMALL: |
654 | $height = 8; |
655 | break; |
656 | case SMOPT_SIZE_LARGE: |
657 | $height = 15; |
658 | break; |
659 | case SMOPT_SIZE_HUGE: |
660 | $height = 25; |
661 | break; |
662 | case SMOPT_SIZE_NORMAL: |
663 | default: |
664 | $height = 5; |
665 | } |
666 | |
dd6f9627 |
667 | if (empty($this->possible_values)) $this->possible_values = array(); |
2ebfc729 |
668 | if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values); |
dd6f9627 |
669 | |
cb606dfd |
670 | //FIXME: $this->aExtraAttribs probably should only be used in one place |
671 | $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs)); |
672 | $oTemplate->assign('trailing_text', $this->trailing_text); |
673 | $oTemplate->assign('select_widget', addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput) == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name . '\' ) { pos = j; } j++; } i++; } if( pos >= 0 ) { window.addinput = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput.value = this.options[x].value; break; } }'), $this->aExtraAttribs), TRUE, $height)); |
674 | $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs))); |
675 | $oTemplate->assign('name', $this->name); |
676 | return $oTemplate->fetch('edit_list_widget.tpl'); |
38d93650 |
677 | |
678 | } |
679 | |
b4856b14 |
680 | /** |
681 | * |
682 | */ |
cbe5423b |
683 | function save() { |
684 | $function = $this->save_function; |
685 | $function($this); |
44ef0f47 |
686 | } |
cbe5423b |
687 | |
b4856b14 |
688 | /** |
689 | * |
690 | */ |
cbe5423b |
691 | function changed() { |
38d93650 |
692 | |
693 | // edit lists have a lot going on, so we'll always process them |
694 | // |
695 | if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE; |
696 | |
6206f6c4 |
697 | return ($this->value != $this->new_value); |
cbe5423b |
698 | } |
b4856b14 |
699 | } /* End of SquirrelOption class*/ |
cbe5423b |
700 | |
b4856b14 |
701 | /** |
f2aba536 |
702 | * Saves the option value (this is the default save function |
703 | * unless overridden by the user) |
704 | * |
b4856b14 |
705 | * @param object $option object that holds option name and new_value |
706 | */ |
cbe5423b |
707 | function save_option($option) { |
f2aba536 |
708 | |
709 | // Can't save the pref if we don't have the username |
710 | // |
dac16606 |
711 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
dac16606 |
712 | return; |
0b97a708 |
713 | } |
f2aba536 |
714 | |
0b97a708 |
715 | global $data_dir; |
f2aba536 |
716 | |
38d93650 |
717 | // edit lists: first add new elements to list, then |
718 | // remove any selected ones (note that we must add |
719 | // before deleting because the javascript that populates |
720 | // the "add" textbox when selecting items in the list |
721 | // (for deletion)) |
722 | // |
723 | if ($option->type == SMOPT_TYPE_EDIT_LIST) { |
724 | |
8eb63bb6 |
725 | if (empty($option->possible_values)) $option->possible_values = array(); |
95dbbd91 |
726 | if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values); |
8eb63bb6 |
727 | |
38d93650 |
728 | // add element if given |
729 | // |
730 | if (sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) { |
731 | $new_element = trim($new_element); |
732 | if (!empty($new_element) |
733 | && !in_array($new_element, $option->possible_values)) |
734 | $option->possible_values[] = $new_element; |
735 | } |
736 | |
737 | // delete selected elements if needed |
738 | // |
739 | if (is_array($option->new_value) |
740 | && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) |
741 | $option->possible_values = array_diff($option->possible_values, $option->new_value); |
742 | |
743 | // save full list (stored in "possible_values") |
744 | // |
745 | setPref($data_dir, $username, $option->name, serialize($option->possible_values)); |
746 | |
f2aba536 |
747 | // Certain option types need to be serialized because |
748 | // they are not scalar |
749 | // |
5a42c101 |
750 | } else if ($option->is_multiple_valued()) |
f2aba536 |
751 | setPref($data_dir, $username, $option->name, serialize($option->new_value)); |
38d93650 |
752 | |
f2aba536 |
753 | else |
754 | setPref($data_dir, $username, $option->name, $option->new_value); |
755 | |
cbe5423b |
756 | } |
757 | |
b4856b14 |
758 | /** |
759 | * save function that does not save |
760 | * @param object $option |
761 | */ |
cbe5423b |
762 | function save_option_noop($option) { |
763 | /* Do nothing here... */ |
9962527a |
764 | } |
44ef0f47 |
765 | |
b4856b14 |
766 | /** |
767 | * Create hidden 'optpage' input field with value set by argument |
768 | * @param string $optpage identification of option page |
769 | * @return string html formated hidden input field |
770 | */ |
cbe5423b |
771 | function create_optpage_element($optpage) { |
0177059f |
772 | return addHidden('optpage', $optpage); |
cbe5423b |
773 | } |
774 | |
b4856b14 |
775 | /** |
776 | * Create hidden 'optmode' input field with value set by argument |
777 | * @param string $optmode |
778 | * @return string html formated hidden input field |
779 | */ |
cbe5423b |
780 | function create_optmode_element($optmode) { |
0177059f |
781 | return addHidden('optmode', $optmode); |
cbe5423b |
782 | } |
783 | |
b4856b14 |
784 | /** |
785 | * @param array $optgrps |
786 | * @param array $optvals |
787 | * @return array |
788 | */ |
cbe5423b |
789 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
790 | /* Build a simple array with which to start. */ |
791 | $result = array(); |
792 | |
bbcafebd |
793 | /* Create option group for each option group name. */ |
794 | foreach ($optgrps as $grpkey => $grpname) { |
795 | $result[$grpkey] = array(); |
796 | $result[$grpkey]['name'] = $grpname; |
797 | $result[$grpkey]['options'] = array(); |
798 | } |
799 | |
a3ec3c91 |
800 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
801 | foreach ($optvals as $grpkey => $grpopts) { |
802 | foreach ($grpopts as $optset) { |
28520c87 |
803 | /* Create a new option with all values given. */ |
804 | $next_option = new SquirrelOption( |
7390e240 |
805 | $optset['name'], |
806 | $optset['caption'], |
807 | $optset['type'], |
808 | (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE), |
809 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
810 | (isset($optset['posvals']) ? $optset['posvals'] : ''), |
811 | (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false) |
812 | ); |
bbcafebd |
813 | |
814 | /* If provided, set the size for this option. */ |
815 | if (isset($optset['size'])) { |
816 | $next_option->setSize($optset['size']); |
817 | } |
818 | |
361d6e1b |
819 | /* If provided, set the trailing_text for this option. */ |
820 | if (isset($optset['trailing_text'])) { |
821 | $next_option->setTrailingText($optset['trailing_text']); |
822 | } |
823 | |
5b277d00 |
824 | /* If provided, set the yes_text for this option. */ |
825 | if (isset($optset['yes_text'])) { |
826 | $next_option->setYesText($optset['yes_text']); |
827 | } |
828 | |
829 | /* If provided, set the no_text for this option. */ |
830 | if (isset($optset['no_text'])) { |
831 | $next_option->setNoText($optset['no_text']); |
832 | } |
833 | |
bbcafebd |
834 | /* If provided, set the comment for this option. */ |
835 | if (isset($optset['comment'])) { |
836 | $next_option->setComment($optset['comment']); |
837 | } |
838 | |
cbe5423b |
839 | /* If provided, set the save function for this option. */ |
840 | if (isset($optset['save'])) { |
841 | $next_option->setSaveFunction($optset['save']); |
842 | } |
843 | |
0177059f |
844 | /* If provided, set the extra attributes for this option. */ |
845 | if (isset($optset['extra_attributes'])) { |
846 | $next_option->setExtraAttributes($optset['extra_attributes']); |
cbe5423b |
847 | } |
848 | |
6ae9e729 |
849 | /* If provided, set the "post script" for this option. */ |
850 | if (isset($optset['post_script'])) { |
851 | $next_option->setPostScript($optset['post_script']); |
852 | } |
853 | |
99ecf044 |
854 | /* If provided, set the folder_filter for this option. */ |
855 | if (isset($optset['folder_filter'])) { |
856 | $next_option->setFolderFilter($optset['folder_filter']); |
857 | } |
858 | |
bbcafebd |
859 | /* Add this option to the option array. */ |
860 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
861 | } |
862 | } |
863 | |
864 | /* Return our resulting array. */ |
865 | return ($result); |
866 | } |
867 | |