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

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