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