From 530ced3642e275a5daee010ddd8f3857187bc6f8 Mon Sep 17 00:00:00 2001
From: Mikhael Artur <mikhael.artur.d@gmail.com>
Date: Fri, 6 Oct 2017 17:12:53 +0700
Subject: [PATCH] Login feature added

---
 controller/login.php             |  3 ---
 js/edit-prefered-location.js     |  3 ++-
 model/user.php                   |  9 ++++++++
 view/Login.html                  | 38 --------------------------------
 view/edit-prefered-locations.php |  2 +-
 view/{index.html => index.php}   | 24 +++++++++++++++++---
 view/register.php                |  2 +-
 7 files changed, 34 insertions(+), 47 deletions(-)
 delete mode 100644 controller/login.php
 delete mode 100644 view/Login.html
 rename view/{index.html => index.php} (53%)

diff --git a/controller/login.php b/controller/login.php
deleted file mode 100644
index 6d97b5e8..00000000
--- a/controller/login.php
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php 
-
-echo "login controller";
\ No newline at end of file
diff --git a/js/edit-prefered-location.js b/js/edit-prefered-location.js
index 5338cebd..15481ca9 100644
--- a/js/edit-prefered-location.js
+++ b/js/edit-prefered-location.js
@@ -5,8 +5,9 @@ function hideMe(i){
 	var inputBox = document.getElementById("locationinput"+i);
 	var image = document.getElementById("imageeditsave"+i);
 	
-	if(textHidden){
+	if(inputBox.type=="hidden"){
 		textBox.setAttribute("style","display:none");
+		inputBox.type = "text";
 		inputBox.setAttribute("style","display:block");
 		inputBox.value = textBox.innerHTML;
 		image.src = "../img/floppy.png";
diff --git a/model/user.php b/model/user.php
index fb5e18c4..415b9170 100644
--- a/model/user.php
+++ b/model/user.php
@@ -18,6 +18,15 @@ class User {
         mysqli_close($con);
         return $result;
     }
+	
+	function getUserByLogIn($username, $password){
+        $con = mysqli_connect('localhost','root','','projekers');
+        mysqli_select_db($con, 'projekers');
+        $sql = "SELECT * FROM user WHERE username='" . (string)$username . "' AND password='" . (string)$password . "'";
+        $result = mysqli_query($con, $sql);
+        mysqli_close($con);
+        return $result;
+    }
 
     function getUserById($id){
         $con = mysqli_connect('localhost','root','','projekers');
diff --git a/view/Login.html b/view/Login.html
deleted file mode 100644
index 01809295..00000000
--- a/view/Login.html
+++ /dev/null
@@ -1,38 +0,0 @@
-<html>
-	<head>
-		<title>Projekers - Login</title>
-		<link rel = "stylesheet" href = "../css/primary.css">
-		<link rel = "stylesheet" href = "../css/index.css">
-	</head>
-	<body>
-		<div class = "container">
-			<div>
-				<div class = "horizontal"></div>
-				<div class = "judul">LOGIN</div>
-				<div class = "horizontal"></div>
-			</div>
-			
-			<div class = "form_input">
-				<form>
-					<div class="set">
-						<div class="label">Username</div>
-						<div class="field"><input type = "text"></div>
-					</div>
-					
-					<div class="set">
-						<div class="label">Password</div>
-						<div class="field"><input type = "password"></div>
-					</div>
-					
-					<div class="linkset">
-						<div class="link"><a href="signup">Don't have an account?</a></div>
-						<div class="submitbutton">
-							<input type = "Submit" value = "GO!" class = "submit">
-						</div>
-					</div>
-				</form>
-			</div>
-		</div>
-	</body>
-</html>
-
diff --git a/view/edit-prefered-locations.php b/view/edit-prefered-locations.php
index 891b182b..9c45d20c 100644
--- a/view/edit-prefered-locations.php
+++ b/view/edit-prefered-locations.php
@@ -29,7 +29,7 @@
 								<td class="tabno">'.$i.'</td>
 								<td class="tabloc">
 									<div  id="locationtext'.$i.'">'.$row["location"].'</div>
-									<input class="locationinput" id="locationinput'.$i.'" type="text">
+									<input class="locationinput" id="locationinput'.$i.'" type="hidden">
 								</td>
 								<td class="tabact">
 									<div class="addset" align="center">
diff --git a/view/index.html b/view/index.php
similarity index 53%
rename from view/index.html
rename to view/index.php
index 8abba563..d849f9e6 100644
--- a/view/index.html
+++ b/view/index.php
@@ -1,3 +1,21 @@
+<?php
+	include "../model/user.php";
+	if($_SERVER["REQUEST_METHOD"] == "POST") {
+		$user = new User;
+		$result = $user->getUserByLogIn($_POST['username'], $_POST['password']);
+		$count = mysqli_num_rows($result);
+		$result = $result->fetch_assoc();
+		if($count == 1) {
+			if ($result['isDriver']) {
+				header("location:dashboard.php?id=" . $result["id"]);
+			} else {
+				header("location:dashboard.php?id=" . $result["id"]);
+			}
+		} else {
+			echo "<script>alert('Username dan/atau password yang anda masukkan salah.')</script>";
+		}
+	}
+?>
 <html>
 	<head>
 		<title>Projekers - Login</title>
@@ -13,15 +31,15 @@
 			</div>
 			
 			<div class = "form_input">
-				<form>
+				<form method="POST">
 					<div class="set">
 						<div class="label">Username</div>
-						<div class="field"><input type = "text"></div>
+						<div class="field"><input type="text" name="username"></div>
 					</div>
 					
 					<div class="set">
 						<div class="label">Password</div>
-						<div class="field"><input type = "password"></div>
+						<div class="field"><input type="password" name="password"></div>
 					</div>
 					
 					<div class="linkset">
diff --git a/view/register.php b/view/register.php
index 06fb4f6a..2c0a426a 100644
--- a/view/register.php
+++ b/view/register.php
@@ -59,7 +59,7 @@
                     <br>
                     <div class="bottom">
                         <div class="redirect-login">
-                            <a href="login.html">Already have an account?</a>
+                            <a href="index.php">Already have an account?</a>
                         </div>
                         <div class="register">
                             <input type="submit" value="REGISTER" name="register">
-- 
GitLab