Add convenience function; keep track of multi-value widget types in one place
[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
5a42c101 205 /** Convenience function that identifies which types of
206 widgets are stored as (serialized) array values. */
207 function is_multiple_valued() {
208 return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
209 || $this->type == SMOPT_TYPE_STRLIST_MULTI
210 || $this->type == SMOPT_TYPE_EDIT_LIST);
211 }
212
b4856b14 213 /**
214 * Set the value for this option.
215 * @param mixed $value
216 */
cbe5423b 217 function setValue($value) {
218 $this->value = $value;
219 }
220
b4856b14 221 /**
222 * Set the new value for this option.
223 * @param mixed $new_value
224 */
cbe5423b 225 function setNewValue($new_value) {
226 $this->new_value = $new_value;
9962527a 227 }
44ef0f47 228
b4856b14 229 /**
230 * Set the size for this option.
231 * @param integer $size
232 */
bbcafebd 233 function setSize($size) {
234 $this->size = $size;
235 }
236
b4856b14 237 /**
238 * Set the trailing_text for this option.
239 * @param string $trailing_text
240 */
361d6e1b 241 function setTrailingText($trailing_text) {
242 $this->trailing_text = $trailing_text;
243 }
244
b4856b14 245 /**
246 * Set the comment for this option.
247 * @param string $comment
248 */
bbcafebd 249 function setComment($comment) {
250 $this->comment = $comment;
251 }
252
b4856b14 253 /**
0177059f 254 * Set the extra attributes for this option.
255 * @param array $aExtraAttribs
b4856b14 256 */
0177059f 257 function setExtraAttributes($aExtraAttribs) {
258 $this->aExtraAttribs = $aExtraAttribs;
cbe5423b 259 }
260
b4856b14 261 /**
262 * Set the "post script" for this option.
263 * @param string $post_script
264 */
6ae9e729 265 function setPostScript($post_script) {
266 $this->post_script = $post_script;
267 }
268
b4856b14 269 /**
270 * Set the save function for this option.
271 * @param string $save_function
272 */
cbe5423b 273 function setSaveFunction($save_function) {
274 $this->save_function = $save_function;
275 }
276
b4856b14 277 /**
99ecf044 278 * Set the trailing_text for this option.
279 * @param string $folder_filter
280 * @since 1.5.1
281 */
282 function setFolderFilter($folder_filter) {
283 $this->folder_filter = $folder_filter;
284 }
285
286 /**
b4856b14 287 * Creates fields on option pages according to option type
288 *
9786ea94 289 * This is the function that calls all other createWidget* functions.
290 *
291 * @return string The formated option field
292 *
b4856b14 293 */
9786ea94 294 function createWidget() {
ce68b76b 295 global $color;
cbe5423b 296
62f7daa5 297 // Use new value if available
74e44765 298 if (!empty($this->new_value)) {
299 $tempValue = $this->value;
300 $this->value = $this->new_value;
301 }
302
cbe5423b 303 /* Get the widget for this option type. */
a3ec3c91 304 switch ($this->type) {
305 case SMOPT_TYPE_STRING:
37a3ed17 306 $result = $this->createWidget_String();
a3ec3c91 307 break;
308 case SMOPT_TYPE_STRLIST:
37a3ed17 309 $result = $this->createWidget_StrList();
a3ec3c91 310 break;
7e6d5ea3 311 case SMOPT_TYPE_TEXTAREA:
37a3ed17 312 $result = $this->createWidget_TextArea();
a3ec3c91 313 break;
314 case SMOPT_TYPE_INTEGER:
37a3ed17 315 $result = $this->createWidget_Integer();
a3ec3c91 316 break;
317 case SMOPT_TYPE_FLOAT:
37a3ed17 318 $result = $this->createWidget_Float();
a3ec3c91 319 break;
320 case SMOPT_TYPE_BOOLEAN:
37a3ed17 321 $result = $this->createWidget_Boolean();
a3ec3c91 322 break;
2a50fbd7 323 case SMOPT_TYPE_HIDDEN:
37a3ed17 324 $result = $this->createWidget_Hidden();
a3ec3c91 325 break;
bbcafebd 326 case SMOPT_TYPE_COMMENT:
37a3ed17 327 $result = $this->createWidget_Comment();
bbcafebd 328 break;
be2d5495 329 case SMOPT_TYPE_FLDRLIST:
37a3ed17 330 $result = $this->createWidget_FolderList();
be2d5495 331 break;
42b7c9d4 332 case SMOPT_TYPE_FLDRLIST_MULTI:
333 $result = $this->createWidget_FolderList(TRUE);
334 break;
38d93650 335 case SMOPT_TYPE_EDIT_LIST:
336 $result = $this->createWidget_EditList();
337 break;
40268626 338 case SMOPT_TYPE_STRLIST_MULTI:
339 $result = $this->createWidget_StrList(TRUE);
340 break;
a3ec3c91 341 default:
fb8c4296 342 error_box (
343 sprintf(_("Option Type '%s' Not Found"), $this->type)
344 );
a3ec3c91 345 }
346
6ae9e729 347 /* Add the "post script" for this option. */
348 $result .= $this->post_script;
62f7daa5 349
74e44765 350 // put correct value back if need be
351 if (!empty($this->new_value)) {
352 $this->value = $tempValue;
353 }
354
a3ec3c91 355 /* Now, return the created widget. */
9786ea94 356 return $result;
a3ec3c91 357 }
358
b4856b14 359 /**
360 * Create string field
361 * @return string html formated option field
362 */
37a3ed17 363 function createWidget_String() {
bbcafebd 364 switch ($this->size) {
88cb1b4d 365 case SMOPT_SIZE_TINY:
366 $width = 5;
367 break;
368 case SMOPT_SIZE_SMALL:
369 $width = 12;
370 break;
371 case SMOPT_SIZE_LARGE:
372 $width = 38;
373 break;
374 case SMOPT_SIZE_HUGE:
375 $width = 50;
376 break;
bbcafebd 377 case SMOPT_SIZE_NORMAL:
88cb1b4d 378 default:
379 $width = 25;
bbcafebd 380 }
381
0177059f 382 return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
a3ec3c91 383 }
384
b4856b14 385 /**
386 * Create selection box
0177059f 387 *
388 * When $this->htmlencoded is TRUE, the keys and values in
389 * $this->possible_values are assumed to be display-safe.
390 * Use with care!
391 *
40268626 392 * @param boolean $multiple_select When TRUE, the select widget
393 * will allow multiple selections
394 * (OPTIONAL; default is FALSE
395 * (single select list))
396 *
b4856b14 397 * @return string html formated selection box
40268626 398 *
b4856b14 399 */
40268626 400 function createWidget_StrList($multiple_select=FALSE) {
98e88751 401//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 402
403 switch ($this->size) {
404//FIXME: not sure about these sizes... seems like we could add another on the "large" side...
405 case SMOPT_SIZE_TINY:
406 $height = 3;
407 break;
408 case SMOPT_SIZE_SMALL:
409 $height = 8;
410 break;
411 case SMOPT_SIZE_LARGE:
412 $height = 15;
413 break;
414 case SMOPT_SIZE_HUGE:
415 $height = 25;
416 break;
417 case SMOPT_SIZE_NORMAL:
418 default:
419 $height = 5;
420 }
421
422 return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text);
a3ec3c91 423
a3ec3c91 424 }
425
b4856b14 426 /**
427 * Create folder selection box
42b7c9d4 428 *
429 * @param boolean $multiple_select When TRUE, the select widget
430 * will allow multiple selections
431 * (OPTIONAL; default is FALSE
432 * (single select list))
433 *
b4856b14 434 * @return string html formated selection box
42b7c9d4 435 *
b4856b14 436 */
42b7c9d4 437 function createWidget_FolderList($multiple_select=FALSE) {
be2d5495 438
38d93650 439 switch ($this->size) {
440//FIXME: not sure about these sizes... seems like we could add another on the "large" side...
441 case SMOPT_SIZE_TINY:
442 $height = 3;
443 break;
444 case SMOPT_SIZE_SMALL:
445 $height = 8;
446 break;
447 case SMOPT_SIZE_LARGE:
448 $height = 15;
449 break;
450 case SMOPT_SIZE_HUGE:
451 $height = 25;
452 break;
453 case SMOPT_SIZE_NORMAL:
454 default:
455 $height = 5;
456 }
457
0177059f 458 // possible values might include a nested array of
459 // possible values (list of folders)
460 //
461 $option_list = array();
462 foreach ($this->possible_values as $value => $text) {
62f7daa5 463
0177059f 464 // list of folders (boxes array)
465 //
466 if (is_array($text)) {
42b7c9d4 467 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
62f7daa5 468
0177059f 469 // just one option here
470 //
471 } else {
472 $option_list = array_merge($option_list, array($value => $text));
be2d5495 473 }
0177059f 474
62f7daa5 475 }
0177059f 476 if (empty($option_list))
477 $option_list = array('ignore' => _("unavailable"));
99ecf044 478
479
38d93650 480 return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
0177059f 481
be2d5495 482 }
483
b4856b14 484 /**
485 * Creates textarea
486 * @return string html formated textarea field
487 */
37a3ed17 488 function createWidget_TextArea() {
bbcafebd 489 switch ($this->size) {
490 case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
491 case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
492 case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
493 case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
494 case SMOPT_SIZE_NORMAL:
495 default: $rows = 5; $cols = 50;
496 }
ba556ce5 497 return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
a3ec3c91 498 }
499
b4856b14 500 /**
501 * Creates field for integer
502 *
503 * Difference from createWidget_String is visible only when javascript is enabled
504 * @return string html formated option field
505 */
37a3ed17 506 function createWidget_Integer() {
0d08ea5a 507
b65d1a08 508 // add onChange javascript handler to a regular string widget
509 // which will strip out all non-numeric chars
83aff890 510 if (checkForJavascript())
0177059f 511 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 512 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
513 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
0177059f 514 . 'this.value=newVal;';
515
516 return $this->createWidget_String();
a3ec3c91 517 }
518
b4856b14 519 /**
520 * Creates field for floating number
521 * Difference from createWidget_String is visible only when javascript is enabled
522 * @return string html formated option field
523 */
37a3ed17 524 function createWidget_Float() {
37a3ed17 525
b65d1a08 526 // add onChange javascript handler to a regular string widget
62f7daa5 527 // which will strip out all non-numeric (period also OK) chars
83aff890 528 if (checkForJavascript())
0177059f 529 $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
b65d1a08 530 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
531 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
0177059f 532 . 'newVal += origVal.charAt(i); } this.value=newVal;';
533
534 return $this->createWidget_String();
a3ec3c91 535 }
536
b4856b14 537 /**
538 * Creates radio field (yes/no)
539 * @return string html formated radio field
540 */
37a3ed17 541 function createWidget_Boolean() {
0177059f 542
5f88daeb 543 global $oTemplate, $nbsp;
fd87494d 544
545 /* Build the yes choice. */
0177059f 546 $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 547
548 /* Build the no choice. */
0177059f 549 $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 550
551 /* Build and return the combined "boolean widget". */
0177059f 552 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
fd87494d 553 return ($result);
a3ec3c91 554 }
555
b4856b14 556 /**
557 * Creates hidden field
558 * @return string html formated hidden input field
559 */
37a3ed17 560 function createWidget_Hidden() {
0177059f 561 return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
a3ec3c91 562 }
563
b4856b14 564 /**
565 * Creates comment
566 * @return string comment
567 */
37a3ed17 568 function createWidget_Comment() {
bbcafebd 569 $result = $this->comment;
570 return ($result);
571 }
572
b4856b14 573 /**
38d93650 574 * Creates an edit list
575 * @return string html formated list of edit fields and
576 * their associated controls
577 */
578 function createWidget_EditList() {
579
cb606dfd 580 global $oTemplate;
38d93650 581
582 switch ($this->size) {
583//FIXME: not sure about these sizes... seems like we could add another on the "large" side...
584 case SMOPT_SIZE_TINY:
585 $height = 3;
586 break;
587 case SMOPT_SIZE_SMALL:
588 $height = 8;
589 break;
590 case SMOPT_SIZE_LARGE:
591 $height = 15;
592 break;
593 case SMOPT_SIZE_HUGE:
594 $height = 25;
595 break;
596 case SMOPT_SIZE_NORMAL:
597 default:
598 $height = 5;
599 }
600
dd6f9627 601 if (empty($this->possible_values)) $this->possible_values = array();
2ebfc729 602 if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
dd6f9627 603
cb606dfd 604//FIXME: $this->aExtraAttribs probably should only be used in one place
605 $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
606 $oTemplate->assign('trailing_text', $this->trailing_text);
607 $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));
608 $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
609 $oTemplate->assign('name', $this->name);
610 return $oTemplate->fetch('edit_list_widget.tpl');
38d93650 611
612 }
613
614 /**
b4856b14 615 *
616 */
cbe5423b 617 function save() {
618 $function = $this->save_function;
619 $function($this);
44ef0f47 620 }
cbe5423b 621
b4856b14 622 /**
623 *
624 */
cbe5423b 625 function changed() {
38d93650 626
627 // edit lists have a lot going on, so we'll always process them
628 //
629 if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
630
6206f6c4 631 return ($this->value != $this->new_value);
cbe5423b 632 }
b4856b14 633} /* End of SquirrelOption class*/
cbe5423b 634
b4856b14 635/**
f2aba536 636 * Saves the option value (this is the default save function
637 * unless overridden by the user)
638 *
b4856b14 639 * @param object $option object that holds option name and new_value
640 */
cbe5423b 641function save_option($option) {
f2aba536 642
643 // Can't save the pref if we don't have the username
644 //
dac16606 645 if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
dac16606 646 return;
0b97a708 647 }
f2aba536 648
0b97a708 649 global $data_dir;
f2aba536 650
38d93650 651 // edit lists: first add new elements to list, then
652 // remove any selected ones (note that we must add
653 // before deleting because the javascript that populates
654 // the "add" textbox when selecting items in the list
655 // (for deletion))
656 //
657 if ($option->type == SMOPT_TYPE_EDIT_LIST) {
658
8eb63bb6 659 if (empty($option->possible_values)) $option->possible_values = array();
95dbbd91 660 if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
8eb63bb6 661
38d93650 662 // add element if given
663 //
664 if (sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
665 $new_element = trim($new_element);
666 if (!empty($new_element)
667 && !in_array($new_element, $option->possible_values))
668 $option->possible_values[] = $new_element;
669 }
670
671 // delete selected elements if needed
672 //
673 if (is_array($option->new_value)
674 && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
675 $option->possible_values = array_diff($option->possible_values, $option->new_value);
676
677 // save full list (stored in "possible_values")
678 //
679 setPref($data_dir, $username, $option->name, serialize($option->possible_values));
680
f2aba536 681 // Certain option types need to be serialized because
682 // they are not scalar
683 //
5a42c101 684 } else if ($option->is_multiple_valued())
f2aba536 685 setPref($data_dir, $username, $option->name, serialize($option->new_value));
38d93650 686
f2aba536 687 else
688 setPref($data_dir, $username, $option->name, $option->new_value);
689
cbe5423b 690}
691
b4856b14 692/**
693 * save function that does not save
694 * @param object $option
695 */
cbe5423b 696function save_option_noop($option) {
697 /* Do nothing here... */
9962527a 698}
44ef0f47 699
b4856b14 700/**
701 * Create hidden 'optpage' input field with value set by argument
702 * @param string $optpage identification of option page
703 * @return string html formated hidden input field
704 */
cbe5423b 705function create_optpage_element($optpage) {
0177059f 706 return addHidden('optpage', $optpage);
cbe5423b 707}
708
b4856b14 709/**
710 * Create hidden 'optmode' input field with value set by argument
711 * @param string $optmode
712 * @return string html formated hidden input field
713 */
cbe5423b 714function create_optmode_element($optmode) {
0177059f 715 return addHidden('optmode', $optmode);
cbe5423b 716}
717
b4856b14 718/**
719 * @param array $optgrps
720 * @param array $optvals
721 * @return array
722 */
cbe5423b 723function create_option_groups($optgrps, $optvals) {
a3ec3c91 724 /* Build a simple array with which to start. */
725 $result = array();
726
bbcafebd 727 /* Create option group for each option group name. */
728 foreach ($optgrps as $grpkey => $grpname) {
729 $result[$grpkey] = array();
730 $result[$grpkey]['name'] = $grpname;
731 $result[$grpkey]['options'] = array();
732 }
733
a3ec3c91 734 /* Create a new SquirrelOption for each set of option values. */
bbcafebd 735 foreach ($optvals as $grpkey => $grpopts) {
736 foreach ($grpopts as $optset) {
28520c87 737 /* Create a new option with all values given. */
738 $next_option = new SquirrelOption(
7390e240 739 $optset['name'],
740 $optset['caption'],
741 $optset['type'],
742 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
743 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
744 (isset($optset['posvals']) ? $optset['posvals'] : ''),
745 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
746 );
bbcafebd 747
748 /* If provided, set the size for this option. */
749 if (isset($optset['size'])) {
750 $next_option->setSize($optset['size']);
751 }
752
361d6e1b 753 /* If provided, set the trailing_text for this option. */
754 if (isset($optset['trailing_text'])) {
755 $next_option->setTrailingText($optset['trailing_text']);
756 }
757
bbcafebd 758 /* If provided, set the comment for this option. */
759 if (isset($optset['comment'])) {
760 $next_option->setComment($optset['comment']);
761 }
762
cbe5423b 763 /* If provided, set the save function for this option. */
764 if (isset($optset['save'])) {
765 $next_option->setSaveFunction($optset['save']);
766 }
767
0177059f 768 /* If provided, set the extra attributes for this option. */
769 if (isset($optset['extra_attributes'])) {
770 $next_option->setExtraAttributes($optset['extra_attributes']);
cbe5423b 771 }
772
6ae9e729 773 /* If provided, set the "post script" for this option. */
774 if (isset($optset['post_script'])) {
775 $next_option->setPostScript($optset['post_script']);
776 }
777
99ecf044 778 /* If provided, set the folder_filter for this option. */
779 if (isset($optset['folder_filter'])) {
780 $next_option->setFolderFilter($optset['folder_filter']);
781 }
782
bbcafebd 783 /* Add this option to the option array. */
784 $result[$grpkey]['options'][] = $next_option;
a3ec3c91 785 }
786 }
787
788 /* Return our resulting array. */
789 return ($result);
790}
791