| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains the FormContent class. |
| 4 |
* |
| 5 |
* $Id: FormContent.inc,v 1.47 2004/03/25 21:02:57 hemna Exp $ |
| 6 |
* |
| 7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.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 |
* This holds how many actions we have |
| 175 |
* for this form content |
| 176 |
*/ |
| 177 |
var $_action_counter = 0; |
| 178 |
|
| 179 |
/** |
| 180 |
* Automatically strip slashes from |
| 181 |
* form values? |
| 182 |
*/ |
| 183 |
var $_stripslashes = FALSE; |
| 184 |
|
| 185 |
/** |
| 186 |
* Flag to mark this as having a |
| 187 |
* File Form Element (child of FEFile) |
| 188 |
*/ |
| 189 |
var $_has_file_element = FALSE; |
| 190 |
|
| 191 |
/** |
| 192 |
* index names of all of the file |
| 193 |
* form elements (if any). |
| 194 |
*/ |
| 195 |
var $_file_elements = array(); |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* The onsubmit value for the form |
| 200 |
* tag. FormElement childs can |
| 201 |
* automatically add to this |
| 202 |
* by implementing the |
| 203 |
* form_tag_onsubmit() method |
| 204 |
*/ |
| 205 |
var $_form_on_submit = ''; |
| 206 |
|
| 207 |
|
| 208 |
function FormContent($width="100%", $cancel_action=NULL) { |
| 209 |
$this->set_form_width( $width ); |
| 210 |
$this->set_cancel_action( $cancel_action ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* This method is what is called to |
| 215 |
* build the list of FormElements that |
| 216 |
* will be used by this form. |
| 217 |
* |
| 218 |
*/ |
| 219 |
function form_init_elements() { |
| 220 |
user_error("FormContent::form_init_elements() - Child class must override"); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* This method is called by the |
| 226 |
* Form Processor to allow this |
| 227 |
* class to do any fetching of |
| 228 |
* data to prepopulate the |
| 229 |
* form field values. |
| 230 |
* You typically use this to |
| 231 |
* read data from a DB. |
| 232 |
* |
| 233 |
* This method is only called once |
| 234 |
* when the form is first hit. |
| 235 |
* |
| 236 |
* NOTE: you should build the data |
| 237 |
* and save it in $this->_init_data |
| 238 |
*/ |
| 239 |
function form_init_data() { |
| 240 |
$this->_init_data = array(); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* This method builds the html form. |
| 246 |
* It is up to the child class to define |
| 247 |
* the layout of the form and return it |
| 248 |
* in a phpHtmllib container. |
| 249 |
* |
| 250 |
* @return Container |
| 251 |
*/ |
| 252 |
function form() { |
| 253 |
user_error("FormContent::form() - Child class must override"); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* This method lets you provide any javascript |
| 259 |
* that is associated with the form content. |
| 260 |
* The FormProcessor will automatically call |
| 261 |
* this and wrap it in a script tag. |
| 262 |
* |
| 263 |
* @return string - raw js. |
| 264 |
*/ |
| 265 |
function javascript() { |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* This method allows this class to do any |
| 271 |
* data munging prior to the form_confirm |
| 272 |
* method being called @ render time. |
| 273 |
* |
| 274 |
* @return TRUE = success |
| 275 |
* FALSE if u want to trigger an error |
| 276 |
*/ |
| 277 |
function pre_confirm() { |
| 278 |
return TRUE; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* This is a 'hidden' version of pre_confirm |
| 283 |
* to allow calling the form elements pre_confirm |
| 284 |
* methods. This is needed for forms that have |
| 285 |
* a confirmation requirement and has file inputs. |
| 286 |
* If the file input FormElement doesn't save off |
| 287 |
* the uploaded file, then the confirmation page |
| 288 |
* will not get the file. apache may nuke the temp |
| 289 |
* file in /tmp. |
| 290 |
* |
| 291 |
*/ |
| 292 |
function _pre_confirm() { |
| 293 |
foreach( $this->_file_elements as $name ) { |
| 294 |
$this->_elements[$name]->_pre_confirm(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/** |
| 300 |
* This function is used to show an intermediary |
| 301 |
* confirmation page. Use this function to |
| 302 |
* show a confirmation of the data that was |
| 303 |
* submitted by the user. |
| 304 |
* This will get called after all of the |
| 305 |
* form data was successfully validated. |
| 306 |
* All of the form data will automatically |
| 307 |
* be created as hidden form fields. All you |
| 308 |
* have to do is show the data, and a confirm |
| 309 |
* submit button. |
| 310 |
* |
| 311 |
* @param string - the title for the table |
| 312 |
* @param boolean - show the action buttons? |
| 313 |
* @return mixed - either raw html, or some |
| 314 |
* container HTMLTag object. |
| 315 |
*/ |
| 316 |
function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) { |
| 317 |
$table = new InfoTable($title, $this->_width); |
| 318 |
|
| 319 |
$this->build_confirm_table( $table ); |
| 320 |
|
| 321 |
//now add the confirmation button |
| 322 |
$td = new TDtag(array("colspan" => 2, |
| 323 |
"class" => "contentnovertical", |
| 324 |
"align" => "center"), |
| 325 |
$this->add_action("Confirm")); |
| 326 |
|
| 327 |
if ($this->_cancel_action) { |
| 328 |
$td->add(_HTML_SPACE, $this->add_cancel()); |
| 329 |
} |
| 330 |
|
| 331 |
if ($show_buttons) { |
| 332 |
$table->add_row( $td ); |
| 333 |
} |
| 334 |
|
| 335 |
return $table; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
* This method allows the child to ovveride the |
| 341 |
* default confirm data. By default the form_confirm() |
| 342 |
* will show ALL FormElements. This is prolly not good |
| 343 |
* in case of a form where a password exists. |
| 344 |
* |
| 345 |
* @param InfoTable object |
| 346 |
*/ |
| 347 |
function build_confirm_table( &$table ) { |
| 348 |
foreach( $this->_elements as $label => $element) { |
| 349 |
$c = container(_HTML_SPACE,_HTML_SPACE, |
| 350 |
$element->get_value_text()); |
| 351 |
$c->set_collapse(); |
| 352 |
$div = html_div("", $element->get_label() ); |
| 353 |
$div->set_style("white-space:nowrap;"); |
| 354 |
$table->add_row( $div, $c); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* This method is called after the FormElements |
| 360 |
* have all been validated, and the form_confirm |
| 361 |
* has been confirmed. It enables the form to |
| 362 |
* validate any data against a DB or other backend |
| 363 |
* processing. This will always get called |
| 364 |
* before the form_action method is called to ensure |
| 365 |
* that all form data is valid before form_action() |
| 366 |
* is called. |
| 367 |
* |
| 368 |
* |
| 369 |
* @return boolean TRUE if successfull |
| 370 |
* FALSE if errors were detected. |
| 371 |
*/ |
| 372 |
function form_backend_validation() { |
| 373 |
return TRUE; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* This method handles the |
| 379 |
* action (submit button) that |
| 380 |
* was submitted. This method |
| 381 |
* only gets called AFTER all |
| 382 |
* data has been validated. |
| 383 |
* This includes the backend |
| 384 |
* validation. If the form |
| 385 |
* has the confirmation on, |
| 386 |
* then this method will be |
| 387 |
* called after the confirmation |
| 388 |
* has been accepted. |
| 389 |
* |
| 390 |
* NOTE : return TRUE if the action |
| 391 |
* was succesfully handled. |
| 392 |
* If FALSE is returned, the |
| 393 |
* form will be displayed again |
| 394 |
* with the error message. |
| 395 |
* |
| 396 |
* @param array - array of errors if any |
| 397 |
* @return boolean TRUE = success |
| 398 |
* FALSE = failed. |
| 399 |
*/ |
| 400 |
function form_action() { |
| 401 |
user_error("FormContent::form_action() - Child class ". |
| 402 |
"must override"); |
| 403 |
return FALSE; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/** |
| 408 |
* This method is called when the form_action() |
| 409 |
* was successfull, and the form wants to render |
| 410 |
* some kind of message |
| 411 |
* |
| 412 |
* @return mixed |
| 413 |
*/ |
| 414 |
function form_success() { |
| 415 |
if ($this->_action_message != "") { |
| 416 |
return container($this->_action_message, html_br(2)); |
| 417 |
} else { |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* This function is used to render the error |
| 424 |
* table for the form. The error data comes |
| 425 |
* from the FormProcessor or the FormValidation. |
| 426 |
* NOTE: You can override this method to |
| 427 |
* show a customized error message(s) |
| 428 |
* |
| 429 |
* @return TABLEtag object. |
| 430 |
*/ |
| 431 |
function form_errors() { |
| 432 |
$table = new InfoTable($this->_form_errors_title, $this->get_form_errors_width()); |
| 433 |
$errors = FALSE; |
| 434 |
|
| 435 |
//walk each visible form element and see if there is an error in it |
| 436 |
foreach( $this->_elements as $label => $element ) { |
| 437 |
if ($element->has_error()) { |
| 438 |
$e_errors = $element->get_errors(); |
| 439 |
foreach( $e_errors as $err ) { |
| 440 |
$span = new SPANtag( array("style" => "padding-right:10px;white-space:nowrap;"), |
| 441 |
$err['label'] ); |
| 442 |
$table->add_row($span, $err['message']); |
| 443 |
} |
| 444 |
|
| 445 |
$errors = TRUE; |
| 446 |
} |
| 447 |
} |
| 448 |
if ($errors) { |
| 449 |
return $table; |
| 450 |
} else { |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
/** |
| 457 |
* This method returns an array of errors |
| 458 |
* for all the errors. |
| 459 |
* |
| 460 |
* @return array |
| 461 |
*/ |
| 462 |
function get_error_array() { |
| 463 |
$ret = array(); |
| 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 |
$errors = $element->get_errors(); |
| 468 |
foreach ( $errors as $err ) { |
| 469 |
$ret[$err['label']] = $err['message']; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
return $ret; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
/*****************************/ |
| 478 |
/* form element methods */ |
| 479 |
/*****************************/ |
| 480 |
|
| 481 |
/** |
| 482 |
* This method is used to add a |
| 483 |
* form element |
| 484 |
* |
| 485 |
* @param FormElement object |
| 486 |
*/ |
| 487 |
function add_element( &$element ) { |
| 488 |
$element->set_stripslashes( $this->_stripslashes ); |
| 489 |
//in case the element needs it for js |
| 490 |
$element->set_form_name( $this->_form_name ); |
| 491 |
$name = $element->get_label_text(); |
| 492 |
$this->_elements[$name] = &$element; |
| 493 |
|
| 494 |
//do we have a File form element? |
| 495 |
if (is_a($element, 'fefile')) { |
| 496 |
$this->_has_file_element = TRUE; |
| 497 |
$this->_file_elements[] = $name; |
| 498 |
} |
| 499 |
if ($element->_has_form_on_submit) { |
| 500 |
$this->_form_on_submit .= $element->form_tag_onsubmit(); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* This method is used to add a |
| 506 |
* hidden form field |
| 507 |
* |
| 508 |
* @param FormElement object |
| 509 |
*/ |
| 510 |
function add_hidden_element( $label, $value=NULL ) { |
| 511 |
$element = new FEHidden( $label, $value ); |
| 512 |
$element->set_stripslashes( $this->_stripslashes ); |
| 513 |
$this->_hidden_elements[$label] = &$element; |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* This method returns the label object |
| 518 |
* for a visible form element. |
| 519 |
* |
| 520 |
* @param string - the element's label |
| 521 |
* @return Object |
| 522 |
*/ |
| 523 |
function element_label($label) { |
| 524 |
$this->_test_element($label, "element_label"); |
| 525 |
return $this->_elements[$label]->get_label($this); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* This method returns the label object |
| 530 |
* for a visible form element. |
| 531 |
* |
| 532 |
* @param string - the element's label |
| 533 |
* @return Object |
| 534 |
*/ |
| 535 |
function hidden_element_label($label) { |
| 536 |
$this->_test_element($label, "element_label", TRUE); |
| 537 |
return $this->_hidden_elements[$label]->get_label(); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* This method returns the actual form |
| 542 |
* object that renders the form field. |
| 543 |
* Such as an INPUTtag object. |
| 544 |
* |
| 545 |
* @param string - the element's label |
| 546 |
* @return Object |
| 547 |
*/ |
| 548 |
function element_form($label) { |
| 549 |
$this->_test_element($label, "element_form"); |
| 550 |
return $this->_elements[$label]->get_element(); |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* This method returns the FormElement |
| 555 |
* based on the label. |
| 556 |
* |
| 557 |
* @param string - the element's label |
| 558 |
* @return Object |
| 559 |
*/ |
| 560 |
function &get_element($label) { |
| 561 |
$this->_test_element($label, "get_element"); |
| 562 |
return $this->_elements[$label]; |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* This method is used to set |
| 567 |
* the value for a non hidden |
| 568 |
* element |
| 569 |
* |
| 570 |
* @param string - the form label |
| 571 |
* @param value - the new value |
| 572 |
*/ |
| 573 |
function set_element_value($label, $value) { |
| 574 |
$this->_test_element($label, "set_element_value"); |
| 575 |
$this->_elements[$label]->set_value($value); |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* This method is used to get |
| 580 |
* the value for a non hidden |
| 581 |
* element |
| 582 |
* |
| 583 |
* @param string - the form label |
| 584 |
* @return value - the new value |
| 585 |
*/ |
| 586 |
function get_element_value($label) { |
| 587 |
$this->_test_element($label, "get_element_value"); |
| 588 |
return $this->_elements[$label]->get_value(); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* This method is used to set |
| 593 |
* the value for a hidden element |
| 594 |
* |
| 595 |
* @param string - the form label |
| 596 |
* @param value - the new value |
| 597 |
*/ |
| 598 |
function set_hidden_element_value($label, $value) { |
| 599 |
$this->_test_element($label, "set_hidden_element_value", TRUE); |
| 600 |
$this->_hidden_elements[$label]->set_value( $value ); |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* This method is used to get |
| 605 |
* the value for a hidden element |
| 606 |
* |
| 607 |
* @param string - the form label |
| 608 |
* @return value - the new value |
| 609 |
*/ |
| 610 |
function get_hidden_element_value($label) { |
| 611 |
$this->_test_element($label, "get_hidden_element_value", TRUE); |
| 612 |
return $this->_hidden_elements[$label]->get_value(); |
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* This method is a helper method to set the |
| 617 |
* FormElement's tabindex. |
| 618 |
* |
| 619 |
* @param string - the form label |
| 620 |
* @param int - the FormElement's tabindex |
| 621 |
* @return none |
| 622 |
*/ |
| 623 |
function set_form_tabindex($label, $index) { |
| 624 |
$this->_test_element($label, "set_form_tabindex"); |
| 625 |
$this->_elements[$label]->set_tabindex($index); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* This method is a helper method to get the |
| 630 |
* FormElement's tabindex. |
| 631 |
* |
| 632 |
* @param string - the form label |
| 633 |
* @param int - the FormElement's tabindex |
| 634 |
* @return the FormElement's current tabindex |
| 635 |
*/ |
| 636 |
function get_form_tabindex($label, $index) { |
| 637 |
$this->_test_element($label, "set_form_tabindex"); |
| 638 |
return $this->_elements[$label]->get_tabindex(); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
/** |
| 644 |
* This method is used to create a new error element |
| 645 |
* during the call to form_action(). This enables us |
| 646 |
* to do error handling during a transaction into |
| 647 |
* a DB. |
| 648 |
* |
| 649 |
* @param string - the label |
| 650 |
* @param string - the error message |
| 651 |
*/ |
| 652 |
function add_error( $label, $message ) { |
| 653 |
if (isset($this->_elements[$label])) { |
| 654 |
$this->_elements[$label]->set_error_message($message); |
| 655 |
} else { |
| 656 |
$this->add_element( new FEError($label, $message) ); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
/*****************************/ |
| 662 |
/* Util methods */ |
| 663 |
/*****************************/ |
| 664 |
|
| 665 |
function set_form_name($name) { |
| 666 |
$this->_form_name = $name; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
/** |
| 671 |
* Save the action for the form |
| 672 |
* |
| 673 |
* @param string - the action from the post. |
| 674 |
*/ |
| 675 |
function set_action($action) { |
| 676 |
$this->action = $action; |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Get the current status of the |
| 681 |
* action. |
| 682 |
* |
| 683 |
* @return string |
| 684 |
*/ |
| 685 |
function get_action() { |
| 686 |
return $this->action; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* this method sets the form name |
| 691 |
* |
| 692 |
* @param string - the form name |
| 693 |
*/ |
| 694 |
function set_form_width($width) { |
| 695 |
$this->_width = $width; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
/** |
| 700 |
* This method returns the width |
| 701 |
* of the form errors table. |
| 702 |
* By default it is supposed to be |
| 703 |
* the same width as the form itself. |
| 704 |
* |
| 705 |
* @return int |
| 706 |
*/ |
| 707 |
function get_form_errors_width() { |
| 708 |
if ($this->_form_errors_width == null) { |
| 709 |
return $this->_width; |
| 710 |
} else { |
| 711 |
return $this->_form_errors_width; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* This method allows you to override |
| 717 |
* the width of the form errors table. |
| 718 |
* By default it uses the width of the |
| 719 |
* form as its size. |
| 720 |
* |
| 721 |
* @param string - the width |
| 722 |
*/ |
| 723 |
function set_form_errors_width($width) { |
| 724 |
$this->_form_errors_width = $width; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/** |
| 729 |
* This allows us to change the form errors |
| 730 |
* table title |
| 731 |
* |
| 732 |
* @param string - the new title |
| 733 |
*/ |
| 734 |
function set_form_errors_title($title) { |
| 735 |
$this->_form_errors_title = $title; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
/** |
| 742 |
* This function is used to set the |
| 743 |
* default CSS class used on |
| 744 |
* form field text when there is no |
| 745 |
* error on that field |
| 746 |
* |
| 747 |
* @param string - the css class to use |
| 748 |
*/ |
| 749 |
function set_default_css( $css ) { |
| 750 |
$this->_default_label_css = $css; |
| 751 |
} |
| 752 |
|
| 753 |
/** |
| 754 |
* This function returns the |
| 755 |
* default css class used for |
| 756 |
* NON error text. |
| 757 |
* |
| 758 |
* @return string - the current default css |
| 759 |
*/ |
| 760 |
function get_default_css() { |
| 761 |
return $this->_default_label_css; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
/** |
| 766 |
* This function is used to set the |
| 767 |
* css class that is used on text |
| 768 |
* when an error on that field is |
| 769 |
* detected. |
| 770 |
* |
| 771 |
* @param string - the css class to use. |
| 772 |
*/ |
| 773 |
function set_error_css( $css ) { |
| 774 |
$this->_err_label_css = $css; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
/** |
| 779 |
* This sets the $this->_has_confirmation |
| 780 |
* flag. to let the object know it has |
| 781 |
* a required confirmation page, which |
| 782 |
* should get called after the validation |
| 783 |
* is successfull, and before the action is |
| 784 |
* handled on the back-end. |
| 785 |
* |
| 786 |
* @param boolean - the flag value TRUE/FALSE |
| 787 |
*/ |
| 788 |
function set_confirm( $flag = TRUE ) { |
| 789 |
$this->_has_confirm = $flag; |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* This gets the value of the confirmation flag. |
| 794 |
* which tells the caller that this object |
| 795 |
* has/doesn't have a required confirmation |
| 796 |
* page. |
| 797 |
* |
| 798 |
* @return boolean |
| 799 |
*/ |
| 800 |
function has_confirm() { |
| 801 |
return $this->_has_confirm; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
/** |
| 806 |
* This sets the stripslashes flag for |
| 807 |
* this object. |
| 808 |
* |
| 809 |
* @param boolean |
| 810 |
*/ |
| 811 |
function set_stripslashes( $flag = TRUE ) { |
| 812 |
$this->_stripslashes = $flag; |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* This sets the action message. |
| 817 |
* This is called from withint the |
| 818 |
* form_action() method |
| 819 |
* |
| 820 |
* @param string - the action message |
| 821 |
*/ |
| 822 |
function set_action_message($message) { |
| 823 |
$this->_action_message = $message; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
/** |
| 828 |
* This method sets the javasript action |
| 829 |
* to be taken when the cancel button |
| 830 |
* is clicked. Calling this method |
| 831 |
* w/ a non NULL value automatically enables |
| 832 |
* the Cancel button. |
| 833 |
* |
| 834 |
* @param string - the javascript cancel action |
| 835 |
*/ |
| 836 |
function set_cancel_action($action) { |
| 837 |
$this->_cancel_action = $action; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
/** |
| 845 |
* This function adds a form submit button |
| 846 |
* with the appropriate action. |
| 847 |
* |
| 848 |
* @param string - the labe/action for the submit |
| 849 |
* button. |
| 850 |
* @param bool disable the button |
| 851 |
* @param string - the onSubmit function (if any) |
| 852 |
* @param boolean - disable opon submit? |
| 853 |
* @return INPUTtag object |
| 854 |
*/ |
| 855 |
function add_action( $label, $disabled = false, $submit_function = false, |
| 856 |
$disable_on_submit = TRUE ) { |
| 857 |
$style="vertical-align:middle;"; |
| 858 |
// here we mantain a fixed button size if a label length is reasonably small |
| 859 |
// i.e. OK or Cancel buttons or use padding otherwise |
| 860 |
if ( strlen($label)<8 ) $style .= "width: 90px;"; |
| 861 |
else $style .= "padding-left: 5px;padding-right:5px;"; |
| 862 |
|
| 863 |
// Unique button label |
| 864 |
if (!$this->_action_counter) { |
| 865 |
$this->_action_counter = 1; |
| 866 |
} |
| 867 |
$blabel = FORM_ACTION.$this->_action_counter; |
| 868 |
$this->_action_counter++; |
| 869 |
|
| 870 |
// The onClick is responsible for: |
| 871 |
// -disabling the action button to discourage stupid users |
| 872 |
// -setting the action in the _form_action hidden variable |
| 873 |
// -calling the onSubmit function (if any), since form.submit() will not trigger |
| 874 |
// the registered onSubmit function |
| 875 |
// -call the form.submit() function |
| 876 |
|
| 877 |
//build the action |
| 878 |
$onclick = ""; |
| 879 |
if ($disable_on_submit) { |
| 880 |
$onclick .= "this.form.".$blabel.".disabled=true;"; |
| 881 |
} |
| 882 |
$onclick .= "this.form.".FORM_ACTION.".value='".$label."';"; |
| 883 |
if ($submit_function) { |
| 884 |
$onclick .= $submit_function.";"; |
| 885 |
} |
| 886 |
$onclick .= "this.form.submit();"; |
| 887 |
|
| 888 |
$attrib = array("style" => $style, |
| 889 |
"onClick" => $onclick); |
| 890 |
|
| 891 |
if ($disabled) $attrib[] = "disabled"; |
| 892 |
return form_submit($blabel, $label, $attrib); |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
/** |
| 897 |
* This function adds a submit button that |
| 898 |
* can have any label. It just makes the |
| 899 |
* _form_action a hidden field. |
| 900 |
* NOTE: you can only do this ONCE per form. |
| 901 |
* |
| 902 |
* @param string - the label of the button. |
| 903 |
* @param string - the action value. |
| 904 |
* @param boolean - disable opon submit? |
| 905 |
* @return ContainerWidget object |
| 906 |
*/ |
| 907 |
function add_hidden_action( $label, $action, $disable_on_submit=TRUE) { |
| 908 |
$container = new ContainerWidget; |
| 909 |
// Unique button label |
| 910 |
if (!$this->_action_counter) { |
| 911 |
$this->_action_counter = 1; |
| 912 |
} |
| 913 |
$blabel = FORM_ACTION.$this->_action_counter; |
| 914 |
$this->_action_counter++; |
| 915 |
|
| 916 |
//build the action |
| 917 |
$onclick = ""; |
| 918 |
if ($disable_on_submit) { |
| 919 |
$onclick .= "this.form.".$blabel.".disabled=true;"; |
| 920 |
} |
| 921 |
$onclick .= "this.form.".FORM_ACTION.".value='".$action."';"; |
| 922 |
$onclick .= "this.form.submit();"; |
| 923 |
|
| 924 |
$style = "padding-left: 5px;padding-right:5px;"; |
| 925 |
$attrib = array("style" => $style, |
| 926 |
"onClick" => $onclick); |
| 927 |
|
| 928 |
$container->push(form_submit($blabel, $label, |
| 929 |
array("style" => $style, |
| 930 |
"onClick" => $onclick))); |
| 931 |
return $container; |
| 932 |
} |
| 933 |
|
| 934 |
/** |
| 935 |
* This function adds an action as an image to |
| 936 |
* submit the form. |
| 937 |
* |
| 938 |
* @param string - the image name path |
| 939 |
* @param string - the action |
| 940 |
* @param boolean - disable opon submit? |
| 941 |
* |
| 942 |
* @return Atag object |
| 943 |
*/ |
| 944 |
function add_image_action( $image_name, $action, $disable_on_submit=TRUE ) { |
| 945 |
// Unique button label |
| 946 |
if (!$this->_action_counter) { |
| 947 |
$this->_action_counter = 1; |
| 948 |
} |
| 949 |
$blabel = "_form_action".$this->_action_counter; |
| 950 |
$this->_action_counter++; |
| 951 |
|
| 952 |
$onclick = "document.".$this->_form_name."._form_action.value='".$action."';"; |
| 953 |
$onclick .= "document.".$this->_form_name.".submit();"; |
| 954 |
|
| 955 |
if ($disable_on_submit) { |
| 956 |
$click = "javascript:if (!window.global_cntr) {window.global_cntr = 1;} else {window.global_cntr++;} "; |
| 957 |
$click .= " if(window.global_cntr<2) {"; |
| 958 |
$click .= $onclick."}"; |
| 959 |
} |
| 960 |
|
| 961 |
$img = html_img($image_name); |
| 962 |
$img->set_tag_attribute("border", 0 ); |
| 963 |
$img->set_tag_attribute("style", "cursor:hand;"); |
| 964 |
|
| 965 |
$link = html_a("#", $img); |
| 966 |
$link->set_tag_attribute("onclick", $click); |
| 967 |
return $link; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
/** |
| 972 |
* build a cancel button with a url |
| 973 |
* to go to |
| 974 |
* |
| 975 |
* @param string - the cancel action |
| 976 |
* @return form button |
| 977 |
*/ |
| 978 |
function add_cancel() { |
| 979 |
|
| 980 |
$cancel_button = form_button("cancel","Cancel", |
| 981 |
array("type"=>"button", |
| 982 |
"style"=>"width: 90px;", |
| 983 |
"onClick"=> |
| 984 |
"javascript:document.location='".$this->_cancel_action."'")); |
| 985 |
$cancel_button->set_style("vertical-align:middle"); |
| 986 |
return $cancel_button; |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* This function is used to set the required field marker |
| 991 |
* |
| 992 |
* @param string - the marker |
| 993 |
*/ |
| 994 |
function set_required_marker($marker) { |
| 995 |
$this->_required_field_marker = $marker; |
| 996 |
} |
| 997 |
|
| 998 |
/** |
| 999 |
* This sets the required text |
| 1000 |
* |
| 1001 |
* @param string |
| 1002 |
*/ |
| 1003 |
function set_required_text($text) { |
| 1004 |
$this->_required_field_text = $text; |
| 1005 |
|
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
/** |
| 1010 |
* This method tests to see if we |
| 1011 |
* have an element named $label, |
| 1012 |
* so we can operate on it. |
| 1013 |
* |
| 1014 |
* @param string - the element's label |
| 1015 |
* @param string - the name of the method that called us |
| 1016 |
* @param boolean - test a hidden element? |
| 1017 |
*/ |
| 1018 |
function _test_element( $label, $method, $hidden=FALSE ) { |
| 1019 |
if ($hidden) { |
| 1020 |
if (!isset($this->_hidden_elements[$label])) { |
| 1021 |
trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); |
| 1022 |
} |
| 1023 |
} else { |
| 1024 |
if (!isset($this->_elements[$label])) { |
| 1025 |
trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
} |
| 1032 |
?> |