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 * Indicates that the widget's "trailing text"
81 * contains HTML and should not thus be
86 var $trailing_text_is_html;
88 * Text that overrides the "Yes" label for boolean
89 * radio option widgets
95 * Text that overrides the "No" label for boolean
96 * radio option widgets
102 * Some widgets support more than one layout type
108 * Indicates if the Add widget should be included
115 * Indicates if the Delete widget should be included
120 var $use_delete_widget;
122 * associative array, treated the same as $possible_values
123 * (see its documentation below), but usually expected to
124 * have its first value contain a list of IMAP folders, an
125 * array itself in the format as passed back by
126 * sqimap_mailbox_list(). Used to display folder selector
127 * for possible values of an associative edit list option
133 var $poss_value_folders;
135 * text displayed to the user
137 * Used with SMOPT_TYPE_COMMENT options
142 * additional javascript or other widget attributes added to the
143 * user input; must be an array where keys are attribute names
144 * ("onclick", etc) and values are the attribute values.
149 * script (usually Javascript) that will be placed after (outside of)
156 * The name of the Save Function for this option.
161 /* The various 'values' for this options. */
163 * default/preselected value for this option
173 * associative array, where each key is an actual input value
174 * and the corresponding value is what is displayed to the user
175 * for that list item in the drop-down list
178 var $possible_values;
180 * disables html sanitizing.
182 * WARNING - don't use it, if user input is possible in option
183 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
186 var $htmlencoded=false;
188 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
189 * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
190 * embedded folder lists provided for inputting values for
191 * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
193 * See $flag argument in sqimap_mailbox_option_list() function.
197 var $folder_filter='noselect';
200 * Constructor (PHP5 style, required in some future version of PHP)
201 * @param array $raw_option_array
202 * @param string $name
203 * @param string $caption
204 * @param integer $type
205 * @param integer $refresh_level
206 * @param mixed $initial_value
207 * @param array $possible_values
208 * @param bool $htmlencoded
211 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
212 /* Set the basic stuff. */
213 $this->raw_option_array
= $raw_option_array;
215 $this->caption
= $caption;
216 $this->caption_wrap
= TRUE;
218 $this->refresh_level
= $refresh_level;
219 $this->possible_values
= $possible_values;
220 $this->htmlencoded
= $htmlencoded;
221 $this->size
= SMOPT_SIZE_NORMAL
;
222 $this->trailing_text
= '';
223 $this->trailing_text_small
= FALSE;
224 $this->trailing_text_is_html
= FALSE;
225 $this->yes_text
= '';
228 $this->layout_type
= 0;
229 $this->use_add_widget
= TRUE;
230 $this->use_delete_widget
= TRUE;
231 $this->poss_value_folders
= '';
232 $this->aExtraAttribs
= array();
233 $this->post_script
= '';
235 //Check for a current value.
236 if (isset($GLOBALS[$name])) {
237 $this->value
= $GLOBALS[$name];
238 // TODO: This code should be something more like the following, but who knows what would break if it was changed at this point
239 // } else if (initial_value !== '') {
240 } else if (!empty($initial_value)) {
241 $this->value
= $initial_value;
246 /* Check for a new value. */
247 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
248 $this->new_value
= NULL;
251 /* Set the default save function. */
252 if ($type != SMOPT_TYPE_HIDDEN
253 && $type != SMOPT_TYPE_INFO
254 && $type != SMOPT_TYPE_COMMENT
) {
255 $this->save_function
= SMOPT_SAVE_DEFAULT
;
257 $this->save_function
= SMOPT_SAVE_NOOP
;
262 * Constructor (PHP4 style, kept for compatibility reasons)
263 * @param array $raw_option_array
264 * @param string $name
265 * @param string $caption
266 * @param integer $type
267 * @param integer $refresh_level
268 * @param mixed $initial_value
269 * @param array $possible_values
270 * @param bool $htmlencoded
272 function SquirrelOption
273 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
274 self
::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
277 /** Convenience function that identifies which types of
278 widgets are stored as (serialized) array values. */
279 function is_multiple_valued() {
280 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
281 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
282 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
283 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
);
287 * Set the value for this option.
288 * @param mixed $value
290 function setValue($value) {
291 $this->value
= $value;
295 * Set the new value for this option.
296 * @param mixed $new_value
298 function setNewValue($new_value) {
299 $this->new_value
= $new_value;
303 * Set whether the caption is allowed to wrap for this option.
304 * @param boolean $caption_wrap
306 function setCaptionWrap($caption_wrap) {
307 $this->caption_wrap
= $caption_wrap;
311 * Set the size for this option.
312 * @param integer $size
314 function setSize($size) {
319 * Set the trailing_text for this option.
320 * @param string $trailing_text
322 function setTrailingText($trailing_text) {
323 $this->trailing_text
= $trailing_text;
327 * Set the trailing_text_small for this option.
328 * @param boolean $trailing_text_small
330 function setTrailingTextSmall($trailing_text_small) {
331 $this->trailing_text_small
= $trailing_text_small;
335 * Set the trailing_text_is_html for this option.
336 * @param boolean $trailing_text_is_html
338 function setTrailingTextIsHtml($trailing_text_is_html) {
339 $this->trailing_text_is_html
= $trailing_text_is_html;
343 * Set the yes_text for this option.
344 * @param string $yes_text
346 function setYesText($yes_text) {
347 $this->yes_text
= $yes_text;
351 * Set the no_text for this option.
352 * @param string $no_text
354 function setNoText($no_text) {
355 $this->no_text
= $no_text;
358 /* Set the "use add widget" value for this option. */
359 function setUseAddWidget($use_add_widget) {
360 $this->use_add_widget
= $use_add_widget;
363 /* Set the "use delete widget" value for this option. */
364 function setUseDeleteWidget($use_delete_widget) {
365 $this->use_delete_widget
= $use_delete_widget;
368 /* Set the "poss value folders" value for this option.
369 See the associative edit list widget, which uses this
370 to offer folder list selection for the values */
371 function setPossValueFolders($poss_value_folders) {
372 $this->poss_value_folders
= $poss_value_folders;
376 * Set the layout type for this option.
377 * @param int $layout_type
379 function setLayoutType($layout_type) {
380 $this->layout_type
= $layout_type;
384 * Set the comment for this option.
385 * @param string $comment
387 function setComment($comment) {
388 $this->comment
= $comment;
392 * Set the extra attributes for this option.
393 * @param array $aExtraAttribs
395 function setExtraAttributes($aExtraAttribs) {
396 $this->aExtraAttribs
= $aExtraAttribs;
400 * Set the "post script" for this option.
401 * @param string $post_script
403 function setPostScript($post_script) {
404 $this->post_script
= $post_script;
408 * Set the save function for this option.
409 * @param string $save_function
411 function setSaveFunction($save_function) {
412 $this->save_function
= $save_function;
416 * Set the folder_filter for this option.
417 * @param string $folder_filter
420 function setFolderFilter($folder_filter) {
421 $this->folder_filter
= $folder_filter;
425 * Creates fields on option pages according to option type
427 * This is the function that calls all other createWidget* functions.
429 * @return string The formated option field
432 function createWidget() {
435 // Use new value if available
436 if (!is_null($this->new_value
)) {
437 $tempValue = $this->value
;
438 $this->value
= $this->new_value
;
441 /* Get the widget for this option type. */
442 switch ($this->type
) {
443 case SMOPT_TYPE_PASSWORD
:
444 $result = $this->createWidget_String(TRUE);
446 case SMOPT_TYPE_STRING
:
447 $result = $this->createWidget_String();
449 case SMOPT_TYPE_STRLIST
:
450 $result = $this->createWidget_StrList();
452 case SMOPT_TYPE_TEXTAREA
:
453 $result = $this->createWidget_TextArea();
455 case SMOPT_TYPE_INTEGER
:
456 $result = $this->createWidget_Integer();
458 case SMOPT_TYPE_FLOAT
:
459 $result = $this->createWidget_Float();
461 case SMOPT_TYPE_BOOLEAN
:
462 $result = $this->createWidget_Boolean();
464 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
465 $result = $this->createWidget_Boolean(TRUE);
467 case SMOPT_TYPE_BOOLEAN_RADIO
:
468 $result = $this->createWidget_Boolean(FALSE);
470 case SMOPT_TYPE_HIDDEN
:
471 $result = $this->createWidget_Hidden();
473 case SMOPT_TYPE_COMMENT
:
474 $result = $this->createWidget_Comment();
476 case SMOPT_TYPE_FLDRLIST
:
477 $result = $this->createWidget_FolderList();
479 case SMOPT_TYPE_FLDRLIST_MULTI
:
480 $result = $this->createWidget_FolderList(TRUE);
482 case SMOPT_TYPE_EDIT_LIST
:
483 $result = $this->createWidget_EditList();
485 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
:
486 $result = $this->createWidget_EditListAssociative();
488 case SMOPT_TYPE_STRLIST_MULTI
:
489 $result = $this->createWidget_StrList(TRUE);
491 case SMOPT_TYPE_STRLIST_RADIO
:
492 $result = $this->createWidget_StrList(FALSE, TRUE);
494 case SMOPT_TYPE_SUBMIT
:
495 $result = $this->createWidget_Submit();
497 case SMOPT_TYPE_INFO
:
498 $result = $this->createWidget_Info();
502 sprintf(_("Option Type '%s' Not Found"), $this->type
)
506 /* Add the "post script" for this option. */
507 $result .= $this->post_script
;
509 // put correct value back if need be
510 if (!is_null($this->new_value
)) {
511 $this->value
= $tempValue;
514 /* Now, return the created widget. */
520 * @return string html formated output
522 function createWidget_Info() {
523 // return sq_htmlspecialchars($this->value);
524 // like COMMENT, allow HTML here
529 * Create string field
531 * @param boolean $password When TRUE, the text in the input
532 * widget will be obscured (OPTIONAL;
535 * @return string html formated option field
538 function createWidget_String($password=FALSE) {
539 switch ($this->size
) {
540 case SMOPT_SIZE_TINY
:
543 case SMOPT_SIZE_SMALL
:
546 case SMOPT_SIZE_LARGE
:
549 case SMOPT_SIZE_HUGE
:
552 case SMOPT_SIZE_NORMAL
:
557 //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here (also, we have <small> tags here; don't want HTML here!)
559 return addPwField('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
561 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . ' ' . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
565 * Create selection box or radio button group
567 * When $this->htmlencoded is TRUE, the keys and values in
568 * $this->possible_values are assumed to be display-safe.
571 * Note that when building radio buttons instead of a select
572 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
573 * radio buttons will be output one after another without
574 * linebreaks between them. Otherwise, each radio button
575 * goes on a line of its own.
577 * @param boolean $multiple_select When TRUE, the select widget
578 * will allow multiple selections
579 * (OPTIONAL; default is FALSE
580 * (single select list))
581 * @param boolean $radio_buttons When TRUE, the widget will
582 * instead be built as a group
583 * of radio buttons (and
584 * $multiple_select will be
585 * forced to FALSE) (OPTIONAL;
586 * default is FALSE (select widget))
588 * @return string html formated selection box or radio buttons
591 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
592 //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
594 // radio buttons instead of select widget?
596 if ($radio_buttons) {
600 foreach ($this->possible_values
as $real_value => $disp_value) {
601 $result .= addRadioBox('new_' . $this->name
, ($this->value
== $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name
. '_' . $real_value), $this->aExtraAttribs
)) . $nbsp . create_label($disp_value, 'new_' . $this->name
. '_' . $real_value);
602 if ($this->size
!= SMOPT_SIZE_TINY
)
610 // everything below applies to select widgets
612 switch ($this->size
) {
613 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
614 case SMOPT_SIZE_TINY
:
617 case SMOPT_SIZE_SMALL
:
620 case SMOPT_SIZE_LARGE
:
623 case SMOPT_SIZE_HUGE
:
626 case SMOPT_SIZE_NORMAL
:
631 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height, !$this->htmlencoded
) . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
636 * Create folder selection box
638 * @param boolean $multiple_select When TRUE, the select widget
639 * will allow multiple selections
640 * (OPTIONAL; default is FALSE
641 * (single select list))
643 * @return string html formated selection box
646 function createWidget_FolderList($multiple_select=FALSE) {
648 switch ($this->size
) {
649 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
650 case SMOPT_SIZE_TINY
:
653 case SMOPT_SIZE_SMALL
:
656 case SMOPT_SIZE_LARGE
:
659 case SMOPT_SIZE_HUGE
:
662 case SMOPT_SIZE_NORMAL
:
667 // possible values might include a nested array of
668 // possible values (list of folders)
670 $option_list = array();
671 foreach ($this->possible_values
as $value => $text) {
673 // list of folders (boxes array)
675 if (is_array($text)) {
676 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
678 // just one option here
681 $option_list = array_merge($option_list, array($value => $text));
685 if (empty($option_list))
686 $option_list = array('ignore' => _("unavailable"));
689 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select, $height) . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
695 * @return string html formated textarea field
697 function createWidget_TextArea() {
698 switch ($this->size
) {
699 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
700 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
701 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
702 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
703 case SMOPT_SIZE_NORMAL
:
704 default: $rows = 5; $cols = 50;
706 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
710 * Creates field for integer
712 * Difference from createWidget_String is visible only when javascript is enabled
713 * @return string html formated option field
715 function createWidget_Integer() {
717 // add onChange javascript handler to a regular string widget
718 // which will strip out all non-numeric chars
719 if (checkForJavascript())
720 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
721 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
722 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
723 . 'this.value=newVal;';
725 return $this->createWidget_String();
729 * Creates field for floating number
730 * Difference from createWidget_String is visible only when javascript is enabled
731 * @return string html formated option field
733 function createWidget_Float() {
735 // add onChange javascript handler to a regular string widget
736 // which will strip out all non-numeric (period also OK) chars
737 if (checkForJavascript())
738 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
739 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
740 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
741 . 'newVal += origVal.charAt(i); } this.value=newVal;';
743 return $this->createWidget_String();
747 * Create boolean widget
749 * When creating Yes/No radio buttons, the "yes_text"
750 * and "no_text" option attributes are used to override
751 * the typical "Yes" and "No" text.
753 * @param boolean $checkbox When TRUE, the widget will be
754 * constructed as a checkbox,
755 * otherwise it will be a set of
756 * Yes/No radio buttons (OPTIONAL;
757 * default is TRUE (checkbox)).
759 * @return string html formated boolean widget
762 function createWidget_Boolean($checkbox=TRUE) {
764 global $oTemplate, $nbsp;
770 //TODO: Why isn't trailing_text being sanitized with sm_encode_special_chars()??? If this is a bug, add that, then add the option to display unsanitized if $this->trailing_text_is_html is enabled
771 $result = addCheckbox('new_' . $this->name
, ($this->value
!= SMPREF_NO
), SMPREF_YES
, array_merge(array('id' => 'new_' . $this->name
), $this->aExtraAttribs
)) . $nbsp . create_label(($this->trailing_text_small ?
'<small>' : '') . $this->trailing_text
. ($this->trailing_text_small ?
'</small>' : ''), 'new_' . $this->name
);
778 /* Build the yes choice. */
779 $yes_option = addRadioBox('new_' . $this->name
, ($this->value
!= SMPREF_NO
), SMPREF_YES
, array_merge(array('id' => 'new_' . $this->name
. '_yes'), $this->aExtraAttribs
)) . $nbsp . create_label((!empty($this->yes_text
) ?
$this->yes_text
: _("Yes")), 'new_' . $this->name
. '_yes');
781 /* Build the no choice. */
782 $no_option = addRadioBox('new_' . $this->name
, ($this->value
== SMPREF_NO
), SMPREF_NO
, array_merge(array('id' => 'new_' . $this->name
. '_no'), $this->aExtraAttribs
)) . $nbsp . create_label((!empty($this->no_text
) ?
$this->no_text
: _("No")), 'new_' . $this->name
. '_no');
784 /* Build the combined "boolean widget". */
785 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option " . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
793 * Creates hidden field
794 * @return string html formated hidden input field
796 function createWidget_Hidden() {
797 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
802 * @return string comment
804 function createWidget_Comment() {
805 $result = $this->comment
;
810 * Creates a (non-associative) edit list
812 * Note that multiple layout types are supported for this widget.
813 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
816 * @return string html formated list of edit fields and
817 * their associated controls
819 function createWidget_EditList() {
823 switch ($this->size
) {
824 case SMOPT_SIZE_TINY
:
827 case SMOPT_SIZE_SMALL
:
830 case SMOPT_SIZE_MEDIUM
:
833 case SMOPT_SIZE_LARGE
:
836 case SMOPT_SIZE_HUGE
:
839 case SMOPT_SIZE_NORMAL
:
844 if (empty($this->possible_values
)) $this->possible_values
= array();
845 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
847 //FIXME: $this->aExtraAttribs probably should only be used in one place
848 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
849 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
850 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
852 $oTemplate->assign('trailing_text', $this->trailing_text
);
853 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
854 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html
);
855 $oTemplate->assign('possible_values', $this->possible_values
);
856 $oTemplate->assign('current_value', $this->value
);
857 $oTemplate->assign('select_widget', addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, FALSE, !checkForJavascript() ?
$this->aExtraAttribs
: array_merge(array('onchange' => 'if (typeof(window.addinput_' . $this->name
. ') == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name
. '\' ) { pos = j; i=f-1; j=e-1; } j++; } i++; } if( pos >= 0 ) { window.addinput_' . $this->name
. ' = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput_' . $this->name
. '.value = this.options[x].text; break; } }'), $this->aExtraAttribs
), TRUE, $height));
858 // NOTE: i=f-1; j=e-1 is in lieu of break 2
859 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
860 $oTemplate->assign('name', $this->name
);
862 switch ($this->layout_type
) {
863 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
864 return $oTemplate->fetch('edit_list_widget.tpl');
865 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
866 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
868 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type
));
874 * Creates an associative edit list
876 * Note that multiple layout types are supported for this widget.
877 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
880 * @return string html formated list of edit fields and
881 * their associated controls
883 function createWidget_EditListAssociative() {
887 switch ($this->size
) {
888 case SMOPT_SIZE_TINY
:
891 case SMOPT_SIZE_SMALL
:
894 case SMOPT_SIZE_MEDIUM
:
897 case SMOPT_SIZE_LARGE
:
900 case SMOPT_SIZE_HUGE
:
903 case SMOPT_SIZE_NORMAL
:
909 // ensure correct format of current value(s)
911 if (empty($this->possible_values
)) $this->possible_values
= array();
912 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
915 $oTemplate->assign('name', $this->name
);
916 $oTemplate->assign('current_value', $this->value
);
917 $oTemplate->assign('possible_values', $this->possible_values
);
918 $oTemplate->assign('poss_value_folders', $this->poss_value_folders
);
919 $oTemplate->assign('folder_filter', $this->folder_filter
);
921 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
922 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
924 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
926 //FIXME: $this->aExtraAttribs probably should only be used in one place
927 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name
. '_key', '', 22, 0, $this->aExtraAttribs
));
928 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name
. '_value', '', 12, 0, $this->aExtraAttribs
));
930 $oTemplate->assign('select_height', $height);
932 $oTemplate->assign('aAttribs', $this->aExtraAttribs
);
934 $oTemplate->assign('trailing_text', $this->trailing_text
);
935 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
936 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html
);
938 switch ($this->layout_type
) {
939 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
940 return $oTemplate->fetch('edit_list_associative_widget.tpl');
941 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
942 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
944 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type
));
950 * Creates a submit button
952 * @return string html formated submit button widget
955 function createWidget_Submit() {
957 return addSubmit($this->comment
, $this->name
, $this->aExtraAttribs
) . ($this->trailing_text_small ?
'<small>' : '') . ($this->trailing_text_is_html ?
$this->trailing_text
: sm_encode_html_special_chars($this->trailing_text
)) . ($this->trailing_text_small ?
'</small>' : '');
965 $function = $this->save_function
;
974 // edit lists have a lot going on, so we'll always process them
976 if ($this->type
== SMOPT_TYPE_EDIT_LIST
977 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
)
980 return ($this->value
!= $this->new_value
);
982 } /* End of SquirrelOption class*/
985 * Saves the option value (this is the default save function
986 * unless overridden by the user)
988 * @param object $option object that holds option name and new_value
990 function save_option($option) {
992 // Can't save the pref if we don't have the username
994 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
998 // if the widget is a selection list, make sure the new
999 // value is actually in the selection list and is not an
1002 if ($option->type
== SMOPT_TYPE_STRLIST
1003 && !array_key_exists($option->new_value
, $option->possible_values
))
1007 // all other widgets except TEXTAREAs should never be allowed to have newlines
1009 else if ($option->type
!= SMOPT_TYPE_TEXTAREA
)
1010 $option->new_value
= str_replace(array("\r", "\n"), '', $option->new_value
);
1015 // edit lists: first add new elements to list, then
1016 // remove any selected ones (note that we must add
1017 // before deleting because the javascript that populates
1018 // the "add" textbox when selecting items in the list
1021 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
1023 if (empty($option->possible_values
)) $option->possible_values
= array();
1024 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1026 // add element if given
1028 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1029 && sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
1030 $new_element = trim($new_element);
1031 if (!empty($new_element)
1032 && !in_array($new_element, $option->possible_values
))
1033 $option->possible_values
[] = $new_element;
1036 // delete selected elements if needed
1038 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1039 && is_array($option->new_value
)
1040 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
1041 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1043 // save full list (stored in "possible_values")
1045 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1047 // associative edit lists are handled similar to
1048 // non-associative ones
1050 } else if ($option->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
) {
1052 if (empty($option->possible_values
)) $option->possible_values
= array();
1053 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1055 // add element if given
1057 $new_element_key = '';
1058 $new_element_value = '';
1059 $retrieve_key = sqGetGlobalVar('add_' . $option->name
. '_key', $new_element_key, SQ_POST
);
1060 $retrieve_value = sqGetGlobalVar('add_' . $option->name
. '_value', $new_element_value, SQ_POST
);
1062 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1063 && ($retrieve_key ||
$retrieve_value)) {
1064 $new_element_key = trim($new_element_key);
1065 $new_element_value = trim($new_element_value);
1066 if ($option->poss_value_folders
&& empty($new_element_key))
1067 $new_element_value = '';
1068 if (!empty($new_element_key) ||
!empty($new_element_value)) {
1069 if (empty($new_element_key)) $new_element_key = '0';
1070 $option->possible_values
[$new_element_key] = $new_element_value;
1074 // delete selected elements if needed
1076 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1077 && is_array($option->new_value
)
1078 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
)) {
1080 if ($option->layout_type
== SMOPT_EDIT_LIST_LAYOUT_SELECT
) {
1081 foreach ($option->new_value
as $key)
1082 unset($option->possible_values
[urldecode($key)]);
1085 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1088 // save full list (stored in "possible_values")
1090 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1092 // Certain option types need to be serialized because
1093 // they are not scalar
1095 } else if ($option->is_multiple_valued())
1096 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
1098 // Checkboxes, when unchecked, don't submit anything in
1099 // the POST, so set to SMPREF_OFF if not found
1101 else if (($option->type
== SMOPT_TYPE_BOOLEAN
1102 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1103 && empty($option->new_value
))
1104 setPref($data_dir, $username, $option->name
, SMPREF_OFF
);
1106 // For integer fields, make sure we only have digits...
1107 // We'll be nice and instead of just converting to an integer,
1108 // we'll physically remove each non-digit in the string.
1110 else if ($option->type
== SMOPT_TYPE_INTEGER
) {
1111 $option->new_value
= preg_replace('/[^0-9]/', '', $option->new_value
);
1112 setPref($data_dir, $username, $option->name
, $option->new_value
);
1116 setPref($data_dir, $username, $option->name
, $option->new_value
);
1119 // if a checkbox or multi select is zeroed/cleared out, it
1120 // needs to have an empty value pushed into its "new_value" slot
1122 if (($option->type
== SMOPT_TYPE_STRLIST_MULTI
1123 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1124 && is_null($option->new_value
))
1125 $option->new_value
= '';
1130 * save function that does not save
1131 * @param object $option
1133 function save_option_noop($option) {
1134 /* Do nothing here... */
1138 * Create hidden 'optpage' input field with value set by argument
1139 * @param string $optpage identification of option page
1140 * @return string html formated hidden input field
1142 function create_optpage_element($optpage) {
1143 return addHidden('optpage', $optpage);
1147 * Create hidden 'optmode' input field with value set by argument
1148 * @param string $optmode
1149 * @return string html formated hidden input field
1151 function create_optmode_element($optmode) {
1152 return addHidden('optmode', $optmode);
1156 * @param array $optgrps
1157 * @param array $optvals
1160 function create_option_groups($optgrps, $optvals) {
1161 /* Build a simple array with which to start. */
1164 /* Create option group for each option group name. */
1165 foreach ($optgrps as $grpkey => $grpname) {
1166 $result[$grpkey] = array();
1167 $result[$grpkey]['name'] = $grpname;
1168 $result[$grpkey]['options'] = array();
1171 /* Create a new SquirrelOption for each set of option values. */
1172 foreach ($optvals as $grpkey => $grpopts) {
1173 foreach ($grpopts as $optset) {
1174 /* Create a new option with all values given. */
1175 $next_option = new SquirrelOption(
1180 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
1181 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
1182 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
1183 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
1186 /* If provided, set if the caption is allowed to wrap for this option. */
1187 if (isset($optset['caption_wrap'])) {
1188 $next_option->setCaptionWrap($optset['caption_wrap']);
1191 /* If provided, set the size for this option. */
1192 if (isset($optset['size'])) {
1193 $next_option->setSize($optset['size']);
1196 /* If provided, set the trailing_text for this option. */
1197 if (isset($optset['trailing_text'])) {
1198 $next_option->setTrailingText($optset['trailing_text']);
1201 /* If provided, set the trailing_text_small for this option. */
1202 if (isset($optset['trailing_text_small'])) {
1203 $next_option->setTrailingTextSmall($optset['trailing_text_small']);
1206 /* If provided, set the trailing_text_is_html for this option. */
1207 if (isset($optset['trailing_text_is_html'])) {
1208 $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']);
1211 /* If provided, set the yes_text for this option. */
1212 if (isset($optset['yes_text'])) {
1213 $next_option->setYesText($optset['yes_text']);
1216 /* If provided, set the no_text for this option. */
1217 if (isset($optset['no_text'])) {
1218 $next_option->setNoText($optset['no_text']);
1221 /* If provided, set the poss_value_folders value for this option. */
1222 if (isset($optset['poss_value_folders'])) {
1223 $next_option->setPossValueFolders($optset['poss_value_folders']);
1226 /* If provided, set the layout type for this option. */
1227 if (isset($optset['layout_type'])) {
1228 $next_option->setLayoutType($optset['layout_type']);
1231 /* If provided, set the use_add_widget value for this option. */
1232 if (isset($optset['use_add_widget'])) {
1233 $next_option->setUseAddWidget($optset['use_add_widget']);
1236 /* If provided, set the use_delete_widget value for this option. */
1237 if (isset($optset['use_delete_widget'])) {
1238 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1241 /* If provided, set the comment for this option. */
1242 if (isset($optset['comment'])) {
1243 $next_option->setComment($optset['comment']);
1246 /* If provided, set the save function for this option. */
1247 if (isset($optset['save'])) {
1248 $next_option->setSaveFunction($optset['save']);
1251 /* If provided, set the extra attributes for this option. */
1252 if (isset($optset['extra_attributes'])) {
1253 $next_option->setExtraAttributes($optset['extra_attributes']);
1256 /* If provided, set the "post script" for this option. */
1257 if (isset($optset['post_script'])) {
1258 $next_option->setPostScript($optset['post_script']);
1261 /* If provided, set the folder_filter for this option. */
1262 if (isset($optset['folder_filter'])) {
1263 $next_option->setFolderFilter($optset['folder_filter']);
1266 /* Add this option to the option array. */
1267 $result[$grpkey]['options'][] = $next_option;
1271 /* Return our resulting array. */