Add 'edit list' type 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 /* 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
32 /* Define constants for the options refresh levels. */
33 define('SMOPT_REFRESH_NONE', 0);
34 define('SMOPT_REFRESH_FOLDERLIST', 1);
35 define('SMOPT_REFRESH_ALL', 2);
36
37 /* Define constants for the options size. */
38 define('SMOPT_SIZE_TINY', 0);
39 define('SMOPT_SIZE_SMALL', 1);
40 define('SMOPT_SIZE_MEDIUM', 2);
41 define('SMOPT_SIZE_LARGE', 3);
42 define('SMOPT_SIZE_HUGE', 4);
43 define('SMOPT_SIZE_NORMAL', 5);
44
45 define('SMOPT_SAVE_DEFAULT', 'save_option');
46 define('SMOPT_SAVE_NOOP', 'save_option_noop');
47
48 /**
49 * SquirrelOption: An option for SquirrelMail.
50 *
51 * @package squirrelmail
52 * @subpackage prefs
53 */
54 class SquirrelOption {
55 /**
56 * The name of this setting
57 * @var string
58 */
59 var $name;
60 /**
61 * The text that prefaces setting on the preferences page
62 * @var string
63 */
64 var $caption;
65 /**
66 * The type of INPUT element
67 *
68 * See SMOPT_TYPE_* defines
69 * @var integer
70 */
71 var $type;
72 /**
73 * Indicates if a link should be shown to refresh part
74 * or all of the window
75 *
76 * See SMOPT_REFRESH_* defines
77 * @var integer
78 */
79 var $refresh_level;
80 /**
81 * Specifies the size of certain input items
82 *
83 * See SMOPT_SIZE_* defines
84 * @var integer
85 */
86 var $size;
87 /**
88 * Text that follows a text input or
89 * select list input on the preferences page
90 *
91 * useful for indicating units, meanings of special values, etc.
92 * @var string
93 */
94 var $trailing_text;
95 /**
96 * text displayed to the user
97 *
98 * Used with SMOPT_TYPE_COMMENT options
99 * @var string
100 */
101 var $comment;
102 /**
103 * additional javascript or other widget attributes added to the
104 * user input; must be an array where keys are attribute names
105 * ("onclick", etc) and values are the attribute values.
106 * @var array
107 */
108 var $aExtraAttribs;
109 /**
110 * script (usually Javascript) that will be placed after (outside of)
111 * the INPUT tag
112 * @var string
113 */
114 var $post_script;
115
116 /**
117 * The name of the Save Function for this option.
118 * @var string
119 */
120 var $save_function;
121
122 /* The various 'values' for this options. */
123 /**
124 * default/preselected value for this option
125 * @var mixed
126 */
127 var $value;
128 /**
129 * new option value
130 * @var mixed
131 */
132 var $new_value;
133 /**
134 * associative array, where each key is an actual input value
135 * and the corresponding value is what is displayed to the user
136 * for that list item in the drop-down list
137 * @var array
138 */
139 var $possible_values;
140 /**
141 * disables html sanitizing.
142 *
143 * WARNING - don't use it, if user input is possible in option
144 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
145 * @var bool
146 */
147 var $htmlencoded=false;
148 /**
149 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
150 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
151 * See $flag argument in sqimap_mailbox_option_list() function.
152 * @var string
153 * @since 1.5.1
154 */
155 var $folder_filter='noselect';
156
157 /**
158 * Constructor function
159 * @param string $name
160 * @param string $caption
161 * @param integer $type
162 * @param integer $refresh_level
163 * @param mixed $initial_value
164 * @param array $possible_values
165 * @param bool $htmlencoded
166 */
167 function SquirrelOption
168 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
169 /* Set the basic stuff. */
170 $this->name = $name;
171 $this->caption = $caption;
172 $this->type = $type;
173 $this->refresh_level = $refresh_level;
174 $this->possible_values = $possible_values;
175 $this->htmlencoded = $htmlencoded;
176 $this->size = SMOPT_SIZE_MEDIUM;
177 $this->trailing_text = '';
178 $this->comment = '';
179 $this->aExtraAttribs = array();
180 $this->post_script = '';
181
182 //Check for a current value.
183 if (isset($GLOBALS[$name])) {
184 $this->value = $GLOBALS[$name];
185 } else if (!empty($initial_value)) {
186 $this->value = $initial_value;
187 } else {
188 $this->value = '';
189 }
190
191 /* Check for a new value. */
192 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
193 $this->new_value = '';
194 }
195
196 /* Set the default save function. */
197 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
198 $this->save_function = SMOPT_SAVE_DEFAULT;
199 } else {
200 $this->save_function = SMOPT_SAVE_NOOP;
201 }
202 }
203
204 /**
205 * Set the value for this option.
206 * @param mixed $value
207 */
208 function setValue($value) {
209 $this->value = $value;
210 }
211
212 /**
213 * Set the new value for this option.
214 * @param mixed $new_value
215 */
216 function setNewValue($new_value) {
217 $this->new_value = $new_value;
218 }
219
220 /**
221 * Set the size for this option.
222 * @param integer $size
223 */
224 function setSize($size) {
225 $this->size = $size;
226 }
227
228 /**
229 * Set the trailing_text for this option.
230 * @param string $trailing_text
231 */
232 function setTrailingText($trailing_text) {
233 $this->trailing_text = $trailing_text;
234 }
235
236 /**
237 * Set the comment for this option.
238 * @param string $comment
239 */
240 function setComment($comment) {
241 $this->comment = $comment;
242 }
243
244 /**
245 * Set the extra attributes for this option.
246 * @param array $aExtraAttribs
247 */
248 function setExtraAttributes($aExtraAttribs) {
249 $this->aExtraAttribs = $aExtraAttribs;
250 }
251
252 /**
253 * Set the "post script" for this option.
254 * @param string $post_script
255 */
256 function setPostScript($post_script) {
257 $this->post_script = $post_script;
258 }
259
260 /**
261 * Set the save function for this option.
262 * @param string $save_function
263 */
264 function setSaveFunction($save_function) {
265 $this->save_function = $save_function;
266 }
267
268 /**
269 * Set the trailing_text for this option.
270 * @param string $folder_filter
271 * @since 1.5.1
272 */
273 function setFolderFilter($folder_filter) {
274 $this->folder_filter = $folder_filter;
275 }
276
277 /**
278 * Creates fields on option pages according to option type
279 *
280 * This is the function that calls all other createWidget* functions.
281 *
282 * @return string The formated option field
283 *
284 */
285 function createWidget() {
286 global $color;
287
288 // Use new value if available
289 if (!empty($this->new_value)) {
290 $tempValue = $this->value;
291 $this->value = $this->new_value;
292 }
293
294 /* Get the widget for this option type. */
295 switch ($this->type) {
296 case SMOPT_TYPE_STRING:
297 $result = $this->createWidget_String();
298 break;
299 case SMOPT_TYPE_STRLIST:
300 $result = $this->createWidget_StrList();
301 break;
302 case SMOPT_TYPE_TEXTAREA:
303 $result = $this->createWidget_TextArea();
304 break;
305 case SMOPT_TYPE_INTEGER:
306 $result = $this->createWidget_Integer();
307 break;
308 case SMOPT_TYPE_FLOAT:
309 $result = $this->createWidget_Float();
310 break;
311 case SMOPT_TYPE_BOOLEAN:
312 $result = $this->createWidget_Boolean();
313 break;
314 case SMOPT_TYPE_HIDDEN:
315 $result = $this->createWidget_Hidden();
316 break;
317 case SMOPT_TYPE_COMMENT:
318 $result = $this->createWidget_Comment();
319 break;
320 case SMOPT_TYPE_FLDRLIST:
321 $result = $this->createWidget_FolderList();
322 break;
323 case SMOPT_TYPE_FLDRLIST_MULTI:
324 $result = $this->createWidget_FolderList(TRUE);
325 break;
326 case SMOPT_TYPE_EDIT_LIST:
327 $result = $this->createWidget_EditList();
328 break;
329 default:
330 error_box (
331 sprintf(_("Option Type '%s' Not Found"), $this->type)
332 );
333 }
334
335 /* Add the "post script" for this option. */
336 $result .= $this->post_script;
337
338 // put correct value back if need be
339 if (!empty($this->new_value)) {
340 $this->value = $tempValue;
341 }
342
343 /* Now, return the created widget. */
344 return $result;
345 }
346
347 /**
348 * Create string field
349 * @return string html formated option field
350 */
351 function createWidget_String() {
352 switch ($this->size) {
353 case SMOPT_SIZE_TINY:
354 $width = 5;
355 break;
356 case SMOPT_SIZE_SMALL:
357 $width = 12;
358 break;
359 case SMOPT_SIZE_LARGE:
360 $width = 38;
361 break;
362 case SMOPT_SIZE_HUGE:
363 $width = 50;
364 break;
365 case SMOPT_SIZE_NORMAL:
366 default:
367 $width = 25;
368 }
369
370 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
371 }
372
373 /**
374 * Create selection box
375 *
376 * When $this->htmlencoded is TRUE, the keys and values in
377 * $this->possible_values are assumed to be display-safe.
378 * Use with care!
379 *
380 * @return string html formated selection box
381 */
382 function createWidget_StrList() {
383 //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
384 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
385
386 }
387
388 /**
389 * Create folder selection box
390 *
391 * @param boolean $multiple_select When TRUE, the select widget
392 * will allow multiple selections
393 * (OPTIONAL; default is FALSE
394 * (single select list))
395 *
396 * @return string html formated selection box
397 *
398 */
399 function createWidget_FolderList($multiple_select=FALSE) {
400
401 switch ($this->size) {
402 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
403 case SMOPT_SIZE_TINY:
404 $height = 3;
405 break;
406 case SMOPT_SIZE_SMALL:
407 $height = 8;
408 break;
409 case SMOPT_SIZE_LARGE:
410 $height = 15;
411 break;
412 case SMOPT_SIZE_HUGE:
413 $height = 25;
414 break;
415 case SMOPT_SIZE_NORMAL:
416 default:
417 $height = 5;
418 }
419
420 // possible values might include a nested array of
421 // possible values (list of folders)
422 //
423 $option_list = array();
424 foreach ($this->possible_values as $value => $text) {
425
426 // list of folders (boxes array)
427 //
428 if (is_array($text)) {
429 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
430
431 // just one option here
432 //
433 } else {
434 $option_list = array_merge($option_list, array($value => $text));
435 }
436
437 }
438 if (empty($option_list))
439 $option_list = array('ignore' => _("unavailable"));
440
441
442 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
443
444 }
445
446 /**
447 * Creates textarea
448 * @return string html formated textarea field
449 */
450 function createWidget_TextArea() {
451 switch ($this->size) {
452 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
453 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
454 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
455 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
456 case SMOPT_SIZE_NORMAL:
457 default: $rows = 5; $cols = 50;
458 }
459 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
460 }
461
462 /**
463 * Creates field for integer
464 *
465 * Difference from createWidget_String is visible only when javascript is enabled
466 * @return string html formated option field
467 */
468 function createWidget_Integer() {
469
470 // add onChange javascript handler to a regular string widget
471 // which will strip out all non-numeric chars
472 if (checkForJavascript())
473 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
474 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
475 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
476 . 'this.value=newVal;';
477
478 return $this->createWidget_String();
479 }
480
481 /**
482 * Creates field for floating number
483 * Difference from createWidget_String is visible only when javascript is enabled
484 * @return string html formated option field
485 */
486 function createWidget_Float() {
487
488 // add onChange javascript handler to a regular string widget
489 // which will strip out all non-numeric (period also OK) chars
490 if (checkForJavascript())
491 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
492 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
493 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
494 . 'newVal += origVal.charAt(i); } this.value=newVal;';
495
496 return $this->createWidget_String();
497 }
498
499 /**
500 * Creates radio field (yes/no)
501 * @return string html formated radio field
502 */
503 function createWidget_Boolean() {
504
505 global $oTemplate, $nbsp;
506
507 /* Build the yes choice. */
508 $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(_("Yes"), 'new_' . $this->name . '_yes');
509
510 /* Build the no choice. */
511 $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(_("No"), 'new_' . $this->name . '_no');
512
513 /* Build and return the combined "boolean widget". */
514 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
515 return ($result);
516 }
517
518 /**
519 * Creates hidden field
520 * @return string html formated hidden input field
521 */
522 function createWidget_Hidden() {
523 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
524 }
525
526 /**
527 * Creates comment
528 * @return string comment
529 */
530 function createWidget_Comment() {
531 $result = $this->comment;
532 return ($result);
533 }
534
535 /**
536 * Creates an edit list
537 * @return string html formated list of edit fields and
538 * their associated controls
539 */
540 function createWidget_EditList() {
541
542 global $br, $nbsp;
543
544 switch ($this->size) {
545 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
546 case SMOPT_SIZE_TINY:
547 $height = 3;
548 break;
549 case SMOPT_SIZE_SMALL:
550 $height = 8;
551 break;
552 case SMOPT_SIZE_LARGE:
553 $height = 15;
554 break;
555 case SMOPT_SIZE_HUGE:
556 $height = 25;
557 break;
558 case SMOPT_SIZE_NORMAL:
559 default:
560 $height = 5;
561 }
562
563 //FIXME: $this->aExtraAttribs and $this->trailing_text probably should only be used in one place
564 //FIXME: might be nice to have this in a template file instead of creating layout here
565 return create_label(_("Add"), '')
566 . $nbsp . addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text)
567 . $br . addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput) == \'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 = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput.value = this.options[x].value; break; } }'), $this->aExtraAttribs), TRUE, $height) . htmlspecialchars($this->trailing_text)
568 . $br
569 . addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs))
570 . $nbsp . create_label(_("Delete Selected"), 'delete_' . $this->name);
571
572 }
573
574 /**
575 *
576 */
577 function save() {
578 $function = $this->save_function;
579 $function($this);
580 }
581
582 /**
583 *
584 */
585 function changed() {
586
587 // edit lists have a lot going on, so we'll always process them
588 //
589 if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
590
591 return ($this->value != $this->new_value);
592 }
593 } /* End of SquirrelOption class*/
594
595 /**
596 * Saves the option value (this is the default save function
597 * unless overridden by the user)
598 *
599 * @param object $option object that holds option name and new_value
600 */
601 function save_option($option) {
602
603 // Can't save the pref if we don't have the username
604 //
605 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
606 return;
607 }
608
609 global $data_dir;
610
611 // edit lists: first add new elements to list, then
612 // remove any selected ones (note that we must add
613 // before deleting because the javascript that populates
614 // the "add" textbox when selecting items in the list
615 // (for deletion))
616 //
617 if ($option->type == SMOPT_TYPE_EDIT_LIST) {
618
619 // add element if given
620 //
621 if (sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
622 $new_element = trim($new_element);
623 if (!empty($new_element)
624 && !in_array($new_element, $option->possible_values))
625 $option->possible_values[] = $new_element;
626 }
627
628 // delete selected elements if needed
629 //
630 if (is_array($option->new_value)
631 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
632 $option->possible_values = array_diff($option->possible_values, $option->new_value);
633
634 // save full list (stored in "possible_values")
635 //
636 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
637
638 // Certain option types need to be serialized because
639 // they are not scalar
640 //
641 } else if ($option->type == SMOPT_TYPE_FLDRLIST_MULTI)
642 setPref($data_dir, $username, $option->name, serialize($option->new_value));
643
644 else
645 setPref($data_dir, $username, $option->name, $option->new_value);
646
647 }
648
649 /**
650 * save function that does not save
651 * @param object $option
652 */
653 function save_option_noop($option) {
654 /* Do nothing here... */
655 }
656
657 /**
658 * Create hidden 'optpage' input field with value set by argument
659 * @param string $optpage identification of option page
660 * @return string html formated hidden input field
661 */
662 function create_optpage_element($optpage) {
663 return addHidden('optpage', $optpage);
664 }
665
666 /**
667 * Create hidden 'optmode' input field with value set by argument
668 * @param string $optmode
669 * @return string html formated hidden input field
670 */
671 function create_optmode_element($optmode) {
672 return addHidden('optmode', $optmode);
673 }
674
675 /**
676 * @param array $optgrps
677 * @param array $optvals
678 * @return array
679 */
680 function create_option_groups($optgrps, $optvals) {
681 /* Build a simple array with which to start. */
682 $result = array();
683
684 /* Create option group for each option group name. */
685 foreach ($optgrps as $grpkey => $grpname) {
686 $result[$grpkey] = array();
687 $result[$grpkey]['name'] = $grpname;
688 $result[$grpkey]['options'] = array();
689 }
690
691 /* Create a new SquirrelOption for each set of option values. */
692 foreach ($optvals as $grpkey => $grpopts) {
693 foreach ($grpopts as $optset) {
694 /* Create a new option with all values given. */
695 $next_option = new SquirrelOption(
696 $optset['name'],
697 $optset['caption'],
698 $optset['type'],
699 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
700 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
701 (isset($optset['posvals']) ? $optset['posvals'] : ''),
702 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
703 );
704
705 /* If provided, set the size for this option. */
706 if (isset($optset['size'])) {
707 $next_option->setSize($optset['size']);
708 }
709
710 /* If provided, set the trailing_text for this option. */
711 if (isset($optset['trailing_text'])) {
712 $next_option->setTrailingText($optset['trailing_text']);
713 }
714
715 /* If provided, set the comment for this option. */
716 if (isset($optset['comment'])) {
717 $next_option->setComment($optset['comment']);
718 }
719
720 /* If provided, set the save function for this option. */
721 if (isset($optset['save'])) {
722 $next_option->setSaveFunction($optset['save']);
723 }
724
725 /* If provided, set the extra attributes for this option. */
726 if (isset($optset['extra_attributes'])) {
727 $next_option->setExtraAttributes($optset['extra_attributes']);
728 }
729
730 /* If provided, set the "post script" for this option. */
731 if (isset($optset['post_script'])) {
732 $next_option->setPostScript($optset['post_script']);
733 }
734
735 /* If provided, set the folder_filter for this option. */
736 if (isset($optset['folder_filter'])) {
737 $next_option->setFolderFilter($optset['folder_filter']);
738 }
739
740 /* Add this option to the option array. */
741 $result[$grpkey]['options'][] = $next_option;
742 }
743 }
744
745 /* Return our resulting array. */
746 return ($result);
747 }
748