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