diff --git a/src/admin/controllers/offer.php b/src/admin/controllers/offer.php new file mode 100644 index 0000000..234d2cf --- /dev/null +++ b/src/admin/controllers/offer.php @@ -0,0 +1,108 @@ +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)); + } + + function change() + { + $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)); + } + + 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 new file mode 100644 index 0000000..3143b96 --- /dev/null +++ b/src/admin/mymodels/offer.php @@ -0,0 +1,157 @@ +id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name) + { + $this->name = $name; + } + + protected function loadData(array $data) + { + $this->id = $data['id']; + $this->name = $data['name']; + } + + protected function __construct() + {} + + 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; + } + + + 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; + } + + public static function createOffer() + { + $offer = new ClubsOffer(); + $offer->id = 'new'; + return $offer; + } + + + public function save() + { + if($this->id === 'new') + $this->insertOffer(); + else + $this->updateOffer(); + } + + private function insertOffer() + { + $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(); + } + + 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/user.php b/src/admin/mymodels/user.php index c6d64a1..1b4ddbd 100644 --- a/src/admin/mymodels/user.php +++ b/src/admin/mymodels/user.php @@ -13,7 +13,6 @@ class PasswordInvalidException extends Exception class ClubsUser { - protected $dbo; protected $id; protected $user; protected $hash; @@ -184,9 +183,7 @@ class ClubsUser } protected function __construct() - { - $this->dbo = Factory::getDbo(); - } + {} public static function loadUsers() { @@ -261,7 +258,8 @@ class ClubsUser private function insertUser() { - $q = $this->dbo->getQuery(true); + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); $vuser = $q->q($this->user); $vpassword = $q->q($this->hash); @@ -277,16 +275,17 @@ class ClubsUser ->values("$vuser, $vpassword, $vname, $vaddress, $vcity, $vmail, $vphone, $vmobile") ; - $this->dbo->transactionStart(); - $this->dbo->setQuery($q); - $this->dbo->execute(); - $this->id = $this->dbo->insertid(); - $this->dbo->transactionCommit(); + $dbo->transactionStart(); + $dbo->setQuery($q); + $dbo->execute(); + $this->id = $dbo->insertid(); + $dbo->transactionCommit(); } private function updateUser() { - $q = $this->dbo->getQuery(true); + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); $vuser = $q->q($this->user); $vpassword = $q->q($this->hash); @@ -311,8 +310,8 @@ class ClubsUser ->where("id=". (int) $this->id) ; - $this->dbo->setQuery($q); - $this->dbo->execute(); + $dbo->setQuery($q); + $dbo->execute(); } private function checkHash(string $password) @@ -323,10 +322,13 @@ class ClubsUser if(password_needs_rehash($this->hash, PASSWORD_DEFAULT)) { $this->hash = password_hash($password, PASSWORD_DEFAULT); - $q = $this->dbo->getQuery(true); + + $dbo = Factory::getDbo(); + + $q = $dbo->getQuery(true); $q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id); - $this->dbo->setQuery($q); - $this->dbo->execute(); + $dbo->setQuery($q); + $dbo->execute(); } } @@ -334,13 +336,14 @@ class ClubsUser { if($this->id === 'new') return; + $dbo = Factory::getDbo(); - $q = $this->dbo->getQuery(true); + $q = $dbo->getQuery(true); $q->delete('#__club_users') ->where('id=' . (int) $this->id); - $this->dbo->setQuery($q); - $this->dbo->execute(); + $dbo->setQuery($q); + $dbo->execute(); } public static function isUserNameFree($username, int $id = -1) diff --git a/src/admin/views/offer/tmpl/default.php b/src/admin/views/offer/tmpl/default.php new file mode 100644 index 0000000..a52c08a --- /dev/null +++ b/src/admin/views/offer/tmpl/default.php @@ -0,0 +1,27 @@ + + +
+ + + + + + + isNew): ?> + + + + + +
Bezeichnung
IDoffer->getId(); ?>
+ +
'>Zurück zur Übersicht +
+ diff --git a/src/admin/views/offer/view.html.php b/src/admin/views/offer/view.html.php new file mode 100644 index 0000000..92cf22f --- /dev/null +++ b/src/admin/views/offer/view.html.php @@ -0,0 +1,47 @@ +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); + } +} diff --git a/src/admin/views/offers/tmpl/default.php b/src/admin/views/offers/tmpl/default.php new file mode 100644 index 0000000..433af8d --- /dev/null +++ b/src/admin/views/offers/tmpl/default.php @@ -0,0 +1,28 @@ + + + + + + + + + + +offers as $offer): ?> +getId()); ?> + + + + + + +
BezeichnungLöschen?id
getName()); ?>getId()); ?>'>DelgetId()); ?>
+ +
'>Neues Angebot anlegen
diff --git a/src/admin/views/offers/view.html.php b/src/admin/views/offers/view.html.php new file mode 100644 index 0000000..36b7fbb --- /dev/null +++ b/src/admin/views/offers/view.html.php @@ -0,0 +1,20 @@ +offers = ClubsOffer::loadOffers(); + + ToolbarHelper::title('Club-Management - Angebote'); + parent::display($tpl); + } + +}