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