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