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