diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index caf64fa..5a43f05 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -14,4 +14,28 @@ class Teamtable $sth = $this->dbh->query('SELECT * FROM lagtabell'); return $sth->fetchAll(PDO::FETCH_ASSOC); } + + public function deleteTeamByID(int $LagID): void + { + $sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?'); + $sth->execute([$LagID]); + } + + public function getTeamByID(int $LagID) # Mixed, array if exists, false if not + { + $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?'); + $sth->execute([$LagID]); + return $sth->fetch(PDO::FETCH_ASSOC); + } + + public function updateTeam(int $id, array $params): void + { + $sth = $this->dbh->prepare('UPDATE lagtabell SET WHERE LagID = ?'); + $sth->execute([$params]); + } + + public function addTeam(int $id, array $params): void + { + // todo ... + } } \ No newline at end of file diff --git a/app/view/pages/teamtable/edit/alter.php b/app/view/pages/teamtable/edit/alter.php new file mode 100644 index 0000000..11b4c8c --- /dev/null +++ b/app/view/pages/teamtable/edit/alter.php @@ -0,0 +1,40 @@ +

Endre lagdetaljer

+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ +
\ 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 e5f492a..18554d8 100644 --- a/app/view/pages/teamtable/edit/index.php +++ b/app/view/pages/teamtable/edit/index.php @@ -27,7 +27,7 @@ echo "{$row['Deltagere']}"; echo "{$row['Runder']}"; echo "{$row['Bestetid']}"; - echo "Slett"; + echo "SlettEndre"; echo ''; } ?> diff --git a/public/teamtable/edit/alter.php b/public/teamtable/edit/alter.php new file mode 100644 index 0000000..7a3ba72 --- /dev/null +++ b/public/teamtable/edit/alter.php @@ -0,0 +1,52 @@ +model('Teamtable'); + +if (!isset($_GET['item'])) +{ + $app->session->flash('LagID ikke definert som GET parameter', 'danger'); + $app->redirect('index.php'); +} +$id = $_GET['item']; + +// ID must be numeric +if (!is_numeric($id)) +{ + $app->session->flash('LagID må være tall', 'danger'); + $app->redirect('index.php'); +} + +// Check if ID is in teamtable +$currentTeam = $model->getTeamByID($id); +if (!$currentTeam) +{ + $app->session->flash("Kunne ikke endre lag: LagID \"$id\" finnes ikke", "danger"); + $app->redirect('index.php'); +} + +// Change team +if ($_SERVER['REQUEST_METHOD'] === 'POST') +{ + $model->updateTeam( + $id, [ + $_POST['navn'], + $_POST['bedrift'], + $_POST['kortnummer'], + $_POST['leder'], + $_POST['telefon'], + $_POST['deltagere'], + $_POST['runder'], + ], + ); +} + +// Escape all values +foreach ($currentTeam as $key => $value) +{ + $currentTeam[$key] = htmlspecialchars($currentTeam[$key]); +} + +$app->view('template/header', ['title' => 'Endre lagdetaljer']); +$app->view('pages/teamtable/edit/alter', ["team" => $currentTeam]); +$app->view('template/footer'); \ No newline at end of file diff --git a/public/teamtable/edit/delete.php b/public/teamtable/edit/delete.php new file mode 100644 index 0000000..16644a7 --- /dev/null +++ b/public/teamtable/edit/delete.php @@ -0,0 +1,32 @@ +model('Teamtable'); + +if (!isset($_GET['item'])) +{ + $app->session->flash('LagID ikke definert som GET parameter', 'danger'); + $app->redirect('index.php'); +} +$id = $_GET['item']; + +// ID must be numeric +if (!is_numeric($id)) +{ + $app->session->flash('LagID må være tall', 'danger'); + $app->redirect('index.php'); +} + +// Check if ID is in teamtable +$currentTeam = $model->getTeamByID($id); +if (!$currentTeam) +{ + $app->session->flash("Kunne ikke slette lag: LagID \"$id\" finnes ikke", "danger"); + $app->redirect('index.php'); +} + +$model->deleteTeamByID($id); + +$app->session->flash("Slettet lag: {$currentTeam['LagNavn']}", "success"); + +$app->redirect('index.php'); \ No newline at end of file