Add simulator

This commit is contained in:
William 2022-01-26 14:38:22 +01:00
parent 50569c4f26
commit 1e1635ebb4
2 changed files with 41 additions and 1 deletions

33
app/model/Simulator.php Normal file
View File

@ -0,0 +1,33 @@
<?php
class Simulator
{
public PDO $dbh;
public function __construct(Database $database)
{
$this->dbh = $database->conn;
}
// Return true if team exists, false if not
public function insert(string $cardnumber): bool
{
$sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?');
$sth->execute([$cardnumber]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row)
{
// 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` (`LagID`, `LagNavn`, `Bedrift`, `Kortnummer`, `Lagleder`, `Telefon`, `Deltagere`, `Runder`, `Bestetid`) VALUES (NULL, 'NN', 'NN', ?, 'NN', '0', '0', '0', now())"
);
$sth->execute([$cardnumber]);
return FALSE;
}
}

View File

@ -7,7 +7,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$app->session->flash('Kortnummer kan ikke være tom!', 'danger');
} else {
$app->session->flash("Kortnummer: \"{$_POST['cardnumber']}\" er ikke blitt lastet opp men burde bli implementert snart!", 'success');
$model = $app->model('Simulator');
if ($model->insert($_POST['cardnumber']))
{
$app->session->flash("Lag funnet for \"{$_POST['cardnumber']}\"", "success");
} else {
$app->session->flash("Opprettet lag for \"{$_POST['cardnumber']}\"");
}
}
}