. */ /** * Form object to display a form */ require_once(ROOTPATH."include/form/CheckboxField.php"); require_once(ROOTPATH."include/form/DateSelectField.php"); require_once(ROOTPATH."include/form/HiddenField.php"); require_once(ROOTPATH."include/form/PasswordField.php"); require_once(ROOTPATH."include/form/RadioField.php"); require_once(ROOTPATH."include/form/SelectField.php"); require_once(ROOTPATH."include/form/TextField.php"); abstract class AbstractForm { private $model; private $fields; private $hiddenFields; private $submitText = "Save"; private $resetText = NULL; private $headerText = NULL; /** * Constructor */ public function __construct(array $model) { $this->model = $model; $this->fields = array(); $this->hiddenFields = array(); } /** * Save the edited data * * @param Model to be saved */ protected abstract function save(array $model); /** * Retrun the html of the form * * @return String html statements to display the form */ public function display() { $ret = ""; if( $_SERVER['REQUEST_METHOD']=="GET" ) { $this->setModelValuesOnFields(); $ret .= $this->printForm(); }else { $errors = $this->setFieldValuesOnModel(); if( $errors == NULL && $this->isValid($this->model)) { $this->save($this->model); }else { $ret .= $errors; $ret .= $this->printForm(); } } return $ret; } /** * Add a field to the form * * @param IFormField field to be added */ public function addField(IFormField $field) { $this->fields[$field->getName()] = $field; } /** * Add a hidden field to the form (shouldn't generate visible output) * * @param IFormField field that does not generate visible HTML output */ public function addHiddenField(IFormField $field) { $this->hiddenFields[$field->getName()] = $field; } /** * Set the text for the submit button * * @param string text to be displayed on the submit button */ public function setSubmitText($submitText) { $this->submitText = $submitText; } /** * Get the text for the submit button * * @return string the text to be displayed on the submit button */ public function getSubmitText() { return $this->submitText; } /** * Set the text for the reset button, set to NULL to disable the button * * @param string text to be displayed on the reset button, or NULL to disable the button */ public function setResetText($resetText) { $this->resetText = $resetText; } /** * Get the text for the reset button * * @return string the text to be displayed on the reset button */ public function getResetText() { return $this->resetText; } /** * Set the text to be displayed above the form, set to NULL not show anything * * @param string text to be displayed above the form, or NULL to not show anything */ public function setHeaderText($headerText) { $this->headerText = $headerText; } /** * Get the text to be displayed above the form * * @return string the text to be displayed above the form */ public function getHeaderText() { return $this->headerText; } /** * Overridable validator for the submitted form * * @param array filled model * @return boolean true in case model validates, false otherwise */ protected function isValid(array $model) { return true; } protected function getModel() { return $this->model; } protected function setModel(array $model) { $this->model = $model; } /** * Overridable validator for validating separate fields * * @param string $field the field on which the value will be set * @param string $value the value which should be set on the field * @return true in case of success, or an error string in case of failure */ protected function isValidField($field, $value) { return true; } protected function setModelValuesOnFields() { foreach( $this->fields as $fieldKey => $fieldObject ) { $this->setModelValueOnField($fieldKey, $fieldObject); } } private function setModelValueOnField($fieldKey, $fieldObject) { $fieldObject->setValue($this->model[$fieldKey]); } /** * @return NULL in case of success, or string of html elements to display error */ protected function setFieldValuesOnModel() { $success = true; $errors = ""; foreach( $this->fields as $fieldKey => $fieldObject ) { try { if( $fieldObject->isReadonly() ) { $this->setModelValueOnField($fieldKey, $fieldObject); }else { $fieldObject->setValue($fieldObject->getInputValue()); $this->model[$fieldKey] = $fieldObject->getInputValue(); $validator = $this->isValidField($fieldKey, $fieldObject->getInputValue()); if( $validator!==true ) { $errors .= "
  • " . $validator . "
  • "; $success = false; }elseif( $fieldObject->isCompulsory() && ((is_array($fieldObject->getInputValue()) && count($fieldObject->getInputValue())==0) || (!is_array($fieldObject->getInputValue()) && trim($fieldObject->getInputValue())=="")) ) { $errors .= "
  • " . sprintf(_("The field '%s' is compulsory."), $fieldObject->getName()) . "
  • "; $success = false; } } }catch( InvalidInputException $e ) { $errors .= "
  • " . $e->getMessage() . "
  • "; $success = false; } } if( !$success ) { return ""; }else { return NULL; } } protected function printForm() { $ret = ""; $ret .= "
    "; $ret .= $this->printFormFields(); $ret .= "
    submitText."\">"; if( $this->resetText!=NULL ) { $ret .= "   resetText."\">"; } $ret .= "
    "; return $ret; } protected function printFormFields() { $ret = ""; foreach( $this->hiddenFields as $key => $field ) { $ret .= $field->getField(); } if( $this->headerText!=NULL ) { $ret .= $this->headerText . "

    "; } $ret .= ""; $c = 0; foreach( $this->fields as $key => $field ) { $ret .= ""; $ret .= ""; $ret .= ""; $ret .= ""; } $ret .= "
    " . $field->getTitle() . "getName()."\">"; $ret .= $field->getField() . ($field->isCompulsory() ? "*" : ""); if( $field->getExplanation()!=NULL ) { $ret .= "
    " . $field->getExplanation() . ""; } $ret .= "
    "; return $ret; } } ?>