Updated abstract controller to allow common CRUD operations, errors not yet testet
This commit is contained in:
parent
319911d52f
commit
1b43ab8356
@ -9,79 +9,75 @@ defined('_JEXEC') or die;
|
||||
|
||||
abstract class AbstractClubsController extends BaseController
|
||||
{
|
||||
/**
|
||||
* @return AbstractCommonClubsModelFactory
|
||||
*/
|
||||
protected abstract function getFactory();
|
||||
|
||||
protected abstract function getNameOfElement();
|
||||
/**
|
||||
* @return string The name of the underlying object in lower letters.
|
||||
*/
|
||||
protected abstract function getSingleBaseName();
|
||||
|
||||
protected function getModelName()
|
||||
/**
|
||||
* @return string The name of the view to show a single object
|
||||
*/
|
||||
protected function getSingleViewName()
|
||||
{
|
||||
return $this->getNameOfElement();
|
||||
}
|
||||
|
||||
protected function getNameOfView()
|
||||
{
|
||||
return strtolower($this->getNameOfElement());
|
||||
return $this->getSingleBaseName();
|
||||
}
|
||||
|
||||
protected abstract function getDataMapping();
|
||||
|
||||
function new()
|
||||
public function new()
|
||||
{
|
||||
$obj = call_user_func(array('Clubs' . $this->getNameOfElement(), 'create' . $this->getNameOfElement()));
|
||||
$factory = $this->getFactory();
|
||||
$obj = $factory->createNew();
|
||||
|
||||
// Fetch the posted data
|
||||
$values = $this->loadData();
|
||||
|
||||
$this->filterPreCheck($values);
|
||||
|
||||
// Check the input data
|
||||
$error = ! $this->checkDataIsValid($values, true, Null);
|
||||
|
||||
$view = $this->getNameOfView();
|
||||
|
||||
if($error)
|
||||
{
|
||||
$urldata = $this->packData($values);
|
||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id=new&data={$urldata}", false));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->applyData($obj, $values);
|
||||
|
||||
// Do the actual work
|
||||
$obj->save();
|
||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
||||
$this->saveToDatabase($obj, 'new');
|
||||
}
|
||||
|
||||
function change()
|
||||
public function change()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
$input = $app->input;
|
||||
$id = (int) $input->post->getInt('id');
|
||||
|
||||
$obj = call_user_func(array('Clubs' . $this->getNameOfElement(), 'load' . $this->getNameOfElement()), (int) $id);
|
||||
$factory = $this->getFactory();
|
||||
$obj = $factory->loadById($id);
|
||||
|
||||
$this->saveToDatabase($obj, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractCommonClubsModel $obj
|
||||
* @param int $id
|
||||
*/
|
||||
protected function saveToDatabase($obj, $id)
|
||||
{
|
||||
// Fetch the posted data
|
||||
$values = $this->loadData();
|
||||
|
||||
$this->filterPreCheck($values);
|
||||
|
||||
// Check the input data
|
||||
$error = ! $this->checkDataIsValid($values, false, $obj);
|
||||
$error = ! $this->checkDataIsValid($values, $obj);
|
||||
|
||||
$view = $this->getNameOfView();
|
||||
$view = $this->getSingleViewName();
|
||||
|
||||
if($error)
|
||||
{
|
||||
$urldata = $this->packData($values);
|
||||
$urldata = $obj->pack();
|
||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->applyData($obj, $values);
|
||||
$obj->setValues($values);
|
||||
|
||||
// Do the actual work
|
||||
$obj->save();
|
||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
||||
|
||||
}
|
||||
|
||||
protected function loadData()
|
||||
@ -101,7 +97,12 @@ abstract class AbstractClubsController extends BaseController
|
||||
|
||||
protected function filterPreCheck(&$values){}
|
||||
|
||||
protected function checkDataIsValid($values, bool $isNew, $obj)
|
||||
/**
|
||||
* @param array $values
|
||||
* @param AbstractCommonClubsModel $obj
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkDataIsValid($values, $obj)
|
||||
{
|
||||
$error = false;
|
||||
// Check existence of the required fields
|
||||
@ -140,72 +141,19 @@ abstract class AbstractClubsController extends BaseController
|
||||
return true;
|
||||
}
|
||||
|
||||
private function packData($values)
|
||||
{
|
||||
// FIXME Multiple bugs: filtering not working as expected and Mapping msut be checked
|
||||
$this->filterPrePacking($values);
|
||||
|
||||
$data = array();
|
||||
foreach($this->getDataMapping() as $m => $i)
|
||||
{
|
||||
if(isset($values[$m]))
|
||||
$data[$m] = $values[$m];
|
||||
}
|
||||
$json = json_encode($data);
|
||||
return urlencode($json);
|
||||
}
|
||||
|
||||
protected function filterPrePacking(&$values){}
|
||||
|
||||
public function applyData($obj, $values)
|
||||
{
|
||||
$this->applyDataToObject($obj, $values, $this->getDataMapping());
|
||||
}
|
||||
|
||||
protected function applyDataToObject($obj, $values, $mapping)
|
||||
{
|
||||
foreach($mapping as $m => $v)
|
||||
{
|
||||
$functionName = $this->getSetterMethodName($m, $v);
|
||||
|
||||
if($functionName === null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(isset($v['skip_null_check']))
|
||||
$value = $values[$m];
|
||||
else
|
||||
$value = (isset($values[$m]) && strlen($values[$m]) > 0) ? $values[$m] : null;
|
||||
|
||||
$obj->$functionName($value);
|
||||
}
|
||||
}
|
||||
|
||||
private function getSetterMethodName($m, $options)
|
||||
{
|
||||
if(array_key_exists('setter', $options))
|
||||
return $options['setter'];
|
||||
|
||||
$firstChar = substr($m, 0, 1);
|
||||
$restChars = substr($m, 1);
|
||||
return 'set' . strtoupper($firstChar) . $restChars;
|
||||
}
|
||||
|
||||
function delete()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
|
||||
$id = $app->input->get->getInt('id');
|
||||
$name = $this->getNameOfElement();
|
||||
$name = $this->getSingleBaseName();
|
||||
$app->enqueueMessage("Removal of $name with id $id.");
|
||||
|
||||
$className = 'Clubs' . $this->getModelName();
|
||||
$functionName = 'load' . $this->getModelName();
|
||||
$element = call_user_func(array($className, $functionName), $id);
|
||||
$factory = $this->getFactory();
|
||||
$element = $factory->loadById($id);
|
||||
$element->delete();
|
||||
|
||||
$view = $this->getNameOfView();
|
||||
$view = $this->getSingleViewName();
|
||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
||||
}
|
||||
|
||||
|
@ -60,30 +60,8 @@ abstract class AbstractClubsViewSingle extends HtmlView
|
||||
parent::display($tpl);
|
||||
}
|
||||
|
||||
// protected abstract function getViewName();
|
||||
|
||||
protected abstract function getControllerName();
|
||||
|
||||
// protected function getModelName()
|
||||
// {
|
||||
// $name = $this->getViewName();
|
||||
// return $this->capitalize($name);
|
||||
// }
|
||||
|
||||
// protected function getModelClass()
|
||||
// {
|
||||
// return 'Clubs' . $this->getModelName();
|
||||
// }
|
||||
|
||||
// private function capitalize($s)
|
||||
// {
|
||||
// $first = substr($s, 0, 1);
|
||||
// $rest = substr($s, 1);
|
||||
// return strtoupper($first) . $rest;
|
||||
// }
|
||||
|
||||
// protected abstract function getElementController();
|
||||
|
||||
/**
|
||||
* @return AbstractCommonClubsModelFactory
|
||||
*/
|
||||
|
@ -319,6 +319,9 @@ abstract class AbstractCommonClubsModel
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function pack()
|
||||
{
|
||||
$vals = $this->getValues();
|
||||
@ -330,6 +333,10 @@ abstract class AbstractCommonClubsModel
|
||||
return urlencode($json);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
* @param boolean $decode
|
||||
*/
|
||||
public function unpack($str, $decode = false)
|
||||
{
|
||||
if($decode)
|
||||
|
@ -96,7 +96,7 @@ abstract class AbstractCommonClubsModelFactory
|
||||
*
|
||||
* @param string $condition
|
||||
* @param string|array $sorting
|
||||
* @return array
|
||||
* @return AbstractCommonClubsModel[]
|
||||
*/
|
||||
public function loadElements($condition = null, $sorting = null, $callback = null)
|
||||
{
|
||||
@ -138,8 +138,8 @@ abstract class AbstractCommonClubsModelFactory
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param int $id
|
||||
* @return AbstractCommonClubsModel
|
||||
*/
|
||||
public function loadById($id) {
|
||||
$arr = $this->loadElements("main.id = " . ((int)$id) );
|
||||
@ -159,6 +159,10 @@ abstract class AbstractCommonClubsModelFactory
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $row
|
||||
* @return AbstractCommonClubsModel
|
||||
*/
|
||||
protected function generateObject($row)
|
||||
{
|
||||
$obj = $this->generatePlainObject($row['id']);
|
||||
@ -173,6 +177,9 @@ abstract class AbstractCommonClubsModelFactory
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractCommonClubsModel
|
||||
*/
|
||||
public function createNew()
|
||||
{
|
||||
$obj = $this->generatePlainObject('new');
|
||||
|
@ -6,16 +6,21 @@ defined('_JEXEC') or die;
|
||||
|
||||
class ClubsControllerPosition extends AbstractClubsController
|
||||
{
|
||||
protected function getNameOfElement()
|
||||
{
|
||||
return 'position';
|
||||
}
|
||||
|
||||
protected function getDataMapping()
|
||||
{
|
||||
return array(
|
||||
'name'=>array('required'=>true, 'filter'=>'string', 'name'=>'Bezeichung')
|
||||
);
|
||||
}
|
||||
protected function getFactory()
|
||||
{
|
||||
return new CommonClubsModelFactoryPosition();
|
||||
}
|
||||
|
||||
protected function getSingleBaseName()
|
||||
{
|
||||
return 'position';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user