Advertisement
Guest User

Untitled

a guest
Jun 6th, 2023
60
0
27 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 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 \mysqli $link;
  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.      * Constructor
  29.      */
  30.     public function __construct($dbconf) {
  31.         $this->connectDB($dbconf);
  32.     }
  33.  
  34.     /**
  35.      * Normalize birthday
  36.      */    
  37.     private function fixBirthday(string &$birthday): void {
  38.         $birthday = ($birthday !== '0000-00-00') ? date("d.m.Y", strtotime($birthday)) : '' ;
  39.     }
  40.  
  41.     /**
  42.      * Extract staff from row
  43.      * @param array $row Tabllenreihe aus der Datenbank
  44.      * @return array Ausgewählte Benutzerdaten
  45.      */    
  46.     private function extractStaffFromRow(array $row): array {
  47.         return [
  48.             'id_staff' => $row['id_staff'],
  49.             'birthday' => $row['birthday'],
  50.             'surname'  => $row['surname'],
  51.         ];
  52.     }
  53.  
  54.     /**
  55.      * Verbinde zur Datenbank und speichere das Handle $link
  56.      * @throws \Exception on error
  57.      */
  58.     private function connectDB($dbconf): void {
  59.  
  60.         $this->link = new \mysqli($dbconf["host"], $dbconf["user"], $dbconf["password"], $dbconf["name"]);
  61.  
  62.         // check connection
  63.         if (mysqli_connect_errno())
  64.             throw new \Exception('Connect failed: ' . mysqli_connect_error());
  65.  
  66.         if (!$this->link->set_charset("utf8"))
  67.             throw new \Exception('Error loading character set utf8: ' . $this->link->error);
  68.     }
  69.  
  70.     /**
  71.      * Get Staff List
  72.      * @param array $user Benutzer
  73.      * @return array Benutzerdaten
  74.      * @throws \Exception on error
  75.      */
  76.     public function getStaffList(array $user): array {
  77.         try {
  78.             if ($user['role'] === self::ADMIN) {
  79.                 $stmt = $this->link->prepare('SELECT * FROM staff');
  80.             }
  81.             else {
  82.                 $stmt = $this->link->prepare('SELECT * FROM staff WHERE surname=?');
  83.                 $stmt->bind_param('s', $user['name']);
  84.             }
  85.  
  86.             $stmt->execute();
  87.  
  88.             $result = $stmt->get_result();
  89.  
  90.             if (!$result)
  91.                 throw new \Exception(mysqli_error($this->link));
  92.  
  93.             $staff = [];
  94.  
  95.             while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  96.                 $this->fixBirthday($row['birthday']);
  97.                 $staff[] = $this->extractStaffFromRow($row);
  98.             }
  99.  
  100.             return $staff;
  101.         }
  102.         catch(\Exception $e) {
  103.             throw new \Exception($e->getMessage());
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Get Staff by ID
  109.      * @param string $id_staff Staff ID
  110.      * @return array Benutzerdaten
  111.      * @throws \Exception on error
  112.      */
  113.     public function getStaff($id_staff) {
  114.         try {
  115.             $stmt = $this->link->prepare('SELECT * FROM staff WHERE id_staff=?');
  116.             $stmt->bind_param('s', $id_staff);
  117.             $stmt->execute();
  118.  
  119.             $result = $stmt->get_result();
  120.  
  121.             if (!$result)
  122.                 throw new \Exception(mysqli_error($this->link));
  123.  
  124.             $row = $result->fetch_array(MYSQLI_ASSOC);
  125.             $this->fixBirthday($row['birthday']);
  126.             return $this->extractStaffFromRow($row);
  127.         }
  128.         catch(\Exception $e) {
  129.             throw new \Exception($e->getMessage());
  130.         }
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement