From d089d352eec173a18cde773f1ab083a1d0c47bdf Mon Sep 17 00:00:00 2001 From: William Date: Mon, 14 Feb 2022 11:23:05 +0100 Subject: [PATCH] TODO: Allow creation of teams --- app/model/Teamtable.php | 19 +++- app/view/pages/simulator.php | 2 +- app/view/pages/teamtable/edit/update.php | 28 +++--- public/simulator.php | 13 ++- public/teamtable/edit/update.php | 112 ++++++++++++++++++++++- 5 files changed, 145 insertions(+), 29 deletions(-) diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index 7e11af4..7160ff9 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -28,14 +28,23 @@ class Teamtable return $sth->fetch(PDO::FETCH_ASSOC); } - public function updateTeamByID(int $id, array $params): void + // why this is so long who cares??? + public function updateTeamByID( + int $id, + string $LagNavn, + string $Bedrift, + string $Kortnummer, + string $Lagleder, + int $Telefon, + string $Deltagere, + int $Runder + ): void { - // todo ... - $sth = $this->dbh->prepare('UPDATE lagtabell SET WHERE LagID = ?'); - $sth->execute([$params]); + $sth = $this->dbh->prepare('UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, Runder = ? WHERE LagID = ?'); + $sth->execute([$LagNavn, $Bedrift, $Kortnummer, $Lagleder, $Telefon, $Deltagere, $Runder, $id]); } - public function addTeam(int $id, array $params): void + public function addTeam(): int { // todo ... } diff --git a/app/view/pages/simulator.php b/app/view/pages/simulator.php index 573ca4f..a61f735 100644 --- a/app/view/pages/simulator.php +++ b/app/view/pages/simulator.php @@ -1,7 +1,7 @@

Simulator

Skriv inn et kortnummer:

- +

diff --git a/app/view/pages/teamtable/edit/update.php b/app/view/pages/teamtable/edit/update.php index 3ac8b58..6145cd9 100644 --- a/app/view/pages/teamtable/edit/update.php +++ b/app/view/pages/teamtable/edit/update.php @@ -1,39 +1,39 @@

Endre lagdetaljer

Her kan du oppdatere informasjonen om laget

- +
- +
- +
- +
- +
- +
- +
- +
- +
- +
- +
- +
- +
- +

diff --git a/public/simulator.php b/public/simulator.php index 12615b3..eaad648 100644 --- a/public/simulator.php +++ b/public/simulator.php @@ -8,13 +8,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $cardnumber = $_POST['cardnumber']; - if ($model->insert($cardnumber)) - { - $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); + if (!(strlen($cardnumber) > 32)) { + if ($model->insert($cardnumber)) + { + $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); + } else { + $app->session->flash("Opprettet lag for \"{$cardnumber}\""); + } } else { - $app->session->flash("Opprettet lag for \"{$cardnumber}\""); + $app->session->flash('Kortnummer for langt!', 'danger'); } - } else { $app->session->flash('Kortnummer kan ikke være tom!', 'danger'); } diff --git a/public/teamtable/edit/update.php b/public/teamtable/edit/update.php index b8fa8da..26a9fb2 100644 --- a/public/teamtable/edit/update.php +++ b/public/teamtable/edit/update.php @@ -10,14 +10,14 @@ if (!isset($_GET['item'])) } $id = $_GET['item']; -// ID must be numeric +// Id must be a number if (!is_numeric($id)) { $app->session->flash('Kunne ikke endre lag: ID må være tall', 'danger'); $app->redirect('index.php'); } -// Check if ID is in teamtable +// Check if team with supplied ID exists $currentTeam = $model->getTeamByID($id); if (!$currentTeam) { @@ -25,8 +25,112 @@ if (!$currentTeam) $app->redirect('index.php'); } -// todo: update team details here... -// $model->updateTeamByID(...) +if ($_SERVER['REQUEST_METHOD'] === 'POST') +{ + // Check that all parameters are present + if (!isset( + $_POST['LagNavn'], + $_POST['Bedrift'], + $_POST['Kortnummer'], + $_POST['Lagleder'], + $_POST['Telefon'], + $_POST['Deltagere'], + $_POST['Runder'], + ) + ) + { + $app->session->flash("Kunne ikke endre lag: Ikke alle POST parametere er til stede!", "danger"); + $app->redirect('./'); + } + + $LagNavn = $_POST['LagNavn']; + $Bedrift = $_POST['Bedrift']; + $Kortnummer = $_POST['Kortnummer']; + $Lagleder = $_POST['Lagleder']; + $Telefon = $_POST['Telefon']; + $Deltagere = $_POST['Deltagere']; + $Runder = $_POST['Runder']; + + //====Validate Input====// + + $validationError = FALSE; + + // LagNavn + if (empty($LagNavn)) { + $LagNavn = 'NN'; + } + if (strlen($LagNavn) > 32) { + $validationError = TRUE; + } + + // Bedrift + if (empty($Bedrift)) { + $Bedrift = 'NN'; + } + if (strlen($Bedrift) > 32) { + $validationError = TRUE; + } + + // Kortnummer + if (empty($Kortnummer)) { + $Kortnummer = 'NN'; + } + if (strlen($Kortnummer) > 32) { + $validationError = TRUE; + } + + // Lagleder + if (empty($Lagleder)) { + $Lagleder = 'NN'; + } + if (strlen($Lagleder) > 32) { + $validationError = TRUE; + } + + // Telefon + if (empty($Telefon)) { + $Telefon = 0; + } + if (strlen((string) $Telefon) > 32) { + $validationError = TRUE; + } + + // Deltagere + if (empty($Deltagere)) { + $Deltagere = 0; + } + if (strlen((string) $Deltagere) > 32) { + $validationError = TRUE; + } + + // Runder + if (empty($Runder)) { + $Runder = 0; + } + if (strlen((string) $Runder) > 32) { + $validationError = TRUE; + } + + + if (!$validationError) { + // All is good! Lets update the team details + $model->updateTeamByID( + $id, + $LagNavn, + $Bedrift, + $Kortnummer, + $Lagleder, + $Telefon, + $Deltagere, + $Runder, + ); + + $app->session->flash('Lagret endringer for lag: '.$LagNavn, 'success'); + $app->redirect('./'); + } else { + $app->session->flash('Kunne ikke endre lag: Validerings feil!', 'danger'); + } +} // Escape all values foreach ($currentTeam as $key => $value)