From 22566986a80de0a4e11ffb17ee245edb7b0113af Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Wed, 17 Apr 2019 15:04:24 +0200 Subject: [PATCH] Added backend for position of persons --- sql/init3.sql | 76 ++++++++++ src/admin/controllers/position.php | 108 ++++++++++++++ src/admin/mymodels/position.php | 157 +++++++++++++++++++++ src/admin/views/position/tmpl/default.php | 27 ++++ src/admin/views/position/view.html.php | 47 ++++++ src/admin/views/positions/tmpl/default.php | 28 ++++ src/admin/views/positions/view.html.php | 20 +++ 7 files changed, 463 insertions(+) create mode 100644 sql/init3.sql create mode 100644 src/admin/controllers/position.php create mode 100644 src/admin/mymodels/position.php create mode 100644 src/admin/views/position/tmpl/default.php create mode 100644 src/admin/views/position/view.html.php create mode 100644 src/admin/views/positions/tmpl/default.php create mode 100644 src/admin/views/positions/view.html.php diff --git a/sql/init3.sql b/sql/init3.sql new file mode 100644 index 0000000..a16215e --- /dev/null +++ b/sql/init3.sql @@ -0,0 +1,76 @@ +CREATE TABLE `dev_club_keys` ( + `privkey` text NOT NULL, + `publickey` text NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `dev_club_users` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `user` varchar(30) NOT NULL, + `password` varchar(150) DEFAULT NULL, + `name` varchar(255) NOT NULL, + `address` tinytext NOT NULL, + `city` varchar(50) NOT NULL, + `mail` varchar(100) NOT NULL, + `phone` varchar(50) DEFAULT NULL, + `mobile` varchar(50) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + +CREATE TABLE `dev_club_offers` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `name` varchar(100) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `dev_club_positions` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `name` varchar(100) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + +CREATE TABLE `dev_club_clubs` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `name` varchar(100) NOT NULL, + `address` tinytext NOT NULL, + `city` varchar(50) NOT NULL, + `homepage` varchar(100) NULL, + `mail` varchar(100) NOT NULL, + `iban` char(34) NOT NULL, + `bic` char(11) NOT NULL, + `charitable` tinyint(1) NOT NULL, + `president` int(10) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `dev_club_offer_assocs` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `clubid` int(10) NOT NULL, + `offerid` int(10) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `dev_club_places` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `clubid` int(10) NOT NULL, + `name` varchar(100) NOT NULL, + `address` tinytext NOT NULL, + `area` int(10) NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `dev_club_user_assocs` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `clubid` int(10) NOT NULL, + `userid` int(10) NOT NULL, + `positionid` int(10) NOT NULL, + `admin` tinyint(1) NOT NULL DEFAULT 0, + `state` enum('regular', 'vacant', 'temporary') NOT NULL DEFAULT 'vacant', + `address` tinytext DEFAULT NULL, + `mail` varchar(100) DEFAULT NULL, + `phone` varchar(50) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + diff --git a/src/admin/controllers/position.php b/src/admin/controllers/position.php new file mode 100644 index 0000000..3ffacd6 --- /dev/null +++ b/src/admin/controllers/position.php @@ -0,0 +1,108 @@ +input; + $p = ClubsPosition::createPosition(); + + // 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=position&id=new&data=' . $urldata, false)); + return; + } + + $p->setName($name); + + // Do the actual work + $p->save(); + $this->setRedirect(Route::_('index.php?option=com_clubs&view=positions', false)); + } + + function change() + { + $app = Factory::getApplication(); + $input = $app->input; + $id = (int) $input->post->getInt('id'); + $p = ClubsPosition::loadPosition((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; + } + + $p->setName($name); + + // Do the actual work + $p->save(); + $this->setRedirect(Route::_('index.php?option=com_clubs&view=positions', false)); + } + + function delete() + { + $app = Factory::getApplication(); + $id = $app->input->get->getInt('id'); + $app->enqueueMessage("Removal of position with id $id."); + $user = ClubsPosition::loadPosition($id); + $user->delete(); + $this->setRedirect(Route::_('index.php?option=com_clubs&view=positions', false)); + } + +} diff --git a/src/admin/mymodels/position.php b/src/admin/mymodels/position.php new file mode 100644 index 0000000..a70c06e --- /dev/null +++ b/src/admin/mymodels/position.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 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; + } + + + 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; + } + + public static function createPosition() + { + $position = new ClubsPosition(); + $position->id = 'new'; + return $position; + } + + + public function save() + { + if($this->id === 'new') + $this->insertPosition(); + else + $this->updatePosition(); + } + + private function insertPosition() + { + $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(); + } + + +} diff --git a/src/admin/views/position/tmpl/default.php b/src/admin/views/position/tmpl/default.php new file mode 100644 index 0000000..46d4d53 --- /dev/null +++ b/src/admin/views/position/tmpl/default.php @@ -0,0 +1,27 @@ + + +
+ + + + + + + isNew): ?> + + + + + +
Bezeichnung
IDposition->getId(); ?>
+ +
'>Zurück zur Übersicht +
+ diff --git a/src/admin/views/position/view.html.php b/src/admin/views/position/view.html.php new file mode 100644 index 0000000..07cf47c --- /dev/null +++ b/src/admin/views/position/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=position.new'); + $this->position = ClubsPosition::createPosition(); + $this->isNew = true; + } + else if(is_numeric($id)) + { + $this->address = Route::_('index.php?option=com_clubs&task=position.change'); + $this->position = ClubsPosition::loadPosition((int) $id); + $this->isNew = false; + } + else + throw new Exception('Need a position 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->position->setName($data['name']); + + } + + ToolbarHelper::title('Club-Management - Position'); + parent::display($tpl); + } +} diff --git a/src/admin/views/positions/tmpl/default.php b/src/admin/views/positions/tmpl/default.php new file mode 100644 index 0000000..cca0ee1 --- /dev/null +++ b/src/admin/views/positions/tmpl/default.php @@ -0,0 +1,28 @@ + + + + + + + + + + +positions as $position): ?> +getId()); ?> + + + + + + +
BezeichnungLöschen?id
getName()); ?>getId()); ?>'>DelgetId()); ?>
+ +
'>Neues Angebot anlegen
diff --git a/src/admin/views/positions/view.html.php b/src/admin/views/positions/view.html.php new file mode 100644 index 0000000..d165d50 --- /dev/null +++ b/src/admin/views/positions/view.html.php @@ -0,0 +1,20 @@ +positions = ClubsPosition::loadPositions(); + + ToolbarHelper::title('Club-Management - Positionen'); + parent::display($tpl); + } + +}