133 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| use App\Core\Database as Database;
 | |
| use App\Teamtable\Team as Team;
 | |
| 
 | |
| /**
 | |
|  * Do stuff with the teamtable
 | |
|  */
 | |
| class Teamtable
 | |
| {
 | |
|     /**
 | |
|      * Database handler
 | |
|      */
 | |
|     public PDO $dbh;
 | |
| 
 | |
|     public function __construct(Database $database)
 | |
|     {
 | |
|         $this->dbh = $database->conn;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Fetch entire team table
 | |
|      */
 | |
|     public function getTable(): array
 | |
|     {
 | |
|         $sth = $this->dbh->query('SELECT * FROM lagtabell');
 | |
|         return $sth->fetchAll(PDO::FETCH_ASSOC);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Delete team with supplied id
 | |
|      */
 | |
|     public function deleteTeamByID(int $LagID): void
 | |
|     {
 | |
|         $sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?');
 | |
|         $sth->execute([$LagID]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Returns mixed, array if exists, FALSE if not.
 | |
|      */
 | |
|     public function getTeamByID(int $LagID)
 | |
|     {
 | |
|         $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->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;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Returns TRUE if team exists, FALSE if not
 | |
|      */
 | |
|     public function recieveStick(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` (`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'],
 | |
|         ]);
 | |
|         return FALSE;
 | |
|     }
 | |
| } |