Simple controllers (offer and position) are workign in the new oo format

This commit is contained in:
2019-06-04 16:51:50 +02:00
parent 616a0b7dd9
commit 16e7ed0bc0
17 changed files with 275 additions and 70 deletions

View File

@@ -7,6 +7,9 @@ use Joomla\CMS\Router\Route;
// No direct access.
defined('_JEXEC') or die;
class DataParsingException extends Exception {}
class DataInvalidException extends Exception {}
abstract class AbstractClubsController extends BaseController
{
/**
@@ -27,11 +30,6 @@ abstract class AbstractClubsController extends BaseController
return $this->getSingleBaseName();
}
/**
* @todo Make this OO conforme
*/
protected abstract function getDataMapping();
public function new()
{
$factory = $this->getFactory();
@@ -58,67 +56,109 @@ abstract class AbstractClubsController extends BaseController
*/
protected function saveToDatabase($obj, $id)
{
// Fetch the posted data
$values = $this->loadData();
$this->filterPreCheck($values);
// Check the input data
$error = ! $this->checkDataIsValid($values, $obj);
$view = $this->getSingleViewName();
if($error)
try
{
// Fetch the posted data
$values = $this->loadData();
$this->filterRawCheck($values);
// Check the input data
if( ! $this->requiredDataIsAvailable($values) )
throw new DataParsingException();
if( ! $this->rawDataIsValid($values) )
throw new DataParsingException();
$obj->setValues($values, true);
// Do some additional tests by the controller
if( ! $this->objectValid($obj) )
throw new DataInvalidException();
// Check if the object complains about valitity
if( ! $obj->dataIsValid() )
throw new DataInvalidException();
// Do the actual work
$obj->save();
// Redirect to the list of objects
$view = $this->getSingleViewName();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
}
catch(DataParsingException $e)
{
// FIXME Make this robust (are external refs already dereferenced?)
$view = $this->getSingleViewName();
$obj->setValues($values, true);
$urldata = $obj->pack();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
}
catch(DataInvalidException $e)
{
$view = $this->getSingleViewName();
$urldata = $obj->pack();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
return;
}
$obj->setValues($values);
// Do the actual work
$obj->save();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
}
protected function loadData()
{
$values = array();
$factory = $this->getFactory();
$input = Factory::getApplication()->input->post;
foreach($this->getDataMapping() as $m => $f)
foreach($factory->getAttributes() as $column)
{
$filter = (isset($f['filter'])) ? $f['filter'] : 'string';
$values[$m] = $input->get($m, null, $filter);
$values[$column->getAlias()] = $column->getFilter()->getFilteredValue($input, $column->getAlias());
}
return $values;
}
protected function filterPreCheck(&$values){}
protected function filterRawCheck(&$values){}
protected function objectValid($obj)
{
return true;
}
private function requiredDataIsAvailable($values)
{
$ok = true;
foreach($this->getFactory()->getAttributes() as $column)
{
$filter = $column->getFilter();
if(! $filter->requiredDataAvailable($values[$column->getAlias()]))
{
$fname = $filter->getName();
Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$ok = false;
}
}
return $ok;
}
/**
* @param array $values
* @param AbstractCommonClubsModel $obj
* @return boolean
*/
protected function checkDataIsValid($values, $obj)
protected function rawDataIsValid($values)
{
$error = false;
// Check existence of the required fields
foreach ($this->getDataMapping() as $m => $v)
$factory = $this->getFactory();
foreach($factory->getAttributes() as $column)
{
if(! isset($v['required']) || ! $v['required'])
continue;
// Field is required
if(! $this->fieldValid($m, $values[$m], $v))
if(! $column->getFilter()->rawValueValid($values[$column->getAlias()]))
{
$fname = (isset($v['name'])) ? $v['name'] : $m;
Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
$fname = $column->getFilter()->getName();
Factory::getApplication()->enqueueMessage("Das Feld $fname ist fehlerhaft.", 'error');
$error = true;
}
}