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