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.
71 //TODO: add trailing_text_small as has been done with 1.4.x
74 * Text that overrides the "Yes" label for boolean
75 * radio option widgets
81 * Text that overrides the "No" label for boolean
82 * radio option widgets
88 * Some widgets support more than one layout type
94 * Indicates if the Add widget should be included
101 * Indicates if the Delete widget should be included
106 var $use_delete_widget;
108 * associative array, treated the same as $possible_values
109 * (see its documentation below), but usually expected to
110 * have its first value contain a list of IMAP folders, an
111 * array itself in the format as passed back by
112 * sqimap_mailbox_list(). Used to display folder selector
113 * for possible values of an associative edit list option
119 var $poss_value_folders;
121 * text displayed to the user
123 * Used with SMOPT_TYPE_COMMENT options
128 * additional javascript or other widget attributes added to the
129 * user input; must be an array where keys are attribute names
130 * ("onclick", etc) and values are the attribute values.
135 * script (usually Javascript) that will be placed after (outside of)
142 * The name of the Save Function for this option.
147 /* The various 'values' for this options. */
149 * default/preselected value for this option
159 * associative array, where each key is an actual input value
160 * and the corresponding value is what is displayed to the user
161 * for that list item in the drop-down list
164 var $possible_values;
166 * disables html sanitizing.
168 * WARNING - don't use it, if user input is possible in option
169 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
172 var $htmlencoded=false;
174 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
175 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
176 * embedded folder lists provided for inputting values for
177 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
179 * See $flag argument in sqimap_mailbox_option_list() function.
183 var $folder_filter='noselect';
186 * Constructor (PHP5 style, required in some future version of PHP)
187 * @param array $raw_option_array
188 * @param string $name
189 * @param string $caption
190 * @param integer $type
191 * @param integer $refresh_level
192 * @param mixed $initial_value
193 * @param array $possible_values
194 * @param bool $htmlencoded
197 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
198 /* Set the basic stuff. */
199 $this->raw_option_array
= $raw_option_array;
201 $this->caption
= $caption;
202 $this->caption_wrap
= TRUE;
204 $this->refresh_level
= $refresh_level;
205 $this->possible_values
= $possible_values;
206 $this->htmlencoded
= $htmlencoded;
207 $this->size
= SMOPT_SIZE_NORMAL
;
208 $this->trailing_text
= '';
209 $this->yes_text
= '';
212 $this->layout_type
= 0;
213 $this->use_add_widget
= TRUE;
214 $this->use_delete_widget
= TRUE;
215 $this->poss_value_folders
= '';
216 $this->aExtraAttribs
= array();
217 $this->post_script
= '';
219 //Check for a current value.
220 if (isset($GLOBALS[$name])) {
221 $this->value
= $GLOBALS[$name];
222 } else if (!empty($initial_value)) {
223 $this->value
= $initial_value;
228 /* Check for a new value. */
229 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
230 $this->new_value
= NULL;
233 /* Set the default save function. */
234 if ($type != SMOPT_TYPE_HIDDEN
235 && $type != SMOPT_TYPE_INFO
236 && $type != SMOPT_TYPE_COMMENT
) {
237 $this->save_function
= SMOPT_SAVE_DEFAULT
;
239 $this->save_function
= SMOPT_SAVE_NOOP
;
244 * Constructor (PHP4 style, kept for compatibility reasons)
245 * @param array $raw_option_array
246 * @param string $name
247 * @param string $caption
248 * @param integer $type
249 * @param integer $refresh_level
250 * @param mixed $initial_value
251 * @param array $possible_values
252 * @param bool $htmlencoded
254 function SquirrelOption
255 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
256 self
::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
259 /** Convenience function that identifies which types of
260 widgets are stored as (serialized) array values. */
261 function is_multiple_valued() {
262 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
263 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
264 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
265 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
);
269 * Set the value for this option.
270 * @param mixed $value
272 function setValue($value) {
273 $this->value
= $value;
277 * Set the new value for this option.
278 * @param mixed $new_value
280 function setNewValue($new_value) {
281 $this->new_value
= $new_value;
285 * Set whether the caption is allowed to wrap for this option.
286 * @param boolean $caption_wrap
288 function setCaptionWrap($caption_wrap) {
289 $this->caption_wrap
= $caption_wrap;
293 * Set the size for this option.
294 * @param integer $size
296 function setSize($size) {
301 * Set the trailing_text for this option.
302 * @param string $trailing_text
304 function setTrailingText($trailing_text) {
305 $this->trailing_text
= $trailing_text;
309 * Set the yes_text for this option.
310 * @param string $yes_text
312 function setYesText($yes_text) {
313 $this->yes_text
= $yes_text;
317 * Set the no_text for this option.
318 * @param string $no_text
320 function setNoText($no_text) {
321 $this->no_text
= $no_text;
324 /* Set the "use add widget" value for this option. */
325 function setUseAddWidget($use_add_widget) {
326 $this->use_add_widget
= $use_add_widget;
329 /* Set the "use delete widget" value for this option. */
330 function setUseDeleteWidget($use_delete_widget) {
331 $this->use_delete_widget
= $use_delete_widget;
334 /* Set the "poss value folders" value for this option.
335 See the associative edit list widget, which uses this
336 to offer folder list selection for the values */
337 function setPossValueFolders($poss_value_folders) {
338 $this->poss_value_folders
= $poss_value_folders;
342 * Set the layout type for this option.
343 * @param int $layout_type
345 function setLayoutType($layout_type) {
346 $this->layout_type
= $layout_type;
350 * Set the comment for this option.
351 * @param string $comment
353 function setComment($comment) {
354 $this->comment
= $comment;
358 * Set the extra attributes for this option.
359 * @param array $aExtraAttribs
361 function setExtraAttributes($aExtraAttribs) {
362 $this->aExtraAttribs
= $aExtraAttribs;
366 * Set the "post script" for this option.
367 * @param string $post_script
369 function setPostScript($post_script) {
370 $this->post_script
= $post_script;
374 * Set the save function for this option.
375 * @param string $save_function
377 function setSaveFunction($save_function) {
378 $this->save_function
= $save_function;
382 * Set the folder_filter for this option.
383 * @param string $folder_filter
386 function setFolderFilter($folder_filter) {
387 $this->folder_filter
= $folder_filter;
391 * Creates fields on option pages according to option type
393 * This is the function that calls all other createWidget* functions.
395 * @return string The formated option field
398 function createWidget() {
401 // Use new value if available
402 if (!is_null($this->new_value
)) {
403 $tempValue = $this->value
;
404 $this->value
= $this->new_value
;
407 /* Get the widget for this option type. */
408 switch ($this->type
) {
409 case SMOPT_TYPE_PASSWORD
:
410 $result = $this->createWidget_String(TRUE);
412 case SMOPT_TYPE_STRING
:
413 $result = $this->createWidget_String();
415 case SMOPT_TYPE_STRLIST
:
416 $result = $this->createWidget_StrList();
418 case SMOPT_TYPE_TEXTAREA
:
419 $result = $this->createWidget_TextArea();
421 case SMOPT_TYPE_INTEGER
:
422 $result = $this->createWidget_Integer();
424 case SMOPT_TYPE_FLOAT
:
425 $result = $this->createWidget_Float();
427 case SMOPT_TYPE_BOOLEAN
:
428 $result = $this->createWidget_Boolean();
430 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
431 $result = $this->createWidget_Boolean(TRUE);
433 case SMOPT_TYPE_BOOLEAN_RADIO
:
434 $result = $this->createWidget_Boolean(FALSE);
436 case SMOPT_TYPE_HIDDEN
:
437 $result = $this->createWidget_Hidden();
439 case SMOPT_TYPE_COMMENT
:
440 $result = $this->createWidget_Comment();
442 case SMOPT_TYPE_FLDRLIST
:
443 $result = $this->createWidget_FolderList();
445 case SMOPT_TYPE_FLDRLIST_MULTI
:
446 $result = $this->createWidget_FolderList(TRUE);
448 case SMOPT_TYPE_EDIT_LIST
:
449 $result = $this->createWidget_EditList();
451 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
:
452 $result = $this->createWidget_EditListAssociative();
454 case SMOPT_TYPE_STRLIST_MULTI
:
455 $result = $this->createWidget_StrList(TRUE);
457 case SMOPT_TYPE_STRLIST_RADIO
:
458 $result = $this->createWidget_StrList(FALSE, TRUE);
460 case SMOPT_TYPE_SUBMIT
:
461 $result = $this->createWidget_Submit();
463 case SMOPT_TYPE_INFO
:
464 $result = $this->createWidget_Info();
468 sprintf(_("Option Type '%s' Not Found"), $this->type
)
472 /* Add the "post script" for this option. */
473 $result .= $this->post_script
;
475 // put correct value back if need be
476 if (!is_null($this->new_value
)) {
477 $this->value
= $tempValue;
480 /* Now, return the created widget. */
486 * @return string html formated output
488 function createWidget_Info() {
489 // return sq_htmlspecialchars($this->value);
490 // like COMMENT, allow HTML here
495 * Create string field
497 * @param boolean $password When TRUE, the text in the input
498 * widget will be obscured (OPTIONAL;
501 * @return string html formated option field
504 function createWidget_String($password=FALSE) {
505 switch ($this->size
) {
506 case SMOPT_SIZE_TINY
:
509 case SMOPT_SIZE_SMALL
:
512 case SMOPT_SIZE_LARGE
:
515 case SMOPT_SIZE_HUGE
:
518 case SMOPT_SIZE_NORMAL
:
523 //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
525 return addPwField('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . sm_encode_html_special_chars($this->trailing_text
);
527 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . sm_encode_html_special_chars($this->trailing_text
);
531 * Create selection box or radio button group
533 * When $this->htmlencoded is TRUE, the keys and values in
534 * $this->possible_values are assumed to be display-safe.
537 * Note that when building radio buttons instead of a select
538 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
539 * radio buttons will be output one after another without
540 * linebreaks between them. Otherwise, each radio button
541 * goes on a line of its own.
543 * @param boolean $multiple_select When TRUE, the select widget
544 * will allow multiple selections
545 * (OPTIONAL; default is FALSE
546 * (single select list))
547 * @param boolean $radio_buttons When TRUE, the widget will
548 * instead be built as a group
549 * of radio buttons (and
550 * $multiple_select will be
551 * forced to FALSE) (OPTIONAL;
552 * default is FALSE (select widget))
554 * @return string html formated selection box or radio buttons
557 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
558 //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
560 // radio buttons instead of select widget?
562 if ($radio_buttons) {
566 foreach ($this->possible_values
as $real_value => $disp_value) {
567 $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);
568 if ($this->size
!= SMOPT_SIZE_TINY
)
576 // everything below applies to select widgets
578 switch ($this->size
) {
579 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
580 case SMOPT_SIZE_TINY
:
583 case SMOPT_SIZE_SMALL
:
586 case SMOPT_SIZE_LARGE
:
589 case SMOPT_SIZE_HUGE
:
592 case SMOPT_SIZE_NORMAL
:
597 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
);
602 * Create folder selection box
604 * @param boolean $multiple_select When TRUE, the select widget
605 * will allow multiple selections
606 * (OPTIONAL; default is FALSE
607 * (single select list))
609 * @return string html formated selection box
612 function createWidget_FolderList($multiple_select=FALSE) {
614 switch ($this->size
) {
615 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
616 case SMOPT_SIZE_TINY
:
619 case SMOPT_SIZE_SMALL
:
622 case SMOPT_SIZE_LARGE
:
625 case SMOPT_SIZE_HUGE
:
628 case SMOPT_SIZE_NORMAL
:
633 // possible values might include a nested array of
634 // possible values (list of folders)
636 $option_list = array();
637 foreach ($this->possible_values
as $value => $text) {
639 // list of folders (boxes array)
641 if (is_array($text)) {
642 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
644 // just one option here
647 $option_list = array_merge($option_list, array($value => $text));
651 if (empty($option_list))
652 $option_list = array('ignore' => _("unavailable"));
655 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . sm_encode_html_special_chars($this->trailing_text
);
661 * @return string html formated textarea field
663 function createWidget_TextArea() {
664 switch ($this->size
) {
665 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
666 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
667 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
668 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
669 case SMOPT_SIZE_NORMAL
:
670 default: $rows = 5; $cols = 50;
672 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
676 * Creates field for integer
678 * Difference from createWidget_String is visible only when javascript is enabled
679 * @return string html formated option field
681 function createWidget_Integer() {
683 // add onChange javascript handler to a regular string widget
684 // which will strip out all non-numeric chars
685 if (checkForJavascript())
686 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
687 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
688 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
689 . 'this.value=newVal;';
691 return $this->createWidget_String();
695 * Creates field for floating number
696 * Difference from createWidget_String is visible only when javascript is enabled
697 * @return string html formated option field
699 function createWidget_Float() {
701 // add onChange javascript handler to a regular string widget
702 // which will strip out all non-numeric (period also OK) chars
703 if (checkForJavascript())
704 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
705 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
706 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
707 . 'newVal += origVal.charAt(i); } this.value=newVal;';
709 return $this->createWidget_String();
713 * Create boolean widget
715 * When creating Yes/No radio buttons, the "yes_text"
716 * and "no_text" option attributes are used to override
717 * the typical "Yes" and "No" text.
719 * @param boolean $checkbox When TRUE, the widget will be
720 * constructed as a checkbox,
721 * otherwise it will be a set of
722 * Yes/No radio buttons (OPTIONAL;
723 * default is TRUE (checkbox)).
725 * @return string html formated boolean widget
728 function createWidget_Boolean($checkbox=TRUE) {
730 global $oTemplate, $nbsp;
736 $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
);
743 /* Build the yes choice. */
744 $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');
746 /* Build the no choice. */
747 $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');
749 /* Build the combined "boolean widget". */
750 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
758 * Creates hidden field
759 * @return string html formated hidden input field
761 function createWidget_Hidden() {
762 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
767 * @return string comment
769 function createWidget_Comment() {
770 $result = $this->comment
;
775 * Creates a (non-associative) edit list
777 * Note that multiple layout types are supported for this widget.
778 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
781 * @return string html formated list of edit fields and
782 * their associated controls
784 function createWidget_EditList() {
788 switch ($this->size
) {
789 case SMOPT_SIZE_TINY
:
792 case SMOPT_SIZE_SMALL
:
795 case SMOPT_SIZE_MEDIUM
:
798 case SMOPT_SIZE_LARGE
:
801 case SMOPT_SIZE_HUGE
:
804 case SMOPT_SIZE_NORMAL
:
809 if (empty($this->possible_values
)) $this->possible_values
= array();
810 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
812 //FIXME: $this->aExtraAttribs probably should only be used in one place
813 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
814 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
815 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
817 $oTemplate->assign('trailing_text', $this->trailing_text
);
818 $oTemplate->assign('possible_values', $this->possible_values
);
819 $oTemplate->assign('current_value', $this->value
);
820 $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));
821 // NOTE: i=f-1; j=e-1 is in lieu of break 2
822 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
823 $oTemplate->assign('name', $this->name
);
825 switch ($this->layout_type
) {
826 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
827 return $oTemplate->fetch('edit_list_widget.tpl');
828 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
829 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
831 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type
));
837 * Creates an associative edit list
839 * Note that multiple layout types are supported for this widget.
840 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
843 * @return string html formated list of edit fields and
844 * their associated controls
846 function createWidget_EditListAssociative() {
850 switch ($this->size
) {
851 case SMOPT_SIZE_TINY
:
854 case SMOPT_SIZE_SMALL
:
857 case SMOPT_SIZE_MEDIUM
:
860 case SMOPT_SIZE_LARGE
:
863 case SMOPT_SIZE_HUGE
:
866 case SMOPT_SIZE_NORMAL
:
872 // ensure correct format of current value(s)
874 if (empty($this->possible_values
)) $this->possible_values
= array();
875 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
878 $oTemplate->assign('name', $this->name
);
879 $oTemplate->assign('current_value', $this->value
);
880 $oTemplate->assign('possible_values', $this->possible_values
);
881 $oTemplate->assign('poss_value_folders', $this->poss_value_folders
);
882 $oTemplate->assign('folder_filter', $this->folder_filter
);
884 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
885 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
887 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
889 //FIXME: $this->aExtraAttribs probably should only be used in one place
890 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name
. '_key', '', 22, 0, $this->aExtraAttribs
));
891 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name
. '_value', '', 12, 0, $this->aExtraAttribs
));
893 $oTemplate->assign('select_height', $height);
895 $oTemplate->assign('aAttribs', $this->aExtraAttribs
);
897 $oTemplate->assign('trailing_text', $this->trailing_text
);
899 switch ($this->layout_type
) {
900 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
901 return $oTemplate->fetch('edit_list_associative_widget.tpl');
902 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
903 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
905 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type
));
911 * Creates a submit button
913 * @return string html formated submit button widget
916 function createWidget_Submit() {
918 return addSubmit($this->comment
, $this->name
, $this->aExtraAttribs
) . sm_encode_html_special_chars($this->trailing_text
);
926 $function = $this->save_function
;
935 // edit lists have a lot going on, so we'll always process them
937 if ($this->type
== SMOPT_TYPE_EDIT_LIST
938 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
)
941 return ($this->value
!= $this->new_value
);
943 } /* End of SquirrelOption class*/
946 * Saves the option value (this is the default save function
947 * unless overridden by the user)
949 * @param object $option object that holds option name and new_value
951 function save_option($option) {
953 // Can't save the pref if we don't have the username
955 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
959 // if the widget is a selection list, make sure the new
960 // value is actually in the selection list and is not an
963 if ($option->type
== SMOPT_TYPE_STRLIST
964 && !array_key_exists($option->new_value
, $option->possible_values
))
968 // all other widgets except TEXTAREAs should never be allowed to have newlines
970 else if ($option->type
!= SMOPT_TYPE_TEXTAREA
)
971 $option->new_value
= str_replace(array("\r", "\n"), '', $option->new_value
);
976 // edit lists: first add new elements to list, then
977 // remove any selected ones (note that we must add
978 // before deleting because the javascript that populates
979 // the "add" textbox when selecting items in the list
982 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
984 if (empty($option->possible_values
)) $option->possible_values
= array();
985 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
987 // add element if given
989 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
990 && sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
991 $new_element = trim($new_element);
992 if (!empty($new_element)
993 && !in_array($new_element, $option->possible_values
))
994 $option->possible_values
[] = $new_element;
997 // delete selected elements if needed
999 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1000 && is_array($option->new_value
)
1001 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
1002 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1004 // save full list (stored in "possible_values")
1006 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1008 // associative edit lists are handled similar to
1009 // non-associative ones
1011 } else if ($option->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
) {
1013 if (empty($option->possible_values
)) $option->possible_values
= array();
1014 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1016 // add element if given
1018 $new_element_key = '';
1019 $new_element_value = '';
1020 $retrieve_key = sqGetGlobalVar('add_' . $option->name
. '_key', $new_element_key, SQ_POST
);
1021 $retrieve_value = sqGetGlobalVar('add_' . $option->name
. '_value', $new_element_value, SQ_POST
);
1023 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1024 && ($retrieve_key ||
$retrieve_value)) {
1025 $new_element_key = trim($new_element_key);
1026 $new_element_value = trim($new_element_value);
1027 if ($option->poss_value_folders
&& empty($new_element_key))
1028 $new_element_value = '';
1029 if (!empty($new_element_key) ||
!empty($new_element_value)) {
1030 if (empty($new_element_key)) $new_element_key = '0';
1031 $option->possible_values
[$new_element_key] = $new_element_value;
1035 // delete selected elements if needed
1037 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1038 && is_array($option->new_value
)
1039 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
)) {
1041 if ($option->layout_type
== SMOPT_EDIT_LIST_LAYOUT_SELECT
) {
1042 foreach ($option->new_value
as $key)
1043 unset($option->possible_values
[urldecode($key)]);
1046 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1049 // save full list (stored in "possible_values")
1051 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1053 // Certain option types need to be serialized because
1054 // they are not scalar
1056 } else if ($option->is_multiple_valued())
1057 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
1059 // Checkboxes, when unchecked, don't submit anything in
1060 // the POST, so set to SMPREF_OFF if not found
1062 else if (($option->type
== SMOPT_TYPE_BOOLEAN
1063 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1064 && empty($option->new_value
))
1065 setPref($data_dir, $username, $option->name
, SMPREF_OFF
);
1067 // For integer fields, make sure we only have digits...
1068 // We'll be nice and instead of just converting to an integer,
1069 // we'll physically remove each non-digit in the string.
1071 else if ($option->type
== SMOPT_TYPE_INTEGER
) {
1072 $option->new_value
= preg_replace('/[^0-9]/', '', $option->new_value
);
1073 setPref($data_dir, $username, $option->name
, $option->new_value
);
1077 setPref($data_dir, $username, $option->name
, $option->new_value
);
1080 // if a checkbox or multi select is zeroed/cleared out, it
1081 // needs to have an empty value pushed into its "new_value" slot
1083 if (($option->type
== SMOPT_TYPE_STRLIST_MULTI
1084 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1085 && is_null($option->new_value
))
1086 $option->new_value
= '';
1091 * save function that does not save
1092 * @param object $option
1094 function save_option_noop($option) {
1095 /* Do nothing here... */
1099 * Create hidden 'optpage' input field with value set by argument
1100 * @param string $optpage identification of option page
1101 * @return string html formated hidden input field
1103 function create_optpage_element($optpage) {
1104 return addHidden('optpage', $optpage);
1108 * Create hidden 'optmode' input field with value set by argument
1109 * @param string $optmode
1110 * @return string html formated hidden input field
1112 function create_optmode_element($optmode) {
1113 return addHidden('optmode', $optmode);
1117 * @param array $optgrps
1118 * @param array $optvals
1121 function create_option_groups($optgrps, $optvals) {
1122 /* Build a simple array with which to start. */
1125 /* Create option group for each option group name. */
1126 foreach ($optgrps as $grpkey => $grpname) {
1127 $result[$grpkey] = array();
1128 $result[$grpkey]['name'] = $grpname;
1129 $result[$grpkey]['options'] = array();
1132 /* Create a new SquirrelOption for each set of option values. */
1133 foreach ($optvals as $grpkey => $grpopts) {
1134 foreach ($grpopts as $optset) {
1135 /* Create a new option with all values given. */
1136 $next_option = new SquirrelOption(
1141 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
1142 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
1143 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
1144 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
1147 /* If provided, set if the caption is allowed to wrap for this option. */
1148 if (isset($optset['caption_wrap'])) {
1149 $next_option->setCaptionWrap($optset['caption_wrap']);
1152 /* If provided, set the size for this option. */
1153 if (isset($optset['size'])) {
1154 $next_option->setSize($optset['size']);
1157 /* If provided, set the trailing_text for this option. */
1158 if (isset($optset['trailing_text'])) {
1159 $next_option->setTrailingText($optset['trailing_text']);
1162 /* If provided, set the yes_text for this option. */
1163 if (isset($optset['yes_text'])) {
1164 $next_option->setYesText($optset['yes_text']);
1167 /* If provided, set the no_text for this option. */
1168 if (isset($optset['no_text'])) {
1169 $next_option->setNoText($optset['no_text']);
1172 /* If provided, set the poss_value_folders value for this option. */
1173 if (isset($optset['poss_value_folders'])) {
1174 $next_option->setPossValueFolders($optset['poss_value_folders']);
1177 /* If provided, set the layout type for this option. */
1178 if (isset($optset['layout_type'])) {
1179 $next_option->setLayoutType($optset['layout_type']);
1182 /* If provided, set the use_add_widget value for this option. */
1183 if (isset($optset['use_add_widget'])) {
1184 $next_option->setUseAddWidget($optset['use_add_widget']);
1187 /* If provided, set the use_delete_widget value for this option. */
1188 if (isset($optset['use_delete_widget'])) {
1189 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1192 /* If provided, set the comment for this option. */
1193 if (isset($optset['comment'])) {
1194 $next_option->setComment($optset['comment']);
1197 /* If provided, set the save function for this option. */
1198 if (isset($optset['save'])) {
1199 $next_option->setSaveFunction($optset['save']);
1202 /* If provided, set the extra attributes for this option. */
1203 if (isset($optset['extra_attributes'])) {
1204 $next_option->setExtraAttributes($optset['extra_attributes']);
1207 /* If provided, set the "post script" for this option. */
1208 if (isset($optset['post_script'])) {
1209 $next_option->setPostScript($optset['post_script']);
1212 /* If provided, set the folder_filter for this option. */
1213 if (isset($optset['folder_filter'])) {
1214 $next_option->setFolderFilter($optset['folder_filter']);
1217 /* Add this option to the option array. */
1218 $result[$grpkey]['options'][] = $next_option;
1222 /* Return our resulting array. */