Spaghettification

This commit is contained in:
William 2022-02-15 12:26:15 +01:00
parent fa05590c76
commit 7646d2cc32
6 changed files with 78 additions and 16 deletions

View File

@ -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(

View File

@ -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;
}
}

View File

@ -1,5 +1,5 @@
<h1>Endre lagtabell</h1>
<span class="float-right">[&nbsp;<a class="success" href="update.php?create=true">Opprett lag</a>&nbsp;]</span>
<span class="float-right">[&nbsp;<a class="success" href="add.php">Opprett lag</a>&nbsp;]</span>
<br>
<table>
<tr>
@ -33,4 +33,4 @@
}
?>
</table>
<span class="float-right">[&nbsp;<a class="success" href="update.php?create=true">Opprett lag</a>&nbsp;]</span>
<span class="float-right">[&nbsp;<a class="success" href="add.php">Opprett lag</a>&nbsp;]</span>

View File

@ -0,0 +1,10 @@
<?php
$app = require '../../../app/inc.php';
$model = $app->model('Teamtable');
$id = $model->addEmptyTeam();
$app->session->flash('Opprettet ny lagmal, <a href=' . "update.php?item=$id" . '>klikk her</a> for å endre på den', 'success', TRUE);
$app->redirect('index.php');

View File

@ -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');

View File

@ -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;