Add password option widget
[squirrelmail.git] / functions / options.php
1 <?php
2
3 /**
4 * options.php
5 *
6 * Functions needed to display the options pages.
7 *
8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage prefs
13 */
14
15 /**
16 * SquirrelOption: An option for SquirrelMail.
17 *
18 * @package squirrelmail
19 * @subpackage prefs
20 */
21 class SquirrelOption {
22 /**
23 * The original option configuration array
24 * @var array
25 */
26 var $raw_option_array;
27 /**
28 * The name of this setting
29 * @var string
30 */
31 var $name;
32 /**
33 * The text that prefaces setting on the preferences page
34 * @var string
35 */
36 var $caption;
37 /**
38 * Whether or not the caption text is allowed to wrap
39 * @var boolean
40 */
41 var $caption_wrap;
42 /**
43 * The type of INPUT element
44 *
45 * See SMOPT_TYPE_* defines
46 * @var integer
47 */
48 var $type;
49 /**
50 * Indicates if a link should be shown to refresh part
51 * or all of the window
52 *
53 * See SMOPT_REFRESH_* defines
54 * @var integer
55 */
56 var $refresh_level;
57 /**
58 * Specifies the size of certain input items
59 *
60 * See SMOPT_SIZE_* defines
61 * @var integer
62 */
63 var $size;
64 /**
65 * Text that follows a text input or
66 * select list input on the preferences page
67 *
68 * useful for indicating units, meanings of special values, etc.
69 * @var string
70 */
71 var $trailing_text;
72 /**
73 * Text that overrides the "Yes" label for boolean
74 * radio option widgets
75 *
76 * @var string
77 */
78 var $yes_text;
79 /**
80 * Text that overrides the "No" label for boolean
81 * radio option widgets
82 *
83 * @var string
84 */
85 var $no_text;
86 /**
87 * Some widgets support more than one layout type
88 *
89 * @var int
90 */
91 var $layout_type;
92 /**
93 * Indicates if the Add widget should be included
94 * with edit lists.
95 *
96 * @var boolean
97 */
98 var $use_add_widget;
99 /**
100 * Indicates if the Delete widget should be included
101 * with edit lists.
102 *
103 * @var boolean
104 */
105 var $use_delete_widget;
106 /**
107 * text displayed to the user
108 *
109 * Used with SMOPT_TYPE_COMMENT options
110 * @var string
111 */
112 var $comment;
113 /**
114 * additional javascript or other widget attributes added to the
115 * user input; must be an array where keys are attribute names
116 * ("onclick", etc) and values are the attribute values.
117 * @var array
118 */
119 var $aExtraAttribs;
120 /**
121 * script (usually Javascript) that will be placed after (outside of)
122 * the INPUT tag
123 * @var string
124 */
125 var $post_script;
126
127 /**
128 * The name of the Save Function for this option.
129 * @var string
130 */
131 var $save_function;
132
133 /* The various 'values' for this options. */
134 /**
135 * default/preselected value for this option
136 * @var mixed
137 */
138 var $value;
139 /**
140 * new option value
141 * @var mixed
142 */
143 var $new_value;
144 /**
145 * associative array, where each key is an actual input value
146 * and the corresponding value is what is displayed to the user
147 * for that list item in the drop-down list
148 * @var array
149 */
150 var $possible_values;
151 /**
152 * disables html sanitizing.
153 *
154 * WARNING - don't use it, if user input is possible in option
155 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
156 * @var bool
157 */
158 var $htmlencoded=false;
159 /**
160 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
161 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
162 * See $flag argument in sqimap_mailbox_option_list() function.
163 * @var string
164 * @since 1.5.1
165 */
166 var $folder_filter='noselect';
167
168 /**
169 * Constructor function
170 * @param array $raw_option_array
171 * @param string $name
172 * @param string $caption
173 * @param integer $type
174 * @param integer $refresh_level
175 * @param mixed $initial_value
176 * @param array $possible_values
177 * @param bool $htmlencoded
178 */
179 function SquirrelOption
180 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
181 /* Set the basic stuff. */
182 $this->raw_option_array = $raw_option_array;
183 $this->name = $name;
184 $this->caption = $caption;
185 $this->caption_wrap = TRUE;
186 $this->type = $type;
187 $this->refresh_level = $refresh_level;
188 $this->possible_values = $possible_values;
189 $this->htmlencoded = $htmlencoded;
190 $this->size = SMOPT_SIZE_NORMAL;
191 $this->trailing_text = '';
192 $this->yes_text = '';
193 $this->no_text = '';
194 $this->comment = '';
195 $this->layout_type = 0;
196 $this->use_add_widget = TRUE;
197 $this->use_delete_widget = TRUE;
198 $this->aExtraAttribs = array();
199 $this->post_script = '';
200
201 //Check for a current value.
202 if (isset($GLOBALS[$name])) {
203 $this->value = $GLOBALS[$name];
204 } else if (!empty($initial_value)) {
205 $this->value = $initial_value;
206 } else {
207 $this->value = '';
208 }
209
210 /* Check for a new value. */
211 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
212 $this->new_value = NULL;
213 }
214
215 /* Set the default save function. */
216 if ($type != SMOPT_TYPE_HIDDEN
217 && $type != SMOPT_TYPE_INFO
218 && $type != SMOPT_TYPE_COMMENT) {
219 $this->save_function = SMOPT_SAVE_DEFAULT;
220 } else {
221 $this->save_function = SMOPT_SAVE_NOOP;
222 }
223 }
224
225 /** Convenience function that identifies which types of
226 widgets are stored as (serialized) array values. */
227 function is_multiple_valued() {
228 return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
229 || $this->type == SMOPT_TYPE_STRLIST_MULTI
230 || $this->type == SMOPT_TYPE_EDIT_LIST);
231 }
232
233 /**
234 * Set the value for this option.
235 * @param mixed $value
236 */
237 function setValue($value) {
238 $this->value = $value;
239 }
240
241 /**
242 * Set the new value for this option.
243 * @param mixed $new_value
244 */
245 function setNewValue($new_value) {
246 $this->new_value = $new_value;
247 }
248
249 /**
250 * Set whether the caption is allowed to wrap for this option.
251 * @param boolean $caption_wrap
252 */
253 function setCaptionWrap($caption_wrap) {
254 $this->caption_wrap = $caption_wrap;
255 }
256
257 /**
258 * Set the size for this option.
259 * @param integer $size
260 */
261 function setSize($size) {
262 $this->size = $size;
263 }
264
265 /**
266 * Set the trailing_text for this option.
267 * @param string $trailing_text
268 */
269 function setTrailingText($trailing_text) {
270 $this->trailing_text = $trailing_text;
271 }
272
273 /**
274 * Set the yes_text for this option.
275 * @param string $yes_text
276 */
277 function setYesText($yes_text) {
278 $this->yes_text = $yes_text;
279 }
280
281 /**
282 * Set the no_text for this option.
283 * @param string $no_text
284 */
285 function setNoText($no_text) {
286 $this->no_text = $no_text;
287 }
288
289 /* Set the "use add widget" value for this option. */
290 function setUseAddWidget($use_add_widget) {
291 $this->use_add_widget = $use_add_widget;
292 }
293
294 /* Set the "use delete widget" value for this option. */
295 function setUseDeleteWidget($use_delete_widget) {
296 $this->use_delete_widget = $use_delete_widget;
297 }
298
299 /**
300 * Set the layout type for this option.
301 * @param int $layout_type
302 */
303 function setLayoutType($layout_type) {
304 $this->layout_type = $layout_type;
305 }
306
307 /**
308 * Set the comment for this option.
309 * @param string $comment
310 */
311 function setComment($comment) {
312 $this->comment = $comment;
313 }
314
315 /**
316 * Set the extra attributes for this option.
317 * @param array $aExtraAttribs
318 */
319 function setExtraAttributes($aExtraAttribs) {
320 $this->aExtraAttribs = $aExtraAttribs;
321 }
322
323 /**
324 * Set the "post script" for this option.
325 * @param string $post_script
326 */
327 function setPostScript($post_script) {
328 $this->post_script = $post_script;
329 }
330
331 /**
332 * Set the save function for this option.
333 * @param string $save_function
334 */
335 function setSaveFunction($save_function) {
336 $this->save_function = $save_function;
337 }
338
339 /**
340 * Set the folder_filter for this option.
341 * @param string $folder_filter
342 * @since 1.5.1
343 */
344 function setFolderFilter($folder_filter) {
345 $this->folder_filter = $folder_filter;
346 }
347
348 /**
349 * Creates fields on option pages according to option type
350 *
351 * This is the function that calls all other createWidget* functions.
352 *
353 * @return string The formated option field
354 *
355 */
356 function createWidget() {
357 global $color;
358
359 // Use new value if available
360 if (!is_null($this->new_value)) {
361 $tempValue = $this->value;
362 $this->value = $this->new_value;
363 }
364
365 /* Get the widget for this option type. */
366 switch ($this->type) {
367 case SMOPT_TYPE_PASSWORD:
368 $result = $this->createWidget_String(TRUE);
369 break;
370 case SMOPT_TYPE_STRING:
371 $result = $this->createWidget_String();
372 break;
373 case SMOPT_TYPE_STRLIST:
374 $result = $this->createWidget_StrList();
375 break;
376 case SMOPT_TYPE_TEXTAREA:
377 $result = $this->createWidget_TextArea();
378 break;
379 case SMOPT_TYPE_INTEGER:
380 $result = $this->createWidget_Integer();
381 break;
382 case SMOPT_TYPE_FLOAT:
383 $result = $this->createWidget_Float();
384 break;
385 case SMOPT_TYPE_BOOLEAN:
386 $result = $this->createWidget_Boolean();
387 break;
388 case SMOPT_TYPE_BOOLEAN_CHECKBOX:
389 $result = $this->createWidget_Boolean(TRUE);
390 break;
391 case SMOPT_TYPE_BOOLEAN_RADIO:
392 $result = $this->createWidget_Boolean(FALSE);
393 break;
394 case SMOPT_TYPE_HIDDEN:
395 $result = $this->createWidget_Hidden();
396 break;
397 case SMOPT_TYPE_COMMENT:
398 $result = $this->createWidget_Comment();
399 break;
400 case SMOPT_TYPE_FLDRLIST:
401 $result = $this->createWidget_FolderList();
402 break;
403 case SMOPT_TYPE_FLDRLIST_MULTI:
404 $result = $this->createWidget_FolderList(TRUE);
405 break;
406 case SMOPT_TYPE_EDIT_LIST:
407 $result = $this->createWidget_EditList();
408 break;
409 case SMOPT_TYPE_STRLIST_MULTI:
410 $result = $this->createWidget_StrList(TRUE);
411 break;
412 case SMOPT_TYPE_STRLIST_RADIO:
413 $result = $this->createWidget_StrList(FALSE, TRUE);
414 break;
415 case SMOPT_TYPE_SUBMIT:
416 $result = $this->createWidget_Submit();
417 break;
418 case SMOPT_TYPE_INFO:
419 $result = $this->createWidget_Info();
420 break;
421 default:
422 error_box (
423 sprintf(_("Option Type '%s' Not Found"), $this->type)
424 );
425 }
426
427 /* Add the "post script" for this option. */
428 $result .= $this->post_script;
429
430 // put correct value back if need be
431 if (!is_null($this->new_value)) {
432 $this->value = $tempValue;
433 }
434
435 /* Now, return the created widget. */
436 return $result;
437 }
438
439 /**
440 * Creates info block
441 * @return string html formated output
442 */
443 function createWidget_Info() {
444 return sq_htmlspecialchars($this->value);
445 }
446
447 /**
448 * Create string field
449 *
450 * @param boolean $password When TRUE, the text in the input
451 * widget will be obscured (OPTIONAL;
452 * default = FALSE).
453 *
454 * @return string html formated option field
455 *
456 */
457 function createWidget_String($password=FALSE) {
458 switch ($this->size) {
459 case SMOPT_SIZE_TINY:
460 $width = 5;
461 break;
462 case SMOPT_SIZE_SMALL:
463 $width = 12;
464 break;
465 case SMOPT_SIZE_LARGE:
466 $width = 38;
467 break;
468 case SMOPT_SIZE_HUGE:
469 $width = 50;
470 break;
471 case SMOPT_SIZE_NORMAL:
472 default:
473 $width = 25;
474 }
475
476 //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
477 if ($password)
478 addPwField($sName, $sValue = null, $aAttribs=array()) {
479 return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . htmlspecialchars($this->trailing_text);
480 else
481 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . htmlspecialchars($this->trailing_text);
482 }
483
484 /**
485 * Create selection box or radio button group
486 *
487 * When $this->htmlencoded is TRUE, the keys and values in
488 * $this->possible_values are assumed to be display-safe.
489 * Use with care!
490 *
491 * Note that when building radio buttons instead of a select
492 * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
493 * radio buttons will be output one after another without
494 * linebreaks between them. Otherwise, each radio button
495 * goes on a line of its own.
496 *
497 * @param boolean $multiple_select When TRUE, the select widget
498 * will allow multiple selections
499 * (OPTIONAL; default is FALSE
500 * (single select list))
501 * @param boolean $radio_buttons When TRUE, the widget will
502 * instead be built as a group
503 * of radio buttons (and
504 * $multiple_select will be
505 * forced to FALSE) (OPTIONAL;
506 * default is FALSE (select widget))
507 *
508 * @return string html formated selection box or radio buttons
509 *
510 */
511 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
512 //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
513
514 // radio buttons instead of select widget?
515 //
516 if ($radio_buttons) {
517
518 global $br, $nbsp;
519 $result = '';
520 foreach ($this->possible_values as $real_value => $disp_value) {
521 $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);
522 if ($this->size != SMOPT_SIZE_TINY)
523 $result .= $br;
524 }
525
526 return $result;
527 }
528
529
530 // everything below applies to select widgets
531 //
532 switch ($this->size) {
533 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
534 case SMOPT_SIZE_TINY:
535 $height = 3;
536 break;
537 case SMOPT_SIZE_SMALL:
538 $height = 8;
539 break;
540 case SMOPT_SIZE_LARGE:
541 $height = 15;
542 break;
543 case SMOPT_SIZE_HUGE:
544 $height = 25;
545 break;
546 case SMOPT_SIZE_NORMAL:
547 default:
548 $height = 5;
549 }
550
551 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text);
552
553 }
554
555 /**
556 * Create folder selection box
557 *
558 * @param boolean $multiple_select When TRUE, the select widget
559 * will allow multiple selections
560 * (OPTIONAL; default is FALSE
561 * (single select list))
562 *
563 * @return string html formated selection box
564 *
565 */
566 function createWidget_FolderList($multiple_select=FALSE) {
567
568 switch ($this->size) {
569 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
570 case SMOPT_SIZE_TINY:
571 $height = 3;
572 break;
573 case SMOPT_SIZE_SMALL:
574 $height = 8;
575 break;
576 case SMOPT_SIZE_LARGE:
577 $height = 15;
578 break;
579 case SMOPT_SIZE_HUGE:
580 $height = 25;
581 break;
582 case SMOPT_SIZE_NORMAL:
583 default:
584 $height = 5;
585 }
586
587 // possible values might include a nested array of
588 // possible values (list of folders)
589 //
590 $option_list = array();
591 foreach ($this->possible_values as $value => $text) {
592
593 // list of folders (boxes array)
594 //
595 if (is_array($text)) {
596 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
597
598 // just one option here
599 //
600 } else {
601 $option_list = array_merge($option_list, array($value => $text));
602 }
603
604 }
605 if (empty($option_list))
606 $option_list = array('ignore' => _("unavailable"));
607
608
609 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
610
611 }
612
613 /**
614 * Creates textarea
615 * @return string html formated textarea field
616 */
617 function createWidget_TextArea() {
618 switch ($this->size) {
619 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
620 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
621 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
622 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
623 case SMOPT_SIZE_NORMAL:
624 default: $rows = 5; $cols = 50;
625 }
626 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
627 }
628
629 /**
630 * Creates field for integer
631 *
632 * Difference from createWidget_String is visible only when javascript is enabled
633 * @return string html formated option field
634 */
635 function createWidget_Integer() {
636
637 // add onChange javascript handler to a regular string widget
638 // which will strip out all non-numeric chars
639 if (checkForJavascript())
640 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
641 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
642 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
643 . 'this.value=newVal;';
644
645 return $this->createWidget_String();
646 }
647
648 /**
649 * Creates field for floating number
650 * Difference from createWidget_String is visible only when javascript is enabled
651 * @return string html formated option field
652 */
653 function createWidget_Float() {
654
655 // add onChange javascript handler to a regular string widget
656 // which will strip out all non-numeric (period also OK) chars
657 if (checkForJavascript())
658 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
659 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
660 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
661 . 'newVal += origVal.charAt(i); } this.value=newVal;';
662
663 return $this->createWidget_String();
664 }
665
666 /**
667 * Create boolean widget
668 *
669 * When creating Yes/No radio buttons, the "yes_text"
670 * and "no_text" option attributes are used to override
671 * the typical "Yes" and "No" text.
672 *
673 * @param boolean $checkbox When TRUE, the widget will be
674 * constructed as a checkbox,
675 * otherwise it will be a set of
676 * Yes/No radio buttons (OPTIONAL;
677 * default is TRUE (checkbox)).
678 *
679 * @return string html formated boolean widget
680 *
681 */
682 function createWidget_Boolean($checkbox=TRUE) {
683
684 global $oTemplate, $nbsp;
685
686
687 // checkbox...
688 //
689 if ($checkbox) {
690 $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);
691 }
692
693 // radio buttons...
694 //
695 else {
696
697 /* Build the yes choice. */
698 $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');
699
700 /* Build the no choice. */
701 $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');
702
703 /* Build the combined "boolean widget". */
704 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
705
706 }
707
708 return ($result);
709 }
710
711 /**
712 * Creates hidden field
713 * @return string html formated hidden input field
714 */
715 function createWidget_Hidden() {
716 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
717 }
718
719 /**
720 * Creates comment
721 * @return string comment
722 */
723 function createWidget_Comment() {
724 $result = $this->comment;
725 return ($result);
726 }
727
728 /**
729 * Creates an edit list
730 *
731 * Note that multiple layout types are supported for this widget.
732 * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
733 * constants.
734 *
735 * @return string html formated list of edit fields and
736 * their associated controls
737 */
738 function createWidget_EditList() {
739
740 global $oTemplate;
741
742 switch ($this->size) {
743 case SMOPT_SIZE_TINY:
744 $height = 3;
745 break;
746 case SMOPT_SIZE_SMALL:
747 $height = 8;
748 break;
749 case SMOPT_SIZE_MEDIUM:
750 $height = 15;
751 break;
752 case SMOPT_SIZE_LARGE:
753 $height = 25;
754 break;
755 case SMOPT_SIZE_HUGE:
756 $height = 40;
757 break;
758 case SMOPT_SIZE_NORMAL:
759 default:
760 $height = 5;
761 }
762
763 if (empty($this->possible_values)) $this->possible_values = array();
764 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
765
766 //FIXME: $this->aExtraAttribs probably should only be used in one place
767 $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
768 $oTemplate->assign('use_input_widget', $this->use_add_widget);
769 $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
770
771 $oTemplate->assign('trailing_text', $this->trailing_text);
772 $oTemplate->assign('possible_values', $this->possible_values);
773 $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; } 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].value; break; } }'), $this->aExtraAttribs), TRUE, $height));
774 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
775 $oTemplate->assign('name', $this->name);
776
777 switch ($this->layout_type) {
778 case SMOPT_EDIT_LIST_LAYOUT_SELECT:
779 return $oTemplate->fetch('edit_list_widget.tpl');
780 case SMOPT_EDIT_LIST_LAYOUT_LIST:
781 return $oTemplate->fetch('edit_list_widget_list_style.tpl');
782 default:
783 error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $layout_type));
784 }
785
786 }
787
788 /**
789 * Creates a submit button
790 *
791 * @return string html formated submit button widget
792 *
793 */
794 function createWidget_Submit() {
795
796 return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
797
798 }
799
800 /**
801 *
802 */
803 function save() {
804 $function = $this->save_function;
805 $function($this);
806 }
807
808 /**
809 *
810 */
811 function changed() {
812
813 // edit lists have a lot going on, so we'll always process them
814 //
815 if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
816
817 return ($this->value != $this->new_value);
818 }
819 } /* End of SquirrelOption class*/
820
821 /**
822 * Saves the option value (this is the default save function
823 * unless overridden by the user)
824 *
825 * @param object $option object that holds option name and new_value
826 */
827 function save_option($option) {
828
829 // Can't save the pref if we don't have the username
830 //
831 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
832 return;
833 }
834
835 global $data_dir;
836
837 // edit lists: first add new elements to list, then
838 // remove any selected ones (note that we must add
839 // before deleting because the javascript that populates
840 // the "add" textbox when selecting items in the list
841 // (for deletion))
842 //
843 if ($option->type == SMOPT_TYPE_EDIT_LIST) {
844
845 if (empty($option->possible_values)) $option->possible_values = array();
846 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
847
848 // add element if given
849 //
850 if ((isset($option->use_add_widget) && $option->use_add_widget)
851 && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
852 $new_element = trim($new_element);
853 if (!empty($new_element)
854 && !in_array($new_element, $option->possible_values))
855 $option->possible_values[] = $new_element;
856 }
857
858 // delete selected elements if needed
859 //
860 if ((isset($option->use_delete_widget) && $option->use_delete_widget)
861 && is_array($option->new_value)
862 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
863 $option->possible_values = array_diff($option->possible_values, $option->new_value);
864
865 // save full list (stored in "possible_values")
866 //
867 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
868
869 // Certain option types need to be serialized because
870 // they are not scalar
871 //
872 } else if ($option->is_multiple_valued())
873 setPref($data_dir, $username, $option->name, serialize($option->new_value));
874
875 // Checkboxes, when unchecked, don't submit anything in
876 // the POST, so set to SMPREF_OFF if not found
877 //
878 else if (($option->type == SMOPT_TYPE_BOOLEAN
879 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
880 && empty($option->new_value))
881 setPref($data_dir, $username, $option->name, SMPREF_OFF);
882
883 else
884 setPref($data_dir, $username, $option->name, $option->new_value);
885
886
887 // if a checkbox or multi select is zeroed/cleared out, it
888 // needs to have an empty value pushed into its "new_value" slot
889 //
890 if (($option->type == SMOPT_TYPE_STRLIST_MULTI
891 || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
892 && is_null($option->new_value))
893 $option->new_value = '';
894
895 }
896
897 /**
898 * save function that does not save
899 * @param object $option
900 */
901 function save_option_noop($option) {
902 /* Do nothing here... */
903 }
904
905 /**
906 * Create hidden 'optpage' input field with value set by argument
907 * @param string $optpage identification of option page
908 * @return string html formated hidden input field
909 */
910 function create_optpage_element($optpage) {
911 return addHidden('optpage', $optpage);
912 }
913
914 /**
915 * Create hidden 'optmode' input field with value set by argument
916 * @param string $optmode
917 * @return string html formated hidden input field
918 */
919 function create_optmode_element($optmode) {
920 return addHidden('optmode', $optmode);
921 }
922
923 /**
924 * @param array $optgrps
925 * @param array $optvals
926 * @return array
927 */
928 function create_option_groups($optgrps, $optvals) {
929 /* Build a simple array with which to start. */
930 $result = array();
931
932 /* Create option group for each option group name. */
933 foreach ($optgrps as $grpkey => $grpname) {
934 $result[$grpkey] = array();
935 $result[$grpkey]['name'] = $grpname;
936 $result[$grpkey]['options'] = array();
937 }
938
939 /* Create a new SquirrelOption for each set of option values. */
940 foreach ($optvals as $grpkey => $grpopts) {
941 foreach ($grpopts as $optset) {
942 /* Create a new option with all values given. */
943 $next_option = new SquirrelOption(
944 $optset,
945 $optset['name'],
946 $optset['caption'],
947 $optset['type'],
948 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
949 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
950 (isset($optset['posvals']) ? $optset['posvals'] : ''),
951 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
952 );
953
954 /* If provided, set if the caption is allowed to wrap for this option. */
955 if (isset($optset['caption_wrap'])) {
956 $next_option->setCaptionWrap($optset['caption_wrap']);
957 }
958
959 /* If provided, set the size for this option. */
960 if (isset($optset['size'])) {
961 $next_option->setSize($optset['size']);
962 }
963
964 /* If provided, set the trailing_text for this option. */
965 if (isset($optset['trailing_text'])) {
966 $next_option->setTrailingText($optset['trailing_text']);
967 }
968
969 /* If provided, set the yes_text for this option. */
970 if (isset($optset['yes_text'])) {
971 $next_option->setYesText($optset['yes_text']);
972 }
973
974 /* If provided, set the no_text for this option. */
975 if (isset($optset['no_text'])) {
976 $next_option->setNoText($optset['no_text']);
977 }
978
979 /* If provided, set the layout type for this option. */
980 if (isset($optset['layout_type'])) {
981 $next_option->setLayoutType($optset['layout_type']);
982 }
983
984 /* If provided, set the use_add_widget value for this option. */
985 if (isset($optset['use_add_widget'])) {
986 $next_option->setUseAddWidget($optset['use_add_widget']);
987 }
988
989 /* If provided, set the use_delete_widget value for this option. */
990 if (isset($optset['use_delete_widget'])) {
991 $next_option->setUseDeleteWidget($optset['use_delete_widget']);
992 }
993
994 /* If provided, set the comment for this option. */
995 if (isset($optset['comment'])) {
996 $next_option->setComment($optset['comment']);
997 }
998
999 /* If provided, set the save function for this option. */
1000 if (isset($optset['save'])) {
1001 $next_option->setSaveFunction($optset['save']);
1002 }
1003
1004 /* If provided, set the extra attributes for this option. */
1005 if (isset($optset['extra_attributes'])) {
1006 $next_option->setExtraAttributes($optset['extra_attributes']);
1007 }
1008
1009 /* If provided, set the "post script" for this option. */
1010 if (isset($optset['post_script'])) {
1011 $next_option->setPostScript($optset['post_script']);
1012 }
1013
1014 /* If provided, set the folder_filter for this option. */
1015 if (isset($optset['folder_filter'])) {
1016 $next_option->setFolderFilter($optset['folder_filter']);
1017 }
1018
1019 /* Add this option to the option array. */
1020 $result[$grpkey]['options'][] = $next_option;
1021 }
1022 }
1023
1024 /* Return our resulting array. */
1025 return ($result);
1026 }
1027