. * * @author Wim van Ravesteijn */ /** * Form object to display a form as an ajax popup */ require_once(ROOTPATH."include/form/AbstractForm.php"); abstract class AbstractAjaxPopupForm extends AbstractForm { const ACTION_AJAX = "ajax"; const ACTION_NEW = "new"; const ACTION_EDIT = "edit"; const MODEL_ID = "id"; private $displayTitle; private $modelId; /** * Constructor * * @param array $model model to be put on the form * @param string $displayTitle title of the field to be edited */ public function __construct($displayTitle, $modelId="id") { parent::__construct(array()); $this->displayTitle = $displayTitle; $this->modelId = $modelId; $this->addHiddenField(new HiddenField(self::ACTION_AJAX, "1")); if( $this->isNewEntry() ) { $this->addHiddenField(new HiddenField(self::ACTION_NEW, "1")); }else { $this->addHiddenField(new HiddenField(self::ACTION_EDIT, "1")); $this->addHiddenField(new HiddenField(self::MODEL_ID, $_REQUEST[self::MODEL_ID])); } } /** * Do save the data in this method. The returned string will be displayed in the popup * * @param Model to be saved * @return boolean true in case of success, false otherwise */ protected abstract function saveModel(array $model); /** * Load the model of the given id * * @param integer $id * @return array model, or NULL in case of error */ protected abstract function loadModel($id); /** * Return a new empty model * * @return array model */ protected abstract function newModel(); /** * Called when the data in the database has been changed, can be used to refresh a datatable on the screen. * * @param AjaxResponse $response * @return AjaxResponse $response */ protected function onDataUpdated(AjaxResponse $response) { return $response; } /** * Retrun the html of the form * * @return String html statements to display the form */ public function display() { if( $this->initialiseModel() ) { if( $_SERVER['REQUEST_METHOD']=="GET" ) { $this->setModelValuesOnFields(); $popup = new Popup($this->getPopupTitle(), $this->printForm()); echo $popup->createPopup(); }else { if( isset($_REQUEST[self::MODEL_ID]) ) { $model = $this->getModel(); $model[$this->modelId] = $_REQUEST[self::MODEL_ID]; $this->setModel($model); } $errors = $this->setFieldValuesOnModel(); if( $errors == NULL && $this->isValid($this->getModel()) ) { $this->save($this->getModel()); }else { $response = new AjaxResponse(); $response->addRefresh("popup_content", $errors . $this->printForm()); $response->sendResponse(); } } }else { $popup = new AlertPopup("Error", "Unable to load ".$this->displayTitle); $popup->setHeight(230); if( $_SERVER['REQUEST_METHOD']=="GET" ) { echo $popup->createPopup(); }else { $response = new AjaxResponse(); $response->addRefresh("popup", $popup->createPopup()); $response->sendResponse(); } } } protected function printForm() { $ret = "
"; $ret .= $this->printFormFields(); $ret .= "
"; $ret .= "

"; $ret .= ""; $ret .= IconHelper::getIcon("save", $this->getSubmitText())." ".$this->getSubmitText(); $ret .= ""; if( $this->getResetText()!=NULL ) { $ret .= "  "; $ret .= IconHelper::getIcon("cancel", $this->getResetText())." ".$this->getResetText(); $ret .= ""; } $ret .= "

"; return $ret; } protected function save(array $model) { $response = new AjaxResponse(); if( $this->saveModel($model) ) { $content = "

".ucfirst($this->displayTitle)." saved successfully.

"; $content .= "

"; $content .= ""; $content .= IconHelper::getIcon("check", "OK")." OK"; $content .= ""; $content .= "

"; $response->addRefresh("popup_content", $content); $this->onDataUpdated($response); }else { $response->addRefresh("popup_content", "" . $this->printForm()); } $response->sendResponse(); } protected function getPopupTitle() { if( isset($_REQUEST[self::ACTION_NEW]) ) { return "New " . $this->displayTitle; }else { return "Edit " . $this->displayTitle; } } protected function isValidField($field, $value) { return $this->isValidAjaxField($field, $value, $this->isNewEntry()); } /** * Override this function to add extra validation. Compared to isValidField() it knows if the entry is new or edited * * @param string $field the field on which the value will be set * @param string $value the value which should be set on the field * @param boolean $isNewEntry true when this is a new entry, false when it is an edited entry * @return true in case of success, or an error string in case of failure */ protected function isValidAjaxField($field, $value, $isNewEntry) { return true; } private function initialiseModel() { $model = NULL; if( isset($_REQUEST[self::ACTION_EDIT]) && isset($_REQUEST[self::MODEL_ID]) ) { $model = $this->loadModel($_REQUEST[self::MODEL_ID]); }elseif( isset($_REQUEST[self::ACTION_NEW]) ) { $model = $this->newModel(); } if( $model!=NULL ) { $this->setModel($model); return true; }else { return false; } } private function isNewEntry() { return isset($_REQUEST[self::ACTION_NEW]); } } ?>