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
|
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();
|
return $this->getSingleBaseName();
|
||||||
}
|
|
||||||
|
|
||||||
protected function getNameOfView()
|
|
||||||
{
|
|
||||||
return strtolower($this->getNameOfElement());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract function getDataMapping();
|
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
|
$this->saveToDatabase($obj, 'new');
|
||||||
$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);
|
public function change()
|
||||||
|
|
||||||
// Do the actual work
|
|
||||||
$obj->save();
|
|
||||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
|
||||||
}
|
|
||||||
|
|
||||||
function change()
|
|
||||||
{
|
{
|
||||||
$app = Factory::getApplication();
|
$app = Factory::getApplication();
|
||||||
$input = $app->input;
|
$input = $app->input;
|
||||||
$id = (int) $input->post->getInt('id');
|
$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
|
// Fetch the posted data
|
||||||
$values = $this->loadData();
|
$values = $this->loadData();
|
||||||
|
|
||||||
$this->filterPreCheck($values);
|
$this->filterPreCheck($values);
|
||||||
|
|
||||||
// Check the input data
|
// Check the input data
|
||||||
$error = ! $this->checkDataIsValid($values, false, $obj);
|
$error = ! $this->checkDataIsValid($values, $obj);
|
||||||
|
|
||||||
$view = $this->getNameOfView();
|
$view = $this->getSingleViewName();
|
||||||
|
|
||||||
if($error)
|
if($error)
|
||||||
{
|
{
|
||||||
$urldata = $this->packData($values);
|
$urldata = $obj->pack();
|
||||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
|
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->applyData($obj, $values);
|
$obj->setValues($values);
|
||||||
|
|
||||||
// Do the actual work
|
// Do the actual work
|
||||||
$obj->save();
|
$obj->save();
|
||||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function loadData()
|
protected function loadData()
|
||||||
@ -101,7 +97,12 @@ abstract class AbstractClubsController extends BaseController
|
|||||||
|
|
||||||
protected function filterPreCheck(&$values){}
|
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;
|
$error = false;
|
||||||
// Check existence of the required fields
|
// Check existence of the required fields
|
||||||
@ -140,72 +141,19 @@ abstract class AbstractClubsController extends BaseController
|
|||||||
return true;
|
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()
|
function delete()
|
||||||
{
|
{
|
||||||
$app = Factory::getApplication();
|
$app = Factory::getApplication();
|
||||||
|
|
||||||
$id = $app->input->get->getInt('id');
|
$id = $app->input->get->getInt('id');
|
||||||
$name = $this->getNameOfElement();
|
$name = $this->getSingleBaseName();
|
||||||
$app->enqueueMessage("Removal of $name with id $id.");
|
$app->enqueueMessage("Removal of $name with id $id.");
|
||||||
|
|
||||||
$className = 'Clubs' . $this->getModelName();
|
$factory = $this->getFactory();
|
||||||
$functionName = 'load' . $this->getModelName();
|
$element = $factory->loadById($id);
|
||||||
$element = call_user_func(array($className, $functionName), $id);
|
|
||||||
$element->delete();
|
$element->delete();
|
||||||
|
|
||||||
$view = $this->getNameOfView();
|
$view = $this->getSingleViewName();
|
||||||
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,30 +60,8 @@ abstract class AbstractClubsViewSingle extends HtmlView
|
|||||||
parent::display($tpl);
|
parent::display($tpl);
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected abstract function getViewName();
|
|
||||||
|
|
||||||
protected abstract function getControllerName();
|
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
|
* @return AbstractCommonClubsModelFactory
|
||||||
*/
|
*/
|
||||||
|
@ -319,6 +319,9 @@ abstract class AbstractCommonClubsModel
|
|||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function pack()
|
public function pack()
|
||||||
{
|
{
|
||||||
$vals = $this->getValues();
|
$vals = $this->getValues();
|
||||||
@ -330,6 +333,10 @@ abstract class AbstractCommonClubsModel
|
|||||||
return urlencode($json);
|
return urlencode($json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $str
|
||||||
|
* @param boolean $decode
|
||||||
|
*/
|
||||||
public function unpack($str, $decode = false)
|
public function unpack($str, $decode = false)
|
||||||
{
|
{
|
||||||
if($decode)
|
if($decode)
|
||||||
|
@ -96,7 +96,7 @@ abstract class AbstractCommonClubsModelFactory
|
|||||||
*
|
*
|
||||||
* @param string $condition
|
* @param string $condition
|
||||||
* @param string|array $sorting
|
* @param string|array $sorting
|
||||||
* @return array
|
* @return AbstractCommonClubsModel[]
|
||||||
*/
|
*/
|
||||||
public function loadElements($condition = null, $sorting = null, $callback = null)
|
public function loadElements($condition = null, $sorting = null, $callback = null)
|
||||||
{
|
{
|
||||||
@ -138,8 +138,8 @@ abstract class AbstractCommonClubsModelFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param int $id
|
* @param int $id
|
||||||
|
* @return AbstractCommonClubsModel
|
||||||
*/
|
*/
|
||||||
public function loadById($id) {
|
public function loadById($id) {
|
||||||
$arr = $this->loadElements("main.id = " . ((int)$id) );
|
$arr = $this->loadElements("main.id = " . ((int)$id) );
|
||||||
@ -159,6 +159,10 @@ abstract class AbstractCommonClubsModelFactory
|
|||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $row
|
||||||
|
* @return AbstractCommonClubsModel
|
||||||
|
*/
|
||||||
protected function generateObject($row)
|
protected function generateObject($row)
|
||||||
{
|
{
|
||||||
$obj = $this->generatePlainObject($row['id']);
|
$obj = $this->generatePlainObject($row['id']);
|
||||||
@ -173,6 +177,9 @@ abstract class AbstractCommonClubsModelFactory
|
|||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return AbstractCommonClubsModel
|
||||||
|
*/
|
||||||
public function createNew()
|
public function createNew()
|
||||||
{
|
{
|
||||||
$obj = $this->generatePlainObject('new');
|
$obj = $this->generatePlainObject('new');
|
||||||
|
@ -6,16 +6,21 @@ defined('_JEXEC') or die;
|
|||||||
|
|
||||||
class ClubsControllerPosition extends AbstractClubsController
|
class ClubsControllerPosition extends AbstractClubsController
|
||||||
{
|
{
|
||||||
protected function getNameOfElement()
|
|
||||||
{
|
|
||||||
return 'position';
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getDataMapping()
|
protected function getDataMapping()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'name'=>array('required'=>true, 'filter'=>'string', 'name'=>'Bezeichung')
|
'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