From 08cd9e072357988f4dd457d89ce960ca4a85bbbf Mon Sep 17 00:00:00 2001
From: aldrichvalentino <aldrich.vh97@gmail.com>
Date: Fri, 6 Oct 2017 15:07:59 +0700
Subject: [PATCH] driver is limited to pickup and destination location in order

---
 controller/orderController.php | 12 +++++++++---
 js/order.js                    |  4 +++-
 model/user.php                 |  9 +++++++--
 view/order.php                 |  4 ++--
 4 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/controller/orderController.php b/controller/orderController.php
index c3bbcde8..77e0c4bf 100644
--- a/controller/orderController.php
+++ b/controller/orderController.php
@@ -3,11 +3,17 @@
 include "../model/user.php";
 include "../model/order.php";
 
-if(isset($_GET['drivername']) && isset($_GET['id'])){
+if(isset($_GET['drivername']) && isset($_GET['id']) && isset($_GET['pickup']) && isset($_GET['destination'])){
     $userId = $_GET['id'];
     $driverName = $_GET['drivername'];
+    $pickup = $_GET['pickup'];
+    $destination = $_GET['destination'];
+
+
     $user = new User();
-    $result = $user->getDriverNotId($userId);
+    $result = $user->getDriverNotId($userId, $pickup, $destination);
+
+
     header('Content-Type: text/html');
     $txt = '<div class="driver-box"><h1>PREFERRED DRIVERS</h1>';
     //Process to find the driver
@@ -31,7 +37,7 @@ if(isset($_GET['drivername']) && isset($_GET['id'])){
     $txt = $txt . '</div>';
     $txt = $txt . '<div class="driver-box"><h1>OTHER DRIVERS</h1>';
     //Process other drivers
-    $result = $user->getDriverNotId($userId);
+    $result = $user->getDriverNotId($userId, $pickup, $destination);
     while($row = mysqli_fetch_array($result)){
         if($row['name'] !== $driverName){
             //get drier rating
diff --git a/js/order.js b/js/order.js
index 15f67bd8..78b55563 100644
--- a/js/order.js
+++ b/js/order.js
@@ -23,12 +23,14 @@ function getDriver() {
     var xmlhttp = new XMLHttpRequest();
     //Check if user inserted preferred driver's name
     var drivername = (document.getElementById('preferred-driver').value) ? document.getElementById('preferred-driver').value : null;
+    var pickup = document.getElementById('pickup').value;
+    var destination = document.getElementById('destination').value;
     xmlhttp.onreadystatechange = function () {
         if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
             document.getElementsByClassName('second-page')[0].innerHTML = this.responseText;
         }
     };
-    xmlhttp.open('GET','../controller/orderController.php?drivername='+drivername+'&id='+sessionid,true);
+    xmlhttp.open('GET','../controller/orderController.php?drivername='+drivername+'&id='+sessionid+'&pickup='+pickup+'&destination='+destination,true);
     xmlhttp.send();
 }
 function chooseDriver(id){
diff --git a/model/user.php b/model/user.php
index fb5e18c4..346ee59d 100644
--- a/model/user.php
+++ b/model/user.php
@@ -75,10 +75,13 @@ class User {
         mysqli_close($con);
         return $result;
     }
-    function getDriverNotId($id){
+    function getDriverNotId($id, $pickup, $destination){
+        //Driver is limited to the pickup and destination
         $con = mysqli_connect('localhost','root','','projekers');
         mysqli_select_db($con,'projekers');
-        $query = "SELECT id,name,image FROM user WHERE isDriver=1 AND not id=" . $id;
+        //select * from user natural join pref_loc where (location like 'bec%' or location like '%kopo%') and isDriver=1 and not id=1
+        $query = "SELECT DISTINCT id,name,image FROM user NATURAL JOIN pref_loc WHERE (location LIKE '%".
+                $pickup."%' OR location LIKE '%".$destination."%') AND isDriver=1 AND not id=" . $id;
         $result = mysqli_query($con,$query);
         mysqli_close($con);
         return $result;
@@ -107,4 +110,6 @@ class User {
         mysqli_close($con);
         return $result;
     }
+    //function getD
+    //  select * from user natural join pref_loc where (location like 'bec%' or location like '%kopo%') and isDriver=1 and not id=1
 }
\ No newline at end of file
diff --git a/view/order.php b/view/order.php
index 6507b6d6..8d0a6cd2 100644
--- a/view/order.php
+++ b/view/order.php
@@ -10,10 +10,10 @@
     <form action="../controller/orderController.php" method="post">
         <div class="first-page">
             <div class="input-box">
-                <label>Picking Point</label><input type="text" name="pickup" placeholder="Pickup Point"/>
+                <label>Picking Point</label><input type="text" name="pickup" id="pickup" placeholder="Pickup Point"/>
             </div>
             <div class="input-box">
-                <label>Destination</label><input type="text" name="destination" placeholder="Destination Point"/>
+                <label>Destination</label><input type="text" name="destination" id="destination" placeholder="Destination Point"/>
             </div>
             <div class="input-box">
                 <label>Preferred Driver</label><input type="text" name="preferred-driver" id="preferred-driver" placeholder="(optional)" value=""/>
-- 
GitLab