diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..485dee64bcfb48793379b200a1afd14e85a8aaf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/src/controller/ProfilController.php b/src/controller/ProfilController.php index e08bbecb47aa667a56112394fff826e43958b7b9..d5a5b9c9d8a24ee4bd3d48051eefe242118bfd1d 100644 --- a/src/controller/ProfilController.php +++ b/src/controller/ProfilController.php @@ -1,5 +1,8 @@ <?php +require_once __DIR__.'/../model/User.php'; +require_once __DIR__.'/../model/Driver.php'; + class ProfilController { public static function ProfilHandler() { @@ -12,35 +15,17 @@ class ProfilController { // Decrypt user id $uid = simpleCrypt($_GET['u'], 'd'); - - try { - - // Get connection to database - $dbconn = DB::getInstance(); - - // Prepare and execute sql query - $stmt = $dbconn->prepare("SELECT * FROM user WHERE id=$uid"); - $stmt->execute(); - $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); - $user = $stmt->fetchObject(); - if (!$user) { - echo "User not found!"; - return; - } - - $user->is_driver = 1; + // Getting driver profile + $dbconn = DB::getInstance(); + $user = Driver::Create($uid, $dbconn); - if ($user->is_driver) { - $driver_rating = 4.7; - $driver_order = 1728; - } - - require __DIR__.'/../view/profil.php'; - - } catch (PDOException $e) { - echo "Error: ".$e->getMessage(); + if (!$user) { + echo "User not found!"; + return; } + + require __DIR__.'/../view/profil.php'; } diff --git a/src/model/Driver.php b/src/model/Driver.php new file mode 100644 index 0000000000000000000000000000000000000000..af1cf7b4e507aa92e42562db2e1f5f1ef4842cca --- /dev/null +++ b/src/model/Driver.php @@ -0,0 +1,27 @@ +<?php + +class Driver extends User { + public $rating; + public $sumOrder; + + public static function Create($id, PDO $dbconn) { + try { + $stmt = $dbconn->prepare(" + SELECT id, name, username, email, phone, rating, is_driver AS isDriver, sum_order AS sumOrder + FROM user NATURAL JOIN driver + WHERE id =:id" + ); + $stmt->execute(array('id'=>$id)); + + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $result = $stmt->fetchObject('Driver'); + + return $result; + + } catch (PDOException $e) { + echo "Error : ".$e->getMessage(); + return false; + } + } + +} \ No newline at end of file diff --git a/src/model/User.php b/src/model/User.php new file mode 100644 index 0000000000000000000000000000000000000000..780a775e9669f9d0409807a19e80353c97516fbd --- /dev/null +++ b/src/model/User.php @@ -0,0 +1,30 @@ +<?php + +class User { + + public $id; + public $name; + public $username; + public $email; + public $phone; + public $isDriver; + + public static function Create($id, PDO $conn) { + try { + $stmt = $conn->prepare("SELECT * FROM user WHERE id=$id"); + $stmt->execute(); + + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $user = $stmt->fetchObject(); + $result = new User($user->id, $user->name, $user->username, $user->email, $user->phone); + $result->isDriver = $user->is_driver; + + return $result; + } catch (PDOException $e) { + echo "Error : ".$e->getMessage(); + return false; + } + } + + +} \ No newline at end of file diff --git a/src/view/profil.php b/src/view/profil.php index 8771902a4155fa4e84854a4158dcf3937fa9277c..fa2297d74c8c95b77399a8f65102e4242aab8b19 100644 --- a/src/view/profil.php +++ b/src/view/profil.php @@ -27,7 +27,7 @@ <img class="img-circle" src="<?=$user->photo?>"/><br> <h2>@<?=$user->username?></h2> <p><?=$user->name?></p> - <?php if ($user->is_driver) : ?> + <?php if ($user->isDriver) : ?> <p>Driver | <?=$driver_rating?> (<?=$driver_order?> vote<?=($driver_order>1)?'s':''?>)</p> <?php else : ?> <p>Non Driver</p>