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