Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /****************************************
- * Core.php Datei
- ***************************************/
- declare(strict_types=1);
- namespace Kalender;
- /**
- * Hauptklasse mit Datenbank
- */
- class Core {
- /**
- * Handle zur verbundenen Datenbank
- */
- private \mysqli $link;
- /**
- * Benutzerrollen
- */
- public const GAST = 0;
- public const USER = 1;
- public const MOD = 2;
- public const ADMIN = 3;
- /**
- * Constructor
- */
- public function __construct($dbconf) {
- $this->connectDB($dbconf);
- }
- /**
- * Normalize birthday
- */
- private function fixBirthday(string &$birthday): void {
- $birthday = ($birthday !== '0000-00-00') ? date("d.m.Y", strtotime($birthday)) : '' ;
- }
- /**
- * Extract staff from row
- * @param array $row Tabllenreihe aus der Datenbank
- * @return array Ausgewählte Benutzerdaten
- */
- private function extractStaffFromRow(array $row): array {
- return [
- 'id_staff' => $row['id_staff'],
- 'birthday' => $row['birthday'],
- 'surname' => $row['surname'],
- ];
- }
- /**
- * Verbinde zur Datenbank und speichere das Handle $link
- * @throws \Exception on error
- */
- private function connectDB($dbconf): void {
- $this->link = new \mysqli($dbconf["host"], $dbconf["user"], $dbconf["password"], $dbconf["name"]);
- // check connection
- if (mysqli_connect_errno())
- throw new \Exception('Connect failed: ' . mysqli_connect_error());
- if (!$this->link->set_charset("utf8"))
- throw new \Exception('Error loading character set utf8: ' . $this->link->error);
- }
- /**
- * Get Staff List
- * @param array $user Benutzer
- * @return array Benutzerdaten
- * @throws \Exception on error
- */
- public function getStaffList(array $user): array {
- try {
- if ($user['role'] === self::ADMIN) {
- $stmt = $this->link->prepare('SELECT * FROM staff');
- }
- else {
- $stmt = $this->link->prepare('SELECT * FROM staff WHERE surname=?');
- $stmt->bind_param('s', $user['name']);
- }
- $stmt->execute();
- $result = $stmt->get_result();
- if (!$result)
- throw new \Exception(mysqli_error($this->link));
- $staff = [];
- while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
- $this->fixBirthday($row['birthday']);
- $staff[] = $this->extractStaffFromRow($row);
- }
- return $staff;
- }
- catch(\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * Get Staff by ID
- * @param string $id_staff Staff ID
- * @return array Benutzerdaten
- * @throws \Exception on error
- */
- public function getStaff($id_staff) {
- try {
- $stmt = $this->link->prepare('SELECT * FROM staff WHERE id_staff=?');
- $stmt->bind_param('s', $id_staff);
- $stmt->execute();
- $result = $stmt->get_result();
- if (!$result)
- throw new \Exception(mysqli_error($this->link));
- $row = $result->fetch_array(MYSQLI_ASSOC);
- $this->fixBirthday($row['birthday']);
- return $this->extractStaffFromRow($row);
- }
- catch(\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement