Abstract list working in general, tested with single example

This commit is contained in:
Christian Wolf 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; protected $address;
/**
* @var AbstractCommonClubsModel
*/
protected $object; protected $object;
protected $isNew; protected $isNew;
@ -23,32 +26,28 @@ abstract class AbstractClubsViewSingle extends HtmlView
$input = Factory::getApplication()->input; $input = Factory::getApplication()->input;
$id = $input->get->get('id'); $id = $input->get->get('id');
$modelClass = 'Clubs' . $this->getModelName(); $controllerName = $this->getControllerName();
$controller = $this->getControllerName();
if($id === 'new') if($id === 'new')
{ {
$this->address = Route::_("index.php?option=com_clubs&task={$controller}.new"); $this->address = Route::_("index.php?option=com_clubs&task={$controllerName}.new");
$this->object = call_user_func(array($modelClass, 'create' . $this->getModelName())); $this->object = $this->createNewObject();
$this->isNew = true; $this->isNew = true;
} }
else if(is_numeric($id)) else if(is_numeric($id))
{ {
$this->address = Route::_("index.php?option=com_clubs&task={$controller}.change"); $id = (int) $id;
$this->object = call_user_func(array($modelClass, 'load' . $this->getModelName()), (int) $id); $this->address = Route::_("index.php?option=com_clubs&task={$controllerName}.change&id=$id");
$this->object = $this->loadObject($id);
$this->isNew = false; $this->isNew = false;
} }
else else
throw new Exception('Need an object id.'); throw new Exception('Need a valid object id.');
if($input->get->get('data', null, 'json') != null) $jsonData = $input->get->get('data', null, 'json');
if($jsonData !== null)
{ {
// Restore previous data $this->object->unpack($jsonData);
$dataurl = $input->get->get('data', null, 'json');
$data = json_decode($dataurl, true);
$controller = $this->getElementController();
$controller->applyData($this->object, $data);
} }
} }
@ -61,26 +60,45 @@ abstract class AbstractClubsViewSingle extends HtmlView
parent::display($tpl); 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(); $factory = $this->getFactory();
return $this->capitalize($name); 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();
} }

View File

@ -327,12 +327,15 @@ abstract class AbstractCommonClubsModel
$vals = $this->filterPackData($vals); $vals = $this->filterPackData($vals);
$json = json_encode($vals); $json = json_encode($vals);
return urldecode($json); return urlencode($json);
} }
public function unpack($str) public function unpack($str, $decode = false)
{ {
$json = urlencode($str); if($decode)
$json = urldecode($str);
else
$json = $str;
$data = json_decode($json, true); $data = json_decode($json, true);
$vals = $this->unpackExternalReferencesFromKeys($data); $vals = $this->unpackExternalReferencesFromKeys($data);

View File

@ -11,18 +11,18 @@ defined('_JEXEC') or die;
<thead> <thead>
<tr> <tr>
<th width='30%'>Bezeichnung</th> <th width='30%'>Bezeichnung</th>
<th width='5%'>L&ouml;schen?</th> <th width='5%'></th>
<th width='5%'>id</th> <th width='5%'>id</th>
</tr> </tr>
</thead> </thead>
<?php foreach($this->offers as $offer): ?> <?php foreach($this->objects as $offer): ?>
<?php $link = Route::_('index.php?option=com_clubs&view=offer&id=' . $offer->getId()); ?> <?php $link = Route::_('index.php?option=com_clubs&view=offer&id=' . $offer->getId()); ?>
<tr> <tr>
<td><a href='<?php echo $link; ?>'><?php echo htmlentities($offer->getName()); ?></a></td> <td><a href='<?php echo str_replace('__ID__', $offer->getId(), $this->changeUrl); ?>'><?php echo htmlentities($offer->getName()); ?></a></td>
<td><a href='<?php echo Route::_('index.php?option=com_clubs&task=offer.delete&id=' . $offer->getId()); ?>'>Del</a></td> <td><a href='<?php echo str_replace('__ID__', $offer->getId(), $this->delUrl); ?>'><span class='icon-delete'></span>L&ouml;schen</a></td>
<td><?php echo htmlentities($offer->getId()); ?></td> <td><?php echo htmlentities($offer->getId()); ?></td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
</table> </table>
<div><a href='<?php echo Route::_('index.php?option=com_clubs&view=offer&id=new'); ?>'>Neues Angebot anlegen</a></div> <div><a href='<?php echo $this->addUrl; ?>'><span class='icon-new'></span>Neues Angebot anlegen</a></div>

View File

@ -1,20 +1,27 @@
<?php <?php
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper; use Joomla\CMS\Toolbar\ToolbarHelper;
// No direct access. // No direct access.
defined('_JEXEC') or die; defined('_JEXEC') or die;
class ClubsViewOffers extends HtmlView class ClubsViewOffers extends AbstractClubsViewList
{ {
function display($tpl = null) function display($tpl = null)
{ {
$this->offers = ClubsOffer::loadOffers(); ToolbarHelper::title('Club-Management - Angebote', 'list');
ToolbarHelper::title('Club-Management - Angebote');
parent::display($tpl); parent::display($tpl);
} }
protected function getFactory()
{
return new CommonClubsModelFactoryOffer();
}
protected function getSingleBaseName()
{
return 'offer';
}
} }