Don't display page header when browser will be redirected with header() call.
[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);
a3ec3c91 35
52639d23 36/* Define constants for the layout scheme for edit lists. */
37define('SMOPT_EDIT_LIST_LAYOUT_LIST', 0);
38define('SMOPT_EDIT_LIST_LAYOUT_SELECT', 1);
39
a3ec3c91 40/* Define constants for the options refresh levels. */
41define('SMOPT_REFRESH_NONE', 0);
42define('SMOPT_REFRESH_FOLDERLIST', 1);
43define('SMOPT_REFRESH_ALL', 2);
44
bbcafebd 45/* Define constants for the options size. */
46define('SMOPT_SIZE_TINY', 0);
47define('SMOPT_SIZE_SMALL', 1);
48define('SMOPT_SIZE_MEDIUM', 2);
49define('SMOPT_SIZE_LARGE', 3);
50define('SMOPT_SIZE_HUGE', 4);
88cb1b4d 51define('SMOPT_SIZE_NORMAL', 5);
bbcafebd 52
cbe5423b 53define('SMOPT_SAVE_DEFAULT', 'save_option');
54define('SMOPT_SAVE_NOOP', 'save_option_noop');
55
9962527a 56/**
598294a7 57 * SquirrelOption: An option for SquirrelMail.
9962527a 58 *
8f6f9ba5 59 * @package squirrelmail
b4856b14 60 * @subpackage prefs
9962527a 61 */
62class SquirrelOption {
d789daf0 63 /**
64 * The original option configuration array
65 * @var array
66 */
67 var $raw_option_array;
b4856b14 68 /**
69 * The name of this setting
70 * @var string
71 */
9962527a 72 var $name;
b4856b14 73 /**
74 * The text that prefaces setting on the preferences page
75 * @var string
76 */
9962527a 77 var $caption;
b4856b14 78 /**
79 * The type of INPUT element
80 *
81 * See SMOPT_TYPE_* defines
82 * @var integer
83 */
9962527a 84 var $type;
b4856b14 85 /**
598294a7 86 * Indicates if a link should be shown to refresh part
b4856b14 87 * or all of the window
88 *
89 * See SMOPT_REFRESH_* defines
90 * @var integer
91 */
a3ec3c91 92 var $refresh_level;
b4856b14 93 /**
94 * Specifies the size of certain input items
95 *
96 * See SMOPT_SIZE_* defines
97 * @var integer
98 */
bbcafebd 99 var $size;
b4856b14 100 /**
598294a7 101 * Text that follows a text input or
b4856b14 102 * select list input on the preferences page
598294a7 103 *
b4856b14 104 * useful for indicating units, meanings of special values, etc.
105 * @var string
106 */
361d6e1b 107 var $trailing_text;
5b277d00 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;
52639d23 122 /**
123 * Some widgets support more than one layout type
124 *
125 * @var int
126 */
127 var $layout_type;
b4856b14 128 /**
129 * text displayed to the user
130 *
131 * Used with SMOPT_TYPE_COMMENT options
132 * @var string
133 */
bbcafebd 134 var $comment;
b4856b14 135 /**
0177059f 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
b4856b14 140 */
0177059f 141 var $aExtraAttribs;
b4856b14 142 /**
598294a7 143 * script (usually Javascript) that will be placed after (outside of)
b4856b14 144 * the INPUT tag
145 * @var string
146 */
6ae9e729 147 var $post_script;
cbe5423b 148
b4856b14 149 /**
150 * The name of the Save Function for this option.
151 * @var string
152 */
cbe5423b 153 var $save_function;
9962527a 154
155 /* The various 'values' for this options. */
b4856b14 156 /**
157 * default/preselected value for this option
158 * @var mixed
159 */
9962527a 160 var $value;
b4856b14 161 /**
162 * new option value
163 * @var mixed
164 */
9962527a 165 var $new_value;
b4856b14 166 /**
598294a7 167 * associative array, where each key is an actual input value
b4856b14 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 */
a3ec3c91 172 var $possible_values;
b4856b14 173 /**
174 * disables html sanitizing.
598294a7 175 *
176 * WARNING - don't use it, if user input is possible in option
0177059f 177 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
b4856b14 178 * @var bool
179 */
28520c87 180 var $htmlencoded=false;
99ecf044 181 /**
42b7c9d4 182 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
183 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
99ecf044 184 * See $flag argument in sqimap_mailbox_option_list() function.
185 * @var string
186 * @since 1.5.1
187 */
188 var $folder_filter='noselect';
9962527a 189
b4856b14 190 /**
191 * Constructor function
d789daf0 192 * @param array $raw_option_array
b4856b14 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 */
9962527a 201 function SquirrelOption
d789daf0 202 ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
9962527a 203 /* Set the basic stuff. */
d789daf0 204 $this->raw_option_array = $raw_option_array;
9962527a 205 $this->name = $name;
206 $this->caption = $caption;
9962527a 207 $this->type = $type;
a3ec3c91 208 $this->refresh_level = $refresh_level;
209 $this->possible_values = $possible_values;
28520c87 210 $this->htmlencoded = $htmlencoded;
b1dcab7e 211 $this->size = SMOPT_SIZE_NORMAL;
361d6e1b 212 $this->trailing_text = '';
5b277d00 213 $this->yes_text = '';
214 $this->no_text = '';
bbcafebd 215 $this->comment = '';
52639d23 216 $this->layout_type = 0;
0177059f 217 $this->aExtraAttribs = array();
6ae9e729 218 $this->post_script = '';
a3ec3c91 219
991c88e7 220 //Check for a current value.
221 if (isset($GLOBALS[$name])) {
a3ec3c91 222 $this->value = $GLOBALS[$name];
17f3d242 223 } else if (!empty($initial_value)) {
224 $this->value = $initial_value;
a3ec3c91 225 } else {
226 $this->value = '';
227 }
9962527a 228
a3ec3c91 229 /* Check for a new value. */
b4856b14 230 if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
59fc0b63 231 $this->new_value = NULL;
44ef0f47 232 }
cbe5423b 233
234 /* Set the default save function. */
2a50fbd7 235 if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
cbe5423b 236 $this->save_function = SMOPT_SAVE_DEFAULT;
237 } else {
238 $this->save_function = SMOPT_SAVE_NOOP;
239 }
240 }
241
5a42c101 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
b4856b14 250 /**
251 * Set the value for this option.
252 * @param mixed $value
253 */
cbe5423b 254 function setValue($value) {
255 $this->value = $value;
256 }
257
b4856b14 258 /**
259 * Set the new value for this option.
260 * @param mixed $new_value
261 */
cbe5423b 262 function setNewValue($new_value) {
263 $this->new_value = $new_value;
9962527a 264 }
44ef0f47 265
b4856b14 266 /**
267 * Set the size for this option.
268 * @param integer $size
269 */
bbcafebd 270 function setSize($size) {
271 $this->size = $size;
272 }
273
b4856b14 274 /**
275 * Set the trailing_text for this option.
276 * @param string $trailing_text
277 */
361d6e1b 278 function setTrailingText($trailing_text) {
279 $this->trailing_text = $trailing_text;
280 }
281
5b277d00 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
52639d23 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
b4856b14 306 /**
307 * Set the comment for this option.
308 * @param string $comment
309 */
bbcafebd 310 function setComment($comment) {
311 $this->comment = $comment;
312 }
313
b4856b14 314 /**
0177059f 315 * Set the extra attributes for this option.
316 * @param array $aExtraAttribs
b4856b14 317 */
0177059f 318 function setExtraAttributes($aExtraAttribs) {
319 $this->aExtraAttribs = $aExtraAttribs;
cbe5423b 320 }
321
b4856b14 322 /**
323 * Set the "post script" for this option.
324 * @param string $post_script
325 */
6ae9e729 326 function setPostScript($post_script) {
327 $this->post_script = $post_script;
328 }
329
b4856b14 330 /**
331 * Set the save function for this option.
332 * @param string $save_function
333 */
cbe5423b 334 function setSaveFunction($save_function) {
335 $this->save_function = $save_function;
336 }
337
99ecf044 338 /**
5b277d00 339 * Set the folder_filter for this option.
99ecf044 340 * @param string $folder_filter
341 * @since 1.5.1
342 */
343 function setFolderFilter($folder_filter) {
344 $this->folder_filter = $folder_filter;
345 }
346
b4856b14 347 /**
348 * Creates fields on option pages according to option type
349 *
9786ea94 350 * This is the function that calls all other createWidget* functions.
351 *
352 * @return string The formated option field
353 *
b4856b14 354 */
9786ea94 355 function createWidget() {
ce68b76b 356 global $color;
cbe5423b 357
62f7daa5 358 // Use new value if available
59fc0b63 359 if (!is_null($this->new_value)) {
74e44765 360 $tempValue = $this->value;
361 $this->value = $this->new_value;
362 }
363
cbe5423b 364 /* Get the widget for this option type. */
a3ec3c91 365 switch ($this->type) {
366 case SMOPT_TYPE_STRING:
37a3ed17 367 $result = $this->createWidget_String();
a3ec3c91 368 break;
369 case SMOPT_TYPE_STRLIST:
37a3ed17 370 $result = $this->createWidget_StrList();
a3ec3c91 371 break;
7e6d5ea3 372 case SMOPT_TYPE_TEXTAREA:
37a3ed17 373 $result = $this->createWidget_TextArea();
a3ec3c91 374 break;
375 case SMOPT_TYPE_INTEGER:
37a3ed17 376 $result = $this->createWidget_Integer();
a3ec3c91 377 break;
378 case SMOPT_TYPE_FLOAT:
37a3ed17 379 $result = $this->createWidget_Float();
a3ec3c91 380 break;
381 case SMOPT_TYPE_BOOLEAN:
37a3ed17 382 $result = $this->createWidget_Boolean();
a3ec3c91 383 break;
1b7db98b 384 case SMOPT_TYPE_BOOLEAN_CHECKBOX:
385 $result = $this->createWidget_Boolean(TRUE);
386 break;
8b2726c5 387 case SMOPT_TYPE_BOOLEAN_RADIO:
388 $result = $this->createWidget_Boolean(FALSE);
389 break;
2a50fbd7 390 case SMOPT_TYPE_HIDDEN:
37a3ed17 391 $result = $this->createWidget_Hidden();
a3ec3c91 392 break;
bbcafebd 393 case SMOPT_TYPE_COMMENT:
37a3ed17 394 $result = $this->createWidget_Comment();
bbcafebd 395 break;
be2d5495 396 case SMOPT_TYPE_FLDRLIST:
37a3ed17 397 $result = $this->createWidget_FolderList();
be2d5495 398 break;
42b7c9d4 399 case SMOPT_TYPE_FLDRLIST_MULTI:
400 $result = $this->createWidget_FolderList(TRUE);
401 break;
38d93650 402 case SMOPT_TYPE_EDIT_LIST:
403 $result = $this->createWidget_EditList();
404 break;
40268626 405 case SMOPT_TYPE_STRLIST_MULTI:
406 $result = $this->createWidget_StrList(TRUE);
407 break;
daf77710 408 case SMOPT_TYPE_STRLIST_RADIO:
409 $result = $this->createWidget_StrList(FALSE, TRUE);
410 break;
a3ec3c91 411 default:
fb8c4296 412 error_box (
413 sprintf(_("Option Type '%s' Not Found"), $this->type)
414 );
a3ec3c91 415 }
416
6ae9e729 417 /* Add the "post script" for this option. */
418 $result .= $this->post_script;
62f7daa5 419
74e44765 420 // put correct value back if need be
59fc0b63 421 if (!is_null($this->new_value)) {
74e44765 422 $this->value = $tempValue;
423 }
424
a3ec3c91 425 /* Now, return the created widget. */
9786ea94 426 return $result;
a3ec3c91 427 }
428
b4856b14 429 /**
430 * Create string field
431 * @return string html formated option field
432 */
37a3ed17 433 function createWidget_String() {
bbcafebd 434 switch ($this->size) {
88cb1b4d 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;
bbcafebd 447 case SMOPT_SIZE_NORMAL:
88cb1b4d 448 default:
449 $width = 25;
bbcafebd 450 }
451
daf77710 452 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 453 }
454
b4856b14 455 /**
daf77710 456 * Create selection box or radio button group
0177059f 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 *
daf77710 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
40268626 469 * will allow multiple selections
daf77710 470 * (OPTIONAL; default is FALSE
40268626 471 * (single select list))
daf77710 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))
40268626 478 *
daf77710 479 * @return string html formated selection box or radio buttons
40268626 480 *
b4856b14 481 */
daf77710 482 function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
98e88751 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
40268626 484
daf77710 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 //
40268626 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);
a3ec3c91 523
a3ec3c91 524 }
525
b4856b14 526 /**
527 * Create folder selection box
42b7c9d4 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 *
b4856b14 534 * @return string html formated selection box
42b7c9d4 535 *
b4856b14 536 */
42b7c9d4 537 function createWidget_FolderList($multiple_select=FALSE) {
be2d5495 538
38d93650 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
0177059f 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) {
62f7daa5 563
0177059f 564 // list of folders (boxes array)
565 //
566 if (is_array($text)) {
42b7c9d4 567 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
62f7daa5 568
0177059f 569 // just one option here
570 //
571 } else {
572 $option_list = array_merge($option_list, array($value => $text));
be2d5495 573 }
0177059f 574
62f7daa5 575 }
0177059f 576 if (empty($option_list))
577 $option_list = array('ignore' => _("unavailable"));
99ecf044 578
579
38d93650 580 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
0177059f 581
be2d5495 582 }
583
b4856b14 584 /**
585 * Creates textarea
586 * @return string html formated textarea field
587 */
37a3ed17 588 function createWidget_TextArea() {
bbcafebd 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 }
ba556ce5 597 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
a3ec3c91 598 }
599
b4856b14 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 */
37a3ed17 606 function createWidget_Integer() {
0d08ea5a 607
b65d1a08 608 // add onChange javascript handler to a regular string widget
609 // which will strip out all non-numeric chars
83aff890 610 if (checkForJavascript())
0177059f 611 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 612 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
613 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
0177059f 614 . 'this.value=newVal;';
615
616 return $this->createWidget_String();
a3ec3c91 617 }
618
b4856b14 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 */
37a3ed17 624 function createWidget_Float() {
37a3ed17 625
b65d1a08 626 // add onChange javascript handler to a regular string widget
62f7daa5 627 // which will strip out all non-numeric (period also OK) chars
83aff890 628 if (checkForJavascript())
0177059f 629 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 630 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
631 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
0177059f 632 . 'newVal += origVal.charAt(i); } this.value=newVal;';
633
634 return $this->createWidget_String();
a3ec3c91 635 }
636
b4856b14 637 /**
1b7db98b 638 * Create boolean widget
639 *
5b277d00 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 *
1b7db98b 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;
3b6a455c 648 * default is TRUE (checkbox)).
1b7db98b 649 *
650 * @return string html formated boolean widget
651 *
b4856b14 652 */
3b6a455c 653 function createWidget_Boolean($checkbox=TRUE) {
0177059f 654
5f88daeb 655 global $oTemplate, $nbsp;
fd87494d 656
fd87494d 657
1b7db98b 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. */
5b277d00 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');
1b7db98b 670
671 /* Build the no choice. */
5b277d00 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');
1b7db98b 673
674 /* Build the combined "boolean widget". */
675 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
676
677 }
fd87494d 678
fd87494d 679 return ($result);
a3ec3c91 680 }
681
b4856b14 682 /**
683 * Creates hidden field
684 * @return string html formated hidden input field
685 */
37a3ed17 686 function createWidget_Hidden() {
0177059f 687 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
a3ec3c91 688 }
689
b4856b14 690 /**
691 * Creates comment
692 * @return string comment
693 */
37a3ed17 694 function createWidget_Comment() {
bbcafebd 695 $result = $this->comment;
696 return ($result);
697 }
698
38d93650 699 /**
700 * Creates an edit list
52639d23 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 *
38d93650 706 * @return string html formated list of edit fields and
707 * their associated controls
708 */
709 function createWidget_EditList() {
710
cb606dfd 711 global $oTemplate;
38d93650 712
713 switch ($this->size) {
38d93650 714 case SMOPT_SIZE_TINY:
715 $height = 3;
716 break;
717 case SMOPT_SIZE_SMALL:
718 $height = 8;
719 break;
b1dcab7e 720 case SMOPT_SIZE_MEDIUM:
38d93650 721 $height = 15;
722 break;
b1dcab7e 723 case SMOPT_SIZE_LARGE:
38d93650 724 $height = 25;
725 break;
b1dcab7e 726 case SMOPT_SIZE_HUGE:
727 $height = 40;
728 break;
38d93650 729 case SMOPT_SIZE_NORMAL:
730 default:
731 $height = 5;
732 }
733
dd6f9627 734 if (empty($this->possible_values)) $this->possible_values = array();
2ebfc729 735 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
dd6f9627 736
cb606dfd 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);
52639d23 740 $oTemplate->assign('possible_values', $this->possible_values);
f2fdd884 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));
cb606dfd 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);
52639d23 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 }
38d93650 753
754 }
755
b4856b14 756 /**
757 *
758 */
cbe5423b 759 function save() {
760 $function = $this->save_function;
761 $function($this);
44ef0f47 762 }
cbe5423b 763
b4856b14 764 /**
765 *
766 */
cbe5423b 767 function changed() {
38d93650 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
6206f6c4 773 return ($this->value != $this->new_value);
cbe5423b 774 }
b4856b14 775} /* End of SquirrelOption class*/
cbe5423b 776
b4856b14 777/**
f2aba536 778 * Saves the option value (this is the default save function
779 * unless overridden by the user)
780 *
b4856b14 781 * @param object $option object that holds option name and new_value
782 */
cbe5423b 783function save_option($option) {
f2aba536 784
785 // Can't save the pref if we don't have the username
786 //
dac16606 787 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
dac16606 788 return;
0b97a708 789 }
f2aba536 790
0b97a708 791 global $data_dir;
f2aba536 792
38d93650 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
8eb63bb6 801 if (empty($option->possible_values)) $option->possible_values = array();
95dbbd91 802 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
8eb63bb6 803
38d93650 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
f2aba536 823 // Certain option types need to be serialized because
824 // they are not scalar
825 //
5a42c101 826 } else if ($option->is_multiple_valued())
f2aba536 827 setPref($data_dir, $username, $option->name, serialize($option->new_value));
38d93650 828
74b80a51 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
f2aba536 837 else
838 setPref($data_dir, $username, $option->name, $option->new_value);
839
b4f1a9ee 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
cbe5423b 849}
850
b4856b14 851/**
852 * save function that does not save
853 * @param object $option
854 */
cbe5423b 855function save_option_noop($option) {
856 /* Do nothing here... */
9962527a 857}
44ef0f47 858
b4856b14 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 */
cbe5423b 864function create_optpage_element($optpage) {
0177059f 865 return addHidden('optpage', $optpage);
cbe5423b 866}
867
b4856b14 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 */
cbe5423b 873function create_optmode_element($optmode) {
0177059f 874 return addHidden('optmode', $optmode);
cbe5423b 875}
876
b4856b14 877/**
878 * @param array $optgrps
879 * @param array $optvals
880 * @return array
881 */
cbe5423b 882function create_option_groups($optgrps, $optvals) {
a3ec3c91 883 /* Build a simple array with which to start. */
884 $result = array();
885
bbcafebd 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
a3ec3c91 893 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 894 foreach ($optvals as $grpkey => $grpopts) {
895 foreach ($grpopts as $optset) {
28520c87 896 /* Create a new option with all values given. */
897 $next_option = new SquirrelOption(
d789daf0 898 $optset,
7390e240 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 );
bbcafebd 907
908 /* If provided, set the size for this option. */
909 if (isset($optset['size'])) {
910 $next_option->setSize($optset['size']);
911 }
912
361d6e1b 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
5b277d00 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
52639d23 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
bbcafebd 933 /* If provided, set the comment for this option. */
934 if (isset($optset['comment'])) {
935 $next_option->setComment($optset['comment']);
936 }
937
cbe5423b 938 /* If provided, set the save function for this option. */
939 if (isset($optset['save'])) {
940 $next_option->setSaveFunction($optset['save']);
941 }
942
0177059f 943 /* If provided, set the extra attributes for this option. */
944 if (isset($optset['extra_attributes'])) {
945 $next_option->setExtraAttributes($optset['extra_attributes']);
cbe5423b 946 }
947
6ae9e729 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
99ecf044 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
bbcafebd 958 /* Add this option to the option array. */
959 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 960 }
961 }
962
963 /* Return our resulting array. */
964 return ($result);
965}
966