From 893dc754f0ad7ff3984aae17316be2f0dcea8d11 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Wed, 17 Apr 2019 16:41:28 +0200 Subject: [PATCH] Inserted basic structure for clubs and models --- src/admin/mymodels/abstractmodel.php | 155 ++++++++++++ src/admin/mymodels/club.php | 320 +++++++++++++++++++++++++ src/admin/mymodels/place.php | 191 +++++++++++++++ src/admin/views/club/tmpl/default.php | 27 +++ src/admin/views/club/view.html.php | 47 ++++ src/admin/views/clubs/tmpl/default.php | 28 +++ src/admin/views/clubs/view.html.php | 20 ++ 7 files changed, 788 insertions(+) create mode 100644 src/admin/mymodels/abstractmodel.php create mode 100644 src/admin/mymodels/club.php create mode 100644 src/admin/mymodels/place.php create mode 100644 src/admin/views/club/tmpl/default.php create mode 100644 src/admin/views/club/view.html.php create mode 100644 src/admin/views/clubs/tmpl/default.php create mode 100644 src/admin/views/clubs/view.html.php diff --git a/src/admin/mymodels/abstractmodel.php b/src/admin/mymodels/abstractmodel.php new file mode 100644 index 0000000..c3b934d --- /dev/null +++ b/src/admin/mymodels/abstractmodel.php @@ -0,0 +1,155 @@ +id; + } + + protected abstract function getDataMappings(); + protected abstract function getTableName(); + + + protected function loadData(array $data) + { + $this->id = $data['id']; + + foreach($this->getDataMappings() as $m) + $this->$m = $data[$m]; + } + + protected static function loadElements(string $tableName, string $className) + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + $q->select('*') + ->from($tableName); + $dbo->setQuery($q); + $dbo->execute(); + $list = $dbo->loadAssocList(); + + $ret = array(); + foreach($list as $row) + { + $obj = new $className(); + $obj->loadData($row); + + $ret[] = $obj; + } + + return $ret; + } + + /** + * @param int $id + * @throws Exception + * @return ClubsOffer + */ + protected static function loadElement(int $id, string $tableName, string $className) + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + $q->select('*')->from($tableName)->where('id=' . (int) $id); + $dbo->setQuery($q); + $dbo->execute(); + + $row = $dbo->loadAssoc(); + + if($row == null) + { + throw new Exception("No object of class $className found."); + // TODO + } + + $obj = new $className(); + $obj->loadData($row); + return $obj; + } + + public function save() + { + if($this->id === 'new') + $this->insertElement(); + else + $this->updateElement(); + } + + private function insertElement() + { + if(! $this->isDataValid()) + throw new Exception('Data is invalid'); + + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + + $mappings = $this->getDataMappings(); + $values = array(); + foreach($mappings as $m) + $values[$m] = $q->q($this->$m); + + $q->insert($this->getTableName()) + ->columns(array_map(array($q, 'qn'), $mappings)) + ->values(join(',', $values)) + ; + + $dbo->transactionStart(); + $dbo->setQuery($q); + $dbo->execute(); + $this->id = $dbo->insertid(); + $dbo->transactionCommit(); + } + + private function updateElement() + { + if(! $this->isDataValid()) + throw new Exception('Data is invalid'); + + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + + $mapping = $this->getDataMappings(); + $values = array(); + foreach($mapping as $m) + $values[$m] = $q->q($this->$m); + + $q->update($this->getTableName()); + foreach($mapping as $m) + $q->set($q->qn($m) . '=' . $values[$m]); + $q->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($this->getTableName()) + ->where('id=' . (int) $this->id); + + $dbo->setQuery($q); + $dbo->execute(); + } + + protected function isDataValid() + { + return true; + } + +} diff --git a/src/admin/mymodels/club.php b/src/admin/mymodels/club.php new file mode 100644 index 0000000..dbbafff --- /dev/null +++ b/src/admin/mymodels/club.php @@ -0,0 +1,320 @@ +address; + } + + /** + * @return string + */ + public function getCity() + { + return $this->city; + } + + /** + * @return string + */ + public function getHomapge() + { + return $this->homapge; + } + + /** + * @return string + */ + public function getMail() + { + return $this->mail; + } + + /** + * @return string + */ + public function getIban() + { + return $this->iban; + } + + /** + * @return string + */ + public function getBic() + { + return $this->bic; + } + + /** + * @return bool + */ + public function isCharitable() + { + return $this->charitable; + } + + /** + * @return int + */ + public function getPresidentId() + { + return $this->presidentId; + } + + /** + * @return ClubsUser + */ + public function getPresident() + { + return ClubsUser::loadUser((int) $this->presidentId); + } + + /** + * @param string $address + */ + public function setAddress(string $address) + { + $this->address = $address; + } + + /** + * @param string $city + */ + public function setCity(string $city) + { + $this->city = $city; + } + + /** + * @param string $homapge + */ + public function setHomapge(string $homapge) + { + $this->homapge = $homapge; + } + + /** + * @param string $mail + */ + public function setMail(string$mail) + { + $this->mail = $mail; + } + + /** + * @param string $iban + */ + public function setIban(string $iban) + { + $this->iban = $iban; + } + + /** + * @param string $bic + */ + public function setBic(string $bic) + { + $this->bic = $bic; + } + + /** + * @param bool $charitable + */ + public function setCharitable(bool $charitable) + { + $this->charitable = $charitable; + } + + /** + * @param int $presidentId + */ + public function setPresidentId(int $presidentId) + { + $this->presidentId = $presidentId; + } + + /** + * @param ClubsUser $user + */ + public function setPresident(ClubsUser $user) + { + $this->presidentId = $user->getId(); + } + + /** + * @return int + */ + public function getId() + { + return $this->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) + { + $mapping = array('id', 'name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId'); + + foreach($mapping as $m) + $this->$m = $data[$m]; + } + + protected function __construct() + {} + + public static function loadClubs() + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + $q->select('*') + ->from('#__club_clubs'); + $dbo->setQuery($q); + $dbo->execute(); + $list = $dbo->loadAssocList('id'); + + $ret = array(); + foreach($list as $c) + { + $co = new ClubsClub(); + $co->loadData($c); + + $ret[] = $co; + } + + return $ret; + } + + /** + * @param int $id + * @throws Exception + * @return ClubsOffer + */ + public static function loadClub(int $id) + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + $q->select('*')->from('#__club_clubs')->where('id=' . (int) $id); + $dbo->setQuery($q); + $dbo->execute(); + + $row = $dbo->loadAssoc(); + + if($row == null) + { + throw new Exception("No club found."); + // TODO + } + + $club = new ClubsClub(); + $club->loadData($row); + return $club; + } + + public static function createClub() + { + $club = new ClubsClub(); + $club->id = 'new'; + return $club; + } + + + public function save() + { + if($this->id === 'new') + $this->insertClub(); + else + $this->updateClub(); + } + + private function insertClub() + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + + $mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId'); + $values = array(); + foreach($mapping as $m) + $values[$m] = $q->q($this->$m); + + $q->insert('#__club_clubs') + ->columns(array('name')) + ->values(join(',', $values)) + ; + + $dbo->transactionStart(); + $dbo->setQuery($q); + $dbo->execute(); + $this->id = $dbo->insertid(); + $dbo->transactionCommit(); + } + + private function updateClub() + { + $dbo = Factory::getDbo(); + $q = $dbo->getQuery(true); + + $mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId'); + $values = array(); + foreach($mapping as $m) + $values[$m] = $q->q($this->$m); + + $q->update('#__club_clubs'); + foreach($mapping as $m) + $q->set($q->qn($m) . '=' . $values[$m]); + $q->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_clubs') + ->where('id=' . (int) $this->id); + + $dbo->setQuery($q); + $dbo->execute(); + } + + +} diff --git a/src/admin/mymodels/place.php b/src/admin/mymodels/place.php new file mode 100644 index 0000000..aec0100 --- /dev/null +++ b/src/admin/mymodels/place.php @@ -0,0 +1,191 @@ +id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name) + { + $this->name = $name; + } + + /** + * @return string + */ + public function getAddress() + { + return $this->address; + } + + /** + * @return string + */ + public function getArea() + { + return $this->area; + } + + /** + * @param string $address + */ + public function setAddress(string $address) + { + $this->address = $address; + } + + /** + * @param string $area + */ + public function setArea(string $area) + { + $this->area = $area; + } + + 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/views/club/tmpl/default.php b/src/admin/views/club/tmpl/default.php new file mode 100644 index 0000000..a52c08a --- /dev/null +++ b/src/admin/views/club/tmpl/default.php @@ -0,0 +1,27 @@ + + +
+ + + + + + + isNew): ?> + + + + + +
Bezeichnung
IDoffer->getId(); ?>
+ +
'>Zurück zur Übersicht +
+ diff --git a/src/admin/views/club/view.html.php b/src/admin/views/club/view.html.php new file mode 100644 index 0000000..92cf22f --- /dev/null +++ b/src/admin/views/club/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/clubs/tmpl/default.php b/src/admin/views/clubs/tmpl/default.php new file mode 100644 index 0000000..433af8d --- /dev/null +++ b/src/admin/views/clubs/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/clubs/view.html.php b/src/admin/views/clubs/view.html.php new file mode 100644 index 0000000..bee4949 --- /dev/null +++ b/src/admin/views/clubs/view.html.php @@ -0,0 +1,20 @@ +offers = ClubsOffer::loadOffers(); + + ToolbarHelper::title('Club-Management - Clubs'); + parent::display($tpl); + } + +}