. */ /** * Form object to display a form */ 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"); abstract class AbstractForm { protected $layout; private $model; private $fields; private $hiddenFields; private $submitText = "Save"; private $resetText = NULL; private $headerText = NULL; private $name = "myform"; /** * Constructor */ public function __construct(Layout $layout, IModel $model) { $this->layout = $layout; $this->model = $model; $this->fields = array(); $this->hiddenFields = array(); //tiny_mce $this->layout->addJavascript(ROOTPATH."include/form/tiny_mce/tiny_mce.js"); $this->layout->addJavascript(ROOTPATH."include/form/tiny_mce/tiny_mce_init.js"); } /** * 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 { if( $this->setFieldValuesOnModel() && $this->isValid($this->model) ) { $this->save($this->model); }else { $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; } /** * Set the name of the form * * @param string name of the form */ public function setName($name) { $this->name = $name; } /** * Get the name of the form * * @return string name of the form */ public function getName() { return $this->name; } /** * 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; } private 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 .= "