Abstract list working in general, tested with single example

This commit is contained in:
2019-06-03 11:35:07 +02:00
parent dd52d7ca31
commit 904d31843a
5 changed files with 138 additions and 43 deletions

View File

@@ -0,0 +1,67 @@
<?php
// No direct access.
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Router\Route;
defined('_JEXEC') or die;
abstract class AbstractClubsViewList extends HtmlView
{
/**
* @var AbstractCommonClubsModel
*/
protected $objects;
protected $delUrl;
protected $addUrl;
protected $changeUrl;
private $prepared = FALSE;
public function prepareDisplay()
{
$this->prepared = TRUE;
$this->objects = $this->loadObjects();
$baseUrl = "index.php?option=com_clubs";
$viewName = $this->getSingleViewName();
$controllerName = $this->getControllerName();
$this->addUrl = Route::_("$baseUrl&view=$viewName&id=new");
$this->changeUrl = Route::_("$baseUrl&view=$viewName&id=__ID__");
$this->delUrl = Route::_("$baseUrl&task=$controllerName.delete&id=__ID__");
}
public function display($tpl = null)
{
if(!$this->prepared)
$this->prepareDisplay();
parent::display($tpl);
}
protected abstract function getSingleBaseName();
protected function getControllerName()
{
return $this->getSingleBaseName();
}
protected function getSingleViewName()
{
return $this->getSingleBaseName();
}
/**
* @return AbstractCommonClubsModelFactory
*/
protected abstract function getFactory();
protected function loadObjects()
{
$factory = $this->getFactory();
return $factory->loadElements();
}
}

View File

@@ -11,6 +11,9 @@ abstract class AbstractClubsViewSingle extends HtmlView
{
protected $address;
/**
* @var AbstractCommonClubsModel
*/
protected $object;
protected $isNew;
@@ -23,32 +26,28 @@ abstract class AbstractClubsViewSingle extends HtmlView
$input = Factory::getApplication()->input;
$id = $input->get->get('id');
$modelClass = 'Clubs' . $this->getModelName();
$controllerName = $this->getControllerName();
$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->address = Route::_("index.php?option=com_clubs&task={$controllerName}.new");
$this->object = $this->createNewObject();
$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);
$id = (int) $id;
$this->address = Route::_("index.php?option=com_clubs&task={$controllerName}.change&id=$id");
$this->object = $this->loadObject($id);
$this->isNew = false;
}
else
throw new Exception('Need an object id.');
if($input->get->get('data', null, 'json') != null)
throw new Exception('Need a valid object id.');
$jsonData = $input->get->get('data', null, 'json');
if($jsonData !== null)
{
// Restore previous data
$dataurl = $input->get->get('data', null, 'json');
$data = json_decode($dataurl, true);
$controller = $this->getElementController();
$controller->applyData($this->object, $data);
$this->object->unpack($jsonData);
}
}
@@ -61,26 +60,45 @@ abstract class AbstractClubsViewSingle extends HtmlView
parent::display($tpl);
}
protected abstract function getViewName();
// protected abstract function getViewName();
protected function getControllerName()
protected abstract function getControllerName();
// protected function getModelName()
// {
// $name = $this->getViewName();
// return $this->capitalize($name);
// }
// protected function getModelClass()
// {
// return 'Clubs' . $this->getModelName();
// }
// private function capitalize($s)
// {
// $first = substr($s, 0, 1);
// $rest = substr($s, 1);
// return strtoupper($first) . $rest;
// }
// protected abstract function getElementController();
/**
* @return AbstractCommonClubsModelFactory
*/
protected abstract function getFactory();
protected function createNewObject()
{
return $this->getViewName();
$factory = $this->getFactory();
return $factory->createNew();
}
protected function getModelName()
protected function loadObject($id)
{
$name = $this->getViewName();
return $this->capitalize($name);
$factory = $this->getFactory();
return $factory->loadById($id);
}
private function capitalize($s)
{
$first = substr($s, 0, 1);
$rest = substr($s, 1);
return strtoupper($first) . $rest;
}
protected abstract function getElementController();
}