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 } else if (!empty($initial_value)) {
239 $this->value
= $initial_value;
244 /* Check for a new value. */
245 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
246 $this->new_value
= NULL;
249 /* Set the default save function. */
250 if ($type != SMOPT_TYPE_HIDDEN
251 && $type != SMOPT_TYPE_INFO
252 && $type != SMOPT_TYPE_COMMENT
) {
253 $this->save_function
= SMOPT_SAVE_DEFAULT
;
255 $this->save_function
= SMOPT_SAVE_NOOP
;
260 * Constructor (PHP4 style, kept for compatibility reasons)
261 * @param array $raw_option_array
262 * @param string $name
263 * @param string $caption
264 * @param integer $type
265 * @param integer $refresh_level
266 * @param mixed $initial_value
267 * @param array $possible_values
268 * @param bool $htmlencoded
270 function SquirrelOption
271 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
272 self
::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
275 /** Convenience function that identifies which types of
276 widgets are stored as (serialized) array values. */
277 function is_multiple_valued() {
278 return ($this->type
== SMOPT_TYPE_FLDRLIST_MULTI
279 ||
$this->type
== SMOPT_TYPE_STRLIST_MULTI
280 ||
$this->type
== SMOPT_TYPE_EDIT_LIST
281 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
);
285 * Set the value for this option.
286 * @param mixed $value
288 function setValue($value) {
289 $this->value
= $value;
293 * Set the new value for this option.
294 * @param mixed $new_value
296 function setNewValue($new_value) {
297 $this->new_value
= $new_value;
301 * Set whether the caption is allowed to wrap for this option.
302 * @param boolean $caption_wrap
304 function setCaptionWrap($caption_wrap) {
305 $this->caption_wrap
= $caption_wrap;
309 * Set the size for this option.
310 * @param integer $size
312 function setSize($size) {
317 * Set the trailing_text for this option.
318 * @param string $trailing_text
320 function setTrailingText($trailing_text) {
321 $this->trailing_text
= $trailing_text;
325 * Set the trailing_text_small for this option.
326 * @param boolean $trailing_text_small
328 function setTrailingTextSmall($trailing_text_small) {
329 $this->trailing_text_small
= $trailing_text_small;
333 * Set the trailing_text_is_html for this option.
334 * @param boolean $trailing_text_is_html
336 function setTrailingTextIsHtml($trailing_text_is_html) {
337 $this->trailing_text_is_html
= $trailing_text_is_html;
341 * Set the yes_text for this option.
342 * @param string $yes_text
344 function setYesText($yes_text) {
345 $this->yes_text
= $yes_text;
349 * Set the no_text for this option.
350 * @param string $no_text
352 function setNoText($no_text) {
353 $this->no_text
= $no_text;
356 /* Set the "use add widget" value for this option. */
357 function setUseAddWidget($use_add_widget) {
358 $this->use_add_widget
= $use_add_widget;
361 /* Set the "use delete widget" value for this option. */
362 function setUseDeleteWidget($use_delete_widget) {
363 $this->use_delete_widget
= $use_delete_widget;
366 /* Set the "poss value folders" value for this option.
367 See the associative edit list widget, which uses this
368 to offer folder list selection for the values */
369 function setPossValueFolders($poss_value_folders) {
370 $this->poss_value_folders
= $poss_value_folders;
374 * Set the layout type for this option.
375 * @param int $layout_type
377 function setLayoutType($layout_type) {
378 $this->layout_type
= $layout_type;
382 * Set the comment for this option.
383 * @param string $comment
385 function setComment($comment) {
386 $this->comment
= $comment;
390 * Set the extra attributes for this option.
391 * @param array $aExtraAttribs
393 function setExtraAttributes($aExtraAttribs) {
394 $this->aExtraAttribs
= $aExtraAttribs;
398 * Set the "post script" for this option.
399 * @param string $post_script
401 function setPostScript($post_script) {
402 $this->post_script
= $post_script;
406 * Set the save function for this option.
407 * @param string $save_function
409 function setSaveFunction($save_function) {
410 $this->save_function
= $save_function;
414 * Set the folder_filter for this option.
415 * @param string $folder_filter
418 function setFolderFilter($folder_filter) {
419 $this->folder_filter
= $folder_filter;
423 * Creates fields on option pages according to option type
425 * This is the function that calls all other createWidget* functions.
427 * @return string The formated option field
430 function createWidget() {
433 // Use new value if available
434 if (!is_null($this->new_value
)) {
435 $tempValue = $this->value
;
436 $this->value
= $this->new_value
;
439 /* Get the widget for this option type. */
440 switch ($this->type
) {
441 case SMOPT_TYPE_PASSWORD
:
442 $result = $this->createWidget_String(TRUE);
444 case SMOPT_TYPE_STRING
:
445 $result = $this->createWidget_String();
447 case SMOPT_TYPE_STRLIST
:
448 $result = $this->createWidget_StrList();
450 case SMOPT_TYPE_TEXTAREA
:
451 $result = $this->createWidget_TextArea();
453 case SMOPT_TYPE_INTEGER
:
454 $result = $this->createWidget_Integer();
456 case SMOPT_TYPE_FLOAT
:
457 $result = $this->createWidget_Float();
459 case SMOPT_TYPE_BOOLEAN
:
460 $result = $this->createWidget_Boolean();
462 case SMOPT_TYPE_BOOLEAN_CHECKBOX
:
463 $result = $this->createWidget_Boolean(TRUE);
465 case SMOPT_TYPE_BOOLEAN_RADIO
:
466 $result = $this->createWidget_Boolean(FALSE);
468 case SMOPT_TYPE_HIDDEN
:
469 $result = $this->createWidget_Hidden();
471 case SMOPT_TYPE_COMMENT
:
472 $result = $this->createWidget_Comment();
474 case SMOPT_TYPE_FLDRLIST
:
475 $result = $this->createWidget_FolderList();
477 case SMOPT_TYPE_FLDRLIST_MULTI
:
478 $result = $this->createWidget_FolderList(TRUE);
480 case SMOPT_TYPE_EDIT_LIST
:
481 $result = $this->createWidget_EditList();
483 case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
:
484 $result = $this->createWidget_EditListAssociative();
486 case SMOPT_TYPE_STRLIST_MULTI
:
487 $result = $this->createWidget_StrList(TRUE);
489 case SMOPT_TYPE_STRLIST_RADIO
:
490 $result = $this->createWidget_StrList(FALSE, TRUE);
492 case SMOPT_TYPE_SUBMIT
:
493 $result = $this->createWidget_Submit();
495 case SMOPT_TYPE_INFO
:
496 $result = $this->createWidget_Info();
500 sprintf(_("Option Type '%s' Not Found"), $this->type
)
504 /* Add the "post script" for this option. */
505 $result .= $this->post_script
;
507 // put correct value back if need be
508 if (!is_null($this->new_value
)) {
509 $this->value
= $tempValue;
512 /* Now, return the created widget. */
518 * @return string html formated output
520 function createWidget_Info() {
521 // return sq_htmlspecialchars($this->value);
522 // like COMMENT, allow HTML here
527 * Create string field
529 * @param boolean $password When TRUE, the text in the input
530 * widget will be obscured (OPTIONAL;
533 * @return string html formated option field
536 function createWidget_String($password=FALSE) {
537 switch ($this->size
) {
538 case SMOPT_SIZE_TINY
:
541 case SMOPT_SIZE_SMALL
:
544 case SMOPT_SIZE_LARGE
:
547 case SMOPT_SIZE_HUGE
:
550 case SMOPT_SIZE_NORMAL
:
555 //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!)
557 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>' : '');
559 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>' : '');
563 * Create selection box or radio button group
565 * When $this->htmlencoded is TRUE, the keys and values in
566 * $this->possible_values are assumed to be display-safe.
569 * Note that when building radio buttons instead of a select
570 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
571 * radio buttons will be output one after another without
572 * linebreaks between them. Otherwise, each radio button
573 * goes on a line of its own.
575 * @param boolean $multiple_select When TRUE, the select widget
576 * will allow multiple selections
577 * (OPTIONAL; default is FALSE
578 * (single select list))
579 * @param boolean $radio_buttons When TRUE, the widget will
580 * instead be built as a group
581 * of radio buttons (and
582 * $multiple_select will be
583 * forced to FALSE) (OPTIONAL;
584 * default is FALSE (select widget))
586 * @return string html formated selection box or radio buttons
589 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
590 //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
592 // radio buttons instead of select widget?
594 if ($radio_buttons) {
598 foreach ($this->possible_values
as $real_value => $disp_value) {
599 $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);
600 if ($this->size
!= SMOPT_SIZE_TINY
)
608 // everything below applies to select widgets
610 switch ($this->size
) {
611 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
612 case SMOPT_SIZE_TINY
:
615 case SMOPT_SIZE_SMALL
:
618 case SMOPT_SIZE_LARGE
:
621 case SMOPT_SIZE_HUGE
:
624 case SMOPT_SIZE_NORMAL
:
629 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>' : '');
634 * Create folder selection box
636 * @param boolean $multiple_select When TRUE, the select widget
637 * will allow multiple selections
638 * (OPTIONAL; default is FALSE
639 * (single select list))
641 * @return string html formated selection box
644 function createWidget_FolderList($multiple_select=FALSE) {
646 switch ($this->size
) {
647 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
648 case SMOPT_SIZE_TINY
:
651 case SMOPT_SIZE_SMALL
:
654 case SMOPT_SIZE_LARGE
:
657 case SMOPT_SIZE_HUGE
:
660 case SMOPT_SIZE_NORMAL
:
665 // possible values might include a nested array of
666 // possible values (list of folders)
668 $option_list = array();
669 foreach ($this->possible_values
as $value => $text) {
671 // list of folders (boxes array)
673 if (is_array($text)) {
674 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
676 // just one option here
679 $option_list = array_merge($option_list, array($value => $text));
683 if (empty($option_list))
684 $option_list = array('ignore' => _("unavailable"));
687 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>' : '');
693 * @return string html formated textarea field
695 function createWidget_TextArea() {
696 switch ($this->size
) {
697 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
698 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
699 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
700 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
701 case SMOPT_SIZE_NORMAL
:
702 default: $rows = 5; $cols = 50;
704 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
708 * Creates field for integer
710 * Difference from createWidget_String is visible only when javascript is enabled
711 * @return string html formated option field
713 function createWidget_Integer() {
715 // add onChange javascript handler to a regular string widget
716 // which will strip out all non-numeric chars
717 if (checkForJavascript())
718 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
719 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
720 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
721 . 'this.value=newVal;';
723 return $this->createWidget_String();
727 * Creates field for floating number
728 * Difference from createWidget_String is visible only when javascript is enabled
729 * @return string html formated option field
731 function createWidget_Float() {
733 // add onChange javascript handler to a regular string widget
734 // which will strip out all non-numeric (period also OK) chars
735 if (checkForJavascript())
736 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
737 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
738 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
739 . 'newVal += origVal.charAt(i); } this.value=newVal;';
741 return $this->createWidget_String();
745 * Create boolean widget
747 * When creating Yes/No radio buttons, the "yes_text"
748 * and "no_text" option attributes are used to override
749 * the typical "Yes" and "No" text.
751 * @param boolean $checkbox When TRUE, the widget will be
752 * constructed as a checkbox,
753 * otherwise it will be a set of
754 * Yes/No radio buttons (OPTIONAL;
755 * default is TRUE (checkbox)).
757 * @return string html formated boolean widget
760 function createWidget_Boolean($checkbox=TRUE) {
762 global $oTemplate, $nbsp;
768 //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
769 $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
);
776 /* Build the yes choice. */
777 $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');
779 /* Build the no choice. */
780 $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');
782 /* Build the combined "boolean widget". */
783 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
791 * Creates hidden field
792 * @return string html formated hidden input field
794 function createWidget_Hidden() {
795 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
800 * @return string comment
802 function createWidget_Comment() {
803 $result = $this->comment
;
808 * Creates a (non-associative) edit list
810 * Note that multiple layout types are supported for this widget.
811 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
814 * @return string html formated list of edit fields and
815 * their associated controls
817 function createWidget_EditList() {
821 switch ($this->size
) {
822 case SMOPT_SIZE_TINY
:
825 case SMOPT_SIZE_SMALL
:
828 case SMOPT_SIZE_MEDIUM
:
831 case SMOPT_SIZE_LARGE
:
834 case SMOPT_SIZE_HUGE
:
837 case SMOPT_SIZE_NORMAL
:
842 if (empty($this->possible_values
)) $this->possible_values
= array();
843 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
845 //FIXME: $this->aExtraAttribs probably should only be used in one place
846 $oTemplate->assign('input_widget', addInput('add_' . $this->name
, '', 38, 0, $this->aExtraAttribs
));
847 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
848 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
850 $oTemplate->assign('trailing_text', $this->trailing_text
);
851 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
852 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html
);
853 $oTemplate->assign('possible_values', $this->possible_values
);
854 $oTemplate->assign('current_value', $this->value
);
855 $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));
856 // NOTE: i=f-1; j=e-1 is in lieu of break 2
857 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
858 $oTemplate->assign('name', $this->name
);
860 switch ($this->layout_type
) {
861 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
862 return $oTemplate->fetch('edit_list_widget.tpl');
863 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
864 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
866 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type
));
872 * Creates an associative edit list
874 * Note that multiple layout types are supported for this widget.
875 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
878 * @return string html formated list of edit fields and
879 * their associated controls
881 function createWidget_EditListAssociative() {
885 switch ($this->size
) {
886 case SMOPT_SIZE_TINY
:
889 case SMOPT_SIZE_SMALL
:
892 case SMOPT_SIZE_MEDIUM
:
895 case SMOPT_SIZE_LARGE
:
898 case SMOPT_SIZE_HUGE
:
901 case SMOPT_SIZE_NORMAL
:
907 // ensure correct format of current value(s)
909 if (empty($this->possible_values
)) $this->possible_values
= array();
910 if (!is_array($this->possible_values
)) $this->possible_values
= array($this->possible_values
);
913 $oTemplate->assign('name', $this->name
);
914 $oTemplate->assign('current_value', $this->value
);
915 $oTemplate->assign('possible_values', $this->possible_values
);
916 $oTemplate->assign('poss_value_folders', $this->poss_value_folders
);
917 $oTemplate->assign('folder_filter', $this->folder_filter
);
919 $oTemplate->assign('use_input_widget', $this->use_add_widget
);
920 $oTemplate->assign('use_delete_widget', $this->use_delete_widget
);
922 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name
, FALSE, SMPREF_YES
, array_merge(array('id' => 'delete_' . $this->name
), $this->aExtraAttribs
)));
924 //FIXME: $this->aExtraAttribs probably should only be used in one place
925 $oTemplate->assign('input_key_widget', addInput('add_' . $this->name
. '_key', '', 22, 0, $this->aExtraAttribs
));
926 $oTemplate->assign('input_value_widget', addInput('add_' . $this->name
. '_value', '', 12, 0, $this->aExtraAttribs
));
928 $oTemplate->assign('select_height', $height);
930 $oTemplate->assign('aAttribs', $this->aExtraAttribs
);
932 $oTemplate->assign('trailing_text', $this->trailing_text
);
933 $oTemplate->assign('trailing_text_small', $this->trailing_text_small
);
934 $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html
);
936 switch ($this->layout_type
) {
937 case SMOPT_EDIT_LIST_LAYOUT_SELECT
:
938 return $oTemplate->fetch('edit_list_associative_widget.tpl');
939 case SMOPT_EDIT_LIST_LAYOUT_LIST
:
940 return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
942 error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type
));
948 * Creates a submit button
950 * @return string html formated submit button widget
953 function createWidget_Submit() {
955 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>' : '');
963 $function = $this->save_function
;
972 // edit lists have a lot going on, so we'll always process them
974 if ($this->type
== SMOPT_TYPE_EDIT_LIST
975 ||
$this->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
)
978 return ($this->value
!= $this->new_value
);
980 } /* End of SquirrelOption class*/
983 * Saves the option value (this is the default save function
984 * unless overridden by the user)
986 * @param object $option object that holds option name and new_value
988 function save_option($option) {
990 // Can't save the pref if we don't have the username
992 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
996 // if the widget is a selection list, make sure the new
997 // value is actually in the selection list and is not an
1000 if ($option->type
== SMOPT_TYPE_STRLIST
1001 && !array_key_exists($option->new_value
, $option->possible_values
))
1005 // all other widgets except TEXTAREAs should never be allowed to have newlines
1007 else if ($option->type
!= SMOPT_TYPE_TEXTAREA
)
1008 $option->new_value
= str_replace(array("\r", "\n"), '', $option->new_value
);
1013 // edit lists: first add new elements to list, then
1014 // remove any selected ones (note that we must add
1015 // before deleting because the javascript that populates
1016 // the "add" textbox when selecting items in the list
1019 if ($option->type
== SMOPT_TYPE_EDIT_LIST
) {
1021 if (empty($option->possible_values
)) $option->possible_values
= array();
1022 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1024 // add element if given
1026 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1027 && sqGetGlobalVar('add_' . $option->name
, $new_element, SQ_POST
)) {
1028 $new_element = trim($new_element);
1029 if (!empty($new_element)
1030 && !in_array($new_element, $option->possible_values
))
1031 $option->possible_values
[] = $new_element;
1034 // delete selected elements if needed
1036 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1037 && is_array($option->new_value
)
1038 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
))
1039 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1041 // save full list (stored in "possible_values")
1043 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1045 // associative edit lists are handled similar to
1046 // non-associative ones
1048 } else if ($option->type
== SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
) {
1050 if (empty($option->possible_values
)) $option->possible_values
= array();
1051 if (!is_array($option->possible_values
)) $option->possible_values
= array($option->possible_values
);
1053 // add element if given
1055 $new_element_key = '';
1056 $new_element_value = '';
1057 $retrieve_key = sqGetGlobalVar('add_' . $option->name
. '_key', $new_element_key, SQ_POST
);
1058 $retrieve_value = sqGetGlobalVar('add_' . $option->name
. '_value', $new_element_value, SQ_POST
);
1060 if ((isset($option->use_add_widget
) && $option->use_add_widget
)
1061 && ($retrieve_key ||
$retrieve_value)) {
1062 $new_element_key = trim($new_element_key);
1063 $new_element_value = trim($new_element_value);
1064 if ($option->poss_value_folders
&& empty($new_element_key))
1065 $new_element_value = '';
1066 if (!empty($new_element_key) ||
!empty($new_element_value)) {
1067 if (empty($new_element_key)) $new_element_key = '0';
1068 $option->possible_values
[$new_element_key] = $new_element_value;
1072 // delete selected elements if needed
1074 if ((isset($option->use_delete_widget
) && $option->use_delete_widget
)
1075 && is_array($option->new_value
)
1076 && sqGetGlobalVar('delete_' . $option->name
, $ignore, SQ_POST
)) {
1078 if ($option->layout_type
== SMOPT_EDIT_LIST_LAYOUT_SELECT
) {
1079 foreach ($option->new_value
as $key)
1080 unset($option->possible_values
[urldecode($key)]);
1083 $option->possible_values
= array_diff($option->possible_values
, $option->new_value
);
1086 // save full list (stored in "possible_values")
1088 setPref($data_dir, $username, $option->name
, serialize($option->possible_values
));
1090 // Certain option types need to be serialized because
1091 // they are not scalar
1093 } else if ($option->is_multiple_valued())
1094 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
1096 // Checkboxes, when unchecked, don't submit anything in
1097 // the POST, so set to SMPREF_OFF if not found
1099 else if (($option->type
== SMOPT_TYPE_BOOLEAN
1100 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1101 && empty($option->new_value
))
1102 setPref($data_dir, $username, $option->name
, SMPREF_OFF
);
1104 // For integer fields, make sure we only have digits...
1105 // We'll be nice and instead of just converting to an integer,
1106 // we'll physically remove each non-digit in the string.
1108 else if ($option->type
== SMOPT_TYPE_INTEGER
) {
1109 $option->new_value
= preg_replace('/[^0-9]/', '', $option->new_value
);
1110 setPref($data_dir, $username, $option->name
, $option->new_value
);
1114 setPref($data_dir, $username, $option->name
, $option->new_value
);
1117 // if a checkbox or multi select is zeroed/cleared out, it
1118 // needs to have an empty value pushed into its "new_value" slot
1120 if (($option->type
== SMOPT_TYPE_STRLIST_MULTI
1121 ||
$option->type
== SMOPT_TYPE_BOOLEAN_CHECKBOX
)
1122 && is_null($option->new_value
))
1123 $option->new_value
= '';
1128 * save function that does not save
1129 * @param object $option
1131 function save_option_noop($option) {
1132 /* Do nothing here... */
1136 * Create hidden 'optpage' input field with value set by argument
1137 * @param string $optpage identification of option page
1138 * @return string html formated hidden input field
1140 function create_optpage_element($optpage) {
1141 return addHidden('optpage', $optpage);
1145 * Create hidden 'optmode' input field with value set by argument
1146 * @param string $optmode
1147 * @return string html formated hidden input field
1149 function create_optmode_element($optmode) {
1150 return addHidden('optmode', $optmode);
1154 * @param array $optgrps
1155 * @param array $optvals
1158 function create_option_groups($optgrps, $optvals) {
1159 /* Build a simple array with which to start. */
1162 /* Create option group for each option group name. */
1163 foreach ($optgrps as $grpkey => $grpname) {
1164 $result[$grpkey] = array();
1165 $result[$grpkey]['name'] = $grpname;
1166 $result[$grpkey]['options'] = array();
1169 /* Create a new SquirrelOption for each set of option values. */
1170 foreach ($optvals as $grpkey => $grpopts) {
1171 foreach ($grpopts as $optset) {
1172 /* Create a new option with all values given. */
1173 $next_option = new SquirrelOption(
1178 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
1179 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
1180 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
1181 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
1184 /* If provided, set if the caption is allowed to wrap for this option. */
1185 if (isset($optset['caption_wrap'])) {
1186 $next_option->setCaptionWrap($optset['caption_wrap']);
1189 /* If provided, set the size for this option. */
1190 if (isset($optset['size'])) {
1191 $next_option->setSize($optset['size']);
1194 /* If provided, set the trailing_text for this option. */
1195 if (isset($optset['trailing_text'])) {
1196 $next_option->setTrailingText($optset['trailing_text']);
1199 /* If provided, set the trailing_text_small for this option. */
1200 if (isset($optset['trailing_text_small'])) {
1201 $next_option->setTrailingTextSmall($optset['trailing_text_small']);
1204 /* If provided, set the trailing_text_is_html for this option. */
1205 if (isset($optset['trailing_text_is_html'])) {
1206 $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']);
1209 /* If provided, set the yes_text for this option. */
1210 if (isset($optset['yes_text'])) {
1211 $next_option->setYesText($optset['yes_text']);
1214 /* If provided, set the no_text for this option. */
1215 if (isset($optset['no_text'])) {
1216 $next_option->setNoText($optset['no_text']);
1219 /* If provided, set the poss_value_folders value for this option. */
1220 if (isset($optset['poss_value_folders'])) {
1221 $next_option->setPossValueFolders($optset['poss_value_folders']);
1224 /* If provided, set the layout type for this option. */
1225 if (isset($optset['layout_type'])) {
1226 $next_option->setLayoutType($optset['layout_type']);
1229 /* If provided, set the use_add_widget value for this option. */
1230 if (isset($optset['use_add_widget'])) {
1231 $next_option->setUseAddWidget($optset['use_add_widget']);
1234 /* If provided, set the use_delete_widget value for this option. */
1235 if (isset($optset['use_delete_widget'])) {
1236 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
1239 /* If provided, set the comment for this option. */
1240 if (isset($optset['comment'])) {
1241 $next_option->setComment($optset['comment']);
1244 /* If provided, set the save function for this option. */
1245 if (isset($optset['save'])) {
1246 $next_option->setSaveFunction($optset['save']);
1249 /* If provided, set the extra attributes for this option. */
1250 if (isset($optset['extra_attributes'])) {
1251 $next_option->setExtraAttributes($optset['extra_attributes']);
1254 /* If provided, set the "post script" for this option. */
1255 if (isset($optset['post_script'])) {
1256 $next_option->setPostScript($optset['post_script']);
1259 /* If provided, set the folder_filter for this option. */
1260 if (isset($optset['folder_filter'])) {
1261 $next_option->setFolderFilter($optset['folder_filter']);
1264 /* Add this option to the option array. */
1265 $result[$grpkey]['options'][] = $next_option;
1269 /* Return our resulting array. */