Created abstract structures to keep complexity and code redundancy at bay.

This commit is contained in:
Christian Wolf 2019-04-18 14:31:13 +02:00
parent 4b2dbff104
commit 6e1326240b
10 changed files with 339 additions and 351 deletions

View File

@ -0,0 +1,189 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
abstract class AbstractClubsController extends BaseController
{
protected abstract function getNameOfElement();
protected function getModelName()
{
return $this->getNameOfElement();
}
protected function getNameOfView()
{
return strtolower($this->getNameOfElement());
}
protected abstract function getDataMapping();
function new()
{
$obj = call_user_func(array('Clubs' . $this->getNameOfElement(), 'create' . $this->getNameOfElement()));
// Fetch the posted data
$values = $this->loadData();
$this->filterPreCheck($values);
// Check the input data
$error = $this->checkData($values);
$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));
}
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);
// Fetch the posted data
$values = $this->loadData();
$this->filterPreCheck($values);
// Check the input data
$error = $this->checkData($values);
$view = $this->getNameOfView();
if($error)
{
$urldata = $this->packData($values);
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&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));
}
protected function loadData()
{
$values = array();
$input = Factory::getApplication()->input->post;
foreach($this->getDataMapping() as $m => $f)
{
$filter = (isset($f['filter'])) ? $f['filter'] : 'string';
$values[$m] = $input->get($m, null, $filter);
}
return $values;
}
protected function filterPreCheck(&$values){}
protected function checkData($values)
{
$error = false;
// Check existence of the required fields
foreach ($this->getDataMapping() as $m => $v)
{
if(! isset($v['required']) || ! $v['required'])
continue;
// Field is required
if(! $this->fieldValid($m, $values[$m], $v))
{
$fname = (isset($v['name'])) ? $v['name'] : $m;
Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$error = true;
}
}
return $error;
}
protected function fieldValid($name, $value, $options)
{
if(empty($value))
return false;
if(isset($options['filter']))
{
switch($options['filter'])
{
case 'string':
if(empty(trim($value)))
return false;
}
}
return true;
}
private function packData($values)
{
$data = array();
foreach($this->getDataMapping() as $i)
$data[$i] = $values[$i];
return urlencode(json_encode($data));
}
public function applyData($obj, $values)
{
foreach($this->getDataMapping() as $m => $v)
{
$functionName = $this->getSetterMethodName($m, $v);
$value = (isset($values[$m])) ? $values[$m] : null;
$obj->$functionName($value);
}
}
private function getSetterMethodName($m, $options)
{
if(isset($options['setter']))
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();
$app->enqueueMessage("Removal of $name with id $id.");
$className = 'Clubs' . $this->getModelName();
$functionName = 'load' . $this->getModelName();
$element = call_user_func(array($className, $functionName), $id);
$element->delete();
$view = $this->getNameOfView();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
}
}

View File

@ -5,7 +5,7 @@ use Joomla\CMS\Factory;
defined('_JEXEC') or die;
abstract class ClubsAbstractModel
abstract class AbstractClubsModel
{
protected $id;
@ -98,12 +98,12 @@ abstract class ClubsAbstractModel
foreach($mappings as $m)
$values[$m] = $this->$m;
$this->preFilter($values);
$this->filter($values);
foreach($mappings as $m)
$values[$m] = $q->q($values[$m]);
$this->postFilter($values);
$this->postQuoteFilter($values);
$q->insert($this->getTableName())
->columns(array_map(array($q, 'qn'), $mappings))
@ -130,12 +130,12 @@ abstract class ClubsAbstractModel
foreach($mapping as $m)
$values[$m] = $this->$m;
$this->preFilter($values);
$this->filter($values);
foreach($mapping as $m)
$values[$m] = $q->q($values[$m]);
$this->postFilter($values);
$this->postQuoteFilter($values);
$q->update($this->getTableName());
foreach($mapping as $m)
@ -146,11 +146,15 @@ abstract class ClubsAbstractModel
$dbo->execute();
}
protected function prepareDelete(){}
public function delete()
{
if($this->id === 'new')
return;
$this->prepareDelete();
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
@ -161,13 +165,23 @@ abstract class ClubsAbstractModel
$dbo->execute();
}
protected function getRequiredDataMappings()
{
return $this->getDataMappings();
}
protected function isDataValid()
{
foreach($this->getRequiredDataMappings() as $m)
{
if(!isset($this->$m) || empty($this->$m) || $this->$m === null)
return false;
}
return true;
}
protected function preFilter(&$values){}
protected function postFilter(&$values){}
protected function filter(&$values){}
protected function postQuoteFilter(&$values){}
}

View File

@ -0,0 +1,77 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
abstract class AbstractClubsViewSingle extends HtmlView
{
protected $address;
protected $object;
protected $isNew;
function display($tpl = null)
{
$input = Factory::getApplication()->input;
$id = $input->get->get('id');
$modelClass = 'Clubs' . $this->getModelName();
$controller = $this->getControllerName();
if($id === 'new')
{
$this->address = Route::_("index.php?option=com_clubs&task={$controller}.new");
$this->object = call_user_func(array($modelClass, 'create' . $this->getModelName()));
$this->isNew = true;
}
else if(is_numeric($id))
{
$this->address = Route::_("index.php?option=com_clubs&task={$controller}.change");
$this->object = call_user_func(array($modelClass, 'load' . $this->getModelName()), (int) $id);
$this->isNew = false;
}
else
throw new Exception('Need an object id.');
if($input->get->get('data', null, 'json') != null)
{
// Restore previous data
$dataurl = $input->get->get('data', null, 'json');
$data = json_decode($dataurl, true);
$controller = $this->getElementController();
$controller->applyData($this->object, $data);
}
parent::display($tpl);
}
protected abstract function getViewName();
protected function getControllerName()
{
return $this->getViewName();
}
protected function getModelName()
{
$name = $this->getViewName();
return $this->capitalize($name);
}
private function capitalize($s)
{
$first = substr($s, 0, 1);
$rest = substr($s, 1);
return strtoupper($first) . $rest;
}
protected abstract function getElementController();
}

View File

@ -7,6 +7,7 @@ use Joomla\CMS\MVC\Controller\BaseController;
defined('_JEXEC') or die;
JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels');
JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract');
$controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input;

View File

@ -1,108 +1,23 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
class ClubsControllerOffer extends BaseController
class ClubsControllerOffer extends AbstractClubsController
{
function new()
protected function getNameOfElement()
{
$app = Factory::getApplication();
$input = $app->input;
$o = ClubsOffer::createOffer();
// Fetch the posted data
$name = $input->post->getString('name');
// Check the input data
$error = false;
// Check existence of the other fields
$fields = array('name'=>'Bezeichnung');
foreach ($fields as $f => $fname)
{
$fvalue = $$f;
if(! isset($fvalue) || empty(trim($fvalue)))
{
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$error = true;
}
}
if($error)
{
$data = array();
foreach(array('name') as $i)
$data[$i] = $$i;
$urldata = urlencode(json_encode($data));
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=new&data=' . $urldata, false));
return;
}
$o->setName($name);
// Do the actual work
$o->save();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
return 'offer';
}
function change()
protected function getDataMapping()
{
$app = Factory::getApplication();
$input = $app->input;
$id = (int) $input->post->getInt('id');
$o = ClubsOffer::loadOffer((int) $id);
// Fetch the posted data
$name = $input->post->getString('name');
// Check the input data
$error = false;
// Check existence of the other fields
$fields = array('name'=>'Bezeichnung');
foreach ($fields as $f => $fname)
{
$fvalue = $$f;
if(! isset($fvalue) || empty(trim($fvalue)))
{
$app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$error = true;
}
}
if($error)
{
$data = array();
foreach(array('name') as $i)
$data[$i] = $$i;
$urldata = urlencode(json_encode($data));
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=' . $id . '&data=' . $urldata, false));
return;
}
$o->setName($name);
// Do the actual work
$o->save();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
return array(
'name' => array('required'=>true, 'name'=>'Bezeichnung', 'filter'=>'string')
);
}
function delete()
{
$app = Factory::getApplication();
$id = $app->input->get->getInt('id');
$app->enqueueMessage("Removal of offer with id $id.");
$user = ClubsOffer::loadOffer($id);
$user->delete();
$this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false));
}
}

View File

@ -1,21 +1,12 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsOffer
class ClubsOffer extends AbstractClubsModel
{
protected $id;
protected $name;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
@ -28,62 +19,26 @@ class ClubsOffer
/**
* @param string $name
*/
public function setName(string $name)
public function setName($name)
{
$this->name = $name;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct()
{}
private const tableName = '#__club_offers';
private const className = 'ClubsOffer';
public static function loadOffers()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_offers');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $o)
{
$oo = new ClubsOffer();
$oo->loadData($o);
$ret[] = $oo;
}
return $ret;
return self::loadElements(self::tableName, self::className);
}
public static function loadOffer(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_offers')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No offer found.");
// TODO
}
$offer = new ClubsOffer();
$offer->loadData($row);
return $offer;
return self::loadElement($id, self::tableName, self::className);
}
public static function createOffer()
@ -93,65 +48,14 @@ class ClubsOffer
return $offer;
}
public function save()
protected function getDataMappings()
{
if($this->id === 'new')
$this->insertOffer();
else
$this->updateOffer();
return array('name');
}
private function insertOffer()
protected function getTableName()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->insert('#__club_offers')
->columns(array('name'))
->values("$vname")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
return self::tableName;
}
private function updateOffer()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->update('#__club_offers')
->set(array(
"name=$vname"
))
->where("id=". (int) $this->id)
;
$dbo->setQuery($q);
$dbo->execute();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_offers')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
}

View File

@ -1,21 +1,11 @@
<?php
// No direct access.
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class ClubsPosition
class ClubsPosition extends ClubsAbstractModel
{
protected $id;
protected $name;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
@ -33,57 +23,21 @@ class ClubsPosition
$this->name = $name;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
protected function __construct()
{}
private const tableName = '#__club_positions';
private const className = 'ClubsPosition';
public static function loadPositions()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_positions');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $p)
{
$po = new ClubsPosition();
$po->loadData($p);
$ret[] = $po;
}
return $ret;
return self::loadElements(self::tableName, self::className);
}
public static function loadPosition(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_positions')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No position found.");
// TODO
}
$position = new ClubsPosition();
$position->loadData($row);
return $position;
return self::loadElement($id, self::tableName, self::className);
}
public static function createPosition()
@ -93,65 +47,16 @@ class ClubsPosition
return $position;
}
public function save()
protected function getDataMappings()
{
if($this->id === 'new')
$this->insertPosition();
else
$this->updatePosition();
return array('name');
}
private function insertPosition()
protected function getTableName()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->insert('#__club_positions')
->columns(array('name'))
->values("$vname")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
}
private function updatePosition()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vname = $q->q($this->name);
$q->update('#__club_positions')
->set(array(
"name=$vname"
))
->where("id=". (int) $this->id)
;
$dbo->setQuery($q);
$dbo->execute();
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_positions')
->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
return self::tableName;
}
}

View File

@ -8,16 +8,16 @@ defined('_JEXEC') or die;
?>
<form method="post" action="<?php echo $this->address; ?>">
<input type='hidden' name='id' value='<?php echo $this->offer->getId(); ?>'>
<input type='hidden' name='id' value='<?php echo $this->object->getId(); ?>'>
<table>
<tr>
<td>Bezeichnung</td>
<td><input type='text' name='name' value='<?php echo htmlentities($this->offer->getName()); ?>'></td>
<td><input type='text' name='name' value='<?php echo htmlentities($this->object->getName()); ?>'></td>
</tr>
<?php if(! $this->isNew): ?>
<tr>
<td>ID</td>
<td><?php echo $this->offer->getId(); ?></td>
<td><?php echo $this->object->getId(); ?></td>
</tr>
<?php endif; ?>
</table>

View File

@ -1,47 +1,29 @@
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Toolbar\ToolbarHelper;
// No direct access.
defined('_JEXEC') or die;
class ClubsViewOffer extends HtmlView
JLoader::register("ClubsControllerOffer", JPATH_ROOT . "/administrator/components/com_clubs/controllers/offer.php");
class ClubsViewOffer extends AbstractClubsViewSingle
{
function display($tpl = null)
{
$input = Factory::getApplication()->input;
$id = $input->get->get('id');
if($id === 'new')
{
$this->address = Route::_('index.php?option=com_clubs&task=offer.new');
$this->offer = ClubsOffer::createOffer();
$this->isNew = true;
}
else if(is_numeric($id))
{
$this->address = Route::_('index.php?option=com_clubs&task=offer.change');
$this->offer = ClubsOffer::loadOffer((int) $id);
$this->isNew = false;
}
else
throw new Exception('Need a user id.');
if($input->get->get('data', null, 'json') != null)
{
// Restore previous data
$dataurl = $input->get->get('data', null, 'json');
$data = json_decode($dataurl, true);
$this->offer->setName($data['name']);
}
ToolbarHelper::title('Club-Management - Angebot');
parent::display($tpl);
}
protected function getViewName()
{
return 'offer';
}
protected function getElementController()
{
return new ClubsControllerOffer();
}
}

View File

@ -7,6 +7,7 @@ use Joomla\CMS\Factory;
defined('_JEXEC') or die;
JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels');
JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract');
$controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input;