96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
class Teamtable
|
|
{
|
|
public PDO $dbh;
|
|
|
|
public array $emptyTeamTemplate = [
|
|
'LagNavn' => 'NN',
|
|
'Bedrift' => 'NN',
|
|
'Kortnummer' => 'NN',
|
|
'Lagleder' => 'NN',
|
|
'Telefon' => 0,
|
|
'Deltagere' => 0,
|
|
'Runder' => 0
|
|
];
|
|
|
|
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);
|
|
}
|
|
|
|
public function deleteTeamByID(int $LagID): void
|
|
{
|
|
$sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
|
|
$sth->execute([$LagID]);
|
|
}
|
|
|
|
public function getTeamByID(int $LagID) # Mixed, array if exists, false if not
|
|
{
|
|
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?');
|
|
$sth->execute([$LagID]);
|
|
return $sth->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
// 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(
|
|
'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->emptyTeamTemplate;
|
|
$template = [
|
|
$template['LagNavn'],
|
|
$template['Bedrift'],
|
|
$template['Kortnummer'],
|
|
$template['Lagleder'],
|
|
$template['Telefon'],
|
|
$template['Deltagere'],
|
|
$template['Runder'],
|
|
];
|
|
$sth->execute($template);
|
|
return $this->dbh->lastInsertId();
|
|
}
|
|
|
|
// Check if team is empty by comparing it to the template
|
|
public function isEqualEmptyTemplate(array $team): bool
|
|
{
|
|
$template = $this->emptyTeamTemplate;
|
|
$equal = FALSE;
|
|
foreach ($team as $key => $value) {
|
|
if (!isset($template[$key])) {
|
|
continue;
|
|
}
|
|
if ($template[$key] !== $team[$key]) {
|
|
$equal = FALSE;
|
|
break;
|
|
}
|
|
$equal = TRUE;
|
|
}
|
|
return $equal;
|
|
}
|
|
} |