diff --git a/src/admin/abstract/controller.php b/src/admin/abstract/controller.php new file mode 100644 index 0000000..223d435 --- /dev/null +++ b/src/admin/abstract/controller.php @@ -0,0 +1,189 @@ +getNameOfElement(); + } + + protected function getNameOfView() + { + return strtolower($this->getNameOfElement()); + } + + protected abstract function getDataMapping(); + + function new() + { + $obj = call_user_func(array('Clubs' . $this->getNameOfElement(), 'create' . $this->getNameOfElement())); + + // Fetch the posted data + $values = $this->loadData(); + + $this->filterPreCheck($values); + + // Check the input data + $error = $this->checkData($values); + + $view = $this->getNameOfView(); + + if($error) + { + $urldata = $this->packData($values); + $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id=new&data={$urldata}", false)); + return; + } + + $this->applyData($obj, $values); + + // Do the actual work + $obj->save(); + $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false)); + } + + function change() + { + $app = Factory::getApplication(); + $input = $app->input; + $id = (int) $input->post->getInt('id'); + + $obj = call_user_func(array('Clubs' . $this->getNameOfElement(), 'load' . $this->getNameOfElement()), (int) $id); + + // Fetch the posted data + $values = $this->loadData(); + + $this->filterPreCheck($values); + + // Check the input data + $error = $this->checkData($values); + + $view = $this->getNameOfView(); + + if($error) + { + $urldata = $this->packData($values); + $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false)); + return; + } + + $this->applyData($obj, $values); + + // Do the actual work + $obj->save(); + $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false)); + } + + protected function loadData() + { + $values = array(); + $input = Factory::getApplication()->input->post; + + foreach($this->getDataMapping() as $m => $f) + { + $filter = (isset($f['filter'])) ? $f['filter'] : 'string'; + + $values[$m] = $input->get($m, null, $filter); + } + + return $values; + } + + protected function filterPreCheck(&$values){} + + protected function checkData($values) + { + $error = false; + // Check existence of the required fields + foreach ($this->getDataMapping() as $m => $v) + { + if(! isset($v['required']) || ! $v['required']) + continue; + + // Field is required + if(! $this->fieldValid($m, $values[$m], $v)) + { + $fname = (isset($v['name'])) ? $v['name'] : $m; + Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); + $error = true; + } + } + + return $error; + } + + protected function fieldValid($name, $value, $options) + { + if(empty($value)) + return false; + + if(isset($options['filter'])) + { + switch($options['filter']) + { + case 'string': + if(empty(trim($value))) + return false; + } + } + + return true; + } + + private function packData($values) + { + $data = array(); + foreach($this->getDataMapping() as $i) + $data[$i] = $values[$i]; + + return urlencode(json_encode($data)); + } + + public function applyData($obj, $values) + { + foreach($this->getDataMapping() as $m => $v) + { + $functionName = $this->getSetterMethodName($m, $v); + $value = (isset($values[$m])) ? $values[$m] : null; + $obj->$functionName($value); + } + } + + private function getSetterMethodName($m, $options) + { + if(isset($options['setter'])) + return $options['setter']; + + $firstChar = substr($m, 0, 1); + $restChars = substr($m, 1); + return 'set' . strtoupper($firstChar) . $restChars; + } + + function delete() + { + $app = Factory::getApplication(); + + $id = $app->input->get->getInt('id'); + $name = $this->getNameOfElement(); + $app->enqueueMessage("Removal of $name with id $id."); + + $className = 'Clubs' . $this->getModelName(); + $functionName = 'load' . $this->getModelName(); + $element = call_user_func(array($className, $functionName), $id); + $element->delete(); + + $view = $this->getNameOfView(); + $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false)); + } + +} diff --git a/src/admin/mymodels/abstractmodel.php b/src/admin/abstract/model.php similarity index 85% rename from src/admin/mymodels/abstractmodel.php rename to src/admin/abstract/model.php index 6bfe2e2..f6470b9 100644 --- a/src/admin/mymodels/abstractmodel.php +++ b/src/admin/abstract/model.php @@ -5,7 +5,7 @@ use Joomla\CMS\Factory; defined('_JEXEC') or die; -abstract class ClubsAbstractModel +abstract class AbstractClubsModel { protected $id; @@ -98,12 +98,12 @@ abstract class ClubsAbstractModel foreach($mappings as $m) $values[$m] = $this->$m; - $this->preFilter($values); + $this->filter($values); foreach($mappings as $m) $values[$m] = $q->q($values[$m]); - $this->postFilter($values); + $this->postQuoteFilter($values); $q->insert($this->getTableName()) ->columns(array_map(array($q, 'qn'), $mappings)) @@ -130,12 +130,12 @@ abstract class ClubsAbstractModel foreach($mapping as $m) $values[$m] = $this->$m; - $this->preFilter($values); + $this->filter($values); foreach($mapping as $m) $values[$m] = $q->q($values[$m]); - $this->postFilter($values); + $this->postQuoteFilter($values); $q->update($this->getTableName()); foreach($mapping as $m) @@ -146,11 +146,15 @@ abstract class ClubsAbstractModel $dbo->execute(); } + protected function prepareDelete(){} + public function delete() { if($this->id === 'new') return; + $this->prepareDelete(); + $dbo = Factory::getDbo(); $q = $dbo->getQuery(true); @@ -161,13 +165,23 @@ abstract class ClubsAbstractModel $dbo->execute(); } + protected function getRequiredDataMappings() + { + return $this->getDataMappings(); + } + protected function isDataValid() { + foreach($this->getRequiredDataMappings() as $m) + { + if(!isset($this->$m) || empty($this->$m) || $this->$m === null) + return false; + } return true; } - protected function preFilter(&$values){} - protected function postFilter(&$values){} + protected function filter(&$values){} + protected function postQuoteFilter(&$values){} } diff --git a/src/admin/abstract/view/single.php b/src/admin/abstract/view/single.php new file mode 100644 index 0000000..d9df3e2 --- /dev/null +++ b/src/admin/abstract/view/single.php @@ -0,0 +1,77 @@ +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(); + +} diff --git a/src/admin/clubs.php b/src/admin/clubs.php index 0fc46f6..f52ed19 100644 --- a/src/admin/clubs.php +++ b/src/admin/clubs.php @@ -7,6 +7,7 @@ use Joomla\CMS\MVC\Controller\BaseController; defined('_JEXEC') or die; JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels'); +JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract'); $controller = BaseController::getInstance("Clubs"); $input = Factory::getApplication()->input; diff --git a/src/admin/controllers/offer.php b/src/admin/controllers/offer.php index 234d2cf..384ba1e 100644 --- a/src/admin/controllers/offer.php +++ b/src/admin/controllers/offer.php @@ -1,108 +1,23 @@ input; - $o = ClubsOffer::createOffer(); - - // Fetch the posted data - $name = $input->post->getString('name'); - - // Check the input data - $error = false; - - // Check existence of the other fields - $fields = array('name'=>'Bezeichnung'); - foreach ($fields as $f => $fname) - { - $fvalue = $$f; - if(! isset($fvalue) || empty(trim($fvalue))) - { - $app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); - $error = true; - } - } - - if($error) - { - $data = array(); - foreach(array('name') as $i) - $data[$i] = $$i; - - $urldata = urlencode(json_encode($data)); - $this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=new&data=' . $urldata, false)); - return; - } - - $o->setName($name); - - // Do the actual work - $o->save(); - $this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false)); + return 'offer'; } - - function change() + + protected function getDataMapping() { - $app = Factory::getApplication(); - $input = $app->input; - $id = (int) $input->post->getInt('id'); - $o = ClubsOffer::loadOffer((int) $id); - - // Fetch the posted data - $name = $input->post->getString('name'); - - // Check the input data - $error = false; - - // Check existence of the other fields - $fields = array('name'=>'Bezeichnung'); - foreach ($fields as $f => $fname) - { - $fvalue = $$f; - if(! isset($fvalue) || empty(trim($fvalue))) - { - $app->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); - $error = true; - } - } - - if($error) - { - $data = array(); - foreach(array('name') as $i) - $data[$i] = $$i; - - $urldata = urlencode(json_encode($data)); - $this->setRedirect(Route::_('index.php?option=com_clubs&view=offer&id=' . $id . '&data=' . $urldata, false)); - return; - } - - $o->setName($name); - - // Do the actual work - $o->save(); - $this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false)); + return array( + 'name' => array('required'=>true, 'name'=>'Bezeichnung', 'filter'=>'string') + ); } + - function delete() - { - $app = Factory::getApplication(); - $id = $app->input->get->getInt('id'); - $app->enqueueMessage("Removal of offer with id $id."); - $user = ClubsOffer::loadOffer($id); - $user->delete(); - $this->setRedirect(Route::_('index.php?option=com_clubs&view=offers', false)); - } } diff --git a/src/admin/mymodels/offer.php b/src/admin/mymodels/offer.php index 3143b96..127d479 100644 --- a/src/admin/mymodels/offer.php +++ b/src/admin/mymodels/offer.php @@ -1,21 +1,12 @@ id; - } /** * @return string @@ -28,62 +19,26 @@ class ClubsOffer /** * @param string $name */ - public function setName(string $name) + public function setName($name) { $this->name = $name; } - protected function loadData(array $data) - { - $this->id = $data['id']; - $this->name = $data['name']; - } - protected function __construct() {} + private const tableName = '#__club_offers'; + private const className = 'ClubsOffer'; + public static function loadOffers() { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - $q->select('*') - ->from('#__club_offers'); - $dbo->setQuery($q); - $dbo->execute(); - $list = $dbo->loadAssocList('id'); - - $ret = array(); - foreach($list as $o) - { - $oo = new ClubsOffer(); - $oo->loadData($o); - - $ret[] = $oo; - } - - return $ret; + return self::loadElements(self::tableName, self::className); } public static function loadOffer(int $id) { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - $q->select('*')->from('#__club_offers')->where('id=' . (int) $id); - $dbo->setQuery($q); - $dbo->execute(); - - $row = $dbo->loadAssoc(); - - if($row == null) - { - throw new Exception("No offer found."); - // TODO - } - - $offer = new ClubsOffer(); - $offer->loadData($row); - return $offer; + return self::loadElement($id, self::tableName, self::className); } public static function createOffer() @@ -93,65 +48,14 @@ class ClubsOffer return $offer; } - - public function save() + protected function getDataMappings() { - if($this->id === 'new') - $this->insertOffer(); - else - $this->updateOffer(); + return array('name'); } - - private function insertOffer() + + protected function getTableName() { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $vname = $q->q($this->name); - - $q->insert('#__club_offers') - ->columns(array('name')) - ->values("$vname") - ; - - $dbo->transactionStart(); - $dbo->setQuery($q); - $dbo->execute(); - $this->id = $dbo->insertid(); - $dbo->transactionCommit(); + return self::tableName; } - private function updateOffer() - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $vname = $q->q($this->name); - - $q->update('#__club_offers') - ->set(array( - "name=$vname" - )) - ->where("id=". (int) $this->id) - ; - - $dbo->setQuery($q); - $dbo->execute(); - } - - public function delete() - { - if($this->id === 'new') - return; - $dbo = Factory::getDbo(); - - $q = $dbo->getQuery(true); - $q->delete('#__club_offers') - ->where('id=' . (int) $this->id); - - $dbo->setQuery($q); - $dbo->execute(); - } - - } diff --git a/src/admin/mymodels/position.php b/src/admin/mymodels/position.php index a70c06e..df6f884 100644 --- a/src/admin/mymodels/position.php +++ b/src/admin/mymodels/position.php @@ -1,21 +1,11 @@ id; - } /** * @return string @@ -33,57 +23,21 @@ class ClubsPosition $this->name = $name; } - protected function loadData(array $data) - { - $this->id = $data['id']; - $this->name = $data['name']; - } - protected function __construct() {} + private const tableName = '#__club_positions'; + private const className = 'ClubsPosition'; + public static function loadPositions() { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - $q->select('*') - ->from('#__club_positions'); - $dbo->setQuery($q); - $dbo->execute(); - $list = $dbo->loadAssocList('id'); - - $ret = array(); - foreach($list as $p) - { - $po = new ClubsPosition(); - $po->loadData($p); - - $ret[] = $po; - } - - return $ret; + return self::loadElements(self::tableName, self::className); } public static function loadPosition(int $id) { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - $q->select('*')->from('#__club_positions')->where('id=' . (int) $id); - $dbo->setQuery($q); - $dbo->execute(); - - $row = $dbo->loadAssoc(); - - if($row == null) - { - throw new Exception("No position found."); - // TODO - } - - $position = new ClubsPosition(); - $position->loadData($row); - return $position; + return self::loadElement($id, self::tableName, self::className); } public static function createPosition() @@ -93,65 +47,16 @@ class ClubsPosition return $position; } - - public function save() + protected function getDataMappings() { - if($this->id === 'new') - $this->insertPosition(); - else - $this->updatePosition(); + return array('name'); } - - private function insertPosition() + + protected function getTableName() { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $vname = $q->q($this->name); - - $q->insert('#__club_positions') - ->columns(array('name')) - ->values("$vname") - ; - - $dbo->transactionStart(); - $dbo->setQuery($q); - $dbo->execute(); - $this->id = $dbo->insertid(); - $dbo->transactionCommit(); - } - - private function updatePosition() - { - $dbo = Factory::getDbo(); - $q = $dbo->getQuery(true); - - $vname = $q->q($this->name); - - $q->update('#__club_positions') - ->set(array( - "name=$vname" - )) - ->where("id=". (int) $this->id) - ; - - $dbo->setQuery($q); - $dbo->execute(); - } - - public function delete() - { - if($this->id === 'new') - return; - $dbo = Factory::getDbo(); - - $q = $dbo->getQuery(true); - $q->delete('#__club_positions') - ->where('id=' . (int) $this->id); - - $dbo->setQuery($q); - $dbo->execute(); + return self::tableName; } + } diff --git a/src/admin/views/offer/tmpl/default.php b/src/admin/views/offer/tmpl/default.php index a52c08a..9f5b3c7 100644 --- a/src/admin/views/offer/tmpl/default.php +++ b/src/admin/views/offer/tmpl/default.php @@ -8,16 +8,16 @@ defined('_JEXEC') or die; ?>
- + - + isNew): ?> - +
Bezeichnung
IDoffer->getId(); ?>object->getId(); ?>
diff --git a/src/admin/views/offer/view.html.php b/src/admin/views/offer/view.html.php index 92cf22f..aa35699 100644 --- a/src/admin/views/offer/view.html.php +++ b/src/admin/views/offer/view.html.php @@ -1,47 +1,29 @@ input; - $id = $input->get->get('id'); - - if($id === 'new') - { - $this->address = Route::_('index.php?option=com_clubs&task=offer.new'); - $this->offer = ClubsOffer::createOffer(); - $this->isNew = true; - } - else if(is_numeric($id)) - { - $this->address = Route::_('index.php?option=com_clubs&task=offer.change'); - $this->offer = ClubsOffer::loadOffer((int) $id); - $this->isNew = false; - } - else - throw new Exception('Need a user id.'); - - if($input->get->get('data', null, 'json') != null) - { - // Restore previous data - $dataurl = $input->get->get('data', null, 'json'); - $data = json_decode($dataurl, true); - - $this->offer->setName($data['name']); - - } - ToolbarHelper::title('Club-Management - Angebot'); parent::display($tpl); } + + protected function getViewName() + { + return 'offer'; + } + protected function getElementController() + { + return new ClubsControllerOffer(); + } + + } diff --git a/src/site/clubs.php b/src/site/clubs.php index 9342ee0..6b3569b 100644 --- a/src/site/clubs.php +++ b/src/site/clubs.php @@ -7,6 +7,7 @@ use Joomla\CMS\Factory; defined('_JEXEC') or die; JLoader::discover('Clubs', JPATH_ROOT . '/administrator/components/com_clubs/mymodels'); +JLoader::registerPrefix('AbstractClubs', JPATH_ROOT . '/administrator/components/com_clubs/abstract'); $controller = BaseController::getInstance("Clubs"); $input = Factory::getApplication()->input;