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