Fat commit

This commit is contained in:
William 2022-03-07 07:13:17 +01:00
parent 1e00dd7119
commit e234537647
18 changed files with 302 additions and 436 deletions

View File

@ -12,9 +12,6 @@
* Tread carefully * Tread carefully
*/ */
// disable type coercion
declare(strict_types=1);
// PSR-4 like autoloader // PSR-4 like autoloader
spl_autoload_register( spl_autoload_register(
function ($className) { function ($className) {
@ -23,7 +20,6 @@ spl_autoload_register(
} }
); );
// imports
use App\Core\ { use App\Core\ {
ErrorHandler, ErrorHandler,
Config, Config,
@ -34,22 +30,16 @@ use App\Core\ {
AccessControl AccessControl
}; };
// displays a custom page on error or exception
new ErrorHandler(); new ErrorHandler();
// grab configuration file
$config = (new Config(__DIR__ . '/config.php'))->config; $config = (new Config(__DIR__ . '/config.php'))->config;
// start database connection
$database = new Database($config['database']); $database = new Database($config['database']);
// session wrapper
$session = new Session(); $session = new Session();
// handles current user session
$user = new User($session, $database); $user = new User($session, $database);
$app = new App(__DIR__, $config, $database, $session, $user); $app = new App(__DIR__, $config, $database, $session, $user);
// we will use $app instead // we will use $app instead

View File

@ -30,19 +30,22 @@ class App
$this->user = $user; $this->user = $user;
} }
// grab model /**
// TODO: have a look to see if this might name conflict with anything and * Grab model
// maybe also throw an exception if the model class is not found within the file *
* TODO: have a look to see if this might name conflict with anything and
* maybe also throw an exception if the model class is not found within the file
*/
public function model(string $model, $injection = NULL): object public function model(string $model, $injection = NULL): object
{ {
// Require model file // require model file
$path = $this->dir . '/model/' . $model . '.php'; $path = $this->dir . '/model/' . $model . '.php';
if (!file_exists($path)) if (!file_exists($path))
{ {
throw new Exception("Model does not exist"); throw new Exception("Model does not exist");
} }
require $path; require $path;
// Instantiate model // instantiate model
if (!$injection) if (!$injection)
{ {
$injection = $this->database; $injection = $this->database;
@ -71,10 +74,10 @@ class App
*/ */
public function api(array $data, int $status_code = 200): void public function api(array $data, int $status_code = 200): void
{ {
// Set headers // set headers
http_response_code($status_code); http_response_code($status_code);
header('Content-type: application/json'); header('Content-type: application/json');
// Convert and respond with data // convert and respond with data
echo json_encode($data); echo json_encode($data);
die(); die();
} }

View File

@ -8,7 +8,7 @@ use \PDOException;
/** /**
* Encapsulates a single connection to a database. * Encapsulates a single connection to a database.
* TODO: Refactor and add different driver implementations. * TODO: add different driver implementations.
*/ */
class Database class Database
{ {
@ -33,7 +33,7 @@ class Database
$dsn = "mysql:host={$args['host']};dbname={$args['database']};charset={$args['charset']}"; $dsn = "mysql:host={$args['host']};dbname={$args['database']};charset={$args['charset']}";
$options = [ $options = [
PDO::ATTR_PERSISTENT => true, PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // In PHP 8 and above, this will be the default mode. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // in PHP 8 and above, this will be the default mode.
]; ];
return new PDO($dsn, $args['user'], $args['password'], $options); return new PDO($dsn, $args['user'], $args['password'], $options);
} }

View File

@ -3,6 +3,7 @@
namespace App\Core; namespace App\Core;
use \Exception; use \Exception;
use \InvalidArgumentException;
/** /**
* Handles anything to do with sessions * Handles anything to do with sessions
@ -63,7 +64,7 @@ class Session
"warning" "warning"
]; ];
if (!in_array($type, $types)) { if (!in_array($type, $types)) {
throw new Exception("Flash type: \"$type\" does not exist"); throw new InvalidArgumentException("Flash type: \"$type\" does not exist");
} }
$key = 'flashed_messages'; $key = 'flashed_messages';
if (!$this->has($key)) if (!$this->has($key))

View File

@ -2,97 +2,62 @@
namespace App\Teamtable; namespace App\Teamtable;
use \InvalidArgumentException;
/** /**
* Represents a team in the teamtable database * Represents a team in the teamtable database
* TODO: Add validation for setters
*/ */
class Team class Team
{ {
public string $name; public int $id;
public string $company; public string $name = 'NN';
public string $cardnumber; public string $company = 'NN';
public int $phone; public string $cardnumber = 'NN';
public int $participants; public string $leader = 'NN';
public int $rounds; public int $phone = 0;
public int $participants = 0;
public int $rounds = 0;
public function __construct( public function setName(string $name): Self
string $name = 'NN',
string $company = 'NN',
string $cardnumber = 'NN',
int $phone = 0,
int $participants = 0,
int $rounds = 0
)
{ {
$this->name = $name; $this->name = $name;
$this->company = $company; return $this;
$this->cardnumber = $cardnumber;
$this->phone = $phone;
$this->participants = $participants;
$this->rounds = $rounds;
} }
/** public function setCompany(string $company): Self
* Check if all current values are valid
*/
public function validate(): bool
{ {
$validationError = FALSE; $this->company = $company;
$template = $model->template; return $this;
}
// LagNavn public function setCardnumber(string $cardnumber): Self
if (!strlen($LagNavn)) { {
$LagNavn = $template['LagNavn']; $this->cardnumber = $cardnumber;
} return $this;
if (strlen($LagNavn) > 32) { }
$validationError = TRUE;
}
// Bedrift public function setLeader(string $leader): Self
if (!strlen($Bedrift)) { {
$Bedrift = $template['Bedrift']; $this->leader = $leader;
} return $this;
if (strlen($Bedrift) > 32) { }
$validationError = TRUE;
}
// Kortnummer public function setPhone(int $phone): Self
if (!strlen($Kortnummer)) { {
$Kortnummer = $template['Kortnummer']; $this->phone = $phone;
} return $this;
if (strlen($Kortnummer) > 32) { }
$validationError = TRUE;
}
// Lagleder public function setParticipants(int $participants): Self
if (!strlen($Lagleder)) { {
$Lagleder = $template['Lagleder']; $this->participants = $participants;
} return $this;
if (strlen($Lagleder) > 32) { }
$validationError = TRUE;
}
// Telefon public function setRounds(int $rounds): Self
if (!strlen($Telefon)) { {
$Telefon = $template['Telefon']; $this->rounds = $rounds;
} return $this;
if (strlen((string) $Telefon) > 32) {
$validationError = TRUE;
}
// Deltagere
if (!strlen($Deltagere)) {
$Deltagere = $template['Deltagere'];
}
if (strlen((string) $Deltagere) > 32) {
$validationError = TRUE;
}
// Runder
if (!strlen($Runder)) {
$Runder = $template['Runder'];
}
if (strlen((string) $Runder) > 32) {
$validationError = TRUE;
}
} }
} }

View File

@ -0,0 +1,118 @@
<?php
namespace App\Teamtable;
use \PDO;
/**
* Normally this kind of logic will be implemented using the Repository pattern.
* However the important part is in mapRowToTeam(), that will create a business object from the
* data fetched from database
*/
class TeamMapper
{
public PDO $dbh;
public function __construct(PDO $dbh)
{
$this->dbh = $dbh;
}
private function mapRowToTeam(array $row): Team
{
$team = new Team();
$team->id = $row['LagID'];
$team->setName($row['LagNavn']);
$team->setCompany($row['Bedrift']);
$team->setCardnumber($row['Kortnummer']);
$team->setLeader($row['Lagleder']);
$team->setPhone($row['Telefon']);
$team->setParticipants($row['Deltagere']);
$team->setRounds($row['Runder']);
return $team;
}
/**
* Returns an array of all teams
*/
public function getAll(): array
{
$sth = $this->dbh->query('SELECT * FROM lagtabell');
$assoc_array = $sth->fetchAll(PDO::FETCH_ASSOC);
$teams = [];
foreach ($assoc_array as $key => $row)
{
array_push($teams, $this->mapRowToTeam($row));
}
return $teams;
#while ($assoc_array)
#{
# array_push($teams, $this->mapRowToTeam($assoc_array[0]));
# array_pop($assoc_array);
#}
#return $teams;
}
public function get(int $id): ?Team
{
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?');
$sth->execute([$id]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
return $this->mapRowToTeam($row);
}
return NULL;
}
public function create(Team $team): Team
{
$sth = $this->dbh->prepare(
'INSERT INTO lagtabell
(LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder)
VALUES
(?, ?, ?, ?, ?, ?, ?)'
);
$sth->execute([
$team->name,
$team->company,
$team->cardnumber,
$team->leader,
$team->phone,
$team->participants,
$team->rounds
]);
$lastId = $this->dbh->lastInsertId();
return $this->get($lastId);
}
public function update(Team $team): Team
{
$sth = $this->dbh->prepare(
'UPDATE lagtabell SET
LagNavn = ?, Bedrift = ?, Kortnummer = ?,
Lagleder = ?, Telefon = ?, Deltagere = ?,
Runder = ?
WHERE
LagID = ?'
);
$sth->execute([
$team->name,
$team->company,
$team->cardnumber,
$team->leader,
$team->phone,
$team->participants,
$team->rounds,
$team->id
]);
return $this->get($team->id);
}
public function delete(int $id): void
{
$sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
$sth->execute([$id]);
}
}

View File

@ -2,132 +2,75 @@
use App\Core\Database as Database; use App\Core\Database as Database;
use App\Teamtable\Team as Team; use App\Teamtable\Team as Team;
use App\Teamtable\TeamMapper as TeamMapper;
/** /**
* Do stuff with the teamtable * Does stuffs with the teamtable
*/ */
class Teamtable class Teamtable
{ {
/** /**
* Database handler * Database connection
*/ */
public PDO $dbh; public PDO $dbh;
/**
* We use a data mapper pattern
*/
public TeamMapper $teamMapper;
public function __construct(Database $database) public function __construct(Database $database)
{ {
$this->dbh = $database->conn; $this->dbh = $database->conn;
$this->teamMapper = new TeamMapper($this->dbh);
} }
/** /**
* Fetch entire team table * Fetch entire team table
*/ */
public function getTable(): array public function getAll(): array
{ {
$sth = $this->dbh->query('SELECT * FROM lagtabell'); return $this->teamMapper->getAll();
return $sth->fetchAll(PDO::FETCH_ASSOC);
} }
/** /**
* Delete team with supplied id * Find team with supplied id
*/ */
public function deleteTeamByID(int $LagID): void public function get(int $id): ?Team
{ {
$sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?'); return $this->teamMapper->get($id);
$sth->execute([$LagID]);
} }
/** /**
* Returns mixed, array if exists, FALSE if not. * Inserts team into database
*/ */
public function getTeamByID(int $LagID) public function create(Team $team): Team
{ {
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?'); return $this->teamMapper->create($team);
$sth->execute([$LagID]);
return $sth->fetch(PDO::FETCH_ASSOC);
} }
/** public function delete(int $id): 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
{ {
$sth = $this->dbh->prepare( $this->teamMapper->delete($id);
'UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, Runder = ? WHERE LagID = ?'
);
$sth->execute([$LagNavn, $Bedrift, $Kortnummer, $Lagleder, $Telefon, $Deltagere, $Runder, $id]);
}
public function addEmptyTeam(): int
{
$sth = $this->dbh->prepare(
'INSERT INTO lagtabell (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder) VALUES (?, ?, ?, ?, ?, ?, ?)'
);
$template = $this->template;
$sth->execute([
$template['LagNavn'],
$template['Bedrift'],
$template['Kortnummer'],
$template['Lagleder'],
$template['Telefon'],
$template['Deltagere'],
$template['Runder'],
]);
return $this->dbh->lastInsertId();
}
/**
* Check if team is empty by comparing it to the template
*/
public function isEqualEmptyTemplate(array $team): bool
{
$template = $this->template;
foreach ($template as $key => $value) {
if ((string)$team[$key] !== (string)$template[$key]) {
return FALSE;
}
}
return TRUE;
} }
/** /**
* Returns TRUE if team exists, FALSE if not * Returns TRUE if team exists, FALSE if not
*/ */
public function recieveStick(string $cardnumber): bool public function recieveBaton(string $cardnumber): bool
{ {
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?'); $team = $this->getTeamByCardnumber($cardnumber);
$sth->execute([$cardnumber]); if ($team)
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{ {
// 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([$row['LagID']]);
return TRUE; return TRUE;
} }
// team does not exist, lets create it // team does not exist, lets create it
$sth = $this->dbh->prepare( $team = new Team();
"INSERT INTO `lagtabell` (`LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`) VALUES (?, ?, ?, ?, ?, ?, ?)" $team->cardnumber = $cardnumber;
); $this->addTeam($team);
$template = $this->template;
$sth->execute([
$template['LagNavn'],
$template['Bedrift'],
$cardnumber,
$template['Lagleder'],
$template['Telefon'],
$template['Deltagere'],
$template['Runder'],
]);
return FALSE; return FALSE;
} }
} }

View File

@ -1,4 +1,4 @@
<h1>Er du sikker?</h1> <h1>Er du sikker?</h1>
<p>Er du sikker at du vil slette lag <?=htmlspecialchars($currentTeam['LagNavn'])?>?</p> <p>Er du sikker at du vil slette lag <?=htmlspecialchars($team->name)?>?</p>
<span>[&nbsp;<a class="success" href="delete.php?item=<?=$currentTeam['LagID']?>">Slett</a>&nbsp;]</span> <span>[&nbsp;<a class="success" href="delete.php?item=<?=$team->id?>">Slett</a>&nbsp;]</span>
<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

@ -1,5 +1,5 @@
<h1>Endre lagtabell</h1> <h1>Endre lagtabell</h1>
<span class="float-right">[&nbsp;<a class="success" href="add.php">Opprett lag</a>&nbsp;]</span> <span class="float-right">[&nbsp;<a class="success" href="create.php">Opprett lag</a>&nbsp;]</span>
<br> <br>
<table> <table>
<tr> <tr>
@ -18,25 +18,19 @@
$i = 0; $i = 0;
foreach ($teams as $team) { foreach ($teams as $team) {
$i++; $i++;
// Remember to escape your values!
foreach ($team as $key => $value)
{
$team[$key] = htmlspecialchars($team[$key]);
}
echo '<tr>'; echo '<tr>';
echo "<td>{$i}</td>"; echo "<td>" . $i . "</td>";
echo "<td>{$team['LagNavn']}</td>"; echo "<td>" . htmlspecialchars($team->name) . "</td>";
echo "<td>{$team['Bedrift']}</td>"; echo "<td>" . htmlspecialchars($team->company) . "</td>";
echo "<td>{$team['Kortnummer']}</td>"; echo "<td>" . htmlspecialchars($team->cardnumber) . "</td>";
echo "<td>{$team['Lagleder']}</td>"; echo "<td>" . htmlspecialchars($team->leader) . "</td>";
echo "<td>{$team['Telefon']}</td>"; echo "<td>" . htmlspecialchars($team->phone) . "</td>";
echo "<td>{$team['Deltagere']}</td>"; echo "<td>" . htmlspecialchars($team->participants) . "</td>";
echo "<td>{$team['Runder']}</td>"; echo "<td>" . htmlspecialchars($team->rounds) . "</td>";
echo "<td>{$team['Bestetid']}</td>"; echo "<td>" . "Ukjent" . "</td>";
echo "<td>"; echo "<td>";
echo "<span>[&nbsp;<a class='danger' href='delete.php?item={$team['LagID']}&confirmation=true'>Slett</a>&nbsp;]</span>"; echo "<span>[&nbsp;<a class='danger' href='delete.php?item={$team->id}&confirm=1'>Slett</a>&nbsp;]</span>";
echo "<span>[&nbsp;<a class='info' href='update.php?item={$team['LagID']}'>Endre</a>&nbsp;]</span>"; echo "<span>[&nbsp;<a class='info' href='update.php?item={$team->id}'>Endre</a>&nbsp;]</span>";
echo "</td>"; echo "</td>";
echo '</tr>'; echo '</tr>';
} }

View File

@ -1,45 +1,40 @@
<?php
// Escape values
foreach ($team as $key => $value) {
$team[$key] = htmlspecialchars($value);
}
?>
<h1>Endre lagdetaljer</h1> <h1>Endre lagdetaljer</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="LagNavn">Navn:</label>
<br>
<input type="text" id="LagNavn" name="LagNavn" value="<?=$team['LagNavn']?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<label for="Bedrift">Bedrift:</label> <input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<br>
<input type="text" id="Bedrift" name="Bedrift" value="<?=$team['Bedrift']?>" maxlength="32">
<br>
<label for="Kortnummer">Kortnummer:</label>
<br>
<input type="text" id="Kortnummer" name="Kortnummer" value="<?=$team['Kortnummer']?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<label for="Lagleder">Leder:</label> <input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<br>
<input type="text" id="Lagleder" name="Lagleder" value="<?=$team['Lagleder']?>" maxlength="32">
<br>
<label for="Telefon">Telefon:</label>
<br>
<input type="number" id="Telefon" name="Telefon" value="<?=$team['Telefon']?>" maxlength="32">
<br>
<label for="Deltagere">Deltagere:</label>
<br>
<input type="number" id="Deltagere" name="Deltagere" value="<?=$team['Deltagere']?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<label for="Runder">Runder:</label> <input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label>
<br> <br>
<input type="number" id="Runder" name="Runder" value="<?=$team['Runder']?>" maxlength="32"> <input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label>
<br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label>
<br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<label for="name">Navn:</label>
<br>
<input type="text" id="name" name="name" value="<?=htmlspecialchars($team->name)?>" maxlength="32">
<br> <br>
<br> <br>

View File

@ -13,25 +13,19 @@
<th>Bestetid</th> <th>Bestetid</th>
</tr> </tr>
<?php <?php
$i = 0; $i = 0;
foreach ($teams as $team) { foreach ($teams as $team) {
$i++; $i++;
// Remember to escape your values!
foreach ($team as $key => $value)
{
$team[$key] = htmlspecialchars($team[$key]);
}
echo '<tr>'; echo '<tr>';
echo "<td>{$i}</td>"; echo "<td>" . $i . "</td>";
echo "<td>{$team['LagNavn']}</td>"; echo "<td>" . htmlspecialchars($team->name) . "</td>";
echo "<td>{$team['Bedrift']}</td>"; echo "<td>" . htmlspecialchars($team->company) . "</td>";
echo "<td>{$team['Kortnummer']}</td>"; echo "<td>" . htmlspecialchars($team->cardnumber) . "</td>";
echo "<td>{$team['Lagleder']}</td>"; echo "<td>" . htmlspecialchars($team->leader) . "</td>";
echo "<td>{$team['Telefon']}</td>"; echo "<td>" . htmlspecialchars($team->phone) . "</td>";
echo "<td>{$team['Deltagere']}</td>"; echo "<td>" . htmlspecialchars($team->participants) . "</td>";
echo "<td>{$team['Runder']}</td>"; echo "<td>" . htmlspecialchars($team->rounds) . "</td>";
echo "<td>{$team['Bestetid']}</td>"; echo "<td>" . "Ukjent" . "</td>";
echo '</tr>'; echo '</tr>';
} }
?> ?>

View File

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

View File

@ -0,0 +1,16 @@
<?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,47 +1,37 @@
<?php <?php $app = require '../../../app/inc.php';
$app = require '../../../app/inc.php';
use App\Teamtable\Team;
$item = filter_input(INPUT_GET, 'item', FILTER_VALIDATE_INT);
$confirm = filter_input(INPUT_GET, 'confirm', FILTER_VALIDATE_BOOL);
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
if (!isset($_GET['item'])) // item is NULL if not set
if ($item === NULL)
{ {
$app->session->flash('Kunne ikke slette lag: ID ikke definert som GET parameter', 'danger'); $app->session->flash('Kunne ikke slette lag: Mangler parameter.', 'danger');
$app->redirect('index.php');
}
$id = $_GET['item'];
// ID must be numeric
if (!is_numeric($id))
{
$app->session->flash('Kunne ikke slette lag: ID må være tall', 'danger');
$app->redirect('index.php'); $app->redirect('index.php');
} }
// Check if ID is in teamtable $team = $model->get($item);
$currentTeam = $model->getTeamByID($id); if (!$team)
if (!$currentTeam)
{ {
$app->session->flash("Kunne ikke slette lag: ID $id finnes ikke", "danger"); // team does not exist
$app->session->flash('Kunne ikke slette lag: Lag eksisterer ikke', 'danger');
$app->redirect('index.php'); $app->redirect('index.php');
} }
$sameAsTemplate = $model->isEqualEmptyTemplate($currentTeam); // show confirmation page
if ($confirm)
// Show confirmation page
if (isset($_GET['confirmation']) && $_GET['confirmation'] == 'true' && !$sameAsTemplate)
{ {
$app->view('template/header', ['title' => 'Bekreft sletting']); $app->view('template/header', ['title' => 'Bekreft sletting']);
$app->view('pages/teamtable/edit/delete', ['currentTeam' => $currentTeam]); $app->view('pages/teamtable/edit/delete', ['team' => $team]);
$app->view('template/footer'); $app->view('template/footer');
die(); die();
} }
$model->deleteTeamByID($id); // all is good, lets delete the team
$model->delete($team->id);
if ($sameAsTemplate) { $app->session->flash("Slettet lag: {$team->name}", "success");
$app->session->flash("Slettet lagmal", "success");
} else {
$app->session->flash("Slettet lag: {$currentTeam['LagNavn']}", "success");
}
$app->redirect('index.php'); $app->redirect('index.php');

View File

@ -1,9 +1,8 @@
<?php <?php $app = require '../../../app/inc.php';
$app = require '../../../app/inc.php';
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
$teams = $model->getTable(); $teams = $model->getAll();
$app->view('template/header', ['title' => 'Endre lagtabell']); $app->view('template/header', ['title' => 'Endre lagtabell']);
$app->view('pages/teamtable/edit/index', ["teams" => $teams]); $app->view('pages/teamtable/edit/index', ["teams" => $teams]);

View File

@ -1,151 +1,25 @@
<?php <?php $app = require '../../../app/inc.php';
$app = require '../../../app/inc.php';
use App\Teamtable\Team;
$item = filter_input(INPUT_GET, 'item', FILTER_VALIDATE_INT);
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
if (!isset($_GET['item'])) // item is NULL if not set
if ($item !== NULL)
{ {
$app->session->flash('Kunne ikke endre lag: ID ikke definert som GET parameter', 'danger'); // check that team exists
$app->redirect('index.php'); $team = $model->get($item);
} if (!$team)
$id = $_GET['item'];
// 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 team with supplied ID exists
$currentTeam = $model->getTeamByID($id);
if (!$currentTeam)
{
$app->session->flash("Kunne ikke endre lag: ID $id finnes ikke", "danger");
$app->redirect('index.php');
}
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"); // team does not exist
$app->redirect('./'); $app->session->flash('Kunne ikke endre lag: Lag finnes ikke', 'danger');
} $app->redirect('index.php');
$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;
$template = $model->template;
// LagNavn
if (!strlen($LagNavn)) {
$LagNavn = $template['LagNavn'];
}
if (strlen($LagNavn) > 32) {
$validationError = TRUE;
}
// Bedrift
if (!strlen($Bedrift)) {
$Bedrift = $template['Bedrift'];
}
if (strlen($Bedrift) > 32) {
$validationError = TRUE;
}
// Kortnummer
if (!strlen($Kortnummer)) {
$Kortnummer = $template['Kortnummer'];
}
if (strlen($Kortnummer) > 32) {
$validationError = TRUE;
}
// Lagleder
if (!strlen($Lagleder)) {
$Lagleder = $template['Lagleder'];
}
if (strlen($Lagleder) > 32) {
$validationError = TRUE;
}
// Telefon
if (!strlen($Telefon)) {
$Telefon = $template['Telefon'];
}
if (strlen((string) $Telefon) > 32) {
$validationError = TRUE;
}
// Deltagere
if (!strlen($Deltagere)) {
$Deltagere = $template['Deltagere'];
}
if (strlen((string) $Deltagere) > 32) {
$validationError = TRUE;
}
// Runder
if (!strlen($Runder)) {
$Runder = $template['Runder'];
}
if (strlen((string) $Runder) > 32) {
$validationError = TRUE;
}
if (!$validationError) {
if (
$LagNavn == $currentTeam['LagNavn'] &&
$Bedrift == $currentTeam['Bedrift'] &&
$Kortnummer == $currentTeam['Kortnummer'] &&
$Lagleder == $currentTeam['Lagleder'] &&
$Telefon == $currentTeam['Telefon'] &&
$Deltagere == $currentTeam['Deltagere'] &&
$Runder == $currentTeam['Runder']
)
{
$app->session->flash('Fant ingen endringer for lag: '.$LagNavn);
} else {
// 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('index.php');
} else {
$app->session->flash('Kunne ikke endre lag: Validerings feil!', 'danger');
} }
} }
// lets create a team
$app->view('template/header', ['title' => 'Endre lagdetaljer']); $app->view('template/header', ['title' => 'Endre lagdetaljer']);
$app->view('pages/teamtable/edit/update', ["team" => $currentTeam]); $app->view('pages/teamtable/edit/update', ["team" => new Team]);
$app->view('template/footer'); $app->view('template/footer');

View File

@ -3,7 +3,7 @@ $app = require '../../app/inc.php';
$model = $app->model('Teamtable'); $model = $app->model('Teamtable');
$teams = $model->getTable(); $teams = $model->getAll();
$app->view('template/header', ['title' => 'Lagtabell']); $app->view('template/header', ['title' => 'Lagtabell']);
$app->view('pages/teamtable/index', ["teams" => $teams]); $app->view('pages/teamtable/index', ["teams" => $teams]);

View File

@ -1,6 +0,0 @@
<?php
$app = require '../app/inc.php';
$team = new App\Teamtable\Team;
var_dump($team);