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