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