From 7646d2cc32ae596248c01c7048bb6a50ca63d93b Mon Sep 17 00:00:00 2001 From: William Date: Tue, 15 Feb 2022 12:26:15 +0100 Subject: [PATCH] Spaghettification --- app/core/Session.php | 4 +- app/model/Teamtable.php | 51 +++++++++++++++++++++++-- app/view/pages/teamtable/edit/index.php | 4 +- public/teamtable/edit/add.php | 10 +++++ public/teamtable/edit/delete.php | 10 ++++- public/teamtable/edit/update.php | 15 ++++---- 6 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 public/teamtable/edit/add.php diff --git a/app/core/Session.php b/app/core/Session.php index 7e97760..eb0e40a 100644 --- a/app/core/Session.php +++ b/app/core/Session.php @@ -46,7 +46,7 @@ class Session } // TODO: throwaway code; rewrite for readability and also implement proper flashing by removing messages after one request - public function flash(string $msg, string $type = 'info'): void + public function flash(string $msg, string $type = 'info', bool $unsafe = FALSE): void { $types = [ "info", @@ -69,7 +69,7 @@ class Session } $msgs = $this->get($key); $msgs[] = [ - "message" => htmlspecialchars($msg), + "message" => ($unsafe) ? $msg : htmlspecialchars($msg), "type" => $type ]; $this->set( diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index 7160ff9..1247076 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -4,6 +4,16 @@ class Teamtable { public PDO $dbh; + public array $emptyTeamTemplate = [ + 'LagNavn' => 'NN', + 'Bedrift' => 'NN', + 'Kortnummer' => 'NN', + 'Lagleder' => 'NN', + 'Telefon' => 0, + 'Deltagere' => 0, + 'Runder' => 0 + ]; + public function __construct(Database $database) { $this->dbh = $database->conn; @@ -40,12 +50,47 @@ class Teamtable int $Runder ): void { - $sth = $this->dbh->prepare('UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, Runder = ? WHERE LagID = ?'); + $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 + public function addEmptyTeam(): int { - // todo ... + $sth = $this->dbh->prepare( + 'INSERT INTO lagtabell (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder) VALUES (?, ?, ?, ?, ?, ?, ?)' + ); + + $template = $this->emptyTeamTemplate; + $template = [ + $template['LagNavn'], + $template['Bedrift'], + $template['Kortnummer'], + $template['Lagleder'], + $template['Telefon'], + $template['Deltagere'], + $template['Runder'], + ]; + $sth->execute($template); + return $this->dbh->lastInsertId(); + } + + // Check if team is empty by comparing it to the template + public function isEqualEmptyTemplate(array $team): bool + { + $template = $this->emptyTeamTemplate; + $equal = FALSE; + foreach ($team as $key => $value) { + if (!isset($template[$key])) { + continue; + } + if ($template[$key] !== $team[$key]) { + $equal = FALSE; + break; + } + $equal = TRUE; + } + return $equal; } } \ 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 ee176c5..541203d 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 ]
@@ -33,4 +33,4 @@ } ?>
-Opprett lag ] \ No newline at end of file +Opprett lag ] \ No newline at end of file diff --git a/public/teamtable/edit/add.php b/public/teamtable/edit/add.php new file mode 100644 index 0000000..0c8bb1e --- /dev/null +++ b/public/teamtable/edit/add.php @@ -0,0 +1,10 @@ +model('Teamtable'); + +$id = $model->addEmptyTeam(); + +$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/delete.php b/public/teamtable/edit/delete.php index 4d1320b..5f3107c 100644 --- a/public/teamtable/edit/delete.php +++ b/public/teamtable/edit/delete.php @@ -25,8 +25,10 @@ if (!$currentTeam) $app->redirect('index.php'); } +$sameAsTemplate = $model->isEqualEmptyTemplate($currentTeam); + // Show confirmation page -if (isset($_GET['confirmation']) && $_GET['confirmation'] == 'true') +if (isset($_GET['confirmation']) && $_GET['confirmation'] == 'true' && !$sameAsTemplate) { $app->view('template/header', ['title' => 'Bekreft sletting']); $app->view('pages/teamtable/edit/delete', ['currentTeam' => $currentTeam]); @@ -36,6 +38,10 @@ if (isset($_GET['confirmation']) && $_GET['confirmation'] == 'true') $model->deleteTeamByID($id); -$app->session->flash("Slettet lag: {$currentTeam['LagNavn']}", "success"); +if ($sameAsTemplate) { + $app->session->flash("Slettet lagmal", "success"); +} else { + $app->session->flash("Slettet lag: {$currentTeam['LagNavn']}", "success"); +} $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 9486b43..5ea2a94 100644 --- a/public/teamtable/edit/update.php +++ b/public/teamtable/edit/update.php @@ -54,10 +54,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') //====Validate Input====// $validationError = FALSE; + $template = $model->emptyTeamTemplate; // LagNavn if (!strlen($LagNavn)) { - $LagNavn = 'NN'; + $LagNavn = $template['LagNavn']; } if (strlen($LagNavn) > 32) { $validationError = TRUE; @@ -65,7 +66,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Bedrift if (!strlen($Bedrift)) { - $Bedrift = 'NN'; + $Bedrift = $template['Bedrift']; } if (strlen($Bedrift) > 32) { $validationError = TRUE; @@ -73,7 +74,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Kortnummer if (!strlen($Kortnummer)) { - $Kortnummer = 'NN'; + $Kortnummer = $template['Kortnummer']; } if (strlen($Kortnummer) > 32) { $validationError = TRUE; @@ -81,7 +82,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Lagleder if (!strlen($Lagleder)) { - $Lagleder = 'NN'; + $Lagleder = $template['Lagleder']; } if (strlen($Lagleder) > 32) { $validationError = TRUE; @@ -89,7 +90,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Telefon if (!strlen($Telefon)) { - $Telefon = 0; + $Telefon = $template['Telefon']; } if (strlen((string) $Telefon) > 32) { $validationError = TRUE; @@ -97,7 +98,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Deltagere if (!strlen($Deltagere)) { - $Deltagere = 0; + $Deltagere = $template['Deltagere']; } if (strlen((string) $Deltagere) > 32) { $validationError = TRUE; @@ -105,7 +106,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Runder if (!strlen($Runder)) { - $Runder = 0; + $Runder = $template['Runder']; } if (strlen((string) $Runder) > 32) { $validationError = TRUE;