Search page should remember last move-target too
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Functions needed to display the options pages.
7 *
8 * @copyright 1999-2020 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 * Indicates that the widget's "trailing text"
74 * should be displayed in a smaller sized font
75 *
76 * @var boolean
77 */
78 var $trailing_text_small;
79 /**
80 * Indicates that the widget's "trailing text"
81 * contains HTML and should not thus be
82 * sanitized (encoded)
83 *
84 * @var boolean
85 */
86 var $trailing_text_is_html;
87 /**
88 * Text that overrides the "Yes" label for boolean
89 * radio option widgets
90 *
91 * @var string
92 */
93 var $yes_text;
94 /**
95 * Text that overrides the "No" label for boolean
96 * radio option widgets
97 *
98 * @var string
99 */
100 var $no_text;
101 /**
102 * Some widgets support more than one layout type
103 *
104 * @var int
105 */
106 var $layout_type;
107 /**
108 * Indicates if the Add widget should be included
109 * with edit lists.
110 *
111 * @var boolean
112 */
113 var $use_add_widget;
114 /**
115 * Indicates if the Delete widget should be included
116 * with edit lists.
117 *
118 * @var boolean
119 */
120 var $use_delete_widget;
121 /**
122 * associative array, treated the same as $possible_values
123 * (see its documentation below), but usually expected to
124 * have its first value contain a list of IMAP folders, an
125 * array itself in the format as passed back by
126 * sqimap_mailbox_list(). Used to display folder selector
127 * for possible values of an associative edit list option
128 * widget
129 *
130 * @since 1.5.2
131 * @var array
132 */
133 var $poss_value_folders;
134 /**
135 * text displayed to the user
136 *
137 * Used with SMOPT_TYPE_COMMENT options
138 * @var string
139 */
140 var $comment;
141 /**
142 * additional javascript or other widget attributes added to the
143 * user input; must be an array where keys are attribute names
144 * ("onclick", etc) and values are the attribute values.
145 * @var array
146 */
147 var $aExtraAttribs;
148 /**
149 * script (usually Javascript) that will be placed after (outside of)
150 * the INPUT tag
151 * @var string
152 */
153 var $post_script;
154
155 /**
156 * The name of the Save Function for this option.
157 * @var string
158 */
159 var $save_function;
160
161 /* The various 'values' for this options. */
162 /**
163 * default/preselected value for this option
164 * @var mixed
165 */
166 var $value;
167 /**
168 * new option value
169 * @var mixed
170 */
171 var $new_value;
172 /**
173 * associative array, where each key is an actual input value
174 * and the corresponding value is what is displayed to the user
175 * for that list item in the drop-down list
176 * @var array
177 */
178 var $possible_values;
179 /**
180 * disables html sanitizing.
181 *
182 * WARNING - don't use it, if user input is possible in option
183 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
184 * @var bool
185 */
186 var $htmlencoded=false;
187 /**
188 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
189 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
190 * embedded folder lists provided for inputting values for
191 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
192 * :idgets.
193 * See $flag argument in sqimap_mailbox_option_list() function.
194 * @var string
195 * @since 1.5.1
196 */
197 var $folder_filter='noselect';
198
199 /**
200 * Constructor (PHP5 style, required in some future version of PHP)
201 * @param array $raw_option_array
202 * @param string $name
203 * @param string $caption
204 * @param integer $type
205 * @param integer $refresh_level
206 * @param mixed $initial_value
207 * @param array $possible_values
208 * @param bool $htmlencoded
209 */
210 function __construct
211 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
212 /* Set the basic stuff. */
213 $this->raw_option_array = $raw_option_array;
214 $this->name = $name;
215 $this->caption = $caption;
216 $this->caption_wrap = TRUE;
217 $this->type = $type;
218 $this->refresh_level = $refresh_level;
219 $this->possible_values = $possible_values;
220 $this->htmlencoded = $htmlencoded;
221 $this->size = SMOPT_SIZE_NORMAL;
222 $this->trailing_text = '';
223 $this->trailing_text_small = FALSE;
224 $this->trailing_text_is_html = FALSE;
225 $this->yes_text = '';
226 $this->no_text = '';
227 $this->comment = '';
228 $this->layout_type = 0;
229 $this->use_add_widget = TRUE;
230 $this->use_delete_widget = TRUE;
231 $this->poss_value_folders = '';
232 $this->aExtraAttribs = array();
233 $this->post_script = '';
234
235 //Check for a current value.
236 if (isset($GLOBALS[$name])) {
237 $this->value = $GLOBALS[$name];
238 // TODO: This code should be something more like the following, but who knows what would break if it was changed at this point
239 // } else if (initial_value !== '') {
240 } else if (!empty($initial_value)) {
241 $this->value = $initial_value;
242 } else {
243 $this->value = '';
244 }
245
246 /* Check for a new value. */
247 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
248 $this->new_value = NULL;
249 }
250
251 /* Set the default save function. */
252 if ($type != SMOPT_TYPE_HIDDEN
253 && $type != SMOPT_TYPE_INFO
254 && $type != SMOPT_TYPE_COMMENT) {
255 $this->save_function = SMOPT_SAVE_DEFAULT;
256 } else {
257 $this->save_function = SMOPT_SAVE_NOOP;
258 }
259 }
260
261 /**
262 * Constructor (PHP4 style, kept for compatibility reasons)
263 * @param array $raw_option_array
264 * @param string $name
265 * @param string $caption
266 * @param integer $type
267 * @param integer $refresh_level
268 * @param mixed $initial_value
269 * @param array $possible_values
270 * @param bool $htmlencoded
271 */
272 function SquirrelOption
273 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
274 self::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
275 }
276
277 /** Convenience function that identifies which types of
278 widgets are stored as (serialized) array values. */
279 function is_multiple_valued() {
280 return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
281 || $this->type == SMOPT_TYPE_STRLIST_MULTI
282 || $this->type == SMOPT_TYPE_EDIT_LIST
283 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE);
284 }
285
286 /**
287 * Set the value for this option.
288 * @param mixed $value
289 */
290 function setValue($value) {
291 $this->value = $value;
292 }
293
294 /**
295 * Set the new value for this option.
296 * @param mixed $new_value
297 */
298 function setNewValue($new_value) {
299 $this->new_value = $new_value;
300 }
301
302 /**
303 * Set whether the caption is allowed to wrap for this option.
304 * @param boolean $caption_wrap
305 */
306 function setCaptionWrap($caption_wrap) {
307 $this->caption_wrap = $caption_wrap;
308 }
309
310 /**
311 * Set the size for this option.
312 * @param integer $size
313 */
314 function setSize($size) {
315 $this->size = $size;
316 }
317
318 /**
319 * Set the trailing_text for this option.
320 * @param string $trailing_text
321 */
322 function setTrailingText($trailing_text) {
323 $this->trailing_text = $trailing_text;
324 }
325
326 /**
327 * Set the trailing_text_small for this option.
328 * @param boolean $trailing_text_small
329 */
330 function setTrailingTextSmall($trailing_text_small) {
331 $this->trailing_text_small = $trailing_text_small;
332 }
333
334 /**
335 * Set the trailing_text_is_html for this option.
336 * @param boolean $trailing_text_is_html
337 */
338 function setTrailingTextIsHtml($trailing_text_is_html) {
339 $this->trailing_text_is_html = $trailing_text_is_html;
340 }
341
342 /**
343 * Set the yes_text for this option.
344 * @param string $yes_text
345 */
346 function setYesText($yes_text) {
347 $this->yes_text = $yes_text;
348 }
349
350 /**
351 * Set the no_text for this option.
352 * @param string $no_text
353 */
354 function setNoText($no_text) {
355 $this->no_text = $no_text;
356 }
357
358 /* Set the "use add widget" value for this option. */
359 function setUseAddWidget($use_add_widget) {
360 $this->use_add_widget = $use_add_widget;
361 }
362
363 /* Set the "use delete widget" value for this option. */
364 function setUseDeleteWidget($use_delete_widget) {
365 $this->use_delete_widget = $use_delete_widget;
366 }
367
368 /* Set the "poss value folders" value for this option.
369 See the associative edit list widget, which uses this
370 to offer folder list selection for the values */
371 function setPossValueFolders($poss_value_folders) {
372 $this->poss_value_folders = $poss_value_folders;
373 }
374
375 /**
376 * Set the layout type for this option.
377 * @param int $layout_type
378 */
379 function setLayoutType($layout_type) {
380 $this->layout_type = $layout_type;
381 }
382
383 /**
384 * Set the comment for this option.
385 * @param string $comment
386 */
387 function setComment($comment) {
388 $this->comment = $comment;
389 }
390
391 /**
392 * Set the extra attributes for this option.
393 * @param array $aExtraAttribs
394 */
395 function setExtraAttributes($aExtraAttribs) {
396 $this->aExtraAttribs = $aExtraAttribs;
397 }
398
399 /**
400 * Set the "post script" for this option.
401 * @param string $post_script
402 */
403 function setPostScript($post_script) {
404 $this->post_script = $post_script;
405 }
406
407 /**
408 * Set the save function for this option.
409 * @param string $save_function
410 */
411 function setSaveFunction($save_function) {
412 $this->save_function = $save_function;
413 }
414
415 /**
416 * Set the folder_filter for this option.
417 * @param string $folder_filter
418 * @since 1.5.1
419 */
420 function setFolderFilter($folder_filter) {
421 $this->folder_filter = $folder_filter;
422 }
423
424 /**
425 * Creates fields on option pages according to option type
426 *
427 * This is the function that calls all other createWidget* functions.
428 *
429 * @return string The formated option field
430 *
431 */
432 function createWidget() {
433 global $color;
434
435 // Use new value if available
436 if (!is_null($this->new_value)) {
437 $tempValue = $this->value;
438 $this->value = $this->new_value;
439 }
440
441 /* Get the widget for this option type. */
442 switch ($this->type) {
443 case SMOPT_TYPE_PASSWORD:
444 $result = $this->createWidget_String(TRUE);
445 break;
446 case SMOPT_TYPE_STRING:
447 $result = $this->createWidget_String();
448 break;
449 case SMOPT_TYPE_STRLIST:
450 $result = $this->createWidget_StrList();
451 break;
452 case SMOPT_TYPE_TEXTAREA:
453 $result = $this->createWidget_TextArea();
454 break;
455 case SMOPT_TYPE_INTEGER:
456 $result = $this->createWidget_Integer();
457 break;
458 case SMOPT_TYPE_FLOAT:
459 $result = $this->createWidget_Float();
460 break;
461 case SMOPT_TYPE_BOOLEAN:
462 $result = $this->createWidget_Boolean();
463 break;
464 case SMOPT_TYPE_BOOLEAN_CHECKBOX:
465 $result = $this->createWidget_Boolean(TRUE);
466 break;
467 case SMOPT_TYPE_BOOLEAN_RADIO:
468 $result = $this->createWidget_Boolean(FALSE);
469 break;
470 case SMOPT_TYPE_HIDDEN:
471 $result = $this->createWidget_Hidden();
472 break;
473 case SMOPT_TYPE_COMMENT:
474 $result = $this->createWidget_Comment();
475 break;
476 case SMOPT_TYPE_FLDRLIST:
477 $result = $this->createWidget_FolderList();
478 break;
479 case SMOPT_TYPE_FLDRLIST_MULTI:
480 $result = $this->createWidget_FolderList(TRUE);
481 break;
482 case SMOPT_TYPE_EDIT_LIST:
483 $result = $this->createWidget_EditList();
484 break;
485 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE:
486 $result = $this->createWidget_EditListAssociative();
487 break;
488 case SMOPT_TYPE_STRLIST_MULTI:
489 $result = $this->createWidget_StrList(TRUE);
490 break;
491 case SMOPT_TYPE_STRLIST_RADIO:
492 $result = $this->createWidget_StrList(FALSE, TRUE);
493 break;
494 case SMOPT_TYPE_SUBMIT:
495 $result = $this->createWidget_Submit();
496 break;
497 case SMOPT_TYPE_INFO:
498 $result = $this->createWidget_Info();
499 break;
500 default:
501 error_box (
502 sprintf(_("Option Type '%s' Not Found"), $this->type)
503 );
504 }
505
506 /* Add the "post script" for this option. */
507 $result .= $this->post_script;
508
509 // put correct value back if need be
510 if (!is_null($this->new_value)) {
511 $this->value = $tempValue;
512 }
513
514 /* Now, return the created widget. */
515 return $result;
516 }
517
518 /**
519 * Creates info block
520 * @return string html formated output
521 */
522 function createWidget_Info() {
523 // return sq_htmlspecialchars($this->value);
524 // like COMMENT, allow HTML here
525 return $this->value;
526 }
527
528 /**
529 * Create string field
530 *
531 * @param boolean $password When TRUE, the text in the input
532 * widget will be obscured (OPTIONAL;
533 * default = FALSE).
534 *
535 * @return string html formated option field
536 *
537 */
538 function createWidget_String($password=FALSE) {
539 switch ($this->size) {
540 case SMOPT_SIZE_TINY:
541 $width = 5;
542 break;
543 case SMOPT_SIZE_SMALL:
544 $width = 12;
545 break;
546 case SMOPT_SIZE_LARGE:
547 $width = 38;
548 break;
549 case SMOPT_SIZE_HUGE:
550 $width = 50;
551 break;
552 case SMOPT_SIZE_NORMAL:
553 default:
554 $width = 25;
555 }
556
557 //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here (also, we have <small> tags here; don't want HTML here!)
558 if ($password)
559 return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
560 else
561 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
562 }
563
564 /**
565 * Create selection box or radio button group
566 *
567 * When $this->htmlencoded is TRUE, the keys and values in
568 * $this->possible_values are assumed to be display-safe.
569 * Use with care!
570 *
571 * Note that when building radio buttons instead of a select
572 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
573 * radio buttons will be output one after another without
574 * linebreaks between them. Otherwise, each radio button
575 * goes on a line of its own.
576 *
577 * @param boolean $multiple_select When TRUE, the select widget
578 * will allow multiple selections
579 * (OPTIONAL; default is FALSE
580 * (single select list))
581 * @param boolean $radio_buttons When TRUE, the widget will
582 * instead be built as a group
583 * of radio buttons (and
584 * $multiple_select will be
585 * forced to FALSE) (OPTIONAL;
586 * default is FALSE (select widget))
587 *
588 * @return string html formated selection box or radio buttons
589 *
590 */
591 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
592 //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
593
594 // radio buttons instead of select widget?
595 //
596 if ($radio_buttons) {
597
598 global $br, $nbsp;
599 $result = '';
600 foreach ($this->possible_values as $real_value => $disp_value) {
601 $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);
602 if ($this->size != SMOPT_SIZE_TINY)
603 $result .= $br;
604 }
605
606 return $result;
607 }
608
609
610 // everything below applies to select widgets
611 //
612 switch ($this->size) {
613 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
614 case SMOPT_SIZE_TINY:
615 $height = 3;
616 break;
617 case SMOPT_SIZE_SMALL:
618 $height = 8;
619 break;
620 case SMOPT_SIZE_LARGE:
621 $height = 15;
622 break;
623 case SMOPT_SIZE_HUGE:
624 $height = 25;
625 break;
626 case SMOPT_SIZE_NORMAL:
627 default:
628 $height = 5;
629 }
630
631 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
632
633 }
634
635 /**
636 * Create folder selection box
637 *
638 * @param boolean $multiple_select When TRUE, the select widget
639 * will allow multiple selections
640 * (OPTIONAL; default is FALSE
641 * (single select list))
642 *
643 * @return string html formated selection box
644 *
645 */
646 function createWidget_FolderList($multiple_select=FALSE) {
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 // possible values might include a nested array of
668 // possible values (list of folders)
669 //
670 $option_list = array();
671 foreach ($this->possible_values as $value => $text) {
672
673 // list of folders (boxes array)
674 //
675 if (is_array($text)) {
676 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
677
678 // just one option here
679 //
680 } else {
681 $option_list = array_merge($option_list, array($value => $text));
682 }
683
684 }
685 if (empty($option_list))
686 $option_list = array('ignore' => _("unavailable"));
687
688
689 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
690
691 }
692
693 /**
694 * Creates textarea
695 * @return string html formated textarea field
696 */
697 function createWidget_TextArea() {
698 switch ($this->size) {
699 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
700 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
701 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
702 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
703 case SMOPT_SIZE_NORMAL:
704 default: $rows = 5; $cols = 50;
705 }
706 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
707 }
708
709 /**
710 * Creates field for integer
711 *
712 * Difference from createWidget_String is visible only when javascript is enabled
713 * @return string html formated option field
714 */
715 function createWidget_Integer() {
716
717 // add onChange javascript handler to a regular string widget
718 // which will strip out all non-numeric chars
719 if (checkForJavascript())
720 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
721 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
722 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
723 . 'this.value=newVal;';
724
725 return $this->createWidget_String();
726 }
727
728 /**
729 * Creates field for floating number
730 * Difference from createWidget_String is visible only when javascript is enabled
731 * @return string html formated option field
732 */
733 function createWidget_Float() {
734
735 // add onChange javascript handler to a regular string widget
736 // which will strip out all non-numeric (period also OK) chars
737 if (checkForJavascript())
738 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
739 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
740 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
741 . 'newVal += origVal.charAt(i); } this.value=newVal;';
742
743 return $this->createWidget_String();
744 }
745
746 /**
747 * Create boolean widget
748 *
749 * When creating Yes/No radio buttons, the "yes_text"
750 * and "no_text" option attributes are used to override
751 * the typical "Yes" and "No" text.
752 *
753 * @param boolean $checkbox When TRUE, the widget will be
754 * constructed as a checkbox,
755 * otherwise it will be a set of
756 * Yes/No radio buttons (OPTIONAL;
757 * default is TRUE (checkbox)).
758 *
759 * @return string html formated boolean widget
760 *
761 */
762 function createWidget_Boolean($checkbox=TRUE) {
763
764 global $oTemplate, $nbsp;
765
766
767 // checkbox...
768 //
769 if ($checkbox) {
770 //TODO: Why isn't trailing_text being sanitized with sm_encode_special_chars()??? If this is a bug, add that, then add the option to display unsanitized if $this->trailing_text_is_html is enabled
771 $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_small ? '<small>' : '') . $this->trailing_text . ($this->trailing_text_small ? '</small>' : ''), 'new_' . $this->name);
772 }
773
774 // radio buttons...
775 //
776 else {
777
778 /* Build the yes choice. */
779 $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');
780
781 /* Build the no choice. */
782 $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');
783
784 /* Build the combined "boolean widget". */
785 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option " . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
786
787 }
788
789 return ($result);
790 }
791
792 /**
793 * Creates hidden field
794 * @return string html formated hidden input field
795 */
796 function createWidget_Hidden() {
797 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
798 }
799
800 /**
801 * Creates comment
802 * @return string comment
803 */
804 function createWidget_Comment() {
805 $result = $this->comment;
806 return ($result);
807 }
808
809 /**
810 * Creates a (non-associative) edit list
811 *
812 * Note that multiple layout types are supported for this widget.
813 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
814 * constants.
815 *
816 * @return string html formated list of edit fields and
817 * their associated controls
818 */
819 function createWidget_EditList() {
820
821 global $oTemplate;
822
823 switch ($this->size) {
824 case SMOPT_SIZE_TINY:
825 $height = 3;
826 break;
827 case SMOPT_SIZE_SMALL:
828 $height = 8;
829 break;
830 case SMOPT_SIZE_MEDIUM:
831 $height = 15;
832 break;
833 case SMOPT_SIZE_LARGE:
834 $height = 25;
835 break;
836 case SMOPT_SIZE_HUGE:
837 $height = 40;
838 break;
839 case SMOPT_SIZE_NORMAL:
840 default:
841 $height = 5;
842 }
843
844 if (empty($this->possible_values)) $this->possible_values = array();
845 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
846
847 //FIXME: $this->aExtraAttribs probably should only be used in one place
848 $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
849 $oTemplate->assign('use_input_widget', $this->use_add_widget);
850 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
851
852 $oTemplate->assign('trailing_text', $this->trailing_text);
853 $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
854 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
855 $oTemplate->assign('possible_values', $this->possible_values);
856 $oTemplate->assign('current_value', $this->value);
857 $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; i=f-1; j=e-1; } 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].text; break; } }'), $this->aExtraAttribs), TRUE, $height));
858 // NOTE: i=f-1; j=e-1 is in lieu of break 2
859 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
860 $oTemplate->assign('name', $this->name);
861
862 switch ($this->layout_type) {
863 case SMOPT_EDIT_LIST_LAYOUT_SELECT:
864 return $oTemplate->fetch('edit_list_widget.tpl');
865 case SMOPT_EDIT_LIST_LAYOUT_LIST:
866 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
867 default:
868 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type));
869 }
870
871 }
872
873 /**
874 * Creates an associative edit list
875 *
876 * Note that multiple layout types are supported for this widget.
877 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
878 * constants.
879 *
880 * @return string html formated list of edit fields and
881 * their associated controls
882 */
883 function createWidget_EditListAssociative() {
884
885 global $oTemplate;
886
887 switch ($this->size) {
888 case SMOPT_SIZE_TINY:
889 $height = 3;
890 break;
891 case SMOPT_SIZE_SMALL:
892 $height = 8;
893 break;
894 case SMOPT_SIZE_MEDIUM:
895 $height = 15;
896 break;
897 case SMOPT_SIZE_LARGE:
898 $height = 25;
899 break;
900 case SMOPT_SIZE_HUGE:
901 $height = 40;
902 break;
903 case SMOPT_SIZE_NORMAL:
904 default:
905 $height = 5;
906 }
907
908
909 // ensure correct format of current value(s)
910 //
911 if (empty($this->possible_values)) $this->possible_values = array();
912 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
913
914
915 $oTemplate->assign('name', $this->name);
916 $oTemplate->assign('current_value', $this->value);
917 $oTemplate->assign('possible_values', $this->possible_values);
918 $oTemplate->assign('poss_value_folders', $this->poss_value_folders);
919 $oTemplate->assign('folder_filter', $this->folder_filter);
920
921 $oTemplate->assign('use_input_widget', $this->use_add_widget);
922 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
923
924 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
925
926 //FIXME: $this->aExtraAttribs probably should only be used in one place
927 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name . '_key', '', 22, 0, $this->aExtraAttribs));
928 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name . '_value', '', 12, 0, $this->aExtraAttribs));
929
930 $oTemplate->assign('select_height', $height);
931
932 $oTemplate->assign('aAttribs', $this->aExtraAttribs);
933
934 $oTemplate->assign('trailing_text', $this->trailing_text);
935 $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
936 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
937
938 switch ($this->layout_type) {
939 case SMOPT_EDIT_LIST_LAYOUT_SELECT:
940 return $oTemplate->fetch('edit_list_associative_widget.tpl');
941 case SMOPT_EDIT_LIST_LAYOUT_LIST:
942 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
943 default:
944 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type));
945 }
946
947 }
948
949 /**
950 * Creates a submit button
951 *
952 * @return string html formated submit button widget
953 *
954 */
955 function createWidget_Submit() {
956
957 return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
958
959 }
960
961 /**
962 *
963 */
964 function save() {
965 $function = $this->save_function;
966 $function($this);
967 }
968
969 /**
970 *
971 */
972 function changed() {
973
974 // edit lists have a lot going on, so we'll always process them
975 //
976 if ($this->type == SMOPT_TYPE_EDIT_LIST
977 || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE)
978 return TRUE;
979
980 return ($this->value != $this->new_value);
981 }
982 } /* End of SquirrelOption class*/
983
984 /**
985 * Saves the option value (this is the default save function
986 * unless overridden by the user)
987 *
988 * @param object $option object that holds option name and new_value
989 */
990 function save_option($option) {
991
992 // Can't save the pref if we don't have the username
993 //
994 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
995 return;
996 }
997
998 // if the widget is a selection list, make sure the new
999 // value is actually in the selection list and is not an
1000 // injection attack
1001 //
1002 if ($option->type == SMOPT_TYPE_STRLIST
1003 && !array_key_exists($option->new_value, $option->possible_values))
1004 return;
1005
1006
1007 // all other widgets except TEXTAREAs should never be allowed to have newlines
1008 //
1009 else if ($option->type != SMOPT_TYPE_TEXTAREA)
1010 $option->new_value = str_replace(array("\r", "\n"), '', $option->new_value);
1011
1012
1013 global $data_dir;
1014
1015 // edit lists: first add new elements to list, then
1016 // remove any selected ones (note that we must add
1017 // before deleting because the javascript that populates
1018 // the "add" textbox when selecting items in the list
1019 // (for deletion))
1020 //
1021 if ($option->type == SMOPT_TYPE_EDIT_LIST) {
1022
1023 if (empty($option->possible_values)) $option->possible_values = array();
1024 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
1025
1026 // add element if given
1027 //
1028 if ((isset($option->use_add_widget) && $option->use_add_widget)
1029 && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
1030 $new_element = trim($new_element);
1031 if (!empty($new_element)
1032 && !in_array($new_element, $option->possible_values))
1033 $option->possible_values[] = $new_element;
1034 }
1035
1036 // delete selected elements if needed
1037 //
1038 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
1039 && is_array($option->new_value)
1040 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
1041 $option->possible_values = array_diff($option->possible_values, $option->new_value);
1042
1043 // save full list (stored in "possible_values")
1044 //
1045 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
1046
1047 // associative edit lists are handled similar to
1048 // non-associative ones
1049 //
1050 } else if ($option->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) {
1051
1052 if (empty($option->possible_values)) $option->possible_values = array();
1053 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
1054
1055 // add element if given
1056 //
1057 $new_element_key = '';
1058 $new_element_value = '';
1059 $retrieve_key = sqGetGlobalVar('add_' . $option->name . '_key', $new_element_key, SQ_POST);
1060 $retrieve_value = sqGetGlobalVar('add_' . $option->name . '_value', $new_element_value, SQ_POST);
1061
1062 if ((isset($option->use_add_widget) && $option->use_add_widget)
1063 && ($retrieve_key || $retrieve_value)) {
1064 $new_element_key = trim($new_element_key);
1065 $new_element_value = trim($new_element_value);
1066 if ($option->poss_value_folders && empty($new_element_key))
1067 $new_element_value = '';
1068 if (!empty($new_element_key) || !empty($new_element_value)) {
1069 if (empty($new_element_key)) $new_element_key = '0';
1070 $option->possible_values[$new_element_key] = $new_element_value;
1071 }
1072 }
1073
1074 // delete selected elements if needed
1075 //
1076 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
1077 && is_array($option->new_value)
1078 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) {
1079
1080 if ($option->layout_type == SMOPT_EDIT_LIST_LAYOUT_SELECT) {
1081 foreach ($option->new_value as $key)
1082 unset($option->possible_values[urldecode($key)]);
1083 }
1084 else
1085 $option->possible_values = array_diff($option->possible_values, $option->new_value);
1086 }
1087
1088 // save full list (stored in "possible_values")
1089 //
1090 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
1091
1092 // Certain option types need to be serialized because
1093 // they are not scalar
1094 //
1095 } else if ($option->is_multiple_valued())
1096 setPref($data_dir, $username, $option->name, serialize($option->new_value));
1097
1098 // Checkboxes, when unchecked, don't submit anything in
1099 // the POST, so set to SMPREF_OFF if not found
1100 //
1101 else if (($option->type == SMOPT_TYPE_BOOLEAN
1102 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
1103 && empty($option->new_value))
1104 setPref($data_dir, $username, $option->name, SMPREF_OFF);
1105
1106 // For integer fields, make sure we only have digits...
1107 // We'll be nice and instead of just converting to an integer,
1108 // we'll physically remove each non-digit in the string.
1109 //
1110 else if ($option->type == SMOPT_TYPE_INTEGER) {
1111 $option->new_value = preg_replace('/[^0-9]/', '', $option->new_value);
1112 setPref($data_dir, $username, $option->name, $option->new_value);
1113 }
1114
1115 else
1116 setPref($data_dir, $username, $option->name, $option->new_value);
1117
1118
1119 // if a checkbox or multi select is zeroed/cleared out, it
1120 // needs to have an empty value pushed into its "new_value" slot
1121 //
1122 if (($option->type == SMOPT_TYPE_STRLIST_MULTI
1123 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
1124 && is_null($option->new_value))
1125 $option->new_value = '';
1126
1127 }
1128
1129 /**
1130 * save function that does not save
1131 * @param object $option
1132 */
1133 function save_option_noop($option) {
1134 /* Do nothing here... */
1135 }
1136
1137 /**
1138 * Create hidden 'optpage' input field with value set by argument
1139 * @param string $optpage identification of option page
1140 * @return string html formated hidden input field
1141 */
1142 function create_optpage_element($optpage) {
1143 return addHidden('optpage', $optpage);
1144 }
1145
1146 /**
1147 * Create hidden 'optmode' input field with value set by argument
1148 * @param string $optmode
1149 * @return string html formated hidden input field
1150 */
1151 function create_optmode_element($optmode) {
1152 return addHidden('optmode', $optmode);
1153 }
1154
1155 /**
1156 * @param array $optgrps
1157 * @param array $optvals
1158 * @return array
1159 */
1160 function create_option_groups($optgrps, $optvals) {
1161 /* Build a simple array with which to start. */
1162 $result = array();
1163
1164 /* Create option group for each option group name. */
1165 foreach ($optgrps as $grpkey => $grpname) {
1166 $result[$grpkey] = array();
1167 $result[$grpkey]['name'] = $grpname;
1168 $result[$grpkey]['options'] = array();
1169 }
1170
1171 /* Create a new SquirrelOption for each set of option values. */
1172 foreach ($optvals as $grpkey => $grpopts) {
1173 foreach ($grpopts as $optset) {
1174 /* Create a new option with all values given. */
1175 $next_option = new SquirrelOption(
1176 $optset,
1177 $optset['name'],
1178 $optset['caption'],
1179 $optset['type'],
1180 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
1181 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
1182 (isset($optset['posvals']) ? $optset['posvals'] : ''),
1183 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
1184 );
1185
1186 /* If provided, set if the caption is allowed to wrap for this option. */
1187 if (isset($optset['caption_wrap'])) {
1188 $next_option->setCaptionWrap($optset['caption_wrap']);
1189 }
1190
1191 /* If provided, set the size for this option. */
1192 if (isset($optset['size'])) {
1193 $next_option->setSize($optset['size']);
1194 }
1195
1196 /* If provided, set the trailing_text for this option. */
1197 if (isset($optset['trailing_text'])) {
1198 $next_option->setTrailingText($optset['trailing_text']);
1199 }
1200
1201 /* If provided, set the trailing_text_small for this option. */
1202 if (isset($optset['trailing_text_small'])) {
1203 $next_option->setTrailingTextSmall($optset['trailing_text_small']);
1204 }
1205
1206 /* If provided, set the trailing_text_is_html for this option. */
1207 if (isset($optset['trailing_text_is_html'])) {
1208 $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']);
1209 }
1210
1211 /* If provided, set the yes_text for this option. */
1212 if (isset($optset['yes_text'])) {
1213 $next_option->setYesText($optset['yes_text']);
1214 }
1215
1216 /* If provided, set the no_text for this option. */
1217 if (isset($optset['no_text'])) {
1218 $next_option->setNoText($optset['no_text']);
1219 }
1220
1221 /* If provided, set the poss_value_folders value for this option. */
1222 if (isset($optset['poss_value_folders'])) {
1223 $next_option->setPossValueFolders($optset['poss_value_folders']);
1224 }
1225
1226 /* If provided, set the layout type for this option. */
1227 if (isset($optset['layout_type'])) {
1228 $next_option->setLayoutType($optset['layout_type']);
1229 }
1230
1231 /* If provided, set the use_add_widget value for this option. */
1232 if (isset($optset['use_add_widget'])) {
1233 $next_option->setUseAddWidget($optset['use_add_widget']);
1234 }
1235
1236 /* If provided, set the use_delete_widget value for this option. */
1237 if (isset($optset['use_delete_widget'])) {
1238 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1239 }
1240
1241 /* If provided, set the comment for this option. */
1242 if (isset($optset['comment'])) {
1243 $next_option->setComment($optset['comment']);
1244 }
1245
1246 /* If provided, set the save function for this option. */
1247 if (isset($optset['save'])) {
1248 $next_option->setSaveFunction($optset['save']);
1249 }
1250
1251 /* If provided, set the extra attributes for this option. */
1252 if (isset($optset['extra_attributes'])) {
1253 $next_option->setExtraAttributes($optset['extra_attributes']);
1254 }
1255
1256 /* If provided, set the "post script" for this option. */
1257 if (isset($optset['post_script'])) {
1258 $next_option->setPostScript($optset['post_script']);
1259 }
1260
1261 /* If provided, set the folder_filter for this option. */
1262 if (isset($optset['folder_filter'])) {
1263 $next_option->setFolderFilter($optset['folder_filter']);
1264 }
1265
1266 /* Add this option to the option array. */
1267 $result[$grpkey]['options'][] = $next_option;
1268 }
1269 }
1270
1271 /* Return our resulting array. */
1272 return ($result);
1273 }
1274