| 1 | <?php | 
| 2 | /** | 
| 3 | * This file contains the FormContent class. | 
| 4 | * | 
| 5 | * $Id: FormContent.inc,v 1.58.2.6 2005/05/12 01:24:02 hemna Exp $ | 
| 6 | * | 
| 7 | * @author Walter A. Boring IV <waboring@newsblob.com> | 
| 8 | * @author Suren Markossian <suren@cbestwhat?> | 
| 9 | * @package phpHtmlLib | 
| 10 | * @subpackage FormProcessing | 
| 11 | * | 
| 12 | * @copyright LGPL - See LICENCE | 
| 13 | * | 
| 14 | */ | 
| 15 |  | 
| 16 | /** | 
| 17 | * Make sure we include the Processor | 
| 18 | */ | 
| 19 | require_once($phphtmllib."/form/FormProcessor.inc"); | 
| 20 |  | 
| 21 | /** | 
| 22 | * This class is used to build and render the form. | 
| 23 | * It builds a form by creating FormElement objects | 
| 24 | * which have automatic validation.  It leaves the | 
| 25 | * layout of the form up to the child class.  It has | 
| 26 | * a mechanism in place to automagically show a | 
| 27 | * confirmation 'page' after the data has been submitted | 
| 28 | * and validated.  It also provides a hook for any | 
| 29 | * 'back end' validation of data.  Finally, it provides | 
| 30 | * a function for handling the action of the form, which | 
| 31 | * only gets call after ALL validation has passed. | 
| 32 | * | 
| 33 | * Functions: | 
| 34 | * | 
| 35 | * form_init_elements() - This function is used to | 
| 36 | *   build the FormElement objects that will be used | 
| 37 | *   by the form.  This function is called EVERY time | 
| 38 | *   the FormContent class is instantiated.  After you | 
| 39 | *   create the FormElement, you call the | 
| 40 | *   FormContent::add_element() method, to add the | 
| 41 | *   FormElement object to the form.  You will then | 
| 42 | *   call the 2 methods FormContent::element_label() and | 
| 43 | *   FormContent::element_form() to get access to the | 
| 44 | *   FormElement's label and form field respectively. | 
| 45 | * | 
| 46 | * form_init_data() - This is called only the first time | 
| 47 | *   the form is encountered.  It Allows you to populate | 
| 48 | *   the FormElements with data from a DB for example. | 
| 49 | *   You would use FormContent::set_element_value() or | 
| 50 | *   FormContent::set_hidden_element_value() inside here. | 
| 51 | * | 
| 52 | * form() - This is the method that gets called to build | 
| 53 | *   the layout for your form.  Typically you use a table | 
| 54 | *   and add the label in the first <td> and the form field | 
| 55 | *   itself in the 2nd <td>.  So there are 2 methods in | 
| 56 | *   the FormContent object that allow u to get access to | 
| 57 | *   the FormElements label, and form field. | 
| 58 | *   FormContent::element_label() and | 
| 59 | *   FormContent::element_form(). | 
| 60 | * | 
| 61 | * | 
| 62 | * form_backend_validation() - This method enables you to | 
| 63 | *   do any "back end" validation of data.  Such as, check | 
| 64 | *   for a duplicate in the DB on a create/new form.  This | 
| 65 | *   is called after the FormElement's validation methods have | 
| 66 | *   passed. | 
| 67 | * | 
| 68 | * form_action() - This method is called after ALL validation | 
| 69 | *   was successfull, including each FormElement object's | 
| 70 | *   validation methods, as well as the FormContent::form_backend_validation() | 
| 71 | * | 
| 72 | * | 
| 73 | * | 
| 74 | * @package phpHtmlLib | 
| 75 | * @subpackage FormProcessing | 
| 76 | */ | 
| 77 | class FormContent { | 
| 78 |  | 
| 79 | /** | 
| 80 | * This holds the name of the form | 
| 81 | * for js that needs it | 
| 82 | */ | 
| 83 | var $_form_name; | 
| 84 |  | 
| 85 |  | 
| 86 | /** | 
| 87 | * This holds the array of | 
| 88 | * hidden form elements | 
| 89 | * used in this form | 
| 90 | */ | 
| 91 | var $_hidden_elements = array(); | 
| 92 |  | 
| 93 | /** | 
| 94 | * This holds the array of | 
| 95 | * non-hidden elements | 
| 96 | */ | 
| 97 | var $_elements = array(); | 
| 98 |  | 
| 99 |  | 
| 100 | /** | 
| 101 | * This holds the default | 
| 102 | * css class for form field | 
| 103 | * label text. | 
| 104 | * | 
| 105 | */ | 
| 106 | var $_default_label_css = "formlabel"; | 
| 107 |  | 
| 108 | /** | 
| 109 | * This is the css class used | 
| 110 | * for fields that have an error | 
| 111 | * | 
| 112 | */ | 
| 113 | var $_error_label_css = "formlabelerror"; | 
| 114 |  | 
| 115 | /** | 
| 116 | * flag to let the FormProcessor | 
| 117 | * object know this form has a | 
| 118 | * confirmation page that is required | 
| 119 | */ | 
| 120 | var $_has_confirm = FALSE; | 
| 121 |  | 
| 122 |  | 
| 123 | /** | 
| 124 | * Holds the width to be used for the | 
| 125 | * error table | 
| 126 | * DEFAULT: 95% | 
| 127 | * | 
| 128 | */ | 
| 129 | var $_width = "600"; | 
| 130 |  | 
| 131 | /** | 
| 132 | * Holds the width (if any) of the errors | 
| 133 | * table that will be rendered.  If the | 
| 134 | * value is null, the $this->_width value | 
| 135 | * is used instead. | 
| 136 | */ | 
| 137 | var $_form_errors_width = null; | 
| 138 |  | 
| 139 |  | 
| 140 | /** | 
| 141 | * The form errors table title | 
| 142 | */ | 
| 143 | var $_form_errors_title = "Form Errors"; | 
| 144 |  | 
| 145 |  | 
| 146 | /** | 
| 147 | * The message that is set | 
| 148 | * during the form_action | 
| 149 | */ | 
| 150 | var $_action_message = ""; | 
| 151 |  | 
| 152 |  | 
| 153 | /** | 
| 154 | * The action to take opon clicking | 
| 155 | * the "Cancel" button | 
| 156 | */ | 
| 157 | var $_cancel_action = NULL; | 
| 158 |  | 
| 159 |  | 
| 160 | /** | 
| 161 | * Text to show denoted required | 
| 162 | * fields for the form. | 
| 163 | * | 
| 164 | */ | 
| 165 | var $_required_field_text = " - required field"; | 
| 166 |  | 
| 167 | /** | 
| 168 | * marker for the required field | 
| 169 | * | 
| 170 | */ | 
| 171 | var $_required_field_marker = "*"; | 
| 172 |  | 
| 173 | /** | 
| 174 | * The form Element's flag to add the : | 
| 175 | * character at the end of the label. | 
| 176 | */ | 
| 177 | var $_label_colon_flag = FALSE; | 
| 178 |  | 
| 179 | /** | 
| 180 | * This holds how many actions we have | 
| 181 | * for this form content | 
| 182 | */ | 
| 183 | var $_action_counter = 0; | 
| 184 |  | 
| 185 | /** | 
| 186 | * Automatically strip slashes from | 
| 187 | * form values? | 
| 188 | */ | 
| 189 | var $_stripslashes = FALSE; | 
| 190 |  | 
| 191 | /** | 
| 192 | * Flag to mark this as having a | 
| 193 | * File Form Element (child of FEFile) | 
| 194 | */ | 
| 195 | var $_has_file_element = FALSE; | 
| 196 |  | 
| 197 | /** | 
| 198 | * index names of all of the file | 
| 199 | * form elements (if any). | 
| 200 | */ | 
| 201 | var $_file_elements = array(); | 
| 202 |  | 
| 203 |  | 
| 204 | /** | 
| 205 | * The onsubmit value for the form | 
| 206 | * tag.  FormElement childs can | 
| 207 | * automatically add to this | 
| 208 | * by implementing the | 
| 209 | * form_tag_onsubmit() method | 
| 210 | */ | 
| 211 | var $_form_on_submit = ''; | 
| 212 |  | 
| 213 | /** | 
| 214 | * The onsubmit value for the form | 
| 215 | * tag. This is used by form action | 
| 216 | * elements to add disable on submit | 
| 217 | */ | 
| 218 | var $_form_action_elements_on_submit = ''; | 
| 219 |  | 
| 220 | /** | 
| 221 | * This is the FormValidation object | 
| 222 | * used to validate the form elements | 
| 223 | */ | 
| 224 | var $_FormValidation = NULL; | 
| 225 |  | 
| 226 |  | 
| 227 | /** | 
| 228 | * Indicates this entire FormContent | 
| 229 | * is in readonly mode.  This forces | 
| 230 | * all of the FormElement to be in | 
| 231 | * readonly mode. | 
| 232 | * | 
| 233 | */ | 
| 234 | var $_is_readonly = FALSE; | 
| 235 |  | 
| 236 |  | 
| 237 | function FormContent($width="100%", $cancel_action=NULL) { | 
| 238 | $this->set_form_width( $width ); | 
| 239 | $this->set_cancel_action( $cancel_action ); | 
| 240 | } | 
| 241 |  | 
| 242 | /** | 
| 243 | * This method is what is called to | 
| 244 | * build the list of FormElements that | 
| 245 | * will be used by this form. | 
| 246 | * | 
| 247 | */ | 
| 248 | function form_init_elements() { | 
| 249 | user_error("FormContent::form_init_elements() - Child class must override"); | 
| 250 | } | 
| 251 |  | 
| 252 |  | 
| 253 | /** | 
| 254 | * This method is called by the | 
| 255 | * Form Processor to allow this | 
| 256 | * class to do any fetching of | 
| 257 | * data to prepopulate the | 
| 258 | * form field values. | 
| 259 | * You typically use this to | 
| 260 | * read data from a DB. | 
| 261 | * | 
| 262 | * This method is only called once | 
| 263 | * when the form is first hit. | 
| 264 | * | 
| 265 | * NOTE: you should build the data | 
| 266 | *       and save it in $this->_init_data | 
| 267 | */ | 
| 268 | function form_init_data() { | 
| 269 | $this->_init_data = array(); | 
| 270 | } | 
| 271 |  | 
| 272 |  | 
| 273 | /** | 
| 274 | * This method builds the html form. | 
| 275 | * It is up to the child class to define | 
| 276 | * the layout of the form and return it | 
| 277 | * in a phpHtmllib container. | 
| 278 | * | 
| 279 | * @return Container | 
| 280 | */ | 
| 281 | function form() { | 
| 282 | user_error("FormContent::form() - Child class must override"); | 
| 283 | return NULL; | 
| 284 | } | 
| 285 |  | 
| 286 | /** | 
| 287 | * This method lets you provide any javascript | 
| 288 | * that is associated with the form content. | 
| 289 | * The FormProcessor will automatically call | 
| 290 | * this and wrap it in a script tag. | 
| 291 | * | 
| 292 | * @return string - raw js. | 
| 293 | */ | 
| 294 | function javascript() { | 
| 295 | return NULL; | 
| 296 | } | 
| 297 |  | 
| 298 | /** | 
| 299 | * This method allows this class to do any | 
| 300 | * data munging prior to the form_confirm | 
| 301 | * method being called @ render time. | 
| 302 | * | 
| 303 | * @return TRUE = success | 
| 304 | *         FALSE if u want to trigger an error | 
| 305 | */ | 
| 306 | function pre_confirm() { | 
| 307 | return TRUE; | 
| 308 | } | 
| 309 |  | 
| 310 | /** | 
| 311 | * This is a 'hidden' version of pre_confirm | 
| 312 | * to allow calling the form elements pre_confirm | 
| 313 | * methods.  This is needed for forms that have | 
| 314 | * a confirmation requirement and has file inputs. | 
| 315 | * If the file input FormElement doesn't save off | 
| 316 | * the uploaded file, then the confirmation page | 
| 317 | * will not get the file. apache may nuke the temp | 
| 318 | * file in /tmp. | 
| 319 | * | 
| 320 | */ | 
| 321 | function _pre_confirm() { | 
| 322 | foreach( $this->_file_elements as $name ) { | 
| 323 | $this->_elements[$name]->_pre_confirm(); | 
| 324 | } | 
| 325 | } | 
| 326 |  | 
| 327 |  | 
| 328 | /** | 
| 329 | * This function is used to show an intermediary | 
| 330 | * confirmation page.  Use this function to | 
| 331 | * show a confirmation of the data that was | 
| 332 | * submitted by the user. | 
| 333 | * This will get called after all of the | 
| 334 | * form data was successfully validated. | 
| 335 | * All of the form data will automatically | 
| 336 | * be created as hidden form fields. All you | 
| 337 | * have to do is show the data, and a confirm | 
| 338 | * submit button. | 
| 339 | * | 
| 340 | * @param string - the title for the table | 
| 341 | * @param boolean - show the action buttons? | 
| 342 | * @return mixed - either raw html, or some | 
| 343 | *                 container HTMLTag object. | 
| 344 | */ | 
| 345 | function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) { | 
| 346 | $table = new InfoTable($title, $this->_width); | 
| 347 |  | 
| 348 | $this->build_confirm_table( $table ); | 
| 349 |  | 
| 350 | //now add the confirmation button | 
| 351 | $td = new TDtag(array("colspan" => 2, | 
| 352 | "class" => "contentnovertical", | 
| 353 | "align" => "center"), | 
| 354 | $this->add_action("Confirm")); | 
| 355 |  | 
| 356 | if ($this->_cancel_action) { | 
| 357 | $td->add(_HTML_SPACE, $this->add_cancel()); | 
| 358 | } | 
| 359 |  | 
| 360 | if ($show_buttons) { | 
| 361 | $table->add_row( $td ); | 
| 362 | } | 
| 363 |  | 
| 364 | return $table; | 
| 365 | } | 
| 366 |  | 
| 367 |  | 
| 368 | /** | 
| 369 | * This method allows the child to ovveride the | 
| 370 | * default confirm data.  By default the form_confirm() | 
| 371 | * will show ALL FormElements.  This is prolly not good | 
| 372 | * in case of a form where a password exists. | 
| 373 | * | 
| 374 | * @param InfoTable object | 
| 375 | */ | 
| 376 | function build_confirm_table( &$table ) { | 
| 377 | foreach( $this->_elements as $label => $element) { | 
| 378 | $c = container(_HTML_SPACE, $element->get_value_text()); | 
| 379 | $c->set_collapse(); | 
| 380 | $div = html_div("", $element->get_label($this) ); | 
| 381 | $div->set_style("white-space:nowrap;"); | 
| 382 | $table->add_row( $div, $c); | 
| 383 | } | 
| 384 | } | 
| 385 |  | 
| 386 | /** | 
| 387 | * This method is called after the FormElements | 
| 388 | * have all been validated, and the form_confirm | 
| 389 | * has been confirmed.  It enables the form to | 
| 390 | * validate any data against a DB or other backend | 
| 391 | * processing.  This will always get called | 
| 392 | * before the form_action method is called to ensure | 
| 393 | * that all form data is valid before form_action() | 
| 394 | * is called. | 
| 395 | * | 
| 396 | * | 
| 397 | * @return boolean TRUE if successfull | 
| 398 | *                 FALSE if errors were detected. | 
| 399 | */ | 
| 400 | function form_backend_validation() { | 
| 401 | return TRUE; | 
| 402 | } | 
| 403 |  | 
| 404 |  | 
| 405 | /** | 
| 406 | * This method handles the | 
| 407 | * action (submit button) that | 
| 408 | * was submitted.  This method | 
| 409 | * only gets called AFTER all | 
| 410 | * data has been validated. | 
| 411 | * This includes the backend | 
| 412 | * validation.  If the form | 
| 413 | * has the confirmation on, | 
| 414 | * then this method will be | 
| 415 | * called after the confirmation | 
| 416 | * has been accepted. | 
| 417 | * | 
| 418 | * NOTE : return TRUE if the action | 
| 419 | *        was succesfully handled. | 
| 420 | *        If FALSE is returned, the | 
| 421 | *        form will be displayed again | 
| 422 | *        with the error message. | 
| 423 | * | 
| 424 | * @param array - array of errors if any | 
| 425 | * @return boolean TRUE = success | 
| 426 | *                 FALSE = failed. | 
| 427 | */ | 
| 428 | function form_action() { | 
| 429 | user_error("FormContent::form_action() - Child class ". | 
| 430 | "must override"); | 
| 431 | return FALSE; | 
| 432 | } | 
| 433 |  | 
| 434 |  | 
| 435 | /** | 
| 436 | * This method is called when the form_action() | 
| 437 | * was successfull, and the form wants to render | 
| 438 | * some kind of message | 
| 439 | * | 
| 440 | * @return mixed | 
| 441 | */ | 
| 442 | function form_success() { | 
| 443 | if ($this->_action_message != "") { | 
| 444 | return container($this->_action_message, html_br(2)); | 
| 445 | } else { | 
| 446 | return NULL; | 
| 447 | } | 
| 448 | } | 
| 449 |  | 
| 450 | /** | 
| 451 | * This function is used to render the error | 
| 452 | * table for the form.  The error data comes | 
| 453 | * from the FormProcessor or the FormValidation. | 
| 454 | * NOTE: You can override this method to | 
| 455 | *       show a customized error message(s) | 
| 456 | * | 
| 457 | * @return TABLEtag object. | 
| 458 | */ | 
| 459 | function form_errors() { | 
| 460 | $table = new ErrorBoxWidget($this->_form_errors_title, | 
| 461 | $this->get_form_errors_width()); | 
| 462 | $errors = FALSE; | 
| 463 |  | 
| 464 | //walk each visible form element and see if there is an error in it | 
| 465 | foreach( $this->_elements as $label => $element ) { | 
| 466 | if ($element->has_error()) { | 
| 467 | $e_errors = $element->get_errors(); | 
| 468 | foreach( $e_errors as $err ) { | 
| 469 | $table->add($err['label'], $err['message']); | 
| 470 | } | 
| 471 | $errors = TRUE; | 
| 472 | } | 
| 473 | } | 
| 474 | if ($errors) { | 
| 475 | return $table; | 
| 476 | } else { | 
| 477 | return NULL; | 
| 478 | } | 
| 479 | } | 
| 480 |  | 
| 481 |  | 
| 482 | /** | 
| 483 | * This method returns an array of errors | 
| 484 | * for all the errors. | 
| 485 | * | 
| 486 | * @return array | 
| 487 | */ | 
| 488 | function get_error_array() { | 
| 489 | $ret = array(); | 
| 490 | //walk each visible form element and see if there is an error in it | 
| 491 | foreach( $this->_elements as $label => $element ) { | 
| 492 | if ($element->has_error()) { | 
| 493 | $errors = $element->get_errors(); | 
| 494 | foreach ( $errors as $err ) { | 
| 495 | $ret[$err['label']] = $err['message']; | 
| 496 | } | 
| 497 | } | 
| 498 | } | 
| 499 | return $ret; | 
| 500 | } | 
| 501 |  | 
| 502 |  | 
| 503 | /*****************************/ | 
| 504 | /*     form element methods  */ | 
| 505 | /*****************************/ | 
| 506 |  | 
| 507 | /** | 
| 508 | * This method is used to add a | 
| 509 | * form element | 
| 510 | * | 
| 511 | * @param FormElement object | 
| 512 | */ | 
| 513 | function add_element( &$element ) { | 
| 514 | $element->set_stripslashes( $this->_stripslashes ); | 
| 515 | //in case the element needs it for js | 
| 516 | $element->set_form_name( $this->_form_name ); | 
| 517 | $name = $element->get_label_text(); | 
| 518 | $this->_elements[$name] = &$element; | 
| 519 |  | 
| 520 | //do we have a File form element? | 
| 521 | if (is_a($element, 'fefile')) { | 
| 522 | $this->_has_file_element = TRUE; | 
| 523 | $this->_file_elements[] = $name; | 
| 524 | } | 
| 525 | if ($element->_has_form_on_submit) { | 
| 526 | $this->_form_on_submit .= $element->form_tag_onsubmit(); | 
| 527 | } | 
| 528 |  | 
| 529 | if ($this->_label_colon_flag) { | 
| 530 | $element->set_colon_flag(TRUE); | 
| 531 | } | 
| 532 | } | 
| 533 |  | 
| 534 | /** | 
| 535 | * This method is used to add a | 
| 536 | * hidden form field | 
| 537 | * | 
| 538 | * @param FormElement object | 
| 539 | */ | 
| 540 | function add_hidden_element( $label, $value=NULL ) { | 
| 541 | $element = new FEHidden( $label, $value ); | 
| 542 | $element->set_stripslashes( $this->_stripslashes ); | 
| 543 | $element->set_form_name( $this->_form_name ); | 
| 544 | $this->_hidden_elements[$label] = &$element; | 
| 545 | } | 
| 546 |  | 
| 547 | /** | 
| 548 | * This function adds a form submit button | 
| 549 | * with the appropriate action. | 
| 550 | * | 
| 551 | * @param string - the labe/action for the submit | 
| 552 | *                 button. | 
| 553 | * @param bool disable the button | 
| 554 | * @param string - the onSubmit function (if any) | 
| 555 | * @param boolean - disable opon submit? | 
| 556 | * @return INPUTtag object | 
| 557 | */ | 
| 558 | function add_action_element($label, $disabled = false, $submit_function = false, | 
| 559 | $disable_on_submit = TRUE) { | 
| 560 |  | 
| 561 | $style="vertical-align:middle;"; | 
| 562 | // here we mantain a fixed button size if a label length is reasonably small | 
| 563 | // i.e. OK or Cancel buttons or use padding otherwise | 
| 564 | if (strlen($label)<8)   $style .= "width:100px;"; | 
| 565 | else $style     .= "padding-left:5px;padding-right:5px;"; | 
| 566 |  | 
| 567 | $button_name = FORM_ACTION . $this->_action_counter++; | 
| 568 | $button_id = $this->_form_name . "action_" . $button_name; | 
| 569 |  | 
| 570 | $attributes = array("id" => $button_id, | 
| 571 | "onclick" =>"this.form." . FORM_ACTION . ".value='".$label."';", | 
| 572 | "style" => $style); | 
| 573 |  | 
| 574 | if ($disabled) $attributes[] = "disabled"; | 
| 575 |  | 
| 576 | $this->_action_elements[$label] = form_submit($button_name, $label, $attributes); | 
| 577 |  | 
| 578 | if ($disable_on_submit) { | 
| 579 | $this->_form_action_elements_on_submit .= "if (document.getElementById('" . $button_id . "')) document.getElementById('" . $button_id . "').disabled=true;"; | 
| 580 | } | 
| 581 | } | 
| 582 |  | 
| 583 | /** | 
| 584 | * This function returns a submit action | 
| 585 | * by label | 
| 586 | * | 
| 587 | * @param string label | 
| 588 | * | 
| 589 | * @return object | 
| 590 | */ | 
| 591 | function get_action_element($label) { | 
| 592 | return $this->_action_elements[$label]; | 
| 593 | } | 
| 594 |  | 
| 595 | /** | 
| 596 | * This method returns the label object | 
| 597 | * for a visible form element. | 
| 598 | * | 
| 599 | * @param string - the element's label | 
| 600 | * @return Object | 
| 601 | */ | 
| 602 | function element_label($label) { | 
| 603 | $this->_test_element($label, "element_label"); | 
| 604 | return $this->_elements[$label]->get_label($this); | 
| 605 | } | 
| 606 |  | 
| 607 | /** | 
| 608 | * This method returns the label object | 
| 609 | * for a visible form element. | 
| 610 | * | 
| 611 | * @param string - the element's label | 
| 612 | * @return Object | 
| 613 | */ | 
| 614 | function hidden_element_label($label) { | 
| 615 | $this->_test_element($label, "element_label", TRUE); | 
| 616 | return $this->_hidden_elements[$label]->get_label($this); | 
| 617 | } | 
| 618 |  | 
| 619 | /** | 
| 620 | * This method returns the actual form | 
| 621 | * object that renders the form field. | 
| 622 | * Such as an INPUTtag object. | 
| 623 | * | 
| 624 | * @param string - the element's label | 
| 625 | * @return Object | 
| 626 | */ | 
| 627 | function element_form($label) { | 
| 628 | $this->_test_element($label, "element_form"); | 
| 629 | return $this->_elements[$label]->get_form_element($this->is_readonly()); | 
| 630 | } | 
| 631 |  | 
| 632 | /** | 
| 633 | * This method returns the FormElement | 
| 634 | * based on the label. | 
| 635 | * | 
| 636 | * @param string - the element's label | 
| 637 | * @return Object | 
| 638 | */ | 
| 639 | function &get_element($label) { | 
| 640 | $this->_test_element($label, "get_element"); | 
| 641 | return $this->_elements[$label]; | 
| 642 | } | 
| 643 |  | 
| 644 | /** | 
| 645 | * This method is used to set | 
| 646 | * the value for a non hidden | 
| 647 | * element | 
| 648 | * | 
| 649 | * @param string - the form label | 
| 650 | * @param value - the new value | 
| 651 | */ | 
| 652 | function set_element_value($label, $value) { | 
| 653 | $this->_test_element($label, "set_element_value"); | 
| 654 | $this->_elements[$label]->set_value($value); | 
| 655 | } | 
| 656 |  | 
| 657 | /** | 
| 658 | * This method is used to get | 
| 659 | * the value for a non hidden | 
| 660 | * element | 
| 661 | * | 
| 662 | * @param string - the form label | 
| 663 | * @return value - the new value | 
| 664 | */ | 
| 665 | function get_element_value($label) { | 
| 666 | $this->_test_element($label, "get_element_value"); | 
| 667 | return $this->_elements[$label]->get_value(); | 
| 668 | } | 
| 669 |  | 
| 670 | /** | 
| 671 | * This method is used to set | 
| 672 | * the value for a hidden element | 
| 673 | * | 
| 674 | * @param string - the form label | 
| 675 | * @param value - the new value | 
| 676 | */ | 
| 677 | function set_hidden_element_value($label, $value) { | 
| 678 | $this->_test_element($label, "set_hidden_element_value", TRUE); | 
| 679 | $this->_hidden_elements[$label]->set_value( $value ); | 
| 680 | } | 
| 681 |  | 
| 682 | /** | 
| 683 | * This method is used to get | 
| 684 | * the value for a hidden element | 
| 685 | * | 
| 686 | * @param string - the form label | 
| 687 | * @return value - the new value | 
| 688 | */ | 
| 689 | function get_hidden_element_value($label) { | 
| 690 | $this->_test_element($label, "get_hidden_element_value", TRUE); | 
| 691 | return $this->_hidden_elements[$label]->get_value(); | 
| 692 | } | 
| 693 |  | 
| 694 | /** | 
| 695 | * This method is a helper method to set the | 
| 696 | * FormElement's tabindex. | 
| 697 | * | 
| 698 | * @param string - the form label | 
| 699 | * @param int - the FormElement's tabindex | 
| 700 | * @return none | 
| 701 | */ | 
| 702 | function set_form_tabindex($label, $index) { | 
| 703 | $this->_test_element($label, "set_form_tabindex"); | 
| 704 | $this->_elements[$label]->set_tabindex($index); | 
| 705 | } | 
| 706 |  | 
| 707 | /** | 
| 708 | * This method is a helper method to get the | 
| 709 | * FormElement's tabindex. | 
| 710 | * | 
| 711 | * @param string - the form label | 
| 712 | * @param int - the FormElement's tabindex | 
| 713 | * @return the FormElement's current tabindex | 
| 714 | */ | 
| 715 | function get_form_tabindex($label, $index) { | 
| 716 | $this->_test_element($label, "set_form_tabindex"); | 
| 717 | return $this->_elements[$label]->get_tabindex(); | 
| 718 | } | 
| 719 |  | 
| 720 | /** | 
| 721 | * This method is used to test | 
| 722 | * if the element exists in a form | 
| 723 | * | 
| 724 | * @param string - the form label | 
| 725 | * @return bool | 
| 726 | */ | 
| 727 | function element_exists($label) { | 
| 728 | return (isset($this->_elements[$label]) || isset($this->_hidden_elements[$label])); | 
| 729 | } | 
| 730 |  | 
| 731 | /** | 
| 732 | * This method is used to create a new error element | 
| 733 | * during the call to form_action().  This enables us | 
| 734 | * to do error handling during a transaction into | 
| 735 | * a DB. | 
| 736 | * | 
| 737 | * @param string - the label | 
| 738 | * @param string - the error message | 
| 739 | */ | 
| 740 | function add_error( $label, $message ) { | 
| 741 | if (isset($this->_elements[$label])) { | 
| 742 | $this->_elements[$label]->set_error_message($message); | 
| 743 | } else { | 
| 744 | $this->add_element( new FEError($label, $message) ); | 
| 745 | } | 
| 746 | } | 
| 747 |  | 
| 748 |  | 
| 749 | /** | 
| 750 | * This sets the readonly flag. | 
| 751 | * When this flag is set, all of the | 
| 752 | * FormElements will be set to readonly. | 
| 753 | * | 
| 754 | * @param bool TRUE = readonly | 
| 755 | */ | 
| 756 | function set_readonly($flag=TRUE) { | 
| 757 | $this->_is_readonly = $flag; | 
| 758 | } | 
| 759 |  | 
| 760 | /** | 
| 761 | * Is this element in read only mode? | 
| 762 | * | 
| 763 | * @return bool | 
| 764 | */ | 
| 765 | function is_readonly() { | 
| 766 | return $this->_is_readonly; | 
| 767 | } | 
| 768 |  | 
| 769 |  | 
| 770 | /*****************************/ | 
| 771 | /*       Util methods        */ | 
| 772 | /*****************************/ | 
| 773 |  | 
| 774 | function set_form_name($name) { | 
| 775 | $this->_form_name = $name; | 
| 776 | } | 
| 777 |  | 
| 778 | function get_form_name() { | 
| 779 | return $this->_form_name; | 
| 780 | } | 
| 781 |  | 
| 782 |  | 
| 783 | /** | 
| 784 | * Save the action for the form | 
| 785 | * | 
| 786 | * @param string - the action from the post. | 
| 787 | */ | 
| 788 | function set_action($action) { | 
| 789 | $this->action = $action; | 
| 790 | } | 
| 791 |  | 
| 792 | /** | 
| 793 | * Get the current status of the | 
| 794 | * action. | 
| 795 | * | 
| 796 | * @return string | 
| 797 | */ | 
| 798 | function get_action() { | 
| 799 | return $this->action; | 
| 800 | } | 
| 801 |  | 
| 802 | /** | 
| 803 | * this method sets the form name | 
| 804 | * | 
| 805 | * @param string - the form name | 
| 806 | */ | 
| 807 | function set_form_width($width) { | 
| 808 | $this->_width = $width; | 
| 809 | } | 
| 810 |  | 
| 811 |  | 
| 812 | /** | 
| 813 | * This method returns the width | 
| 814 | * of the form errors table. | 
| 815 | * By default it is supposed to be | 
| 816 | * the same width as the form itself. | 
| 817 | * | 
| 818 | * @return int | 
| 819 | */ | 
| 820 | function get_form_errors_width() { | 
| 821 | if ($this->_form_errors_width == null) { | 
| 822 | return $this->_width; | 
| 823 | } else { | 
| 824 | return $this->_form_errors_width; | 
| 825 | } | 
| 826 | } | 
| 827 |  | 
| 828 | /** | 
| 829 | * This method allows you to override | 
| 830 | * the width of the form errors table. | 
| 831 | * By default it uses the width of the | 
| 832 | * form as its size. | 
| 833 | * | 
| 834 | * @param string - the width | 
| 835 | */ | 
| 836 | function set_form_errors_width($width) { | 
| 837 | $this->_form_errors_width = $width; | 
| 838 | } | 
| 839 |  | 
| 840 |  | 
| 841 | /** | 
| 842 | * This allows us to change the form errors | 
| 843 | * table title | 
| 844 | * | 
| 845 | * @param string - the new title | 
| 846 | */ | 
| 847 | function set_form_errors_title($title) { | 
| 848 | $this->_form_errors_title = $title; | 
| 849 | } | 
| 850 |  | 
| 851 |  | 
| 852 |  | 
| 853 |  | 
| 854 | /** | 
| 855 | * This function is used to set the | 
| 856 | * default CSS class used on | 
| 857 | * form field text when there is no | 
| 858 | * error on that field | 
| 859 | * | 
| 860 | * @param string - the css class to use | 
| 861 | */ | 
| 862 | function set_default_css( $css ) { | 
| 863 | $this->_default_label_css = $css; | 
| 864 | } | 
| 865 |  | 
| 866 | /** | 
| 867 | * This function returns the | 
| 868 | * default css class used for | 
| 869 | * NON error text. | 
| 870 | * | 
| 871 | * @return string - the current default css | 
| 872 | */ | 
| 873 | function get_default_css() { | 
| 874 | return $this->_default_label_css; | 
| 875 | } | 
| 876 |  | 
| 877 |  | 
| 878 | /** | 
| 879 | * This function is used to set the | 
| 880 | * css class that is used on text | 
| 881 | * when an error on that field is | 
| 882 | * detected. | 
| 883 | * | 
| 884 | * @param string - the css class to use. | 
| 885 | */ | 
| 886 | function set_error_css( $css ) { | 
| 887 | $this->_err_label_css = $css; | 
| 888 | } | 
| 889 |  | 
| 890 |  | 
| 891 | /** | 
| 892 | * This sets the $this->_has_confirmation | 
| 893 | * flag.  to let the object know it has | 
| 894 | * a required confirmation page, which | 
| 895 | * should get called after the validation | 
| 896 | * is successfull, and before the action is | 
| 897 | * handled on the back-end. | 
| 898 | * | 
| 899 | * @param boolean - the flag value TRUE/FALSE | 
| 900 | */ | 
| 901 | function set_confirm( $flag = TRUE ) { | 
| 902 | $this->_has_confirm = $flag; | 
| 903 | } | 
| 904 |  | 
| 905 | /** | 
| 906 | * This gets the value of the confirmation flag. | 
| 907 | * which tells the caller that this object | 
| 908 | * has/doesn't have a required confirmation | 
| 909 | * page. | 
| 910 | * | 
| 911 | * @return boolean | 
| 912 | */ | 
| 913 | function has_confirm() { | 
| 914 | return $this->_has_confirm; | 
| 915 | } | 
| 916 |  | 
| 917 |  | 
| 918 | /** | 
| 919 | * This sets the stripslashes flag for | 
| 920 | * this object. | 
| 921 | * | 
| 922 | * @param boolean | 
| 923 | */ | 
| 924 | function set_stripslashes( $flag = TRUE ) { | 
| 925 | $this->_stripslashes = $flag; | 
| 926 | } | 
| 927 |  | 
| 928 | /** | 
| 929 | * This sets the action message. | 
| 930 | * This is called from withint the | 
| 931 | * form_action() method | 
| 932 | * | 
| 933 | * @param string - the action message | 
| 934 | */ | 
| 935 | function set_action_message($message) { | 
| 936 | $this->_action_message = $message; | 
| 937 | } | 
| 938 |  | 
| 939 |  | 
| 940 | /** | 
| 941 | * This method sets the javasript action | 
| 942 | * to be taken when the cancel button | 
| 943 | * is clicked.  Calling this method | 
| 944 | * w/ a non NULL value automatically enables | 
| 945 | * the Cancel button. | 
| 946 | * | 
| 947 | * @param string - the javascript cancel action | 
| 948 | */ | 
| 949 | function set_cancel_action($action) { | 
| 950 | $this->_cancel_action = $action; | 
| 951 | } | 
| 952 |  | 
| 953 | /** | 
| 954 | * This method sets the flag to have ALL of the | 
| 955 | * FormElement's labels be appended with a : | 
| 956 | * character.  Some folks like this for some | 
| 957 | * completely unexplained reason. | 
| 958 | */ | 
| 959 | function set_colon_flag($flag=TRUE) { | 
| 960 | $this->_label_colon_flag = $flag; | 
| 961 | } | 
| 962 |  | 
| 963 |  | 
| 964 |  | 
| 965 | /** | 
| 966 | * This function adds a form submit button | 
| 967 | * with the appropriate action. | 
| 968 | * | 
| 969 | * @param string - the labe/action for the submit | 
| 970 | *                 button. | 
| 971 | * @param bool disable the button | 
| 972 | * @param string - the onSubmit function (if any) | 
| 973 | * @param boolean - disable opon submit? | 
| 974 | * @return INPUTtag object | 
| 975 | */ | 
| 976 | function add_action( $label, $disabled = false, $submit_function = false, | 
| 977 | $disable_on_submit = TRUE ) { | 
| 978 | $style="vertical-align:middle;"; | 
| 979 | // here we mantain a fixed button size if a label length is reasonably small | 
| 980 | // i.e. OK or Cancel buttons or use padding otherwise | 
| 981 | if ( strlen($label)<8 ) $style .= "width: 90px;"; | 
| 982 | else $style     .= "padding-left: 5px;padding-right:5px;"; | 
| 983 |  | 
| 984 | // Unique button label | 
| 985 | if (!$this->_action_counter) { | 
| 986 | $this->_action_counter = 1; | 
| 987 | } | 
| 988 | $blabel = FORM_ACTION.$this->_action_counter; | 
| 989 | $this->_action_counter++; | 
| 990 |  | 
| 991 | // The onclick is responsible for: | 
| 992 | // -disabling the action button to discourage stupid users | 
| 993 | // -setting the action in the _form_action hidden variable | 
| 994 | // -calling the onSubmit function (if any), since form.submit() will not trigger | 
| 995 | //  the registered onSubmit function | 
| 996 | // -call the form.submit() function | 
| 997 |  | 
| 998 | //build the action | 
| 999 | $onclick = ""; | 
| 1000 | if ($disable_on_submit) { | 
| 1001 | $onclick .= "this.form.".$blabel.".disabled=true;"; | 
| 1002 | } | 
| 1003 | $onclick .= "this.form.".FORM_ACTION.".value='".$label."';"; | 
| 1004 | if ($submit_function) { | 
| 1005 | $onclick .= $submit_function.";"; | 
| 1006 | } | 
| 1007 |  | 
| 1008 | if ($disable_on_submit) { | 
| 1009 | $onclick .= "this.form.submit();"; | 
| 1010 | } | 
| 1011 |  | 
| 1012 | $attrib = array("style" => $style, | 
| 1013 | "onclick" => $onclick); | 
| 1014 |  | 
| 1015 | if ($disabled) $attrib[] = "disabled"; | 
| 1016 | return form_submit($blabel, $label, $attrib); | 
| 1017 | } | 
| 1018 |  | 
| 1019 |  | 
| 1020 | /** | 
| 1021 | * This function adds a submit button that | 
| 1022 | * can have any label.  It just makes the | 
| 1023 | * _form_action a hidden field. | 
| 1024 | * NOTE: you can only do this ONCE per form. | 
| 1025 | * | 
| 1026 | * @param string - the label of the button. | 
| 1027 | * @param string - the action value. | 
| 1028 | * @param boolean - disable opon submit? | 
| 1029 | * @return ContainerWidget object | 
| 1030 | */ | 
| 1031 | function add_hidden_action( $label, $action, $disable_on_submit=TRUE) { | 
| 1032 | $container = new ContainerWidget; | 
| 1033 | // Unique button label | 
| 1034 | if (!$this->_action_counter) { | 
| 1035 | $this->_action_counter = 1; | 
| 1036 | } | 
| 1037 | $blabel = FORM_ACTION.$this->_action_counter; | 
| 1038 | $this->_action_counter++; | 
| 1039 |  | 
| 1040 | //build the action | 
| 1041 | $onclick = ""; | 
| 1042 | if ($disable_on_submit) { | 
| 1043 | $onclick .= "this.form.".$blabel.".disabled=true;"; | 
| 1044 | } | 
| 1045 | $onclick .= "this.form.".FORM_ACTION.".value='".$action."';"; | 
| 1046 | $onclick .= "this.form.submit();"; | 
| 1047 |  | 
| 1048 | $style = "padding-left: 5px;padding-right:5px;"; | 
| 1049 | $attrib = array("style" => $style, | 
| 1050 | "onclick" => $onclick); | 
| 1051 |  | 
| 1052 | $container->push(form_submit($blabel, $label, | 
| 1053 | array("style" => $style, | 
| 1054 | "onclick" => $onclick))); | 
| 1055 | return $container; | 
| 1056 | } | 
| 1057 |  | 
| 1058 | /** | 
| 1059 | * This function adds an action as an image to | 
| 1060 | * submit the form. | 
| 1061 | * | 
| 1062 | * @param string - the image name path | 
| 1063 | * @param string - the action | 
| 1064 | * @param boolean - disable opon submit? | 
| 1065 | * | 
| 1066 | * @return Atag object | 
| 1067 | */ | 
| 1068 | function add_image_action( $image_name, $action, $disable_on_submit=TRUE ) { | 
| 1069 | // Unique button label | 
| 1070 | if (!$this->_action_counter) { | 
| 1071 | $this->_action_counter = 1; | 
| 1072 | } | 
| 1073 | $blabel = "_form_action".$this->_action_counter; | 
| 1074 | $this->_action_counter++; | 
| 1075 |  | 
| 1076 | $onclick = "document.".$this->_form_name."._form_action.value='".$action."';"; | 
| 1077 | $onclick .= "document.".$this->_form_name.".submit();"; | 
| 1078 |  | 
| 1079 | if ($disable_on_submit) { | 
| 1080 | $click = "javascript:if (!window.global_cntr) {window.global_cntr = 1;} else {window.global_cntr++;} "; | 
| 1081 | $click .= " if(window.global_cntr<2) {"; | 
| 1082 | $click .= $onclick."}"; | 
| 1083 | } | 
| 1084 |  | 
| 1085 | $img = html_img($image_name); | 
| 1086 | $img->set_tag_attribute("border", 0 ); | 
| 1087 | $img->set_tag_attribute("style", "cursor:hand;"); | 
| 1088 |  | 
| 1089 | $link = html_a("#", $img); | 
| 1090 | $link->set_tag_attribute("onclick", $click); | 
| 1091 | return $link; | 
| 1092 | } | 
| 1093 |  | 
| 1094 |  | 
| 1095 | /** | 
| 1096 | * build a cancel button with a url | 
| 1097 | * to go to | 
| 1098 | * | 
| 1099 | * @param string - the cancel action | 
| 1100 | * @return form button | 
| 1101 | */ | 
| 1102 | function add_cancel() { | 
| 1103 |  | 
| 1104 | $cancel_button = form_button("cancel","Cancel", | 
| 1105 | array("type"=>"button", | 
| 1106 | "style"=>"width: 90px;", | 
| 1107 | "onclick"=> | 
| 1108 | "javascript:document.location='".$this->_cancel_action."'")); | 
| 1109 | $cancel_button->set_style("vertical-align:middle"); | 
| 1110 | return $cancel_button; | 
| 1111 | } | 
| 1112 |  | 
| 1113 | /** | 
| 1114 | * This function is used to set the required field marker | 
| 1115 | * | 
| 1116 | * @param string - the marker | 
| 1117 | */ | 
| 1118 | function set_required_marker($marker) { | 
| 1119 | $this->_required_field_marker = $marker; | 
| 1120 | } | 
| 1121 |  | 
| 1122 | /** | 
| 1123 | * This function is used to get the required field marker | 
| 1124 | * | 
| 1125 | * @return string - the marker | 
| 1126 | */ | 
| 1127 | function get_required_marker() { | 
| 1128 | if (is_object($this->_required_field_marker)) { | 
| 1129 | return $this->_required_field_marker->render(); | 
| 1130 | } else { | 
| 1131 | return $this->_required_field_marker; | 
| 1132 | } | 
| 1133 | } | 
| 1134 |  | 
| 1135 | /** | 
| 1136 | * This sets the required text | 
| 1137 | * | 
| 1138 | * @param string | 
| 1139 | */ | 
| 1140 | function set_required_text($text) { | 
| 1141 | $this->_required_field_text = $text; | 
| 1142 |  | 
| 1143 | } | 
| 1144 |  | 
| 1145 |  | 
| 1146 | /** | 
| 1147 | * This method tests to see if we | 
| 1148 | * have an element named $label, | 
| 1149 | * so we can operate on it. | 
| 1150 | * | 
| 1151 | * @param string - the element's label | 
| 1152 | * @param string - the name of the method that called us | 
| 1153 | * @param boolean - test a hidden element? | 
| 1154 | */ | 
| 1155 | function _test_element( $label, $method, $hidden=FALSE ) { | 
| 1156 | if ($hidden) { | 
| 1157 | if (!isset($this->_hidden_elements[$label])) { | 
| 1158 | trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); | 
| 1159 | } | 
| 1160 | } else { | 
| 1161 | if (!isset($this->_elements[$label])) { | 
| 1162 | trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); | 
| 1163 | } | 
| 1164 | } | 
| 1165 | } | 
| 1166 |  | 
| 1167 | /** | 
| 1168 | * return the values of all visible form elements as an array | 
| 1169 | * | 
| 1170 | * @access public | 
| 1171 | * @return array | 
| 1172 | * | 
| 1173 | */ | 
| 1174 | function get_all_element_values() | 
| 1175 | { | 
| 1176 |  | 
| 1177 | foreach ($this->_elements as $element) { | 
| 1178 | $data[$element->get_element_name()] = $element->get_value(); | 
| 1179 | } | 
| 1180 |  | 
| 1181 | return $data; | 
| 1182 |  | 
| 1183 | } | 
| 1184 |  | 
| 1185 | /** | 
| 1186 | * This method is used to keep a local reference | 
| 1187 | * of the FormProcessor's FormValidation object | 
| 1188 | * | 
| 1189 | * @param FormValidation object | 
| 1190 | */ | 
| 1191 | function _set_validation_object(&$FormValidation) { | 
| 1192 | $this->_FormValidation =& $FormValidation; | 
| 1193 | } | 
| 1194 |  | 
| 1195 | } | 
| 1196 | ?> |