From 904d31843a9b03b24e53eeb4851322ca4fd4451e Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 3 Jun 2019 11:35:07 +0200 Subject: [PATCH] Abstract list working in general, tested with single example --- src/admin/abstract/view/list.php | 67 +++++++++++++++++++++ src/admin/abstract/view/single.php | 78 +++++++++++++++---------- src/admin/common/abstract/model.php | 9 ++- src/admin/views/offers/tmpl/default.php | 10 ++-- src/admin/views/offers/view.html.php | 17 ++++-- 5 files changed, 138 insertions(+), 43 deletions(-) create mode 100644 src/admin/abstract/view/list.php diff --git a/src/admin/abstract/view/list.php b/src/admin/abstract/view/list.php new file mode 100644 index 0000000..d794306 --- /dev/null +++ b/src/admin/abstract/view/list.php @@ -0,0 +1,67 @@ +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(); + } + +} diff --git a/src/admin/abstract/view/single.php b/src/admin/abstract/view/single.php index 70d1a6a..8afa862 100644 --- a/src/admin/abstract/view/single.php +++ b/src/admin/abstract/view/single.php @@ -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(); - } diff --git a/src/admin/common/abstract/model.php b/src/admin/common/abstract/model.php index 90babdc..65b4fac 100644 --- a/src/admin/common/abstract/model.php +++ b/src/admin/common/abstract/model.php @@ -327,12 +327,15 @@ abstract class AbstractCommonClubsModel $vals = $this->filterPackData($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); $vals = $this->unpackExternalReferencesFromKeys($data); diff --git a/src/admin/views/offers/tmpl/default.php b/src/admin/views/offers/tmpl/default.php index 433af8d..470fbd5 100644 --- a/src/admin/views/offers/tmpl/default.php +++ b/src/admin/views/offers/tmpl/default.php @@ -11,18 +11,18 @@ defined('_JEXEC') or die; Bezeichnung - Löschen? + id -offers as $offer): ?> +objects as $offer): ?> getId()); ?> - getName()); ?> - getId()); ?>'>Del + getId(), $this->changeUrl); ?>'>getName()); ?> + getId(), $this->delUrl); ?>'>Löschen getId()); ?> -
'>Neues Angebot anlegen
+
Neues Angebot anlegen
diff --git a/src/admin/views/offers/view.html.php b/src/admin/views/offers/view.html.php index 36b7fbb..dff78b7 100644 --- a/src/admin/views/offers/view.html.php +++ b/src/admin/views/offers/view.html.php @@ -1,20 +1,27 @@ offers = ClubsOffer::loadOffers(); - - ToolbarHelper::title('Club-Management - Angebote'); + ToolbarHelper::title('Club-Management - Angebote', 'list'); parent::display($tpl); } + protected function getFactory() + { + return new CommonClubsModelFactoryOffer(); + } + + protected function getSingleBaseName() + { + return 'offer'; + } + }