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

This commit is contained in:
Christian Wolf 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();
@ -57,68 +55,110 @@ abstract class AbstractClubsController extends BaseController
* @param int $id
*/
protected function saveToDatabase($obj, $id)
{
try
{
// Fetch the posted data
$values = $this->loadData();
$this->filterPreCheck($values);
$this->filterRawCheck($values);
// Check the input data
$error = ! $this->checkDataIsValid($values, $obj);
if( ! $this->requiredDataIsAvailable($values) )
throw new DataParsingException();
$view = $this->getSingleViewName();
if( ! $this->rawDataIsValid($values) )
throw new DataParsingException();
if($error)
{
$urldata = $obj->pack();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
return;
}
$obj->setValues($values, true);
$obj->setValues($values);
// 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();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false));
// 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));
}
}
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)
{
if(! isset($v['required']) || ! $v['required'])
continue;
// Field is required
if(! $this->fieldValid($m, $values[$m], $v))
$factory = $this->getFactory();
foreach($factory->getAttributes() as $column)
{
$fname = (isset($v['name'])) ? $v['name'] : $m;
Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error');
if(! $column->getFilter()->rawValueValid($values[$column->getAlias()]))
{
$fname = $column->getFilter()->getName();
Factory::getApplication()->enqueueMessage("Das Feld $fname ist fehlerhaft.", 'error');
$error = true;
}
}

View File

@ -10,6 +10,7 @@ JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mym
JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract');
JLoader::registerPrefix('AbstractCommonClubs', JPATH_ROOT . '/administrator/components/com_clubs/common/abstract');
JLoader::registerPrefix('CommonClubsModel', JPATH_ROOT . '/administrator/components/com_clubs/common/models');
JLoader::registerPrefix('CommonClubsControllerMapping', JPATH_ROOT . '/administrator/components/com_clubs/common/controllermappings');
$controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input;

View File

@ -0,0 +1,55 @@
<?php
// No direct access.
defined('_JEXEC') or die;
abstract class AbstractCommonClubsControllerMapping
{
protected $required;
protected $name;
public function __construct($name, $required = true)
{
$this->name = $name;
$this->required = $required;
}
/**
* @param mixed $value
* @return bool
*/
public function requiredDataAvailable($value)
{
if($this->required && ($value === null || $value === ''))
return false;
return true;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param JInput $input
* @param string $name
* @return string
*/
public abstract function getFilteredValue($input, $name);
/**
* @param mixed $value
* @return boolean
*/
public function rawValueValid($value)
{
return true;
}
}

View File

@ -40,8 +40,11 @@ abstract class AbstractCommonClubsModel
return $this->new;
}
public function setValues($values)
public function setValues($values, $unpack = false)
{
if($unpack)
$this->values = $this->unpackExternalReferencesFromKeys($values);
else
$this->values = $values;
}
@ -351,4 +354,9 @@ abstract class AbstractCommonClubsModel
$this->setValues($vals);
}
public function dataIsValid()
{
return true;
}
}

View File

@ -8,7 +8,10 @@ abstract class AbstractCommonClubsModelColumn
protected $alias;
protected $column;
protected $required;
/**
* @var AbstractCommonClubsControllerMapping
*/
protected $filter;
public function getAlias()
{
@ -20,17 +23,12 @@ abstract class AbstractCommonClubsModelColumn
return $this->column;
}
public function isRequired()
{
return $this->required;
}
public abstract function isSimpleType();
public function __construct($alias, $required = true, $column = null)
public function __construct($alias, $filter, $column = null)
{
$this->alias = $alias;
$this->required = $required;
$this->filter = $filter;
if(isset($column))
$this->column = $column;
else
@ -71,4 +69,9 @@ abstract class AbstractCommonClubsModelColumn
return $value;
}
public function getFilter()
{
return $this->filter;
}
}

View File

@ -0,0 +1,14 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsControllerMappingCmd extends AbstractCommonClubsControllerMapping
{
public function getFilteredValue($input, $name)
{
return $input->getCmd($name);
}
}

View File

@ -0,0 +1,14 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsControllerMappingFloat extends AbstractCommonClubsControllerMapping
{
public function getFilteredValue($input, $name)
{
return $input->getFloat($name);
}
}

View File

@ -0,0 +1,14 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsControllerMappingInt extends AbstractCommonClubsControllerMapping
{
public function getFilteredValue($input, $name)
{
return $input->getInt($name);
}
}

View File

@ -0,0 +1,44 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsControllerMappingRef extends AbstractCommonClubsControllerMapping
{
/**
* @var AbstractCommonClubsModelFactory
*/
protected $factory;
/**
* @param string $name
* @param AbstractCommonClubsModelFactory $factory
* @param boolean $required
*/
public function __construct($name, $factory, $required = true)
{
parent::__construct($name, $required);
$this->factory = $factory;
}
public function getFilteredValue($input, $name)
{
return $input->getInt($name);
}
public function rawValueValid($value)
{
try
{
$this->factory->loadById((int) $value);
}
catch(ElementNotFoundException $e)
{
return false;
}
return true;
}
}

View File

@ -0,0 +1,14 @@
<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsControllerMappingString extends AbstractCommonClubsControllerMapping
{
public function getFilteredValue($input, $name)
{
return $input->getString($name);
}
}

View File

@ -14,9 +14,9 @@ class CommonClubsModelColumnRef extends AbstractCommonClubsModelColumn
protected $className;
public function __construct($alias, $className, $required=true, $column=null)
public function __construct($alias, $className, $column=null)
{
parent::__construct($alias, $required, $column);
parent::__construct($alias, $column);
if(empty($className))
throw new Exception('Classname must be non-empty.');

View File

@ -8,7 +8,7 @@ class CommonClubsModelFactoryOffer extends AbstractCommonClubsModelFactory
protected function fetchAttributes()
{
return array(
new CommonClubsModelColumnString('name')
new CommonClubsModelColumnString('name', new CommonClubsControllerMappingString('Bezeichnung'))
);
}

View File

@ -8,7 +8,7 @@ class CommonClubsModelFactoryPosition extends AbstractCommonClubsModelFactory
protected function fetchAttributes()
{
return array(
new CommonClubsModelColumnString('name')
new CommonClubsModelColumnString('name', new CommonClubsControllerMappingString('Bezeichnung'))
);
}

View File

@ -6,7 +6,8 @@ defined('_JEXEC') or die;
class ClubsControllerClub extends AbstractClubsController
{
protected function getNameOfElement()
protected function getSingleBaseName()
{
return 'club';
}
@ -27,7 +28,7 @@ class ClubsControllerClub extends AbstractClubsController
);
}
protected function filterPreCheck(&$values)
protected function filterRawCheck(&$values)
{
if(is_null($values['charitable']))
$values['charitable'] = false;
@ -41,4 +42,10 @@ class ClubsControllerClub extends AbstractClubsController
{
$values['president'] = $values['president']->getId();
}
protected function getFactory()
{
return new CommonClubsModelFactoryClub();
}
}

View File

@ -6,18 +6,15 @@ defined('_JEXEC') or die;
class ClubsControllerOffer extends AbstractClubsController
{
protected function getNameOfElement()
protected function getSingleBaseName()
{
return 'offer';
}
protected function getDataMapping()
protected function getFactory()
{
return array(
'name' => array('required'=>true, 'name'=>'Bezeichnung', 'filter'=>'string')
);
return new CommonClubsModelFactoryOffer();
}
}

View File

@ -6,12 +6,6 @@ defined('_JEXEC') or die;
class ClubsControllerPosition extends AbstractClubsController
{
protected function getDataMapping()
{
return array(
'name'=>array('required'=>true, 'filter'=>'string', 'name'=>'Bezeichung')
);
}
protected function getFactory()
{
return new CommonClubsModelFactoryPosition();

View File

@ -42,9 +42,9 @@ class ClubsControllerUser extends AbstractClubsController
* {@inheritDoc}
* @see AbstractClubsController::checkData()
*/
protected function checkDataIsValid($values, $isNew, $obj)
protected function rawDataIsValid($values, $isNew, $obj)
{
if(! parent::checkDataIsValid($values, $isNew, $obj))
if(! parent::rawDataIsValid($values, $isNew, $obj))
return false;
// TODO Auto-generated method stub