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

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

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));
}
}