. * * @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 .= ""; return $ret; } protected function save(array $model) { $response = new AjaxResponse(); if( $this->saveModel($model) ) { $content = "".ucfirst($this->displayTitle)." saved successfully.
"; $content .= ""; $response->addRefresh("popup_content", $content); $this->onDataUpdated($response); }else { $response->addRefresh("popup_content", "