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