Advertisement
Guest User

Untitled

a guest
Jun 5th, 2023
63
0
26 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. /****************************************
  3.  * Core.php Datei
  4.  ***************************************/
  5. declare(strict_types=1);
  6.  
  7. namespace Kalender;
  8.  
  9. /**
  10.  * Hauptklasse mit Datenbank
  11.  */
  12. class Core {
  13.  
  14.     /**
  15.      * Handle zur verbundenen Datenbank
  16.      */
  17.     private $link = null;
  18.  
  19.     /**
  20.      * Benutzerrollen
  21.      */
  22.     public const GAST = 0;
  23.     public const USER = 1;
  24.     public const MOD = 2;
  25.     public const ADMIN = 3;
  26.  
  27.     /**
  28.      * Verbinde zur Datenbank und speichere das Handle $link
  29.      * @throws \Exception on error
  30.      */
  31.     private function connectDB(): void {
  32.  
  33.         require('conf.php'); // Datenbank Zugangsdaten
  34.         $this->link = new \mysqli($conf["host"], $conf["user"], $conf["password"], $conf["name"]);
  35.  
  36.         // check connection
  37.         if (mysqli_connect_errno())
  38.             throw new \Exception('Connect failed: ' . mysqli_connect_error());
  39.  
  40.         if (!$this->link->set_charset("utf8"))
  41.             throw new \Exception('Error loading character set utf8: ' . $this->link->error);
  42.     }
  43.  
  44.     /**
  45.      * Schliesst die Datenbank (Braucht man aber fast nie, da die Datenbank \
  46.      * beim Beenden automatisch geschlossen wird.)
  47.      */
  48.     private function closeDB() {
  49.         $this->link->close();
  50.     }
  51.  
  52.     /**
  53.      * Get Staff List
  54.      * @param array $user Benutzer
  55.      * @return array Benutzerdaten
  56.      * @throws \Exception on error
  57.      */
  58.     public function getStaffList(array $user): array {
  59.         try {
  60.             $this->connectDB();
  61.  
  62.             if ($user['role'] === self::ADMIN) {
  63.                 $stmt = $this->link->prepare('SELECT * FROM staff');
  64.             }
  65.             else {
  66.                 $stmt = $this->link->prepare('SELECT * FROM staff WHERE surname=?');
  67.                 $stmt->bind_param('s', $user['name']);
  68.             }
  69.  
  70.             $stmt->execute();
  71.  
  72.             $result = $stmt->get_result();
  73.  
  74.             if (!$result)
  75.                 throw new \Exception(mysqli_error($this->link));
  76.  
  77.             $staff = [];
  78.  
  79.             while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  80.                 if ($row['birthday'] !== '0000-00-00')
  81.                     $row['birthday'] = date("d.m.Y", strtotime($row['birthday']));
  82.                 else
  83.                     $row['birthday'] = '';
  84.  
  85.                 $staff[] = [
  86.                     'birthday' => $row['birthday'],
  87.                     'surname' => $row['surname'],
  88.                 ];
  89.             }
  90.  
  91.             return $staff;
  92.         }
  93.         catch(\Exception $e) {
  94.             throw new \Exception($e->getMessage());
  95.         }
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement