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 $link = null;
- /**
- * Benutzerrollen
- */
- public const GAST = 0;
- public const USER = 1;
- public const MOD = 2;
- public const ADMIN = 3;
- /**
- * Verbinde zur Datenbank und speichere das Handle $link
- * @throws \Exception on error
- */
- private function connectDB(): void {
- require('conf.php'); // Datenbank Zugangsdaten
- $this->link = new \mysqli($conf["host"], $conf["user"], $conf["password"], $conf["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);
- }
- /**
- * Schliesst die Datenbank (Braucht man aber fast nie, da die Datenbank \
- * beim Beenden automatisch geschlossen wird.)
- */
- private function closeDB() {
- $this->link->close();
- }
- /**
- * Get Staff List
- * @param array $user Benutzer
- * @return array Benutzerdaten
- * @throws \Exception on error
- */
- public function getStaffList(array $user): array {
- try {
- $this->connectDB();
- 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)) {
- if ($row['birthday'] !== '0000-00-00')
- $row['birthday'] = date("d.m.Y", strtotime($row['birthday']));
- else
- $row['birthday'] = '';
- $staff[] = [
- 'birthday' => $row['birthday'],
- 'surname' => $row['surname'],
- ];
- }
- return $staff;
- }
- catch(\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement