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