6 * Functions needed to display the options pages.
8 * @copyright 1999-2017 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
16 * SquirrelOption: An option for SquirrelMail.
18 * @package squirrelmail
21 class SquirrelOption
{
23 * The original option configuration array
26 var $raw_option_array;
28 * The name of this setting
33 * The text that prefaces setting on the preferences page
38 * Whether or not the caption text is allowed to wrap
43 * The type of INPUT element
45 * See SMOPT_TYPE_* defines
50 * Indicates if a link should be shown to refresh part
51 * or all of the window
53 * See SMOPT_REFRESH_* defines
58 * Specifies the size of certain input items
60 * See SMOPT_SIZE_* defines
65 * Text that follows a text input or
66 * select list input on the preferences page
68 * useful for indicating units, meanings of special values, etc.
73 * Indicates that the widget's "trailing text"
74 * should be displayed in a smaller sized font
78 var $trailing_text_small;
80 * Text that overrides the "Yes" label for boolean
81 * radio option widgets
87 * Text that overrides the "No" label for boolean
88 * radio option widgets
94 * Some widgets support more than one layout type
100 * Indicates if the Add widget should be included
107 * Indicates if the Delete widget should be included
112 var $use_delete_widget;
114 * associative array, treated the same as $possible_values
115 * (see its documentation below), but usually expected to
116 * have its first value contain a list of IMAP folders, an
117 * array itself in the format as passed back by
118 * sqimap_mailbox_list(). Used to display folder selector
119 * for possible values of an associative edit list option
125 var $poss_value_folders;
127 * text displayed to the user
129 * Used with SMOPT_TYPE_COMMENT options
134 * additional javascript or other widget attributes added to the
135 * user input; must be an array where keys are attribute names
136 * ("onclick", etc) and values are the attribute values.
141 * script (usually Javascript) that will be placed after (outside of)
148 * The name of the Save Function for this option.
153 /* The various 'values' for this options. */
155 * default/preselected value for this option
165 * associative array, where each key is an actual input value
166 * and the corresponding value is what is displayed to the user
167 * for that list item in the drop-down list
170 var $possible_values;
172 * disables html sanitizing.
174 * WARNING - don't use it, if user input is possible in option
175 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
178 var $htmlencoded=false;
180 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
181 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
182 * embedded folder lists provided for inputting values for
183 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
185 * See $flag argument in sqimap_mailbox_option_list() function.
189 var $folder_filter='noselect';
192 * Constructor (PHP5 style, required in some future version of PHP)
193 * @param array $raw_option_array
194 * @param string $name
195 * @param string $caption
196 * @param integer $type
197 * @param integer $refresh_level
198 * @param mixed $initial_value
199 * @param array $possible_values
200 * @param bool $htmlencoded
203 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
204 /* Set the basic stuff. */
205 $this->raw_option_array
= $raw_option_array;
207 $this->caption
= $caption;
208 $this->caption_wrap
= TRUE;
210 $this->refresh_level
= $refresh_level;
211 $this->possible_values
= $possible_values;
212 $this->htmlencoded
= $htmlencoded;
213 $this->size
= SMOPT_SIZE_NORMAL
;
214 $this->trailing_text
= '';
215 $this->trailing_text_small
= FALSE;
216 $this->yes_text
= '';
219 $this->layout_type
= 0;
220 $this->use_add_widget
= TRUE;
221 $this->use_delete_widget
= TRUE;
222 $this->poss_value_folders
= '';
223 $this->aExtraAttribs
= array();
224 $this->post_script
= '';
226 //Check for a current value.
227 if (isset($GLOBALS[$name])) {
228 $this->value
= $GLOBALS[$name];
229 } else if (!empty($initial_value)) {
230 $this->value
= $initial_value;
235 /* Check for a new value. */
236 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
237 $this->new_value
= NULL;
240 /* Set the default save function. */
241 if ($type != SMOPT_TYPE_HIDDEN
242 && $type != SMOPT_TYPE_INFO
243 && $type != SMOPT_TYPE_COMMENT
) {
244 $this->save_function
= SMOPT_SAVE_DEFAULT
;
246 $this->save_function
= SMOPT_SAVE_NOOP
;
251 * Constructor (PHP4 style, kept for compatibility reasons)
252 * @param array $raw_option_array
253 * @param string $name
254 * @param string $caption
255 * @param integer $type
256 * @param integer $refresh_level
257 * @param mixed $initial_value
258 * @param array $possible_values
259 * @param bool $htmlencoded
261 function SquirrelOption
262 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
263 self
::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
266 /** Convenience function that identifies which types of
267 widgets are stored as (serialized) array values. */
268 function is_multiple_valued() {
269 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
270 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
271 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
272 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
);
276 * Set the value for this option.
277 * @param mixed $value
279 function setValue($value) {
280 $this->value
= $value;
284 * Set the new value for this option.
285 * @param mixed $new_value
287 function setNewValue($new_value) {
288 $this->new_value
= $new_value;
292 * Set whether the caption is allowed to wrap for this option.
293 * @param boolean $caption_wrap
295 function setCaptionWrap($caption_wrap) {
296 $this->caption_wrap
= $caption_wrap;
300 * Set the size for this option.
301 * @param integer $size
303 function setSize($size) {
308 * Set the trailing_text for this option.
309 * @param string $trailing_text
311 function setTrailingText($trailing_text) {
312 $this->trailing_text
= $trailing_text;
316 * Set the trailing_text for this option.
317 * @param string $trailing_text
319 function setTrailingTextSmall($trailing_text_small) {
320 $this->trailing_text_small
= $trailing_text_small;
324 * Set the yes_text for this option.
325 * @param string $yes_text
327 function setYesText($yes_text) {
328 $this->yes_text
= $yes_text;
332 * Set the no_text for this option.
333 * @param string $no_text
335 function setNoText($no_text) {
336 $this->no_text
= $no_text;
339 /* Set the "use add widget" value for this option. */
340 function setUseAddWidget($use_add_widget) {
341 $this->use_add_widget
= $use_add_widget;
344 /* Set the "use delete widget" value for this option. */
345 function setUseDeleteWidget($use_delete_widget) {
346 $this->use_delete_widget
= $use_delete_widget;
349 /* Set the "poss value folders" value for this option.
350 See the associative edit list widget, which uses this
351 to offer folder list selection for the values */
352 function setPossValueFolders($poss_value_folders) {
353 $this->poss_value_folders
= $poss_value_folders;
357 * Set the layout type for this option.
358 * @param int $layout_type
360 function setLayoutType($layout_type) {
361 $this->layout_type
= $layout_type;
365 * Set the comment for this option.
366 * @param string $comment
368 function setComment($comment) {
369 $this->comment
= $comment;
373 * Set the extra attributes for this option.
374 * @param array $aExtraAttribs
376 function setExtraAttributes($aExtraAttribs) {
377 $this->aExtraAttribs
= $aExtraAttribs;
381 * Set the "post script" for this option.
382 * @param string $post_script
384 function setPostScript($post_script) {
385 $this->post_script
= $post_script;
389 * Set the save function for this option.
390 * @param string $save_function
392 function setSaveFunction($save_function) {
393 $this->save_function
= $save_function;
397 * Set the folder_filter for this option.
398 * @param string $folder_filter
401 function setFolderFilter($folder_filter) {
402 $this->folder_filter
= $folder_filter;
406 * Creates fields on option pages according to option type
408 * This is the function that calls all other createWidget* functions.
410 * @return string The formated option field
413 function createWidget() {
416 // Use new value if available
417 if (!is_null($this->new_value
)) {
418 $tempValue = $this->value
;
419 $this->value
= $this->new_value
;
422 /* Get the widget for this option type. */
423 switch ($this->type
) {
424 case SMOPT_TYPE_PASSWORD
:
425 $result = $this->createWidget_String(TRUE);
427 case SMOPT_TYPE_STRING
:
428 $result = $this->createWidget_String();
430 case SMOPT_TYPE_STRLIST
:
431 $result = $this->createWidget_StrList();
433 case SMOPT_TYPE_TEXTAREA
:
434 $result = $this->createWidget_TextArea();
436 case SMOPT_TYPE_INTEGER
:
437 $result = $this->createWidget_Integer();
439 case SMOPT_TYPE_FLOAT
:
440 $result = $this->createWidget_Float();
442 case SMOPT_TYPE_BOOLEAN
:
443 $result = $this->createWidget_Boolean();
445 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
446 $result = $this->createWidget_Boolean(TRUE);
448 case SMOPT_TYPE_BOOLEAN_RADIO
:
449 $result = $this->createWidget_Boolean(FALSE);
451 case SMOPT_TYPE_HIDDEN
:
452 $result = $this->createWidget_Hidden();
454 case SMOPT_TYPE_COMMENT
:
455 $result = $this->createWidget_Comment();
457 case SMOPT_TYPE_FLDRLIST
:
458 $result = $this->createWidget_FolderList();
460 case SMOPT_TYPE_FLDRLIST_MULTI
:
461 $result = $this->createWidget_FolderList(TRUE);
463 case SMOPT_TYPE_EDIT_LIST
:
464 $result = $this->createWidget_EditList();
466 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
:
467 $result = $this->createWidget_EditListAssociative();
469 case SMOPT_TYPE_STRLIST_MULTI
:
470 $result = $this->createWidget_StrList(TRUE);
472 case SMOPT_TYPE_STRLIST_RADIO
:
473 $result = $this->createWidget_StrList(FALSE, TRUE);
475 case SMOPT_TYPE_SUBMIT
:
476 $result = $this->createWidget_Submit();
478 case SMOPT_TYPE_INFO
:
479 $result = $this->createWidget_Info();
483 sprintf(_("Option Type '%s' Not Found"), $this->type
)
487 /* Add the "post script" for this option. */
488 $result .= $this->post_script
;
490 // put correct value back if need be
491 if (!is_null($this->new_value
)) {
492 $this->value
= $tempValue;
495 /* Now, return the created widget. */
501 * @return string html formated output
503 function createWidget_Info() {
504 // return sq_htmlspecialchars($this->value);
505 // like COMMENT, allow HTML here
510 * Create string field
512 * @param boolean $password When TRUE, the text in the input
513 * widget will be obscured (OPTIONAL;
516 * @return string html formated option field
519 function createWidget_String($password=FALSE) {
520 switch ($this->size
) {
521 case SMOPT_SIZE_TINY
:
524 case SMOPT_SIZE_SMALL
:
527 case SMOPT_SIZE_LARGE
:
530 case SMOPT_SIZE_HUGE
:
533 case SMOPT_SIZE_NORMAL
:
538 //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!)
540 return addPwField('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . ($this->trailing_text_small ?
'<small>' : '') . sm_encode_html_special_chars($this->trailing_text
) . ($this->trailing_text_small ?
'</small>' : '');
542 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . ($this->trailing_text_small ?
'<small>' : '') . sm_encode_html_special_chars($this->trailing_text
) . ($this->trailing_text_small ?
'</small>' : '');
546 * Create selection box or radio button group
548 * When $this->htmlencoded is TRUE, the keys and values in
549 * $this->possible_values are assumed to be display-safe.
552 * Note that when building radio buttons instead of a select
553 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
554 * radio buttons will be output one after another without
555 * linebreaks between them. Otherwise, each radio button
556 * goes on a line of its own.
558 * @param boolean $multiple_select When TRUE, the select widget
559 * will allow multiple selections
560 * (OPTIONAL; default is FALSE
561 * (single select list))
562 * @param boolean $radio_buttons When TRUE, the widget will
563 * instead be built as a group
564 * of radio buttons (and
565 * $multiple_select will be
566 * forced to FALSE) (OPTIONAL;
567 * default is FALSE (select widget))
569 * @return string html formated selection box or radio buttons
572 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
573 //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
575 // radio buttons instead of select widget?
577 if ($radio_buttons) {
581 foreach ($this->possible_values
as $real_value => $disp_value) {
582 $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);
583 if ($this->size
!= SMOPT_SIZE_TINY
)
591 // everything below applies to select widgets
593 switch ($this->size
) {
594 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
595 case SMOPT_SIZE_TINY
:
598 case SMOPT_SIZE_SMALL
:
601 case SMOPT_SIZE_LARGE
:
604 case SMOPT_SIZE_HUGE
:
607 case SMOPT_SIZE_NORMAL
:
612 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . ($this->trailing_text_small ?
'<small>' : '') . sm_encode_html_special_chars($this->trailing_text
) . ($this->trailing_text_small ?
'</small>' : '');
617 * Create folder selection box
619 * @param boolean $multiple_select When TRUE, the select widget
620 * will allow multiple selections
621 * (OPTIONAL; default is FALSE
622 * (single select list))
624 * @return string html formated selection box
627 function createWidget_FolderList($multiple_select=FALSE) {
629 switch ($this->size
) {
630 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
631 case SMOPT_SIZE_TINY
:
634 case SMOPT_SIZE_SMALL
:
637 case SMOPT_SIZE_LARGE
:
640 case SMOPT_SIZE_HUGE
:
643 case SMOPT_SIZE_NORMAL
:
648 // possible values might include a nested array of
649 // possible values (list of folders)
651 $option_list = array();
652 foreach ($this->possible_values
as $value => $text) {
654 // list of folders (boxes array)
656 if (is_array($text)) {
657 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
659 // just one option here
662 $option_list = array_merge($option_list, array($value => $text));
666 if (empty($option_list))
667 $option_list = array('ignore' => _("unavailable"));
670 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . ($this->trailing_text_small ?
'<small>' : '') . sm_encode_html_special_chars($this->trailing_text
) . ($this->trailing_text_small ?
'</small>' : '');
676 * @return string html formated textarea field
678 function createWidget_TextArea() {
679 switch ($this->size
) {
680 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
681 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
682 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
683 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
684 case SMOPT_SIZE_NORMAL
:
685 default: $rows = 5; $cols = 50;
687 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
691 * Creates field for integer
693 * Difference from createWidget_String is visible only when javascript is enabled
694 * @return string html formated option field
696 function createWidget_Integer() {
698 // add onChange javascript handler to a regular string widget
699 // which will strip out all non-numeric chars
700 if (checkForJavascript())
701 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
702 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
703 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
704 . 'this.value=newVal;';
706 return $this->createWidget_String();
710 * Creates field for floating number
711 * Difference from createWidget_String is visible only when javascript is enabled
712 * @return string html formated option field
714 function createWidget_Float() {
716 // add onChange javascript handler to a regular string widget
717 // which will strip out all non-numeric (period also OK) chars
718 if (checkForJavascript())
719 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
720 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
721 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
722 . 'newVal += origVal.charAt(i); } this.value=newVal;';
724 return $this->createWidget_String();
728 * Create boolean widget
730 * When creating Yes/No radio buttons, the "yes_text"
731 * and "no_text" option attributes are used to override
732 * the typical "Yes" and "No" text.
734 * @param boolean $checkbox When TRUE, the widget will be
735 * constructed as a checkbox,
736 * otherwise it will be a set of
737 * Yes/No radio buttons (OPTIONAL;
738 * default is TRUE (checkbox)).
740 * @return string html formated boolean widget
743 function createWidget_Boolean($checkbox=TRUE) {
745 global $oTemplate, $nbsp;
751 $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
);
758 /* Build the yes choice. */
759 $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');
761 /* Build the no choice. */
762 $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');
764 /* Build the combined "boolean widget". */
765 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
773 * Creates hidden field
774 * @return string html formated hidden input field
776 function createWidget_Hidden() {
777 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
782 * @return string comment
784 function createWidget_Comment() {
785 $result = $this->comment
;
790 * Creates a (non-associative) edit list
792 * Note that multiple layout types are supported for this widget.
793 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
796 * @return string html formated list of edit fields and
797 * their associated controls
799 function createWidget_EditList() {
803 switch ($this->size
) {
804 case SMOPT_SIZE_TINY
:
807 case SMOPT_SIZE_SMALL
:
810 case SMOPT_SIZE_MEDIUM
:
813 case SMOPT_SIZE_LARGE
:
816 case SMOPT_SIZE_HUGE
:
819 case SMOPT_SIZE_NORMAL
:
824 if (empty($this->possible_values
)) $this->possible_values
= array();
825 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
827 //FIXME: $this->aExtraAttribs probably should only be used in one place
828 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
829 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
830 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
832 $oTemplate->assign('trailing_text', $this->trailing_text
);
833 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
834 $oTemplate->assign('possible_values', $this->possible_values
);
835 $oTemplate->assign('current_value', $this->value
);
836 $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));
837 // NOTE: i=f-1; j=e-1 is in lieu of break 2
838 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
839 $oTemplate->assign('name', $this->name
);
841 switch ($this->layout_type
) {
842 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
843 return $oTemplate->fetch('edit_list_widget.tpl');
844 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
845 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
847 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type
));
853 * Creates an associative edit list
855 * Note that multiple layout types are supported for this widget.
856 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
859 * @return string html formated list of edit fields and
860 * their associated controls
862 function createWidget_EditListAssociative() {
866 switch ($this->size
) {
867 case SMOPT_SIZE_TINY
:
870 case SMOPT_SIZE_SMALL
:
873 case SMOPT_SIZE_MEDIUM
:
876 case SMOPT_SIZE_LARGE
:
879 case SMOPT_SIZE_HUGE
:
882 case SMOPT_SIZE_NORMAL
:
888 // ensure correct format of current value(s)
890 if (empty($this->possible_values
)) $this->possible_values
= array();
891 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
894 $oTemplate->assign('name', $this->name
);
895 $oTemplate->assign('current_value', $this->value
);
896 $oTemplate->assign('possible_values', $this->possible_values
);
897 $oTemplate->assign('poss_value_folders', $this->poss_value_folders
);
898 $oTemplate->assign('folder_filter', $this->folder_filter
);
900 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
901 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
903 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
905 //FIXME: $this->aExtraAttribs probably should only be used in one place
906 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name
. '_key', '', 22, 0, $this->aExtraAttribs
));
907 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name
. '_value', '', 12, 0, $this->aExtraAttribs
));
909 $oTemplate->assign('select_height', $height);
911 $oTemplate->assign('aAttribs', $this->aExtraAttribs
);
913 $oTemplate->assign('trailing_text', $this->trailing_text
);
914 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
916 switch ($this->layout_type
) {
917 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
918 return $oTemplate->fetch('edit_list_associative_widget.tpl');
919 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
920 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
922 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type
));
928 * Creates a submit button
930 * @return string html formated submit button widget
933 function createWidget_Submit() {
935 return addSubmit($this->comment
, $this->name
, $this->aExtraAttribs
) . ($this->trailing_text_small ?
'<small>' : '') . sm_encode_html_special_chars($this->trailing_text
) . ($this->trailing_text_small ?
'</small>' : '');
943 $function = $this->save_function
;
952 // edit lists have a lot going on, so we'll always process them
954 if ($this->type
== SMOPT_TYPE_EDIT_LIST
955 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
)
958 return ($this->value
!= $this->new_value
);
960 } /* End of SquirrelOption class*/
963 * Saves the option value (this is the default save function
964 * unless overridden by the user)
966 * @param object $option object that holds option name and new_value
968 function save_option($option) {
970 // Can't save the pref if we don't have the username
972 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
976 // if the widget is a selection list, make sure the new
977 // value is actually in the selection list and is not an
980 if ($option->type
== SMOPT_TYPE_STRLIST
981 && !array_key_exists($option->new_value
, $option->possible_values
))
985 // all other widgets except TEXTAREAs should never be allowed to have newlines
987 else if ($option->type
!= SMOPT_TYPE_TEXTAREA
)
988 $option->new_value
= str_replace(array("\r", "\n"), '', $option->new_value
);
993 // edit lists: first add new elements to list, then
994 // remove any selected ones (note that we must add
995 // before deleting because the javascript that populates
996 // the "add" textbox when selecting items in the list
999 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
1001 if (empty($option->possible_values
)) $option->possible_values
= array();
1002 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1004 // add element if given
1006 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1007 && sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
1008 $new_element = trim($new_element);
1009 if (!empty($new_element)
1010 && !in_array($new_element, $option->possible_values
))
1011 $option->possible_values
[] = $new_element;
1014 // delete selected elements if needed
1016 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1017 && is_array($option->new_value
)
1018 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
1019 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1021 // save full list (stored in "possible_values")
1023 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1025 // associative edit lists are handled similar to
1026 // non-associative ones
1028 } else if ($option->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
) {
1030 if (empty($option->possible_values
)) $option->possible_values
= array();
1031 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1033 // add element if given
1035 $new_element_key = '';
1036 $new_element_value = '';
1037 $retrieve_key = sqGetGlobalVar('add_' . $option->name
. '_key', $new_element_key, SQ_POST
);
1038 $retrieve_value = sqGetGlobalVar('add_' . $option->name
. '_value', $new_element_value, SQ_POST
);
1040 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1041 && ($retrieve_key ||
$retrieve_value)) {
1042 $new_element_key = trim($new_element_key);
1043 $new_element_value = trim($new_element_value);
1044 if ($option->poss_value_folders
&& empty($new_element_key))
1045 $new_element_value = '';
1046 if (!empty($new_element_key) ||
!empty($new_element_value)) {
1047 if (empty($new_element_key)) $new_element_key = '0';
1048 $option->possible_values
[$new_element_key] = $new_element_value;
1052 // delete selected elements if needed
1054 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1055 && is_array($option->new_value
)
1056 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
)) {
1058 if ($option->layout_type
== SMOPT_EDIT_LIST_LAYOUT_SELECT
) {
1059 foreach ($option->new_value
as $key)
1060 unset($option->possible_values
[urldecode($key)]);
1063 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1066 // save full list (stored in "possible_values")
1068 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1070 // Certain option types need to be serialized because
1071 // they are not scalar
1073 } else if ($option->is_multiple_valued())
1074 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
1076 // Checkboxes, when unchecked, don't submit anything in
1077 // the POST, so set to SMPREF_OFF if not found
1079 else if (($option->type
== SMOPT_TYPE_BOOLEAN
1080 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1081 && empty($option->new_value
))
1082 setPref($data_dir, $username, $option->name
, SMPREF_OFF
);
1084 // For integer fields, make sure we only have digits...
1085 // We'll be nice and instead of just converting to an integer,
1086 // we'll physically remove each non-digit in the string.
1088 else if ($option->type
== SMOPT_TYPE_INTEGER
) {
1089 $option->new_value
= preg_replace('/[^0-9]/', '', $option->new_value
);
1090 setPref($data_dir, $username, $option->name
, $option->new_value
);
1094 setPref($data_dir, $username, $option->name
, $option->new_value
);
1097 // if a checkbox or multi select is zeroed/cleared out, it
1098 // needs to have an empty value pushed into its "new_value" slot
1100 if (($option->type
== SMOPT_TYPE_STRLIST_MULTI
1101 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1102 && is_null($option->new_value
))
1103 $option->new_value
= '';
1108 * save function that does not save
1109 * @param object $option
1111 function save_option_noop($option) {
1112 /* Do nothing here... */
1116 * Create hidden 'optpage' input field with value set by argument
1117 * @param string $optpage identification of option page
1118 * @return string html formated hidden input field
1120 function create_optpage_element($optpage) {
1121 return addHidden('optpage', $optpage);
1125 * Create hidden 'optmode' input field with value set by argument
1126 * @param string $optmode
1127 * @return string html formated hidden input field
1129 function create_optmode_element($optmode) {
1130 return addHidden('optmode', $optmode);
1134 * @param array $optgrps
1135 * @param array $optvals
1138 function create_option_groups($optgrps, $optvals) {
1139 /* Build a simple array with which to start. */
1142 /* Create option group for each option group name. */
1143 foreach ($optgrps as $grpkey => $grpname) {
1144 $result[$grpkey] = array();
1145 $result[$grpkey]['name'] = $grpname;
1146 $result[$grpkey]['options'] = array();
1149 /* Create a new SquirrelOption for each set of option values. */
1150 foreach ($optvals as $grpkey => $grpopts) {
1151 foreach ($grpopts as $optset) {
1152 /* Create a new option with all values given. */
1153 $next_option = new SquirrelOption(
1158 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
1159 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
1160 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
1161 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
1164 /* If provided, set if the caption is allowed to wrap for this option. */
1165 if (isset($optset['caption_wrap'])) {
1166 $next_option->setCaptionWrap($optset['caption_wrap']);
1169 /* If provided, set the size for this option. */
1170 if (isset($optset['size'])) {
1171 $next_option->setSize($optset['size']);
1174 /* If provided, set the trailing_text for this option. */
1175 if (isset($optset['trailing_text'])) {
1176 $next_option->setTrailingText($optset['trailing_text']);
1179 /* If provided, set the trailing_text_small for this option. */
1180 if (isset($optset['trailing_text_small'])) {
1181 $next_option->setTrailingTextSmall($optset['trailing_text_small']);
1184 /* If provided, set the yes_text for this option. */
1185 if (isset($optset['yes_text'])) {
1186 $next_option->setYesText($optset['yes_text']);
1189 /* If provided, set the no_text for this option. */
1190 if (isset($optset['no_text'])) {
1191 $next_option->setNoText($optset['no_text']);
1194 /* If provided, set the poss_value_folders value for this option. */
1195 if (isset($optset['poss_value_folders'])) {
1196 $next_option->setPossValueFolders($optset['poss_value_folders']);
1199 /* If provided, set the layout type for this option. */
1200 if (isset($optset['layout_type'])) {
1201 $next_option->setLayoutType($optset['layout_type']);
1204 /* If provided, set the use_add_widget value for this option. */
1205 if (isset($optset['use_add_widget'])) {
1206 $next_option->setUseAddWidget($optset['use_add_widget']);
1209 /* If provided, set the use_delete_widget value for this option. */
1210 if (isset($optset['use_delete_widget'])) {
1211 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1214 /* If provided, set the comment for this option. */
1215 if (isset($optset['comment'])) {
1216 $next_option->setComment($optset['comment']);
1219 /* If provided, set the save function for this option. */
1220 if (isset($optset['save'])) {
1221 $next_option->setSaveFunction($optset['save']);
1224 /* If provided, set the extra attributes for this option. */
1225 if (isset($optset['extra_attributes'])) {
1226 $next_option->setExtraAttributes($optset['extra_attributes']);
1229 /* If provided, set the "post script" for this option. */
1230 if (isset($optset['post_script'])) {
1231 $next_option->setPostScript($optset['post_script']);
1234 /* If provided, set the folder_filter for this option. */
1235 if (isset($optset['folder_filter'])) {
1236 $next_option->setFolderFilter($optset['folder_filter']);
1239 /* Add this option to the option array. */
1240 $result[$grpkey]['options'][] = $next_option;
1244 /* Return our resulting array. */