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