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

View File

@ -30,19 +30,22 @@ class App
$this->user = $user;
}
// grab model
// 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
/**
* Grab model
*
* 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
{
// Require model file
// require model file
$path = $this->dir . '/model/' . $model . '.php';
if (!file_exists($path))
{
throw new Exception("Model does not exist");
}
require $path;
// Instantiate model
// instantiate model
if (!$injection)
{
$injection = $this->database;
@ -71,10 +74,10 @@ class App
*/
public function api(array $data, int $status_code = 200): void
{
// Set headers
// set headers
http_response_code($status_code);
header('Content-type: application/json');
// Convert and respond with data
// convert and respond with data
echo json_encode($data);
die();
}

View File

@ -8,7 +8,7 @@ use \PDOException;
/**
* Encapsulates a single connection to a database.
* TODO: Refactor and add different driver implementations.
* TODO: add different driver implementations.
*/
class Database
{
@ -33,7 +33,7 @@ class Database
$dsn = "mysql:host={$args['host']};dbname={$args['database']};charset={$args['charset']}";
$options = [
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);
}

View File

@ -3,6 +3,7 @@
namespace App\Core;
use \Exception;
use \InvalidArgumentException;
/**
* Handles anything to do with sessions
@ -63,7 +64,7 @@ class Session
"warning"
];
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';
if (!$this->has($key))

View File

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

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\Teamtable\Team as Team;
use App\Teamtable\TeamMapper as TeamMapper;
/**
* Do stuff with the teamtable
* Does stuffs with the teamtable
*/
class Teamtable
{
/**
* Database handler
* Database connection
*/
public PDO $dbh;
/**
* We use a data mapper pattern
*/
public TeamMapper $teamMapper;
public function __construct(Database $database)
{
$this->dbh = $database->conn;
$this->teamMapper = new TeamMapper($this->dbh);
}
/**
* Fetch entire team table
*/
public function getTable(): array
public function getAll(): array
{
$sth = $this->dbh->query('SELECT * FROM lagtabell');
return $sth->fetchAll(PDO::FETCH_ASSOC);
return $this->teamMapper->getAll();
}
/**
* 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 = ?');
$sth->execute([$LagID]);
return $this->teamMapper->get($id);
}
/**
* 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 = ?');
$sth->execute([$LagID]);
return $sth->fetch(PDO::FETCH_ASSOC);
return $this->teamMapper->create($team);
}
/**
* 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
public function delete(int $id): void
{
$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 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;
$this->teamMapper->delete($id);
}
/**
* 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 = ?');
$sth->execute([$cardnumber]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
$team = $this->getTeamByCardnumber($cardnumber);
if ($team)
{
// team exists, insert into time table
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
$sth->execute([$row['LagID']]);
return TRUE;
}
// team does not exist, lets create it
$sth = $this->dbh->prepare(
"INSERT INTO `lagtabell` (`LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`) VALUES (?, ?, ?, ?, ?, ?, ?)"
);
$template = $this->template;
$sth->execute([
$template['LagNavn'],
$template['Bedrift'],
$cardnumber,
$template['Lagleder'],
$template['Telefon'],
$template['Deltagere'],
$template['Runder'],
]);
// team does not exist, lets create it
$team = new Team();
$team->cardnumber = $cardnumber;
$this->addTeam($team);
return FALSE;
}
}

View File

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

View File

@ -1,5 +1,5 @@
<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>
<table>
<tr>
@ -18,25 +18,19 @@
$i = 0;
foreach ($teams as $team) {
$i++;
// Remember to escape your values!
foreach ($team as $key => $value)
{
$team[$key] = htmlspecialchars($team[$key]);
}
echo '<tr>';
echo "<td>{$i}</td>";
echo "<td>{$team['LagNavn']}</td>";
echo "<td>{$team['Bedrift']}</td>";
echo "<td>{$team['Kortnummer']}</td>";
echo "<td>{$team['Lagleder']}</td>";
echo "<td>{$team['Telefon']}</td>";
echo "<td>{$team['Deltagere']}</td>";
echo "<td>{$team['Runder']}</td>";
echo "<td>{$team['Bestetid']}</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . htmlspecialchars($team->name) . "</td>";
echo "<td>" . htmlspecialchars($team->company) . "</td>";
echo "<td>" . htmlspecialchars($team->cardnumber) . "</td>";
echo "<td>" . htmlspecialchars($team->leader) . "</td>";
echo "<td>" . htmlspecialchars($team->phone) . "</td>";
echo "<td>" . htmlspecialchars($team->participants) . "</td>";
echo "<td>" . htmlspecialchars($team->rounds) . "</td>";
echo "<td>" . "Ukjent" . "</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='info' href='update.php?item={$team['LagID']}'>Endre</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->id}'>Endre</a>&nbsp;]</span>";
echo "</td>";
echo '</tr>';
}

View File

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

View File

@ -13,25 +13,19 @@
<th>Bestetid</th>
</tr>
<?php
$i = 0;
$i = 0;
foreach ($teams as $team) {
$i++;
// Remember to escape your values!
foreach ($team as $key => $value)
{
$team[$key] = htmlspecialchars($team[$key]);
}
echo '<tr>';
echo "<td>{$i}</td>";
echo "<td>{$team['LagNavn']}</td>";
echo "<td>{$team['Bedrift']}</td>";
echo "<td>{$team['Kortnummer']}</td>";
echo "<td>{$team['Lagleder']}</td>";
echo "<td>{$team['Telefon']}</td>";
echo "<td>{$team['Deltagere']}</td>";
echo "<td>{$team['Runder']}</td>";
echo "<td>{$team['Bestetid']}</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . htmlspecialchars($team->name) . "</td>";
echo "<td>" . htmlspecialchars($team->company) . "</td>";
echo "<td>" . htmlspecialchars($team->cardnumber) . "</td>";
echo "<td>" . htmlspecialchars($team->leader) . "</td>";
echo "<td>" . htmlspecialchars($team->phone) . "</td>";
echo "<td>" . htmlspecialchars($team->participants) . "</td>";
echo "<td>" . htmlspecialchars($team->rounds) . "</td>";
echo "<td>" . "Ukjent" . "</td>";
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
$app = require '../../../app/inc.php';
<?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');
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->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->session->flash('Kunne ikke slette lag: Mangler parameter.', 'danger');
$app->redirect('index.php');
}
// Check if ID is in teamtable
$currentTeam = $model->getTeamByID($id);
if (!$currentTeam)
$team = $model->get($item);
if (!$team)
{
$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');
}
$sameAsTemplate = $model->isEqualEmptyTemplate($currentTeam);
// Show confirmation page
if (isset($_GET['confirmation']) && $_GET['confirmation'] == 'true' && !$sameAsTemplate)
// show confirmation page
if ($confirm)
{
$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');
die();
}
$model->deleteTeamByID($id);
if ($sameAsTemplate) {
$app->session->flash("Slettet lagmal", "success");
} else {
$app->session->flash("Slettet lag: {$currentTeam['LagNavn']}", "success");
}
// all is good, lets delete the team
$model->delete($team->id);
$app->session->flash("Slettet lag: {$team->name}", "success");
$app->redirect('index.php');

View File

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

View File

@ -1,151 +1,25 @@
<?php
$app = require '../../../app/inc.php';
<?php $app = require '../../../app/inc.php';
use App\Teamtable\Team;
$item = filter_input(INPUT_GET, 'item', FILTER_VALIDATE_INT);
$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');
$app->redirect('index.php');
}
$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'],
)
)
// check that team exists
$team = $model->get($item);
if (!$team)
{
$app->session->flash("Kunne ikke endre lag: Ikke alle POST parametere er til stede!", "danger");
$app->redirect('./');
}
$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');
// team does not exist
$app->session->flash('Kunne ikke endre lag: Lag finnes ikke', 'danger');
$app->redirect('index.php');
}
}
// lets create a team
$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');

View File

@ -3,7 +3,7 @@ $app = require '../../app/inc.php';
$model = $app->model('Teamtable');
$teams = $model->getTable();
$teams = $model->getAll();
$app->view('template/header', ['title' => 'Lagtabell']);
$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);