. */ /** * Form object to display a form * -Changes from Abstract form: name added * -tiny_mce removed * -changed visual style at printForm, * since it is intended to be used for one TextField */ require_once(FILESYSTEMROOTPATH."include/form/CheckboxField.php"); require_once(FILESYSTEMROOTPATH."include/form/DateSelectField.php"); require_once(FILESYSTEMROOTPATH."include/form/HiddenField.php"); require_once(FILESYSTEMROOTPATH."include/form/PasswordField.php"); require_once(FILESYSTEMROOTPATH."include/form/RadioField.php"); require_once(FILESYSTEMROOTPATH."include/form/SelectField.php"); require_once(FILESYSTEMROOTPATH."include/form/TextField.php"); require_once(FILESYSTEMROOTPATH."include/form/TextAreaField.php"); require_once(FILESYSTEMROOTPATH."include/form/TextAreaHTMLField.php"); require_once(FILESYSTEMROOTPATH."include/form/FileField.php"); abstract class AbstractForm2 { protected $layout; protected $name; protected $model; protected $fields; protected $hiddenFields; protected $submitText = "Save"; protected $resetText = NULL; protected $headerText = NULL; protected $submitButtons; /** * Constructor */ public function __construct(Layout $layout, IModel $model, $name, $submitButtons=NULL) { $this->layout = $layout; $this->model = $model; $this->fields = array(); $this->hiddenFields = array(); $this->name = $name; if ($submitButtons==NULL){ $this->submitButtons = array("Save"); } else{ $this->submitButtons = $submitButtons; } //Dont include tiny_mce } public function setButtons($buttons){ $this->submitButtons = $buttons; } public function unsetButton($button){ foreach( $this->submitButtons as $key=>$value){ if ($value == $button){ unset($this->submitButtons[$key]); return; } } return; } /** * Save the edited data * * @param Model to be saved */ abstract function save(IModel $model); /** * Display the form */ public function display() { if( $_SERVER['REQUEST_METHOD']=="GET" ) { $this->setModelValuesOnFields(); $this->printForm(); }else { //post if(isset($_POST['form']) && ($this->name == $_POST['form'])){ if( $this->setFieldValuesOnModel() && $this->isValid($this->model) ) { $this->save($this->model); //form remains $this->printForm(); } }else { $this->setModelValuesOnFields(); $this->printForm(); } } } /** * 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 IModel filled model * @return boolean true in case model validates, false otherwise */ protected function isValid(IModel $model) { return true; } public function setModelValuesOnFields() { foreach( $this->fields as $fieldKey => $fieldObject ) { $this->setModelValueOnField($fieldKey, $fieldObject); } } private function setModelValueOnField($fieldKey, $fieldObject) { $getter = "get" . ucfirst($fieldKey); if( is_array($this->model->$getter()) ) { //$this->layout->addJavascript(ROOTPATH."include/form/multivalue.js"); $getterCount = $getter . "Count"; if( $this->model->$getterCount()>0 ) { $fieldObject->setValue($this->model->$getter()); } }else { $fieldObject->setValue($this->model->$getter()); } } private function setFieldValuesOnModel() { $success = true; $errors = ""; foreach( $this->fields as $fieldKey => $fieldObject ) { $setter = "set" . ucfirst($fieldKey); try { if( $fieldObject->isReadonly() ) { $this->setModelValueOnField($fieldKey, $fieldObject); }else { $fieldObject->setValue($fieldObject->getInputValue()); $this->model->$setter($fieldObject->getInputValue()); if( $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->getTitle()); $success = false; } } }catch( InvalidInputException $e ) { $errors .= "
  • " . $e->getMessage(); $success = false; } } if( !$success ) { $this->layout->echoln(""); } return $success; } public function printForm() { $this->layout->echoln('
    '); foreach( $this->hiddenFields as $key => $field ) { $this->layout->echoln($field->getField()); } if( $this->headerText!=NULL ) { $this->layout->echoln(" " . $this->headerText . "

    "); } foreach( $this->fields as $key => $field ) { $this->layout->echoln(' '.$field->getTitle().''); $this->layout->echoln(" " . $field->getField() . ($field->isCompulsory() ? "*" : "")); if( $field->getExplanation()!=NULL ) { $this->layout->echoln("
    " . $field->getExplanation() . ""); } } //display 'hidden' button area $this->layout->echoln('

    "); } } ?>