<?php
/**
* Class Management accomodate database manipulation; show, remove, edit, and add
*
* derived class should redefine protected method with parent::method. it's more php specific.
* should be fine
*
*/
Abstract Class Management extends ControllerBase{
protected $offset;
protected $pglimit;
protected function __construct(){
parent::__construct();
}
public function index(){
$this->session->set('last',$this->getName().'/index');
$this->session->set('lastadmin',$this->getName().'/index');
$this->xml->addText($this->getTitle(),'title',0);
$this->xml->addText(null,'panel',0);
$this->xml->removeAllLoop();
$this->xml->buildSubMenu($this->navigator->getSubMenu($this));
$this->show();
}
protected function show(){
//put the common step here, leave the special case in child class
$this->offset = 0;
if(isset($_POST['ofs'])){
$this->offset = $_POST['ofs'];
}
$this->pglimit = PAGELIMIT;
if(isset($_POST['pglimit'])){
$this->pglimit = $_POST['pglimit'];
}
//special handling for different children wrapped in a method
$this->populateShowContent();
$this->xml->printOffset($this->offset,$this->getTotal(),$this->getName().'/show',$this->pglimit);
$this->xml->flush();
}
//remove item
protected function remove(){
$this->xml->addText(null,'panel',0);
if(isset($_POST['id'])){
$this->offset = $_POST['ofs'];
$itemnumber = $_POST['itemnumber'];
if($itemnumber%PAGELIMIT==1){
if($itemnumber==$this->getTotal()){
$this->offset = $this->offset-PAGELIMIT;
}
if($this->offset<0){
$this->offset = 0;
}
}
$this->removeItem();
$this->xml->addRedirPOST($this->getName().'/show','ofs='.$this->offset);
$this->xml->addText('Removing item','note',0);
$this->xml->flush();
}
}
//add item
public function add(){
$this->session->set('lastadmin',$this->getName().'/add');
//get POST variable
if(isset($_POST[$this->getKeyVar()])){
$this->checkAdd();
//specific for children
$this->addItem();
$total = $this->getTotal();
$this->offset = (floor($total/PAGELIMIT))*PAGELIMIT;
if($total%PAGELIMIT==0){
$this->offset -= PAGELIMIT;
}
if($this->offset<0){
$this->offset = 0;
}
$this->postAddAction();
$this->xml->addRedirPOST($this->getName().'/show','ofs='.$this->offset);
$this->xml->clearForm();
}
else{
$this->xml->addForm($this->getAddID(),$this->getFormPlace(),0);
$this->populateAddForm();
$this->xml->addSubmitBtn('submitForm(\''.$this->getName().'/add\')','Submit');
$this->setAddCaption();
}
$this->xml->flush();
}
//edit item
public function edit(){
if(isset($_POST[$this->getKeyVar()])){
if($_POST[$this->getKeyVar()]==''){
die('ERROR: Field should not be empty');
}
$this->checkEdit();
if($this->editItem()){
$this->postEditAction();
$this->xml->addRedirPOST($this->getName().'/show','ofs='.$_POST['ofs']);
$this->offset = $_POST['ofs'];
$this->xml->addText(null,'panel',0);
}else{
$this->xml->addText('IGNORE: item does not exist','note',0);
$this->xml->addRedirPOST($this->getName().'/show','ofs='.$_POST['ofs']);
}
}
else
if(isset($_POST['id'])){
$this->xml->addForm($this->getEditID(),$this->getFormPlace(),0);
$this->setEditCaption();
$this->xml->addHidden('id',$_POST['id']);
$this->offset = $_POST['ofs'];
$this->xml->addHidden('ofs',$this->offset);
$this->populateEditForm();
$this->xml->addSubmitBtn('submitForm(\''.$this->getName().'/edit\')','Update');
}
$this->xml->flush();
}
//return the form Edit ID
protected function getEditID(){
$result='javascript:sndReqPOST(\''.$this->getName().'/edit\',\'id='.$_POST['id'].'&itemnumber='.$_POST['itemnumber'].'&ofs='.$_POST['ofs'].'\')';
return $result;
}
//return the form Add ID
protected function getAddID(){
$result= 'javascript:sndReq(\''.$this->getName().'/add\')';
return $result;
}
//populate content that should be displayed including table and url offset if exists
//return void
abstract function populateShowContent();
//build add form
abstract function populateAddForm();
//page title
abstract function getTitle();
//get total item
abstract function getTotal();
//add item
abstract function addItem();
//edit item
abstract function editItem();
//remove item
abstract function removeItem();
//set form location
abstract function getFormPlace();
//get key variable to add or update
//it will check for existing post variable with this value
abstract function getKeyVar();
abstract function checkAdd();
abstract function checkEdit();
//do a favor for the methods below. override on derived class when needed
//this is the default ones
protected function postAddAction(){
$this->xml->addText('Adding new item','note',0);
}
protected function postEditAction(){
$this->xml->addText('Updating item','note',0);
}
protected function setEditCaption(){
$this->xml->addText('Edit Item','headbox',1);
}
protected function setAddCaption(){
$this->xml->addText('Add Item','headbox',1);
}
}
?>