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