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. // No direct access.
defined('_JEXEC') or die; defined('_JEXEC') or die;
class DataParsingException extends Exception {}
class DataInvalidException extends Exception {}
abstract class AbstractClubsController extends BaseController abstract class AbstractClubsController extends BaseController
{ {
/** /**
@ -27,11 +30,6 @@ abstract class AbstractClubsController extends BaseController
return $this->getSingleBaseName(); return $this->getSingleBaseName();
} }
/**
* @todo Make this OO conforme
*/
protected abstract function getDataMapping();
public function new() public function new()
{ {
$factory = $this->getFactory(); $factory = $this->getFactory();
@ -57,68 +55,110 @@ abstract class AbstractClubsController extends BaseController
* @param int $id * @param int $id
*/ */
protected function saveToDatabase($obj, $id) protected function saveToDatabase($obj, $id)
{
try
{ {
// Fetch the posted data // Fetch the posted data
$values = $this->loadData(); $values = $this->loadData();
$this->filterPreCheck($values); $this->filterRawCheck($values);
// Check the input data // 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) $obj->setValues($values, true);
{
$urldata = $obj->pack();
$this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false));
return;
}
$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 // Do the actual work
$obj->save(); $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() protected function loadData()
{ {
$values = array(); $values = array();
$factory = $this->getFactory();
$input = Factory::getApplication()->input->post; $input = Factory::getApplication()->input->post;
foreach($this->getDataMapping() as $m => $f) foreach($factory->getAttributes() as $column)
{ {
$filter = (isset($f['filter'])) ? $f['filter'] : 'string'; $values[$column->getAlias()] = $column->getFilter()->getFilteredValue($input, $column->getAlias());
$values[$m] = $input->get($m, null, $filter);
} }
return $values; 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 array $values
* @param AbstractCommonClubsModel $obj * @param AbstractCommonClubsModel $obj
* @return boolean * @return boolean
*/ */
protected function checkDataIsValid($values, $obj) protected function rawDataIsValid($values)
{ {
$error = false; $error = false;
// Check existence of the required fields
foreach ($this->getDataMapping() as $m => $v)
{
if(! isset($v['required']) || ! $v['required'])
continue;
// Field is required $factory = $this->getFactory();
if(! $this->fieldValid($m, $values[$m], $v))
foreach($factory->getAttributes() as $column)
{ {
$fname = (isset($v['name'])) ? $v['name'] : $m; if(! $column->getFilter()->rawValueValid($values[$column->getAlias()]))
Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); {
$fname = $column->getFilter()->getName();
Factory::getApplication()->enqueueMessage("Das Feld $fname ist fehlerhaft.", 'error');
$error = true; $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('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract');
JLoader::registerPrefix('AbstractCommonClubs', JPATH_ROOT . '/administrator/components/com_clubs/common/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('CommonClubsModel', JPATH_ROOT . '/administrator/components/com_clubs/common/models');
JLoader::registerPrefix('CommonClubsControllerMapping', JPATH_ROOT . '/administrator/components/com_clubs/common/controllermappings');
$controller = BaseController::getInstance("Clubs"); $controller = BaseController::getInstance("Clubs");
$input = Factory::getApplication()->input; $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; return $this->new;
} }
public function setValues($values) public function setValues($values, $unpack = false)
{ {
if($unpack)
$this->values = $this->unpackExternalReferencesFromKeys($values);
else
$this->values = $values; $this->values = $values;
} }
@ -351,4 +354,9 @@ abstract class AbstractCommonClubsModel
$this->setValues($vals); $this->setValues($vals);
} }
public function dataIsValid()
{
return true;
}
} }

View File

@ -8,7 +8,10 @@ abstract class AbstractCommonClubsModelColumn
protected $alias; protected $alias;
protected $column; protected $column;
protected $required; /**
* @var AbstractCommonClubsControllerMapping
*/
protected $filter;
public function getAlias() public function getAlias()
{ {
@ -20,17 +23,12 @@ abstract class AbstractCommonClubsModelColumn
return $this->column; return $this->column;
} }
public function isRequired()
{
return $this->required;
}
public abstract function isSimpleType(); public abstract function isSimpleType();
public function __construct($alias, $required = true, $column = null) public function __construct($alias, $filter, $column = null)
{ {
$this->alias = $alias; $this->alias = $alias;
$this->required = $required; $this->filter = $filter;
if(isset($column)) if(isset($column))
$this->column = $column; $this->column = $column;
else else
@ -71,4 +69,9 @@ abstract class AbstractCommonClubsModelColumn
return $value; 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; 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)) if(empty($className))
throw new Exception('Classname must be non-empty.'); throw new Exception('Classname must be non-empty.');

View File

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

View File

@ -8,7 +8,7 @@ class CommonClubsModelFactoryPosition extends AbstractCommonClubsModelFactory
protected function fetchAttributes() protected function fetchAttributes()
{ {
return array( 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 class ClubsControllerClub extends AbstractClubsController
{ {
protected function getNameOfElement()
protected function getSingleBaseName()
{ {
return 'club'; return 'club';
} }
@ -27,7 +28,7 @@ class ClubsControllerClub extends AbstractClubsController
); );
} }
protected function filterPreCheck(&$values) protected function filterRawCheck(&$values)
{ {
if(is_null($values['charitable'])) if(is_null($values['charitable']))
$values['charitable'] = false; $values['charitable'] = false;
@ -41,4 +42,10 @@ class ClubsControllerClub extends AbstractClubsController
{ {
$values['president'] = $values['president']->getId(); $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 class ClubsControllerOffer extends AbstractClubsController
{ {
protected function getNameOfElement() protected function getSingleBaseName()
{ {
return 'offer'; return 'offer';
} }
protected function getDataMapping() protected function getFactory()
{ {
return array( return new CommonClubsModelFactoryOffer();
'name' => array('required'=>true, 'name'=>'Bezeichnung', 'filter'=>'string')
);
} }
} }

View File

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

View File

@ -42,9 +42,9 @@ class ClubsControllerUser extends AbstractClubsController
* {@inheritDoc} * {@inheritDoc}
* @see AbstractClubsController::checkData() * @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; return false;
// TODO Auto-generated method stub // TODO Auto-generated method stub