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 * Text that overrides the "Yes" label for boolean
74 * radio option widgets
80 * Text that overrides the "No" label for boolean
81 * radio option widgets
87 * Some widgets support more than one layout type
93 * Indicates if the Add widget should be included
100 * Indicates if the Delete widget should be included
105 var $use_delete_widget;
107 * associative array, treated the same as $possible_values
108 * (see its documentation below), but usually expected to
109 * have its first value contain a list of IMAP folders, an
110 * array itself in the format as passed back by
111 * sqimap_mailbox_list(). Used to display folder selector
112 * for possible values of an associative edit list option
118 var $poss_value_folders;
120 * text displayed to the user
122 * Used with SMOPT_TYPE_COMMENT options
127 * additional javascript or other widget attributes added to the
128 * user input; must be an array where keys are attribute names
129 * ("onclick", etc) and values are the attribute values.
134 * script (usually Javascript) that will be placed after (outside of)
141 * The name of the Save Function for this option.
146 /* The various 'values' for this options. */
148 * default/preselected value for this option
158 * associative array, where each key is an actual input value
159 * and the corresponding value is what is displayed to the user
160 * for that list item in the drop-down list
163 var $possible_values;
165 * disables html sanitizing.
167 * WARNING - don't use it, if user input is possible in option
168 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
171 var $htmlencoded=false;
173 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
174 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
175 * embedded folder lists provided for inputting values for
176 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
178 * See $flag argument in sqimap_mailbox_option_list() function.
182 var $folder_filter='noselect';
185 * Constructor (PHP5 style, required in some future version of PHP)
186 * @param array $raw_option_array
187 * @param string $name
188 * @param string $caption
189 * @param integer $type
190 * @param integer $refresh_level
191 * @param mixed $initial_value
192 * @param array $possible_values
193 * @param bool $htmlencoded
196 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
197 /* Set the basic stuff. */
198 $this->raw_option_array
= $raw_option_array;
200 $this->caption
= $caption;
201 $this->caption_wrap
= TRUE;
203 $this->refresh_level
= $refresh_level;
204 $this->possible_values
= $possible_values;
205 $this->htmlencoded
= $htmlencoded;
206 $this->size
= SMOPT_SIZE_NORMAL
;
207 $this->trailing_text
= '';
208 $this->yes_text
= '';
211 $this->layout_type
= 0;
212 $this->use_add_widget
= TRUE;
213 $this->use_delete_widget
= TRUE;
214 $this->poss_value_folders
= '';
215 $this->aExtraAttribs
= array();
216 $this->post_script
= '';
218 //Check for a current value.
219 if (isset($GLOBALS[$name])) {
220 $this->value
= $GLOBALS[$name];
221 } else if (!empty($initial_value)) {
222 $this->value
= $initial_value;
227 /* Check for a new value. */
228 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
229 $this->new_value
= NULL;
232 /* Set the default save function. */
233 if ($type != SMOPT_TYPE_HIDDEN
234 && $type != SMOPT_TYPE_INFO
235 && $type != SMOPT_TYPE_COMMENT
) {
236 $this->save_function
= SMOPT_SAVE_DEFAULT
;
238 $this->save_function
= SMOPT_SAVE_NOOP
;
243 * Constructor (PHP4 style, kept for compatibility reasons)
244 * @param array $raw_option_array
245 * @param string $name
246 * @param string $caption
247 * @param integer $type
248 * @param integer $refresh_level
249 * @param mixed $initial_value
250 * @param array $possible_values
251 * @param bool $htmlencoded
253 function SquirrelOption
254 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
255 self
::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
258 /** Convenience function that identifies which types of
259 widgets are stored as (serialized) array values. */
260 function is_multiple_valued() {
261 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
262 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
263 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
264 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
);
268 * Set the value for this option.
269 * @param mixed $value
271 function setValue($value) {
272 $this->value
= $value;
276 * Set the new value for this option.
277 * @param mixed $new_value
279 function setNewValue($new_value) {
280 $this->new_value
= $new_value;
284 * Set whether the caption is allowed to wrap for this option.
285 * @param boolean $caption_wrap
287 function setCaptionWrap($caption_wrap) {
288 $this->caption_wrap
= $caption_wrap;
292 * Set the size for this option.
293 * @param integer $size
295 function setSize($size) {
300 * Set the trailing_text for this option.
301 * @param string $trailing_text
303 function setTrailingText($trailing_text) {
304 $this->trailing_text
= $trailing_text;
308 * Set the yes_text for this option.
309 * @param string $yes_text
311 function setYesText($yes_text) {
312 $this->yes_text
= $yes_text;
316 * Set the no_text for this option.
317 * @param string $no_text
319 function setNoText($no_text) {
320 $this->no_text
= $no_text;
323 /* Set the "use add widget" value for this option. */
324 function setUseAddWidget($use_add_widget) {
325 $this->use_add_widget
= $use_add_widget;
328 /* Set the "use delete widget" value for this option. */
329 function setUseDeleteWidget($use_delete_widget) {
330 $this->use_delete_widget
= $use_delete_widget;
333 /* Set the "poss value folders" value for this option.
334 See the associative edit list widget, which uses this
335 to offer folder list selection for the values */
336 function setPossValueFolders($poss_value_folders) {
337 $this->poss_value_folders
= $poss_value_folders;
341 * Set the layout type for this option.
342 * @param int $layout_type
344 function setLayoutType($layout_type) {
345 $this->layout_type
= $layout_type;
349 * Set the comment for this option.
350 * @param string $comment
352 function setComment($comment) {
353 $this->comment
= $comment;
357 * Set the extra attributes for this option.
358 * @param array $aExtraAttribs
360 function setExtraAttributes($aExtraAttribs) {
361 $this->aExtraAttribs
= $aExtraAttribs;
365 * Set the "post script" for this option.
366 * @param string $post_script
368 function setPostScript($post_script) {
369 $this->post_script
= $post_script;
373 * Set the save function for this option.
374 * @param string $save_function
376 function setSaveFunction($save_function) {
377 $this->save_function
= $save_function;
381 * Set the folder_filter for this option.
382 * @param string $folder_filter
385 function setFolderFilter($folder_filter) {
386 $this->folder_filter
= $folder_filter;
390 * Creates fields on option pages according to option type
392 * This is the function that calls all other createWidget* functions.
394 * @return string The formated option field
397 function createWidget() {
400 // Use new value if available
401 if (!is_null($this->new_value
)) {
402 $tempValue = $this->value
;
403 $this->value
= $this->new_value
;
406 /* Get the widget for this option type. */
407 switch ($this->type
) {
408 case SMOPT_TYPE_PASSWORD
:
409 $result = $this->createWidget_String(TRUE);
411 case SMOPT_TYPE_STRING
:
412 $result = $this->createWidget_String();
414 case SMOPT_TYPE_STRLIST
:
415 $result = $this->createWidget_StrList();
417 case SMOPT_TYPE_TEXTAREA
:
418 $result = $this->createWidget_TextArea();
420 case SMOPT_TYPE_INTEGER
:
421 $result = $this->createWidget_Integer();
423 case SMOPT_TYPE_FLOAT
:
424 $result = $this->createWidget_Float();
426 case SMOPT_TYPE_BOOLEAN
:
427 $result = $this->createWidget_Boolean();
429 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
430 $result = $this->createWidget_Boolean(TRUE);
432 case SMOPT_TYPE_BOOLEAN_RADIO
:
433 $result = $this->createWidget_Boolean(FALSE);
435 case SMOPT_TYPE_HIDDEN
:
436 $result = $this->createWidget_Hidden();
438 case SMOPT_TYPE_COMMENT
:
439 $result = $this->createWidget_Comment();
441 case SMOPT_TYPE_FLDRLIST
:
442 $result = $this->createWidget_FolderList();
444 case SMOPT_TYPE_FLDRLIST_MULTI
:
445 $result = $this->createWidget_FolderList(TRUE);
447 case SMOPT_TYPE_EDIT_LIST
:
448 $result = $this->createWidget_EditList();
450 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
:
451 $result = $this->createWidget_EditListAssociative();
453 case SMOPT_TYPE_STRLIST_MULTI
:
454 $result = $this->createWidget_StrList(TRUE);
456 case SMOPT_TYPE_STRLIST_RADIO
:
457 $result = $this->createWidget_StrList(FALSE, TRUE);
459 case SMOPT_TYPE_SUBMIT
:
460 $result = $this->createWidget_Submit();
462 case SMOPT_TYPE_INFO
:
463 $result = $this->createWidget_Info();
467 sprintf(_("Option Type '%s' Not Found"), $this->type
)
471 /* Add the "post script" for this option. */
472 $result .= $this->post_script
;
474 // put correct value back if need be
475 if (!is_null($this->new_value
)) {
476 $this->value
= $tempValue;
479 /* Now, return the created widget. */
485 * @return string html formated output
487 function createWidget_Info() {
488 return sq_htmlspecialchars($this->value
);
492 * Create string field
494 * @param boolean $password When TRUE, the text in the input
495 * widget will be obscured (OPTIONAL;
498 * @return string html formated option field
501 function createWidget_String($password=FALSE) {
502 switch ($this->size
) {
503 case SMOPT_SIZE_TINY
:
506 case SMOPT_SIZE_SMALL
:
509 case SMOPT_SIZE_LARGE
:
512 case SMOPT_SIZE_HUGE
:
515 case SMOPT_SIZE_NORMAL
:
520 //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
522 return addPwField('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . sm_encode_html_special_chars($this->trailing_text
);
524 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . sm_encode_html_special_chars($this->trailing_text
);
528 * Create selection box or radio button group
530 * When $this->htmlencoded is TRUE, the keys and values in
531 * $this->possible_values are assumed to be display-safe.
534 * Note that when building radio buttons instead of a select
535 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
536 * radio buttons will be output one after another without
537 * linebreaks between them. Otherwise, each radio button
538 * goes on a line of its own.
540 * @param boolean $multiple_select When TRUE, the select widget
541 * will allow multiple selections
542 * (OPTIONAL; default is FALSE
543 * (single select list))
544 * @param boolean $radio_buttons When TRUE, the widget will
545 * instead be built as a group
546 * of radio buttons (and
547 * $multiple_select will be
548 * forced to FALSE) (OPTIONAL;
549 * default is FALSE (select widget))
551 * @return string html formated selection box or radio buttons
554 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
555 //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
557 // radio buttons instead of select widget?
559 if ($radio_buttons) {
563 foreach ($this->possible_values
as $real_value => $disp_value) {
564 $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);
565 if ($this->size
!= SMOPT_SIZE_TINY
)
573 // everything below applies to select widgets
575 switch ($this->size
) {
576 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
577 case SMOPT_SIZE_TINY
:
580 case SMOPT_SIZE_SMALL
:
583 case SMOPT_SIZE_LARGE
:
586 case SMOPT_SIZE_HUGE
:
589 case SMOPT_SIZE_NORMAL
:
594 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . sm_encode_html_special_chars($this->trailing_text
);
599 * Create folder selection box
601 * @param boolean $multiple_select When TRUE, the select widget
602 * will allow multiple selections
603 * (OPTIONAL; default is FALSE
604 * (single select list))
606 * @return string html formated selection box
609 function createWidget_FolderList($multiple_select=FALSE) {
611 switch ($this->size
) {
612 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
613 case SMOPT_SIZE_TINY
:
616 case SMOPT_SIZE_SMALL
:
619 case SMOPT_SIZE_LARGE
:
622 case SMOPT_SIZE_HUGE
:
625 case SMOPT_SIZE_NORMAL
:
630 // possible values might include a nested array of
631 // possible values (list of folders)
633 $option_list = array();
634 foreach ($this->possible_values
as $value => $text) {
636 // list of folders (boxes array)
638 if (is_array($text)) {
639 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
641 // just one option here
644 $option_list = array_merge($option_list, array($value => $text));
648 if (empty($option_list))
649 $option_list = array('ignore' => _("unavailable"));
652 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . sm_encode_html_special_chars($this->trailing_text
);
658 * @return string html formated textarea field
660 function createWidget_TextArea() {
661 switch ($this->size
) {
662 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
663 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
664 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
665 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
666 case SMOPT_SIZE_NORMAL
:
667 default: $rows = 5; $cols = 50;
669 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
673 * Creates field for integer
675 * Difference from createWidget_String is visible only when javascript is enabled
676 * @return string html formated option field
678 function createWidget_Integer() {
680 // add onChange javascript handler to a regular string widget
681 // which will strip out all non-numeric chars
682 if (checkForJavascript())
683 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
684 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
685 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
686 . 'this.value=newVal;';
688 return $this->createWidget_String();
692 * Creates field for floating number
693 * Difference from createWidget_String is visible only when javascript is enabled
694 * @return string html formated option field
696 function createWidget_Float() {
698 // add onChange javascript handler to a regular string widget
699 // which will strip out all non-numeric (period also OK) 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\') || origVal.charAt(i)==\'.\') '
704 . 'newVal += origVal.charAt(i); } this.value=newVal;';
706 return $this->createWidget_String();
710 * Create boolean widget
712 * When creating Yes/No radio buttons, the "yes_text"
713 * and "no_text" option attributes are used to override
714 * the typical "Yes" and "No" text.
716 * @param boolean $checkbox When TRUE, the widget will be
717 * constructed as a checkbox,
718 * otherwise it will be a set of
719 * Yes/No radio buttons (OPTIONAL;
720 * default is TRUE (checkbox)).
722 * @return string html formated boolean widget
725 function createWidget_Boolean($checkbox=TRUE) {
727 global $oTemplate, $nbsp;
733 $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
);
740 /* Build the yes choice. */
741 $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');
743 /* Build the no choice. */
744 $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');
746 /* Build the combined "boolean widget". */
747 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
755 * Creates hidden field
756 * @return string html formated hidden input field
758 function createWidget_Hidden() {
759 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
764 * @return string comment
766 function createWidget_Comment() {
767 $result = $this->comment
;
772 * Creates a (non-associative) edit list
774 * Note that multiple layout types are supported for this widget.
775 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
778 * @return string html formated list of edit fields and
779 * their associated controls
781 function createWidget_EditList() {
785 switch ($this->size
) {
786 case SMOPT_SIZE_TINY
:
789 case SMOPT_SIZE_SMALL
:
792 case SMOPT_SIZE_MEDIUM
:
795 case SMOPT_SIZE_LARGE
:
798 case SMOPT_SIZE_HUGE
:
801 case SMOPT_SIZE_NORMAL
:
806 if (empty($this->possible_values
)) $this->possible_values
= array();
807 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
809 //FIXME: $this->aExtraAttribs probably should only be used in one place
810 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
811 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
812 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
814 $oTemplate->assign('trailing_text', $this->trailing_text
);
815 $oTemplate->assign('possible_values', $this->possible_values
);
816 $oTemplate->assign('current_value', $this->value
);
817 $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));
818 // NOTE: i=f-1; j=e-1 is in lieu of break 2
819 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
820 $oTemplate->assign('name', $this->name
);
822 switch ($this->layout_type
) {
823 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
824 return $oTemplate->fetch('edit_list_widget.tpl');
825 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
826 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
828 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type
));
834 * Creates an associative edit list
836 * Note that multiple layout types are supported for this widget.
837 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
840 * @return string html formated list of edit fields and
841 * their associated controls
843 function createWidget_EditListAssociative() {
847 switch ($this->size
) {
848 case SMOPT_SIZE_TINY
:
851 case SMOPT_SIZE_SMALL
:
854 case SMOPT_SIZE_MEDIUM
:
857 case SMOPT_SIZE_LARGE
:
860 case SMOPT_SIZE_HUGE
:
863 case SMOPT_SIZE_NORMAL
:
869 // ensure correct format of current value(s)
871 if (empty($this->possible_values
)) $this->possible_values
= array();
872 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
875 $oTemplate->assign('name', $this->name
);
876 $oTemplate->assign('current_value', $this->value
);
877 $oTemplate->assign('possible_values', $this->possible_values
);
878 $oTemplate->assign('poss_value_folders', $this->poss_value_folders
);
879 $oTemplate->assign('folder_filter', $this->folder_filter
);
881 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
882 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
884 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
886 //FIXME: $this->aExtraAttribs probably should only be used in one place
887 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name
. '_key', '', 22, 0, $this->aExtraAttribs
));
888 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name
. '_value', '', 12, 0, $this->aExtraAttribs
));
890 $oTemplate->assign('select_height', $height);
892 $oTemplate->assign('aAttribs', $this->aExtraAttribs
);
894 $oTemplate->assign('trailing_text', $this->trailing_text
);
896 switch ($this->layout_type
) {
897 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
898 return $oTemplate->fetch('edit_list_associative_widget.tpl');
899 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
900 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
902 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type
));
908 * Creates a submit button
910 * @return string html formated submit button widget
913 function createWidget_Submit() {
915 return addSubmit($this->comment
, $this->name
, $this->aExtraAttribs
) . sm_encode_html_special_chars($this->trailing_text
);
923 $function = $this->save_function
;
932 // edit lists have a lot going on, so we'll always process them
934 if ($this->type
== SMOPT_TYPE_EDIT_LIST
935 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
)
938 return ($this->value
!= $this->new_value
);
940 } /* End of SquirrelOption class*/
943 * Saves the option value (this is the default save function
944 * unless overridden by the user)
946 * @param object $option object that holds option name and new_value
948 function save_option($option) {
950 // Can't save the pref if we don't have the username
952 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
956 // if the widget is a selection list, make sure the new
957 // value is actually in the selection list and is not an
960 if ($option->type
== SMOPT_TYPE_STRLIST
961 && !array_key_exists($option->new_value
, $option->possible_values
))
965 // all other widgets except TEXTAREAs should never be allowed to have newlines
967 else if ($option->type
!= SMOPT_TYPE_TEXTAREA
)
968 $option->new_value
= str_replace(array("\r", "\n"), '', $option->new_value
);
973 // edit lists: first add new elements to list, then
974 // remove any selected ones (note that we must add
975 // before deleting because the javascript that populates
976 // the "add" textbox when selecting items in the list
979 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
981 if (empty($option->possible_values
)) $option->possible_values
= array();
982 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
984 // add element if given
986 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
987 && sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
988 $new_element = trim($new_element);
989 if (!empty($new_element)
990 && !in_array($new_element, $option->possible_values
))
991 $option->possible_values
[] = $new_element;
994 // delete selected elements if needed
996 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
997 && is_array($option->new_value
)
998 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
999 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1001 // save full list (stored in "possible_values")
1003 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1005 // associative edit lists are handled similar to
1006 // non-associative ones
1008 } else if ($option->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
) {
1010 if (empty($option->possible_values
)) $option->possible_values
= array();
1011 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1013 // add element if given
1015 $new_element_key = '';
1016 $new_element_value = '';
1017 $retrieve_key = sqGetGlobalVar('add_' . $option->name
. '_key', $new_element_key, SQ_POST
);
1018 $retrieve_value = sqGetGlobalVar('add_' . $option->name
. '_value', $new_element_value, SQ_POST
);
1020 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1021 && ($retrieve_key ||
$retrieve_value)) {
1022 $new_element_key = trim($new_element_key);
1023 $new_element_value = trim($new_element_value);
1024 if ($option->poss_value_folders
&& empty($new_element_key))
1025 $new_element_value = '';
1026 if (!empty($new_element_key) ||
!empty($new_element_value)) {
1027 if (empty($new_element_key)) $new_element_key = '0';
1028 $option->possible_values
[$new_element_key] = $new_element_value;
1032 // delete selected elements if needed
1034 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1035 && is_array($option->new_value
)
1036 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
)) {
1038 if ($option->layout_type
== SMOPT_EDIT_LIST_LAYOUT_SELECT
) {
1039 foreach ($option->new_value
as $key)
1040 unset($option->possible_values
[urldecode($key)]);
1043 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1046 // save full list (stored in "possible_values")
1048 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1050 // Certain option types need to be serialized because
1051 // they are not scalar
1053 } else if ($option->is_multiple_valued())
1054 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
1056 // Checkboxes, when unchecked, don't submit anything in
1057 // the POST, so set to SMPREF_OFF if not found
1059 else if (($option->type
== SMOPT_TYPE_BOOLEAN
1060 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1061 && empty($option->new_value
))
1062 setPref($data_dir, $username, $option->name
, SMPREF_OFF
);
1064 // For integer fields, make sure we only have digits...
1065 // We'll be nice and instead of just converting to an integer,
1066 // we'll physically remove each non-digit in the string.
1068 else if ($option->type
== SMOPT_TYPE_INTEGER
) {
1069 $option->new_value
= preg_replace('/[^0-9]/', '', $option->new_value
);
1070 setPref($data_dir, $username, $option->name
, $option->new_value
);
1074 setPref($data_dir, $username, $option->name
, $option->new_value
);
1077 // if a checkbox or multi select is zeroed/cleared out, it
1078 // needs to have an empty value pushed into its "new_value" slot
1080 if (($option->type
== SMOPT_TYPE_STRLIST_MULTI
1081 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1082 && is_null($option->new_value
))
1083 $option->new_value
= '';
1088 * save function that does not save
1089 * @param object $option
1091 function save_option_noop($option) {
1092 /* Do nothing here... */
1096 * Create hidden 'optpage' input field with value set by argument
1097 * @param string $optpage identification of option page
1098 * @return string html formated hidden input field
1100 function create_optpage_element($optpage) {
1101 return addHidden('optpage', $optpage);
1105 * Create hidden 'optmode' input field with value set by argument
1106 * @param string $optmode
1107 * @return string html formated hidden input field
1109 function create_optmode_element($optmode) {
1110 return addHidden('optmode', $optmode);
1114 * @param array $optgrps
1115 * @param array $optvals
1118 function create_option_groups($optgrps, $optvals) {
1119 /* Build a simple array with which to start. */
1122 /* Create option group for each option group name. */
1123 foreach ($optgrps as $grpkey => $grpname) {
1124 $result[$grpkey] = array();
1125 $result[$grpkey]['name'] = $grpname;
1126 $result[$grpkey]['options'] = array();
1129 /* Create a new SquirrelOption for each set of option values. */
1130 foreach ($optvals as $grpkey => $grpopts) {
1131 foreach ($grpopts as $optset) {
1132 /* Create a new option with all values given. */
1133 $next_option = new SquirrelOption(
1138 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
1139 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
1140 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
1141 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
1144 /* If provided, set if the caption is allowed to wrap for this option. */
1145 if (isset($optset['caption_wrap'])) {
1146 $next_option->setCaptionWrap($optset['caption_wrap']);
1149 /* If provided, set the size for this option. */
1150 if (isset($optset['size'])) {
1151 $next_option->setSize($optset['size']);
1154 /* If provided, set the trailing_text for this option. */
1155 if (isset($optset['trailing_text'])) {
1156 $next_option->setTrailingText($optset['trailing_text']);
1159 /* If provided, set the yes_text for this option. */
1160 if (isset($optset['yes_text'])) {
1161 $next_option->setYesText($optset['yes_text']);
1164 /* If provided, set the no_text for this option. */
1165 if (isset($optset['no_text'])) {
1166 $next_option->setNoText($optset['no_text']);
1169 /* If provided, set the poss_value_folders value for this option. */
1170 if (isset($optset['poss_value_folders'])) {
1171 $next_option->setPossValueFolders($optset['poss_value_folders']);
1174 /* If provided, set the layout type for this option. */
1175 if (isset($optset['layout_type'])) {
1176 $next_option->setLayoutType($optset['layout_type']);
1179 /* If provided, set the use_add_widget value for this option. */
1180 if (isset($optset['use_add_widget'])) {
1181 $next_option->setUseAddWidget($optset['use_add_widget']);
1184 /* If provided, set the use_delete_widget value for this option. */
1185 if (isset($optset['use_delete_widget'])) {
1186 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1189 /* If provided, set the comment for this option. */
1190 if (isset($optset['comment'])) {
1191 $next_option->setComment($optset['comment']);
1194 /* If provided, set the save function for this option. */
1195 if (isset($optset['save'])) {
1196 $next_option->setSaveFunction($optset['save']);
1199 /* If provided, set the extra attributes for this option. */
1200 if (isset($optset['extra_attributes'])) {
1201 $next_option->setExtraAttributes($optset['extra_attributes']);
1204 /* If provided, set the "post script" for this option. */
1205 if (isset($optset['post_script'])) {
1206 $next_option->setPostScript($optset['post_script']);
1209 /* If provided, set the folder_filter for this option. */
1210 if (isset($optset['folder_filter'])) {
1211 $next_option->setFolderFilter($optset['folder_filter']);
1214 /* Add this option to the option array. */
1215 $result[$grpkey]['options'][] = $next_option;
1219 /* Return our resulting array. */