From 3296f1b0789c44fc2537f9ad2043b779e4b56124 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 7 Mar 2022 11:11:33 +0100 Subject: [PATCH] Commit --- app/lib/App/Teamtable/TeamMapper.php | 12 ++++ app/model/Teamtable.php | 19 +++++-- app/view/pages/teamtable/edit/index.php | 2 +- app/view/pages/teamtable/edit/update.php | 44 ++++++++------- public/api/v1/recieve.php | 1 + public/simulator.php | 8 ++- public/teamtable/edit/create.php | 16 ------ public/teamtable/edit/update.php | 72 ++++++++++++++++++++++-- 8 files changed, 126 insertions(+), 48 deletions(-) create mode 100644 public/api/v1/recieve.php delete mode 100644 public/teamtable/edit/create.php diff --git a/app/lib/App/Teamtable/TeamMapper.php b/app/lib/App/Teamtable/TeamMapper.php index 19732f0..ada2743 100644 --- a/app/lib/App/Teamtable/TeamMapper.php +++ b/app/lib/App/Teamtable/TeamMapper.php @@ -54,6 +54,18 @@ class TeamMapper #return $teams; } + public function getByCardnumber(string $cardnumber): ?Team + { + $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?'); + $sth->execute([$cardnumber]); + $row = $sth->fetch(PDO::FETCH_ASSOC); + if ($row) + { + return $this->mapRowToTeam($row); + } + return NULL; + } + public function get(int $id): ?Team { $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?'); diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index 73dd1a5..71314a2 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -41,6 +41,11 @@ class Teamtable return $this->teamMapper->get($id); } + public function getByCardnumber(string $cardnumber): ?Team + { + return $this->teamMapper->getByCardnumber($cardnumber); + } + /** * Inserts team into database */ @@ -54,23 +59,29 @@ class Teamtable $this->teamMapper->delete($id); } + public function update(Team $team): Team + { + return $this->teamMapper->update($team); + } + /** * Returns TRUE if team exists, FALSE if not + * TODO: maybe use integers instead of booleans for codes */ public function recieveBaton(string $cardnumber): bool { - $team = $this->getTeamByCardnumber($cardnumber); + $team = $this->getByCardnumber($cardnumber); if ($team) { // team exists, insert into time table $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); - $sth->execute([$row['LagID']]); + $sth->execute([$team->id]); return TRUE; } // team does not exist, lets create it - $team = new Team(); + $team = new Team; $team->cardnumber = $cardnumber; - $this->addTeam($team); + $this->create($team); return FALSE; } } \ No newline at end of file diff --git a/app/view/pages/teamtable/edit/index.php b/app/view/pages/teamtable/edit/index.php index 5a0acf0..b299163 100644 --- a/app/view/pages/teamtable/edit/index.php +++ b/app/view/pages/teamtable/edit/index.php @@ -1,5 +1,5 @@

Endre lagtabell

-Opprett lag ] +Opprett lag ]
diff --git a/app/view/pages/teamtable/edit/update.php b/app/view/pages/teamtable/edit/update.php index 5f54bc4..6384653 100644 --- a/app/view/pages/teamtable/edit/update.php +++ b/app/view/pages/teamtable/edit/update.php @@ -1,42 +1,48 @@ -

Endre lagdetaljer

+

Her kan du oppdatere informasjonen om laget


- - -
- - +
- - - - +
- - +
- + +
- +
- - - - +
- + +
+ +
+ + + +
+ +
+ + + +
+ +
+
Avbryt ] diff --git a/public/api/v1/recieve.php b/public/api/v1/recieve.php new file mode 100644 index 0000000..70b4ba5 --- /dev/null +++ b/public/api/v1/recieve.php @@ -0,0 +1 @@ +model('Teamtable'); if ($_SERVER['REQUEST_METHOD'] === 'POST') @@ -9,7 +11,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') $cardnumber = $_POST['cardnumber']; if (!(strlen($cardnumber) > 32)) { - if ($model->recieveStick($cardnumber)) + if ($model->recieveBaton($cardnumber)) { $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); } else { diff --git a/public/teamtable/edit/create.php b/public/teamtable/edit/create.php deleted file mode 100644 index cf8e0d6..0000000 --- a/public/teamtable/edit/create.php +++ /dev/null @@ -1,16 +0,0 @@ -model('Teamtable'); - -$team = $model->create( - new Team() -); - -$app->session->flash('Opprettet ny lagmal, klikk her for å endre på den', 'success', TRUE); - -$app->redirect('index.php'); \ No newline at end of file diff --git a/public/teamtable/edit/update.php b/public/teamtable/edit/update.php index 5cc1003..48e46ca 100644 --- a/public/teamtable/edit/update.php +++ b/public/teamtable/edit/update.php @@ -1,4 +1,7 @@ model('Teamtable'); -// item is NULL if not set if ($item !== NULL) { - // check that team exists $team = $model->get($item); if (!$team) { // team does not exist - $app->session->flash('Kunne ikke endre lag: Lag finnes ikke', 'danger'); + $app->session->flash('Kunne ikke oppdatere lag: Lag finnes ikke', 'danger'); $app->redirect('index.php'); } } +if ($_SERVER['REQUEST_METHOD'] == 'POST') +{ + $name = filter_input(INPUT_POST, 'name'); + $company = filter_input(INPUT_POST, 'company'); + $cardnumber = filter_input(INPUT_POST, 'cardnumber'); + $leader = filter_input(INPUT_POST, 'leader'); + $phone = filter_input(INPUT_POST, 'phone', FILTER_VALIDATE_INT); + $participants = filter_input(INPUT_POST, 'participants', FILTER_VALIDATE_INT); + $rounds = filter_input(INPUT_POST, 'rounds', FILTER_VALIDATE_INT); + + $team = new Team; + try { + $team->setName($name); + $team->setCompany($company); + $team->setCardnumber($cardnumber); + $team->setLeader($leader); + $team->setPhone($phone); + $team->setParticipants($participants); + $team->setRounds($rounds); + } catch(Throwable $e) { + $app->session->flash('Kunne ikke oppdatere lag: Validerings feil ' . $e->getMessage() , 'danger'); + $app->redirect('index.php'); + } + + if ($item !== NULL) + { + // team exists, lets update it + $team->id = $item; + $model->update($team); + $app->session->flash('Oppdaterte lag', 'success'); + $app->redirect('index.php'); + } + // no team was specified, lets create one + $model->create($team); + $app->session->flash('Opprettet nytt lag', 'success'); + $app->redirect('index.php'); +} + +if ($item !== NULL) +{ + // team exists + $team = $model->get($item); + + $title = "Endre lag"; + $app->view('template/header', [ + 'title' => $title + ]); + $app->view('pages/teamtable/edit/update', [ + "team" => $team, + "title" => $title + ]); + $app->view('template/footer'); + die(); +} + // lets create a team -$app->view('template/header', ['title' => 'Endre lagdetaljer']); -$app->view('pages/teamtable/edit/update', ["team" => new Team]); +$title = "Legg til lag"; +$app->view('template/header', [ + 'title' => $title +]); +$app->view('pages/teamtable/edit/update', [ + "team" => new Team, + "title" => $title +]); $app->view('template/footer'); \ No newline at end of file