diff --git a/app/inc.php b/app/inc.php index 9765dba..470a9ef 100644 --- a/app/inc.php +++ b/app/inc.php @@ -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 diff --git a/app/lib/App/Core/App.php b/app/lib/App/Core/App.php index 65159e7..8d30419 100644 --- a/app/lib/App/Core/App.php +++ b/app/lib/App/Core/App.php @@ -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(); } diff --git a/app/lib/App/Core/Database.php b/app/lib/App/Core/Database.php index e82b421..05abe0a 100644 --- a/app/lib/App/Core/Database.php +++ b/app/lib/App/Core/Database.php @@ -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); } diff --git a/app/lib/App/Core/Session.php b/app/lib/App/Core/Session.php index 2483587..85e34df 100644 --- a/app/lib/App/Core/Session.php +++ b/app/lib/App/Core/Session.php @@ -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)) diff --git a/app/lib/App/Teamtable/Team.php b/app/lib/App/Teamtable/Team.php index 62a1f16..0f21907 100644 --- a/app/lib/App/Teamtable/Team.php +++ b/app/lib/App/Teamtable/Team.php @@ -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; } } \ No newline at end of file diff --git a/app/lib/App/Teamtable/TeamMapper.php b/app/lib/App/Teamtable/TeamMapper.php new file mode 100644 index 0000000..19732f0 --- /dev/null +++ b/app/lib/App/Teamtable/TeamMapper.php @@ -0,0 +1,118 @@ +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]); + } +} \ No newline at end of file diff --git a/app/model/Teamtable.php b/app/model/Teamtable.php index c21ceee..73dd1a5 100644 --- a/app/model/Teamtable.php +++ b/app/model/Teamtable.php @@ -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; } } \ No newline at end of file diff --git a/app/view/pages/teamtable/edit/delete.php b/app/view/pages/teamtable/edit/delete.php index 1a4208f..d870fe9 100644 --- a/app/view/pages/teamtable/edit/delete.php +++ b/app/view/pages/teamtable/edit/delete.php @@ -1,4 +1,4 @@
Er du sikker på at du vil slette lag =htmlspecialchars($currentTeam['LagNavn'])?>?
-[ Slett ] +Er du sikker på at du vil slette lag =htmlspecialchars($team->name)?>?
+[ Slett ] [ Avbryt ] \ 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 6b890b1..5a0acf0 100644 --- a/app/view/pages/teamtable/edit/index.php +++ b/app/view/pages/teamtable/edit/index.php @@ -1,5 +1,5 @@{$i} | "; - echo "{$team['LagNavn']} | "; - echo "{$team['Bedrift']} | "; - echo "{$team['Kortnummer']} | "; - echo "{$team['Lagleder']} | "; - echo "{$team['Telefon']} | "; - echo "{$team['Deltagere']} | "; - echo "{$team['Runder']} | "; - echo "{$team['Bestetid']} | "; + echo "" . $i . " | "; + echo "" . htmlspecialchars($team->name) . " | "; + echo "" . htmlspecialchars($team->company) . " | "; + echo "" . htmlspecialchars($team->cardnumber) . " | "; + echo "" . htmlspecialchars($team->leader) . " | "; + echo "" . htmlspecialchars($team->phone) . " | "; + echo "" . htmlspecialchars($team->participants) . " | "; + echo "" . htmlspecialchars($team->rounds) . " | "; + echo "" . "Ukjent" . " | "; echo ""; - echo "[ Slett ]"; - echo "[ Endre ]"; + echo "[ Slett ]"; + echo "[ Endre ]"; echo " | "; echo '