This commit is contained in:
William 2022-03-07 11:11:33 +01:00
parent 8fe8c67b6a
commit 3296f1b078
8 changed files with 126 additions and 48 deletions

View File

@ -54,6 +54,18 @@ class TeamMapper
#return $teams; #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 public function get(int $id): ?Team
{ {
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?'); $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?');

View File

@ -41,6 +41,11 @@ class Teamtable
return $this->teamMapper->get($id); return $this->teamMapper->get($id);
} }
public function getByCardnumber(string $cardnumber): ?Team
{
return $this->teamMapper->getByCardnumber($cardnumber);
}
/** /**
* Inserts team into database * Inserts team into database
*/ */
@ -54,23 +59,29 @@ class Teamtable
$this->teamMapper->delete($id); $this->teamMapper->delete($id);
} }
public function update(Team $team): Team
{
return $this->teamMapper->update($team);
}
/** /**
* Returns TRUE if team exists, FALSE if not * Returns TRUE if team exists, FALSE if not
* TODO: maybe use integers instead of booleans for codes
*/ */
public function recieveBaton(string $cardnumber): bool public function recieveBaton(string $cardnumber): bool
{ {
$team = $this->getTeamByCardnumber($cardnumber); $team = $this->getByCardnumber($cardnumber);
if ($team) if ($team)
{ {
// team exists, insert into time table // team exists, insert into time table
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)'); $sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
$sth->execute([$row['LagID']]); $sth->execute([$team->id]);
return TRUE; return TRUE;
} }
// team does not exist, lets create it // team does not exist, lets create it
$team = new Team(); $team = new Team;
$team->cardnumber = $cardnumber; $team->cardnumber = $cardnumber;
$this->addTeam($team); $this->create($team);
return FALSE; return FALSE;
} }
} }

View File

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

View File

@ -1,42 +1,48 @@
<h1>Endre lagdetaljer</h1> <h1><?=$title?></h1>
<p>Her kan du oppdatere informasjonen om laget</p> <p>Her kan du oppdatere informasjonen om laget</p>
<form method="post" autocomplete="off"> <form method="post" autocomplete="off">
<label for="name">Navn:</label> <label for="name">Navn:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32"> <input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label> <label for="company">Bedrift:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32"> <input type="text" id="company" name="company" value="<?=htmlspecialchars($team->company)?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label> <label for="cardnumber">Kortnummer:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32"> <input type="text" id="cardnumber" name="cardnumber" value="<?=htmlspecialchars($team->cardnumber)?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label> <label for="leader">Leder:</label>
<br> <br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32"> <input type="text" id="leader" name="leader" value="<?=htmlspecialchars($team->leader)?>" maxlength="32">
<br> <br>
<label for="phone">Telefon:</label>
<br>
<input type="number" id="phone" name="phone" value="<?=htmlspecialchars($team->phone)?>" maxlength="32">
<br>
<label for="participants">Deltagere:</label>
<br>
<input type="number" id="participants" name="participants" value="<?=htmlspecialchars($team->participants)?>" maxlength="32">
<br>
<label for="number">Runder:</label>
<br>
<input type="number" id="rounds" name="rounds" value="<?=htmlspecialchars($team->rounds)?>" maxlength="32">
<br>
<br> <br>
<input type="submit" value="Lagre"> <input type="submit" value="Lagre">
<span>[&nbsp;<a class="danger" href="index.php">Avbryt</a>&nbsp;]</span> <span>[&nbsp;<a class="danger" href="index.php">Avbryt</a>&nbsp;]</span>

View File

@ -0,0 +1 @@
<?php $app = require '../../../app/inc.php';

View File

@ -1,5 +1,7 @@
<?php <?php $app = require '../app/inc.php';
$app = require '../app/inc.php';
// TODO: refactor
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
if ($_SERVER['REQUEST_METHOD'] === 'POST') if ($_SERVER['REQUEST_METHOD'] === 'POST')
@ -9,7 +11,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
$cardnumber = $_POST['cardnumber']; $cardnumber = $_POST['cardnumber'];
if (!(strlen($cardnumber) > 32)) { if (!(strlen($cardnumber) > 32)) {
if ($model->recieveStick($cardnumber)) if ($model->recieveBaton($cardnumber))
{ {
$app->session->flash("Lag funnet for \"{$cardnumber}\"", "success"); $app->session->flash("Lag funnet for \"{$cardnumber}\"", "success");
} else { } else {

View File

@ -1,16 +0,0 @@
<?php $app = require '../../../app/inc.php';
/**
* Insert a team into team table
*/
use App\Teamtable\Team as Team;
$model = $app->model('Teamtable');
$team = $model->create(
new Team()
);
$app->session->flash('Opprettet ny lagmal, <a class="success" href=' . "update.php?item=$team->id" . '>klikk her</a> for å endre på den', 'success', TRUE);
$app->redirect('index.php');

View File

@ -1,4 +1,7 @@
<?php $app = require '../../../app/inc.php'; <?php $app = require '../../../app/inc.php';
/**
* Creates and updates team in teamtable
*/
use App\Teamtable\Team; use App\Teamtable\Team;
@ -6,20 +9,79 @@ $item = filter_input(INPUT_GET, 'item', FILTER_VALIDATE_INT);
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
// item is NULL if not set
if ($item !== NULL) if ($item !== NULL)
{ {
// check that team exists
$team = $model->get($item); $team = $model->get($item);
if (!$team) if (!$team)
{ {
// team does not exist // 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'); $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 // lets create a team
$app->view('template/header', ['title' => 'Endre lagdetaljer']); $title = "Legg til lag";
$app->view('pages/teamtable/edit/update', ["team" => new Team]); $app->view('template/header', [
'title' => $title
]);
$app->view('pages/teamtable/edit/update', [
"team" => new Team,
"title" => $title
]);
$app->view('template/footer'); $app->view('template/footer');