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