44ef0f47 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * options.php |
5 | * |
35586184 |
6 | * Functions needed to display the options pages. |
7 | * |
22387c8d |
8 | * @copyright 1999-2017 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 | |
9962527a |
15 | /** |
598294a7 |
16 | * SquirrelOption: An option for SquirrelMail. |
9962527a |
17 | * |
8f6f9ba5 |
18 | * @package squirrelmail |
b4856b14 |
19 | * @subpackage prefs |
9962527a |
20 | */ |
21 | class SquirrelOption { |
d789daf0 |
22 | /** |
23 | * The original option configuration array |
24 | * @var array |
25 | */ |
26 | var $raw_option_array; |
b4856b14 |
27 | /** |
28 | * The name of this setting |
29 | * @var string |
30 | */ |
9962527a |
31 | var $name; |
b4856b14 |
32 | /** |
33 | * The text that prefaces setting on the preferences page |
34 | * @var string |
35 | */ |
9962527a |
36 | var $caption; |
e40b0e8e |
37 | /** |
38 | * Whether or not the caption text is allowed to wrap |
39 | * @var boolean |
40 | */ |
41 | var $caption_wrap; |
b4856b14 |
42 | /** |
43 | * The type of INPUT element |
44 | * |
45 | * See SMOPT_TYPE_* defines |
46 | * @var integer |
47 | */ |
9962527a |
48 | var $type; |
b4856b14 |
49 | /** |
598294a7 |
50 | * Indicates if a link should be shown to refresh part |
b4856b14 |
51 | * or all of the window |
52 | * |
53 | * See SMOPT_REFRESH_* defines |
54 | * @var integer |
55 | */ |
a3ec3c91 |
56 | var $refresh_level; |
b4856b14 |
57 | /** |
58 | * Specifies the size of certain input items |
59 | * |
60 | * See SMOPT_SIZE_* defines |
61 | * @var integer |
62 | */ |
bbcafebd |
63 | var $size; |
b4856b14 |
64 | /** |
598294a7 |
65 | * Text that follows a text input or |
b4856b14 |
66 | * select list input on the preferences page |
598294a7 |
67 | * |
b4856b14 |
68 | * useful for indicating units, meanings of special values, etc. |
69 | * @var string |
70 | */ |
361d6e1b |
71 | var $trailing_text; |
696129d8 |
72 | /** |
73 | * Indicates that the widget's "trailing text" |
74 | * should be displayed in a smaller sized font |
75 | * |
76 | * @var boolean |
77 | */ |
78 | var $trailing_text_small; |
6881bcbd |
79 | /** |
80 | * Indicates that the widget's "trailing text" |
81 | * contains HTML and should not thus be |
82 | * sanitized (encoded) |
83 | * |
84 | * @var boolean |
85 | */ |
86 | var $trailing_text_is_html; |
5b277d00 |
87 | /** |
88 | * Text that overrides the "Yes" label for boolean |
89 | * radio option widgets |
90 | * |
91 | * @var string |
92 | */ |
93 | var $yes_text; |
94 | /** |
95 | * Text that overrides the "No" label for boolean |
96 | * radio option widgets |
97 | * |
98 | * @var string |
99 | */ |
100 | var $no_text; |
52639d23 |
101 | /** |
102 | * Some widgets support more than one layout type |
103 | * |
104 | * @var int |
105 | */ |
106 | var $layout_type; |
b6a08d2d |
107 | /** |
108 | * Indicates if the Add widget should be included |
109 | * with edit lists. |
110 | * |
111 | * @var boolean |
112 | */ |
113 | var $use_add_widget; |
de4c101c |
114 | /** |
115 | * Indicates if the Delete widget should be included |
116 | * with edit lists. |
117 | * |
118 | * @var boolean |
119 | */ |
120 | var $use_delete_widget; |
282f8e7c |
121 | /** |
122 | * associative array, treated the same as $possible_values |
123 | * (see its documentation below), but usually expected to |
124 | * have its first value contain a list of IMAP folders, an |
125 | * array itself in the format as passed back by |
126 | * sqimap_mailbox_list(). Used to display folder selector |
127 | * for possible values of an associative edit list option |
128 | * widget |
129 | * |
130 | * @since 1.5.2 |
131 | * @var array |
132 | */ |
133 | var $poss_value_folders; |
b4856b14 |
134 | /** |
135 | * text displayed to the user |
136 | * |
137 | * Used with SMOPT_TYPE_COMMENT options |
138 | * @var string |
139 | */ |
bbcafebd |
140 | var $comment; |
b4856b14 |
141 | /** |
0177059f |
142 | * additional javascript or other widget attributes added to the |
143 | * user input; must be an array where keys are attribute names |
144 | * ("onclick", etc) and values are the attribute values. |
145 | * @var array |
b4856b14 |
146 | */ |
0177059f |
147 | var $aExtraAttribs; |
b4856b14 |
148 | /** |
598294a7 |
149 | * script (usually Javascript) that will be placed after (outside of) |
b4856b14 |
150 | * the INPUT tag |
151 | * @var string |
152 | */ |
6ae9e729 |
153 | var $post_script; |
cbe5423b |
154 | |
b4856b14 |
155 | /** |
156 | * The name of the Save Function for this option. |
157 | * @var string |
158 | */ |
cbe5423b |
159 | var $save_function; |
9962527a |
160 | |
161 | /* The various 'values' for this options. */ |
b4856b14 |
162 | /** |
163 | * default/preselected value for this option |
164 | * @var mixed |
165 | */ |
9962527a |
166 | var $value; |
b4856b14 |
167 | /** |
168 | * new option value |
169 | * @var mixed |
170 | */ |
9962527a |
171 | var $new_value; |
b4856b14 |
172 | /** |
598294a7 |
173 | * associative array, where each key is an actual input value |
b4856b14 |
174 | * and the corresponding value is what is displayed to the user |
175 | * for that list item in the drop-down list |
176 | * @var array |
177 | */ |
a3ec3c91 |
178 | var $possible_values; |
b4856b14 |
179 | /** |
180 | * disables html sanitizing. |
598294a7 |
181 | * |
182 | * WARNING - don't use it, if user input is possible in option |
0177059f |
183 | * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST. |
b4856b14 |
184 | * @var bool |
185 | */ |
28520c87 |
186 | var $htmlencoded=false; |
99ecf044 |
187 | /** |
42b7c9d4 |
188 | * Controls folder list limits in SMOPT_TYPE_FLDRLIST and |
282f8e7c |
189 | * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional |
190 | * embedded folder lists provided for inputting values for |
191 | * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE |
192 | * :idgets. |
99ecf044 |
193 | * See $flag argument in sqimap_mailbox_option_list() function. |
194 | * @var string |
195 | * @since 1.5.1 |
196 | */ |
197 | var $folder_filter='noselect'; |
9962527a |
198 | |
b4856b14 |
199 | /** |
b4d52882 |
200 | * Constructor (PHP5 style, required in some future version of PHP) |
d789daf0 |
201 | * @param array $raw_option_array |
b4856b14 |
202 | * @param string $name |
203 | * @param string $caption |
204 | * @param integer $type |
205 | * @param integer $refresh_level |
206 | * @param mixed $initial_value |
207 | * @param array $possible_values |
208 | * @param bool $htmlencoded |
209 | */ |
b4d52882 |
210 | function __construct |
d789daf0 |
211 | ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) { |
9962527a |
212 | /* Set the basic stuff. */ |
d789daf0 |
213 | $this->raw_option_array = $raw_option_array; |
9962527a |
214 | $this->name = $name; |
215 | $this->caption = $caption; |
e40b0e8e |
216 | $this->caption_wrap = TRUE; |
9962527a |
217 | $this->type = $type; |
a3ec3c91 |
218 | $this->refresh_level = $refresh_level; |
219 | $this->possible_values = $possible_values; |
28520c87 |
220 | $this->htmlencoded = $htmlencoded; |
b1dcab7e |
221 | $this->size = SMOPT_SIZE_NORMAL; |
361d6e1b |
222 | $this->trailing_text = ''; |
696129d8 |
223 | $this->trailing_text_small = FALSE; |
6881bcbd |
224 | $this->trailing_text_is_html = FALSE; |
5b277d00 |
225 | $this->yes_text = ''; |
226 | $this->no_text = ''; |
bbcafebd |
227 | $this->comment = ''; |
52639d23 |
228 | $this->layout_type = 0; |
b6a08d2d |
229 | $this->use_add_widget = TRUE; |
de4c101c |
230 | $this->use_delete_widget = TRUE; |
282f8e7c |
231 | $this->poss_value_folders = ''; |
0177059f |
232 | $this->aExtraAttribs = array(); |
6ae9e729 |
233 | $this->post_script = ''; |
a3ec3c91 |
234 | |
991c88e7 |
235 | //Check for a current value. |
236 | if (isset($GLOBALS[$name])) { |
a3ec3c91 |
237 | $this->value = $GLOBALS[$name]; |
17f3d242 |
238 | } else if (!empty($initial_value)) { |
239 | $this->value = $initial_value; |
a3ec3c91 |
240 | } else { |
241 | $this->value = ''; |
242 | } |
9962527a |
243 | |
a3ec3c91 |
244 | /* Check for a new value. */ |
b4856b14 |
245 | if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) { |
59fc0b63 |
246 | $this->new_value = NULL; |
44ef0f47 |
247 | } |
cbe5423b |
248 | |
249 | /* Set the default save function. */ |
6c06cf54 |
250 | if ($type != SMOPT_TYPE_HIDDEN |
251 | && $type != SMOPT_TYPE_INFO |
252 | && $type != SMOPT_TYPE_COMMENT) { |
cbe5423b |
253 | $this->save_function = SMOPT_SAVE_DEFAULT; |
254 | } else { |
255 | $this->save_function = SMOPT_SAVE_NOOP; |
256 | } |
257 | } |
258 | |
b4d52882 |
259 | /** |
260 | * Constructor (PHP4 style, kept for compatibility reasons) |
261 | * @param array $raw_option_array |
262 | * @param string $name |
263 | * @param string $caption |
264 | * @param integer $type |
265 | * @param integer $refresh_level |
266 | * @param mixed $initial_value |
267 | * @param array $possible_values |
268 | * @param bool $htmlencoded |
269 | */ |
270 | function SquirrelOption |
271 | ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) { |
272 | self::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded); |
273 | } |
274 | |
5a42c101 |
275 | /** Convenience function that identifies which types of |
276 | widgets are stored as (serialized) array values. */ |
277 | function is_multiple_valued() { |
278 | return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI |
279 | || $this->type == SMOPT_TYPE_STRLIST_MULTI |
282f8e7c |
280 | || $this->type == SMOPT_TYPE_EDIT_LIST |
281 | || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE); |
5a42c101 |
282 | } |
283 | |
b4856b14 |
284 | /** |
285 | * Set the value for this option. |
286 | * @param mixed $value |
287 | */ |
cbe5423b |
288 | function setValue($value) { |
289 | $this->value = $value; |
290 | } |
291 | |
b4856b14 |
292 | /** |
293 | * Set the new value for this option. |
294 | * @param mixed $new_value |
295 | */ |
cbe5423b |
296 | function setNewValue($new_value) { |
297 | $this->new_value = $new_value; |
9962527a |
298 | } |
44ef0f47 |
299 | |
e40b0e8e |
300 | /** |
301 | * Set whether the caption is allowed to wrap for this option. |
302 | * @param boolean $caption_wrap |
303 | */ |
304 | function setCaptionWrap($caption_wrap) { |
305 | $this->caption_wrap = $caption_wrap; |
306 | } |
307 | |
b4856b14 |
308 | /** |
309 | * Set the size for this option. |
310 | * @param integer $size |
311 | */ |
bbcafebd |
312 | function setSize($size) { |
313 | $this->size = $size; |
314 | } |
315 | |
b4856b14 |
316 | /** |
317 | * Set the trailing_text for this option. |
318 | * @param string $trailing_text |
319 | */ |
361d6e1b |
320 | function setTrailingText($trailing_text) { |
321 | $this->trailing_text = $trailing_text; |
322 | } |
323 | |
696129d8 |
324 | /** |
6881bcbd |
325 | * Set the trailing_text_small for this option. |
326 | * @param boolean $trailing_text_small |
696129d8 |
327 | */ |
328 | function setTrailingTextSmall($trailing_text_small) { |
329 | $this->trailing_text_small = $trailing_text_small; |
330 | } |
331 | |
6881bcbd |
332 | /** |
333 | * Set the trailing_text_is_html for this option. |
334 | * @param boolean $trailing_text_is_html |
335 | */ |
336 | function setTrailingTextIsHtml($trailing_text_is_html) { |
337 | $this->trailing_text_is_html = $trailing_text_is_html; |
338 | } |
339 | |
5b277d00 |
340 | /** |
341 | * Set the yes_text for this option. |
342 | * @param string $yes_text |
343 | */ |
344 | function setYesText($yes_text) { |
345 | $this->yes_text = $yes_text; |
346 | } |
347 | |
348 | /** |
349 | * Set the no_text for this option. |
350 | * @param string $no_text |
351 | */ |
352 | function setNoText($no_text) { |
353 | $this->no_text = $no_text; |
354 | } |
355 | |
b6a08d2d |
356 | /* Set the "use add widget" value for this option. */ |
357 | function setUseAddWidget($use_add_widget) { |
358 | $this->use_add_widget = $use_add_widget; |
359 | } |
360 | |
de4c101c |
361 | /* Set the "use delete widget" value for this option. */ |
362 | function setUseDeleteWidget($use_delete_widget) { |
363 | $this->use_delete_widget = $use_delete_widget; |
364 | } |
365 | |
282f8e7c |
366 | /* Set the "poss value folders" value for this option. |
367 | See the associative edit list widget, which uses this |
368 | to offer folder list selection for the values */ |
369 | function setPossValueFolders($poss_value_folders) { |
370 | $this->poss_value_folders = $poss_value_folders; |
371 | } |
372 | |
52639d23 |
373 | /** |
374 | * Set the layout type for this option. |
375 | * @param int $layout_type |
376 | */ |
377 | function setLayoutType($layout_type) { |
378 | $this->layout_type = $layout_type; |
379 | } |
380 | |
b4856b14 |
381 | /** |
382 | * Set the comment for this option. |
383 | * @param string $comment |
384 | */ |
bbcafebd |
385 | function setComment($comment) { |
386 | $this->comment = $comment; |
387 | } |
388 | |
b4856b14 |
389 | /** |
0177059f |
390 | * Set the extra attributes for this option. |
391 | * @param array $aExtraAttribs |
b4856b14 |
392 | */ |
0177059f |
393 | function setExtraAttributes($aExtraAttribs) { |
394 | $this->aExtraAttribs = $aExtraAttribs; |
cbe5423b |
395 | } |
396 | |
b4856b14 |
397 | /** |
398 | * Set the "post script" for this option. |
399 | * @param string $post_script |
400 | */ |
6ae9e729 |
401 | function setPostScript($post_script) { |
402 | $this->post_script = $post_script; |
403 | } |
404 | |
b4856b14 |
405 | /** |
406 | * Set the save function for this option. |
407 | * @param string $save_function |
408 | */ |
cbe5423b |
409 | function setSaveFunction($save_function) { |
410 | $this->save_function = $save_function; |
411 | } |
412 | |
99ecf044 |
413 | /** |
5b277d00 |
414 | * Set the folder_filter for this option. |
99ecf044 |
415 | * @param string $folder_filter |
416 | * @since 1.5.1 |
417 | */ |
418 | function setFolderFilter($folder_filter) { |
419 | $this->folder_filter = $folder_filter; |
420 | } |
421 | |
b4856b14 |
422 | /** |
423 | * Creates fields on option pages according to option type |
424 | * |
9786ea94 |
425 | * This is the function that calls all other createWidget* functions. |
426 | * |
427 | * @return string The formated option field |
428 | * |
b4856b14 |
429 | */ |
9786ea94 |
430 | function createWidget() { |
ce68b76b |
431 | global $color; |
cbe5423b |
432 | |
62f7daa5 |
433 | // Use new value if available |
59fc0b63 |
434 | if (!is_null($this->new_value)) { |
74e44765 |
435 | $tempValue = $this->value; |
436 | $this->value = $this->new_value; |
437 | } |
438 | |
cbe5423b |
439 | /* Get the widget for this option type. */ |
a3ec3c91 |
440 | switch ($this->type) { |
3fa09710 |
441 | case SMOPT_TYPE_PASSWORD: |
442 | $result = $this->createWidget_String(TRUE); |
443 | break; |
a3ec3c91 |
444 | case SMOPT_TYPE_STRING: |
37a3ed17 |
445 | $result = $this->createWidget_String(); |
a3ec3c91 |
446 | break; |
447 | case SMOPT_TYPE_STRLIST: |
37a3ed17 |
448 | $result = $this->createWidget_StrList(); |
a3ec3c91 |
449 | break; |
7e6d5ea3 |
450 | case SMOPT_TYPE_TEXTAREA: |
37a3ed17 |
451 | $result = $this->createWidget_TextArea(); |
a3ec3c91 |
452 | break; |
453 | case SMOPT_TYPE_INTEGER: |
37a3ed17 |
454 | $result = $this->createWidget_Integer(); |
a3ec3c91 |
455 | break; |
456 | case SMOPT_TYPE_FLOAT: |
37a3ed17 |
457 | $result = $this->createWidget_Float(); |
a3ec3c91 |
458 | break; |
459 | case SMOPT_TYPE_BOOLEAN: |
37a3ed17 |
460 | $result = $this->createWidget_Boolean(); |
a3ec3c91 |
461 | break; |
1b7db98b |
462 | case SMOPT_TYPE_BOOLEAN_CHECKBOX: |
463 | $result = $this->createWidget_Boolean(TRUE); |
464 | break; |
8b2726c5 |
465 | case SMOPT_TYPE_BOOLEAN_RADIO: |
466 | $result = $this->createWidget_Boolean(FALSE); |
467 | break; |
2a50fbd7 |
468 | case SMOPT_TYPE_HIDDEN: |
37a3ed17 |
469 | $result = $this->createWidget_Hidden(); |
a3ec3c91 |
470 | break; |
bbcafebd |
471 | case SMOPT_TYPE_COMMENT: |
37a3ed17 |
472 | $result = $this->createWidget_Comment(); |
bbcafebd |
473 | break; |
be2d5495 |
474 | case SMOPT_TYPE_FLDRLIST: |
37a3ed17 |
475 | $result = $this->createWidget_FolderList(); |
be2d5495 |
476 | break; |
42b7c9d4 |
477 | case SMOPT_TYPE_FLDRLIST_MULTI: |
478 | $result = $this->createWidget_FolderList(TRUE); |
479 | break; |
38d93650 |
480 | case SMOPT_TYPE_EDIT_LIST: |
481 | $result = $this->createWidget_EditList(); |
482 | break; |
282f8e7c |
483 | case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE: |
484 | $result = $this->createWidget_EditListAssociative(); |
485 | break; |
40268626 |
486 | case SMOPT_TYPE_STRLIST_MULTI: |
487 | $result = $this->createWidget_StrList(TRUE); |
488 | break; |
daf77710 |
489 | case SMOPT_TYPE_STRLIST_RADIO: |
490 | $result = $this->createWidget_StrList(FALSE, TRUE); |
491 | break; |
b6a08d2d |
492 | case SMOPT_TYPE_SUBMIT: |
493 | $result = $this->createWidget_Submit(); |
494 | break; |
6c06cf54 |
495 | case SMOPT_TYPE_INFO: |
496 | $result = $this->createWidget_Info(); |
497 | break; |
a3ec3c91 |
498 | default: |
fb8c4296 |
499 | error_box ( |
500 | sprintf(_("Option Type '%s' Not Found"), $this->type) |
501 | ); |
a3ec3c91 |
502 | } |
503 | |
6ae9e729 |
504 | /* Add the "post script" for this option. */ |
505 | $result .= $this->post_script; |
62f7daa5 |
506 | |
74e44765 |
507 | // put correct value back if need be |
59fc0b63 |
508 | if (!is_null($this->new_value)) { |
74e44765 |
509 | $this->value = $tempValue; |
510 | } |
511 | |
a3ec3c91 |
512 | /* Now, return the created widget. */ |
9786ea94 |
513 | return $result; |
a3ec3c91 |
514 | } |
515 | |
6c06cf54 |
516 | /** |
517 | * Creates info block |
518 | * @return string html formated output |
519 | */ |
520 | function createWidget_Info() { |
2d24b622 |
521 | // return sq_htmlspecialchars($this->value); |
522 | // like COMMENT, allow HTML here |
523 | return $this->value; |
6c06cf54 |
524 | } |
525 | |
b4856b14 |
526 | /** |
527 | * Create string field |
3fa09710 |
528 | * |
529 | * @param boolean $password When TRUE, the text in the input |
530 | * widget will be obscured (OPTIONAL; |
531 | * default = FALSE). |
532 | * |
b4856b14 |
533 | * @return string html formated option field |
3fa09710 |
534 | * |
b4856b14 |
535 | */ |
3fa09710 |
536 | function createWidget_String($password=FALSE) { |
bbcafebd |
537 | switch ($this->size) { |
88cb1b4d |
538 | case SMOPT_SIZE_TINY: |
539 | $width = 5; |
540 | break; |
541 | case SMOPT_SIZE_SMALL: |
542 | $width = 12; |
543 | break; |
544 | case SMOPT_SIZE_LARGE: |
545 | $width = 38; |
546 | break; |
547 | case SMOPT_SIZE_HUGE: |
548 | $width = 50; |
549 | break; |
bbcafebd |
550 | case SMOPT_SIZE_NORMAL: |
88cb1b4d |
551 | default: |
552 | $width = 25; |
bbcafebd |
553 | } |
554 | |
696129d8 |
555 | //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here (also, we have <small> tags here; don't want HTML here!) |
3fa09710 |
556 | if ($password) |
6881bcbd |
557 | return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
3fa09710 |
558 | else |
6881bcbd |
559 | return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
a3ec3c91 |
560 | } |
561 | |
b4856b14 |
562 | /** |
daf77710 |
563 | * Create selection box or radio button group |
0177059f |
564 | * |
565 | * When $this->htmlencoded is TRUE, the keys and values in |
566 | * $this->possible_values are assumed to be display-safe. |
567 | * Use with care! |
568 | * |
daf77710 |
569 | * Note that when building radio buttons instead of a select |
570 | * widget, if the "size" attribute is SMOPT_SIZE_TINY, the |
571 | * radio buttons will be output one after another without |
572 | * linebreaks between them. Otherwise, each radio button |
573 | * goes on a line of its own. |
574 | * |
575 | * @param boolean $multiple_select When TRUE, the select widget |
40268626 |
576 | * will allow multiple selections |
daf77710 |
577 | * (OPTIONAL; default is FALSE |
40268626 |
578 | * (single select list)) |
daf77710 |
579 | * @param boolean $radio_buttons When TRUE, the widget will |
580 | * instead be built as a group |
581 | * of radio buttons (and |
582 | * $multiple_select will be |
583 | * forced to FALSE) (OPTIONAL; |
584 | * default is FALSE (select widget)) |
40268626 |
585 | * |
daf77710 |
586 | * @return string html formated selection box or radio buttons |
40268626 |
587 | * |
b4856b14 |
588 | */ |
daf77710 |
589 | function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) { |
98e88751 |
590 | //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 |
591 | |
daf77710 |
592 | // radio buttons instead of select widget? |
593 | // |
594 | if ($radio_buttons) { |
595 | |
596 | global $br, $nbsp; |
597 | $result = ''; |
598 | foreach ($this->possible_values as $real_value => $disp_value) { |
599 | $result .= addRadioBox('new_' . $this->name, ($this->value == $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name . '_' . $real_value), $this->aExtraAttribs)) . $nbsp . create_label($disp_value, 'new_' . $this->name . '_' . $real_value); |
600 | if ($this->size != SMOPT_SIZE_TINY) |
601 | $result .= $br; |
602 | } |
603 | |
604 | return $result; |
605 | } |
606 | |
607 | |
608 | // everything below applies to select widgets |
609 | // |
40268626 |
610 | switch ($this->size) { |
611 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
612 | case SMOPT_SIZE_TINY: |
613 | $height = 3; |
614 | break; |
615 | case SMOPT_SIZE_SMALL: |
616 | $height = 8; |
617 | break; |
618 | case SMOPT_SIZE_LARGE: |
619 | $height = 15; |
620 | break; |
621 | case SMOPT_SIZE_HUGE: |
622 | $height = 25; |
623 | break; |
624 | case SMOPT_SIZE_NORMAL: |
625 | default: |
626 | $height = 5; |
627 | } |
628 | |
6881bcbd |
629 | return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
a3ec3c91 |
630 | |
a3ec3c91 |
631 | } |
632 | |
b4856b14 |
633 | /** |
634 | * Create folder selection box |
42b7c9d4 |
635 | * |
636 | * @param boolean $multiple_select When TRUE, the select widget |
637 | * will allow multiple selections |
638 | * (OPTIONAL; default is FALSE |
639 | * (single select list)) |
640 | * |
b4856b14 |
641 | * @return string html formated selection box |
42b7c9d4 |
642 | * |
b4856b14 |
643 | */ |
42b7c9d4 |
644 | function createWidget_FolderList($multiple_select=FALSE) { |
be2d5495 |
645 | |
38d93650 |
646 | switch ($this->size) { |
647 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
648 | case SMOPT_SIZE_TINY: |
649 | $height = 3; |
650 | break; |
651 | case SMOPT_SIZE_SMALL: |
652 | $height = 8; |
653 | break; |
654 | case SMOPT_SIZE_LARGE: |
655 | $height = 15; |
656 | break; |
657 | case SMOPT_SIZE_HUGE: |
658 | $height = 25; |
659 | break; |
660 | case SMOPT_SIZE_NORMAL: |
661 | default: |
662 | $height = 5; |
663 | } |
664 | |
0177059f |
665 | // possible values might include a nested array of |
666 | // possible values (list of folders) |
667 | // |
668 | $option_list = array(); |
669 | foreach ($this->possible_values as $value => $text) { |
62f7daa5 |
670 | |
0177059f |
671 | // list of folders (boxes array) |
672 | // |
673 | if (is_array($text)) { |
42b7c9d4 |
674 | $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter)); |
62f7daa5 |
675 | |
0177059f |
676 | // just one option here |
677 | // |
678 | } else { |
679 | $option_list = array_merge($option_list, array($value => $text)); |
be2d5495 |
680 | } |
0177059f |
681 | |
62f7daa5 |
682 | } |
0177059f |
683 | if (empty($option_list)) |
684 | $option_list = array('ignore' => _("unavailable")); |
99ecf044 |
685 | |
686 | |
6881bcbd |
687 | return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
0177059f |
688 | |
be2d5495 |
689 | } |
690 | |
b4856b14 |
691 | /** |
692 | * Creates textarea |
693 | * @return string html formated textarea field |
694 | */ |
37a3ed17 |
695 | function createWidget_TextArea() { |
bbcafebd |
696 | switch ($this->size) { |
697 | case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break; |
698 | case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break; |
699 | case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break; |
700 | case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break; |
701 | case SMOPT_SIZE_NORMAL: |
702 | default: $rows = 5; $cols = 50; |
703 | } |
ba556ce5 |
704 | return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs); |
a3ec3c91 |
705 | } |
706 | |
b4856b14 |
707 | /** |
708 | * Creates field for integer |
709 | * |
710 | * Difference from createWidget_String is visible only when javascript is enabled |
711 | * @return string html formated option field |
712 | */ |
37a3ed17 |
713 | function createWidget_Integer() { |
0d08ea5a |
714 | |
b65d1a08 |
715 | // add onChange javascript handler to a regular string widget |
716 | // which will strip out all non-numeric chars |
83aff890 |
717 | if (checkForJavascript()) |
0177059f |
718 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
719 | . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' ' |
720 | . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } ' |
0177059f |
721 | . 'this.value=newVal;'; |
722 | |
723 | return $this->createWidget_String(); |
a3ec3c91 |
724 | } |
725 | |
b4856b14 |
726 | /** |
727 | * Creates field for floating number |
728 | * Difference from createWidget_String is visible only when javascript is enabled |
729 | * @return string html formated option field |
730 | */ |
37a3ed17 |
731 | function createWidget_Float() { |
37a3ed17 |
732 | |
b65d1a08 |
733 | // add onChange javascript handler to a regular string widget |
62f7daa5 |
734 | // which will strip out all non-numeric (period also OK) chars |
83aff890 |
735 | if (checkForJavascript()) |
0177059f |
736 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
737 | . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' ' |
738 | . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') ' |
0177059f |
739 | . 'newVal += origVal.charAt(i); } this.value=newVal;'; |
740 | |
741 | return $this->createWidget_String(); |
a3ec3c91 |
742 | } |
743 | |
b4856b14 |
744 | /** |
1b7db98b |
745 | * Create boolean widget |
746 | * |
5b277d00 |
747 | * When creating Yes/No radio buttons, the "yes_text" |
748 | * and "no_text" option attributes are used to override |
749 | * the typical "Yes" and "No" text. |
750 | * |
1b7db98b |
751 | * @param boolean $checkbox When TRUE, the widget will be |
752 | * constructed as a checkbox, |
753 | * otherwise it will be a set of |
754 | * Yes/No radio buttons (OPTIONAL; |
3b6a455c |
755 | * default is TRUE (checkbox)). |
1b7db98b |
756 | * |
757 | * @return string html formated boolean widget |
758 | * |
b4856b14 |
759 | */ |
3b6a455c |
760 | function createWidget_Boolean($checkbox=TRUE) { |
0177059f |
761 | |
5f88daeb |
762 | global $oTemplate, $nbsp; |
fd87494d |
763 | |
fd87494d |
764 | |
1b7db98b |
765 | // checkbox... |
766 | // |
767 | if ($checkbox) { |
6881bcbd |
768 | //TODO: Why isn't trailing_text being sanitized with sm_encode_special_chars()??? If this is a bug, add that, then add the option to display unsanitized if $this->trailing_text_is_html is enabled |
696129d8 |
769 | $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_small ? '<small>' : '') . $this->trailing_text . ($this->trailing_text_small ? '</small>' : ''), 'new_' . $this->name); |
1b7db98b |
770 | } |
771 | |
772 | // radio buttons... |
773 | // |
774 | else { |
775 | |
776 | /* Build the yes choice. */ |
5b277d00 |
777 | $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 |
778 | |
779 | /* Build the no choice. */ |
5b277d00 |
780 | $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 |
781 | |
782 | /* Build the combined "boolean widget". */ |
5facc3fc |
783 | $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option " . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
1b7db98b |
784 | |
785 | } |
fd87494d |
786 | |
fd87494d |
787 | return ($result); |
a3ec3c91 |
788 | } |
789 | |
b4856b14 |
790 | /** |
791 | * Creates hidden field |
792 | * @return string html formated hidden input field |
793 | */ |
37a3ed17 |
794 | function createWidget_Hidden() { |
0177059f |
795 | return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs); |
a3ec3c91 |
796 | } |
797 | |
b4856b14 |
798 | /** |
799 | * Creates comment |
800 | * @return string comment |
801 | */ |
37a3ed17 |
802 | function createWidget_Comment() { |
bbcafebd |
803 | $result = $this->comment; |
804 | return ($result); |
805 | } |
806 | |
38d93650 |
807 | /** |
282f8e7c |
808 | * Creates a (non-associative) edit list |
52639d23 |
809 | * |
810 | * Note that multiple layout types are supported for this widget. |
811 | * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_* |
812 | * constants. |
813 | * |
38d93650 |
814 | * @return string html formated list of edit fields and |
815 | * their associated controls |
816 | */ |
817 | function createWidget_EditList() { |
818 | |
cb606dfd |
819 | global $oTemplate; |
38d93650 |
820 | |
821 | switch ($this->size) { |
38d93650 |
822 | case SMOPT_SIZE_TINY: |
823 | $height = 3; |
824 | break; |
825 | case SMOPT_SIZE_SMALL: |
826 | $height = 8; |
827 | break; |
b1dcab7e |
828 | case SMOPT_SIZE_MEDIUM: |
38d93650 |
829 | $height = 15; |
830 | break; |
b1dcab7e |
831 | case SMOPT_SIZE_LARGE: |
38d93650 |
832 | $height = 25; |
833 | break; |
b1dcab7e |
834 | case SMOPT_SIZE_HUGE: |
835 | $height = 40; |
836 | break; |
38d93650 |
837 | case SMOPT_SIZE_NORMAL: |
838 | default: |
839 | $height = 5; |
840 | } |
841 | |
dd6f9627 |
842 | if (empty($this->possible_values)) $this->possible_values = array(); |
2ebfc729 |
843 | if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values); |
dd6f9627 |
844 | |
cb606dfd |
845 | //FIXME: $this->aExtraAttribs probably should only be used in one place |
846 | $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs)); |
b6a08d2d |
847 | $oTemplate->assign('use_input_widget', $this->use_add_widget); |
de4c101c |
848 | $oTemplate->assign('use_delete_widget', $this->use_delete_widget); |
b6a08d2d |
849 | |
cb606dfd |
850 | $oTemplate->assign('trailing_text', $this->trailing_text); |
696129d8 |
851 | $oTemplate->assign('trailing_text_small', $this->trailing_text_small); |
6881bcbd |
852 | $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html); |
52639d23 |
853 | $oTemplate->assign('possible_values', $this->possible_values); |
282f8e7c |
854 | $oTemplate->assign('current_value', $this->value); |
855 | $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_' . $this->name . ') == \'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; i=f-1; j=e-1; } j++; } i++; } if( pos >= 0 ) { window.addinput_' . $this->name . ' = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput_' . $this->name . '.value = this.options[x].text; break; } }'), $this->aExtraAttribs), TRUE, $height)); |
856 | // NOTE: i=f-1; j=e-1 is in lieu of break 2 |
cb606dfd |
857 | $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs))); |
858 | $oTemplate->assign('name', $this->name); |
52639d23 |
859 | |
860 | switch ($this->layout_type) { |
861 | case SMOPT_EDIT_LIST_LAYOUT_SELECT: |
862 | return $oTemplate->fetch('edit_list_widget.tpl'); |
863 | case SMOPT_EDIT_LIST_LAYOUT_LIST: |
864 | return $oTemplate->fetch('edit_list_widget_list_style.tpl'); |
865 | default: |
282f8e7c |
866 | error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type)); |
867 | } |
868 | |
869 | } |
870 | |
871 | /** |
872 | * Creates an associative edit list |
873 | * |
874 | * Note that multiple layout types are supported for this widget. |
875 | * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_* |
876 | * constants. |
877 | * |
878 | * @return string html formated list of edit fields and |
879 | * their associated controls |
880 | */ |
881 | function createWidget_EditListAssociative() { |
882 | |
883 | global $oTemplate; |
884 | |
885 | switch ($this->size) { |
886 | case SMOPT_SIZE_TINY: |
887 | $height = 3; |
888 | break; |
889 | case SMOPT_SIZE_SMALL: |
890 | $height = 8; |
891 | break; |
892 | case SMOPT_SIZE_MEDIUM: |
893 | $height = 15; |
894 | break; |
895 | case SMOPT_SIZE_LARGE: |
896 | $height = 25; |
897 | break; |
898 | case SMOPT_SIZE_HUGE: |
899 | $height = 40; |
900 | break; |
901 | case SMOPT_SIZE_NORMAL: |
902 | default: |
903 | $height = 5; |
904 | } |
905 | |
906 | |
907 | // ensure correct format of current value(s) |
908 | // |
909 | if (empty($this->possible_values)) $this->possible_values = array(); |
910 | if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values); |
911 | |
912 | |
913 | $oTemplate->assign('name', $this->name); |
914 | $oTemplate->assign('current_value', $this->value); |
915 | $oTemplate->assign('possible_values', $this->possible_values); |
916 | $oTemplate->assign('poss_value_folders', $this->poss_value_folders); |
917 | $oTemplate->assign('folder_filter', $this->folder_filter); |
918 | |
919 | $oTemplate->assign('use_input_widget', $this->use_add_widget); |
920 | $oTemplate->assign('use_delete_widget', $this->use_delete_widget); |
921 | |
922 | $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs))); |
923 | |
924 | //FIXME: $this->aExtraAttribs probably should only be used in one place |
925 | $oTemplate->assign('input_key_widget', addInput('add_' . $this->name . '_key', '', 22, 0, $this->aExtraAttribs)); |
926 | $oTemplate->assign('input_value_widget', addInput('add_' . $this->name . '_value', '', 12, 0, $this->aExtraAttribs)); |
927 | |
928 | $oTemplate->assign('select_height', $height); |
929 | |
930 | $oTemplate->assign('aAttribs', $this->aExtraAttribs); |
931 | |
932 | $oTemplate->assign('trailing_text', $this->trailing_text); |
696129d8 |
933 | $oTemplate->assign('trailing_text_small', $this->trailing_text_small); |
6881bcbd |
934 | $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html); |
282f8e7c |
935 | |
936 | switch ($this->layout_type) { |
937 | case SMOPT_EDIT_LIST_LAYOUT_SELECT: |
938 | return $oTemplate->fetch('edit_list_associative_widget.tpl'); |
939 | case SMOPT_EDIT_LIST_LAYOUT_LIST: |
940 | return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl'); |
941 | default: |
942 | error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type)); |
52639d23 |
943 | } |
38d93650 |
944 | |
945 | } |
946 | |
b6a08d2d |
947 | /** |
948 | * Creates a submit button |
949 | * |
950 | * @return string html formated submit button widget |
951 | * |
952 | */ |
953 | function createWidget_Submit() { |
954 | |
6881bcbd |
955 | return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : ''); |
b6a08d2d |
956 | |
957 | } |
958 | |
b4856b14 |
959 | /** |
960 | * |
961 | */ |
cbe5423b |
962 | function save() { |
963 | $function = $this->save_function; |
964 | $function($this); |
44ef0f47 |
965 | } |
cbe5423b |
966 | |
b4856b14 |
967 | /** |
968 | * |
969 | */ |
cbe5423b |
970 | function changed() { |
38d93650 |
971 | |
972 | // edit lists have a lot going on, so we'll always process them |
973 | // |
282f8e7c |
974 | if ($this->type == SMOPT_TYPE_EDIT_LIST |
975 | || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) |
976 | return TRUE; |
38d93650 |
977 | |
6206f6c4 |
978 | return ($this->value != $this->new_value); |
cbe5423b |
979 | } |
b4856b14 |
980 | } /* End of SquirrelOption class*/ |
cbe5423b |
981 | |
b4856b14 |
982 | /** |
f2aba536 |
983 | * Saves the option value (this is the default save function |
984 | * unless overridden by the user) |
985 | * |
b4856b14 |
986 | * @param object $option object that holds option name and new_value |
987 | */ |
cbe5423b |
988 | function save_option($option) { |
f2aba536 |
989 | |
990 | // Can't save the pref if we don't have the username |
991 | // |
dac16606 |
992 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
dac16606 |
993 | return; |
0b97a708 |
994 | } |
f2aba536 |
995 | |
ce102fcc |
996 | // if the widget is a selection list, make sure the new |
997 | // value is actually in the selection list and is not an |
998 | // injection attack |
999 | // |
1000 | if ($option->type == SMOPT_TYPE_STRLIST |
1001 | && !array_key_exists($option->new_value, $option->possible_values)) |
1002 | return; |
1003 | |
1004 | |
1005 | // all other widgets except TEXTAREAs should never be allowed to have newlines |
1006 | // |
1007 | else if ($option->type != SMOPT_TYPE_TEXTAREA) |
1008 | $option->new_value = str_replace(array("\r", "\n"), '', $option->new_value); |
1009 | |
1010 | |
0b97a708 |
1011 | global $data_dir; |
f2aba536 |
1012 | |
38d93650 |
1013 | // edit lists: first add new elements to list, then |
1014 | // remove any selected ones (note that we must add |
1015 | // before deleting because the javascript that populates |
1016 | // the "add" textbox when selecting items in the list |
1017 | // (for deletion)) |
1018 | // |
1019 | if ($option->type == SMOPT_TYPE_EDIT_LIST) { |
1020 | |
8eb63bb6 |
1021 | if (empty($option->possible_values)) $option->possible_values = array(); |
95dbbd91 |
1022 | if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values); |
8eb63bb6 |
1023 | |
38d93650 |
1024 | // add element if given |
1025 | // |
b6a08d2d |
1026 | if ((isset($option->use_add_widget) && $option->use_add_widget) |
1027 | && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) { |
38d93650 |
1028 | $new_element = trim($new_element); |
1029 | if (!empty($new_element) |
1030 | && !in_array($new_element, $option->possible_values)) |
1031 | $option->possible_values[] = $new_element; |
1032 | } |
1033 | |
1034 | // delete selected elements if needed |
1035 | // |
de4c101c |
1036 | if ((isset($option->use_delete_widget) && $option->use_delete_widget) |
1037 | && is_array($option->new_value) |
38d93650 |
1038 | && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) |
1039 | $option->possible_values = array_diff($option->possible_values, $option->new_value); |
1040 | |
1041 | // save full list (stored in "possible_values") |
1042 | // |
1043 | setPref($data_dir, $username, $option->name, serialize($option->possible_values)); |
1044 | |
282f8e7c |
1045 | // associative edit lists are handled similar to |
1046 | // non-associative ones |
1047 | // |
1048 | } else if ($option->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) { |
1049 | |
1050 | if (empty($option->possible_values)) $option->possible_values = array(); |
1051 | if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values); |
1052 | |
1053 | // add element if given |
1054 | // |
1055 | $new_element_key = ''; |
1056 | $new_element_value = ''; |
1057 | $retrieve_key = sqGetGlobalVar('add_' . $option->name . '_key', $new_element_key, SQ_POST); |
1058 | $retrieve_value = sqGetGlobalVar('add_' . $option->name . '_value', $new_element_value, SQ_POST); |
1059 | |
1060 | if ((isset($option->use_add_widget) && $option->use_add_widget) |
1061 | && ($retrieve_key || $retrieve_value)) { |
1062 | $new_element_key = trim($new_element_key); |
1063 | $new_element_value = trim($new_element_value); |
1064 | if ($option->poss_value_folders && empty($new_element_key)) |
1065 | $new_element_value = ''; |
1066 | if (!empty($new_element_key) || !empty($new_element_value)) { |
1067 | if (empty($new_element_key)) $new_element_key = '0'; |
1068 | $option->possible_values[$new_element_key] = $new_element_value; |
1069 | } |
1070 | } |
1071 | |
1072 | // delete selected elements if needed |
1073 | // |
1074 | if ((isset($option->use_delete_widget) && $option->use_delete_widget) |
1075 | && is_array($option->new_value) |
1076 | && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) { |
1077 | |
1078 | if ($option->layout_type == SMOPT_EDIT_LIST_LAYOUT_SELECT) { |
1079 | foreach ($option->new_value as $key) |
1080 | unset($option->possible_values[urldecode($key)]); |
1081 | } |
1082 | else |
1083 | $option->possible_values = array_diff($option->possible_values, $option->new_value); |
1084 | } |
1085 | |
1086 | // save full list (stored in "possible_values") |
1087 | // |
1088 | setPref($data_dir, $username, $option->name, serialize($option->possible_values)); |
1089 | |
f2aba536 |
1090 | // Certain option types need to be serialized because |
1091 | // they are not scalar |
1092 | // |
5a42c101 |
1093 | } else if ($option->is_multiple_valued()) |
f2aba536 |
1094 | setPref($data_dir, $username, $option->name, serialize($option->new_value)); |
38d93650 |
1095 | |
74b80a51 |
1096 | // Checkboxes, when unchecked, don't submit anything in |
1097 | // the POST, so set to SMPREF_OFF if not found |
1098 | // |
1099 | else if (($option->type == SMOPT_TYPE_BOOLEAN |
1100 | || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX) |
1101 | && empty($option->new_value)) |
1102 | setPref($data_dir, $username, $option->name, SMPREF_OFF); |
1103 | |
2f8c79ee |
1104 | // For integer fields, make sure we only have digits... |
1105 | // We'll be nice and instead of just converting to an integer, |
1106 | // we'll physically remove each non-digit in the string. |
1107 | // |
1108 | else if ($option->type == SMOPT_TYPE_INTEGER) { |
1109 | $option->new_value = preg_replace('/[^0-9]/', '', $option->new_value); |
1110 | setPref($data_dir, $username, $option->name, $option->new_value); |
1111 | } |
1112 | |
f2aba536 |
1113 | else |
1114 | setPref($data_dir, $username, $option->name, $option->new_value); |
1115 | |
b4f1a9ee |
1116 | |
1117 | // if a checkbox or multi select is zeroed/cleared out, it |
1118 | // needs to have an empty value pushed into its "new_value" slot |
1119 | // |
1120 | if (($option->type == SMOPT_TYPE_STRLIST_MULTI |
1121 | || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX) |
1122 | && is_null($option->new_value)) |
1123 | $option->new_value = ''; |
1124 | |
cbe5423b |
1125 | } |
1126 | |
b4856b14 |
1127 | /** |
1128 | * save function that does not save |
1129 | * @param object $option |
1130 | */ |
cbe5423b |
1131 | function save_option_noop($option) { |
1132 | /* Do nothing here... */ |
9962527a |
1133 | } |
44ef0f47 |
1134 | |
b4856b14 |
1135 | /** |
1136 | * Create hidden 'optpage' input field with value set by argument |
1137 | * @param string $optpage identification of option page |
1138 | * @return string html formated hidden input field |
1139 | */ |
cbe5423b |
1140 | function create_optpage_element($optpage) { |
0177059f |
1141 | return addHidden('optpage', $optpage); |
cbe5423b |
1142 | } |
1143 | |
b4856b14 |
1144 | /** |
1145 | * Create hidden 'optmode' input field with value set by argument |
1146 | * @param string $optmode |
1147 | * @return string html formated hidden input field |
1148 | */ |
cbe5423b |
1149 | function create_optmode_element($optmode) { |
0177059f |
1150 | return addHidden('optmode', $optmode); |
cbe5423b |
1151 | } |
1152 | |
b4856b14 |
1153 | /** |
1154 | * @param array $optgrps |
1155 | * @param array $optvals |
1156 | * @return array |
1157 | */ |
cbe5423b |
1158 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
1159 | /* Build a simple array with which to start. */ |
1160 | $result = array(); |
1161 | |
bbcafebd |
1162 | /* Create option group for each option group name. */ |
1163 | foreach ($optgrps as $grpkey => $grpname) { |
1164 | $result[$grpkey] = array(); |
1165 | $result[$grpkey]['name'] = $grpname; |
1166 | $result[$grpkey]['options'] = array(); |
1167 | } |
1168 | |
a3ec3c91 |
1169 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
1170 | foreach ($optvals as $grpkey => $grpopts) { |
1171 | foreach ($grpopts as $optset) { |
28520c87 |
1172 | /* Create a new option with all values given. */ |
1173 | $next_option = new SquirrelOption( |
d789daf0 |
1174 | $optset, |
7390e240 |
1175 | $optset['name'], |
1176 | $optset['caption'], |
1177 | $optset['type'], |
1178 | (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE), |
1179 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
1180 | (isset($optset['posvals']) ? $optset['posvals'] : ''), |
1181 | (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false) |
1182 | ); |
bbcafebd |
1183 | |
e40b0e8e |
1184 | /* If provided, set if the caption is allowed to wrap for this option. */ |
1185 | if (isset($optset['caption_wrap'])) { |
1186 | $next_option->setCaptionWrap($optset['caption_wrap']); |
1187 | } |
1188 | |
bbcafebd |
1189 | /* If provided, set the size for this option. */ |
1190 | if (isset($optset['size'])) { |
1191 | $next_option->setSize($optset['size']); |
1192 | } |
1193 | |
361d6e1b |
1194 | /* If provided, set the trailing_text for this option. */ |
1195 | if (isset($optset['trailing_text'])) { |
1196 | $next_option->setTrailingText($optset['trailing_text']); |
1197 | } |
1198 | |
696129d8 |
1199 | /* If provided, set the trailing_text_small for this option. */ |
1200 | if (isset($optset['trailing_text_small'])) { |
1201 | $next_option->setTrailingTextSmall($optset['trailing_text_small']); |
1202 | } |
1203 | |
6881bcbd |
1204 | /* If provided, set the trailing_text_is_html for this option. */ |
1205 | if (isset($optset['trailing_text_is_html'])) { |
1206 | $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']); |
1207 | } |
1208 | |
5b277d00 |
1209 | /* If provided, set the yes_text for this option. */ |
1210 | if (isset($optset['yes_text'])) { |
1211 | $next_option->setYesText($optset['yes_text']); |
1212 | } |
1213 | |
1214 | /* If provided, set the no_text for this option. */ |
1215 | if (isset($optset['no_text'])) { |
1216 | $next_option->setNoText($optset['no_text']); |
1217 | } |
1218 | |
282f8e7c |
1219 | /* If provided, set the poss_value_folders value for this option. */ |
1220 | if (isset($optset['poss_value_folders'])) { |
1221 | $next_option->setPossValueFolders($optset['poss_value_folders']); |
1222 | } |
1223 | |
52639d23 |
1224 | /* If provided, set the layout type for this option. */ |
1225 | if (isset($optset['layout_type'])) { |
1226 | $next_option->setLayoutType($optset['layout_type']); |
1227 | } |
1228 | |
b6a08d2d |
1229 | /* If provided, set the use_add_widget value for this option. */ |
1230 | if (isset($optset['use_add_widget'])) { |
1231 | $next_option->setUseAddWidget($optset['use_add_widget']); |
1232 | } |
1233 | |
de4c101c |
1234 | /* If provided, set the use_delete_widget value for this option. */ |
1235 | if (isset($optset['use_delete_widget'])) { |
1236 | $next_option->setUseDeleteWidget($optset['use_delete_widget']); |
1237 | } |
1238 | |
bbcafebd |
1239 | /* If provided, set the comment for this option. */ |
1240 | if (isset($optset['comment'])) { |
1241 | $next_option->setComment($optset['comment']); |
1242 | } |
1243 | |
cbe5423b |
1244 | /* If provided, set the save function for this option. */ |
1245 | if (isset($optset['save'])) { |
1246 | $next_option->setSaveFunction($optset['save']); |
1247 | } |
1248 | |
0177059f |
1249 | /* If provided, set the extra attributes for this option. */ |
1250 | if (isset($optset['extra_attributes'])) { |
1251 | $next_option->setExtraAttributes($optset['extra_attributes']); |
cbe5423b |
1252 | } |
1253 | |
6ae9e729 |
1254 | /* If provided, set the "post script" for this option. */ |
1255 | if (isset($optset['post_script'])) { |
1256 | $next_option->setPostScript($optset['post_script']); |
1257 | } |
1258 | |
99ecf044 |
1259 | /* If provided, set the folder_filter for this option. */ |
1260 | if (isset($optset['folder_filter'])) { |
1261 | $next_option->setFolderFilter($optset['folder_filter']); |
1262 | } |
1263 | |
bbcafebd |
1264 | /* Add this option to the option array. */ |
1265 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
1266 | } |
1267 | } |
1268 | |
1269 | /* Return our resulting array. */ |
1270 | return ($result); |
1271 | } |
1272 | |