This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/model/Teamtable.php

130 lines
3.7 KiB
PHP
Raw Normal View History

2022-02-07 10:09:16 +00:00
<?php
2022-03-02 06:15:12 +00:00
use App\Core\Database as Database;
2022-02-07 10:09:16 +00:00
class Teamtable
{
2022-02-28 04:51:22 +00:00
public array $template = [
2022-02-15 11:26:15 +00:00
'LagNavn' => 'NN',
'Bedrift' => 'NN',
'Kortnummer' => 'NN',
'Lagleder' => 'NN',
'Telefon' => 0,
'Deltagere' => 0,
'Runder' => 0
];
2022-02-28 04:51:22 +00:00
public PDO $dbh;
2022-02-07 10:09:16 +00:00
public function __construct(Database $database)
{
$this->dbh = $database->conn;
}
public function getTable(): array
{
$sth = $this->dbh->query('SELECT * FROM lagtabell');
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
2022-02-09 15:59:20 +00:00
public function deleteTeamByID(int $LagID): void
{
$sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
$sth->execute([$LagID]);
}
2022-03-02 06:24:27 +00:00
/**
* Returns mixed, array if exists, FALSE if not.
*/
public function getTeamByID(int $LagID)
2022-02-09 15:59:20 +00:00
{
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?');
$sth->execute([$LagID]);
return $sth->fetch(PDO::FETCH_ASSOC);
}
2022-03-02 06:24:27 +00:00
/**
* Why this is so long who cares???
*/
2022-02-14 10:23:05 +00:00
public function updateTeamByID(
int $id,
string $LagNavn,
string $Bedrift,
string $Kortnummer,
string $Lagleder,
int $Telefon,
string $Deltagere,
int $Runder
): void
2022-02-09 15:59:20 +00:00
{
2022-02-15 11:26:15 +00:00
$sth = $this->dbh->prepare(
'UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, Runder = ? WHERE LagID = ?'
);
2022-02-14 10:23:05 +00:00
$sth->execute([$LagNavn, $Bedrift, $Kortnummer, $Lagleder, $Telefon, $Deltagere, $Runder, $id]);
2022-02-09 15:59:20 +00:00
}
2022-02-15 11:26:15 +00:00
public function addEmptyTeam(): int
{
$sth = $this->dbh->prepare(
'INSERT INTO lagtabell (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder) VALUES (?, ?, ?, ?, ?, ?, ?)'
);
2022-02-28 04:51:22 +00:00
$template = $this->template;
2022-02-16 10:59:47 +00:00
$sth->execute([
2022-02-15 11:26:15 +00:00
$template['LagNavn'],
$template['Bedrift'],
$template['Kortnummer'],
$template['Lagleder'],
$template['Telefon'],
$template['Deltagere'],
$template['Runder'],
2022-02-16 10:59:47 +00:00
]);
2022-02-15 11:26:15 +00:00
return $this->dbh->lastInsertId();
}
2022-03-02 06:24:27 +00:00
/**
* Check if team is empty by comparing it to the template
*/
2022-02-15 11:26:15 +00:00
public function isEqualEmptyTemplate(array $team): bool
2022-02-09 15:59:20 +00:00
{
2022-02-28 04:51:22 +00:00
$template = $this->template;
2022-02-16 10:59:47 +00:00
foreach ($template as $key => $value) {
if ((string)$team[$key] !== (string)$template[$key]) {
return FALSE;
2022-02-15 11:26:15 +00:00
}
}
2022-02-16 10:59:47 +00:00
return TRUE;
2022-02-09 15:59:20 +00:00
}
2022-02-15 11:40:57 +00:00
2022-03-02 06:24:27 +00:00
/**
* Returns TRUE if team exists, FALSE if not
*/
2022-02-16 12:05:13 +00:00
public function recieveStick(string $cardnumber): bool
2022-02-15 11:40:57 +00:00
{
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?');
$sth->execute([$cardnumber]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
2022-03-02 06:24:27 +00:00
// team exists, insert into time table
2022-02-15 11:40:57 +00:00
$sth = $this->dbh->prepare('INSERT INTO tidtabell (LagID) VALUES (?)');
$sth->execute([$row['LagID']]);
return TRUE;
}
2022-03-02 06:24:27 +00:00
// team does not exist, lets create it
2022-02-15 11:40:57 +00:00
$sth = $this->dbh->prepare(
"INSERT INTO `lagtabell` (`LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`) VALUES (?, ?, ?, ?, ?, ?, ?)"
);
2022-02-28 04:51:22 +00:00
$template = $this->template;
2022-02-16 12:05:13 +00:00
$sth->execute([
2022-02-15 11:40:57 +00:00
$template['LagNavn'],
$template['Bedrift'],
$cardnumber,
$template['Lagleder'],
$template['Telefon'],
$template['Deltagere'],
$template['Runder'],
2022-02-16 12:05:13 +00:00
]);
2022-02-15 11:40:57 +00:00
return FALSE;
}
2022-02-07 10:09:16 +00:00
}