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