From bf9ae0d0591557b9e9290a19fed9bb9b321e64e8 Mon Sep 17 00:00:00 2001
From: Mikhael Artur <mikhael.artur.d@gmail.com>
Date: Sat, 7 Oct 2017 18:29:02 +0700
Subject: [PATCH] Added edit pref loc actions, not yet implemented

---
 .../{edit-location.php => add-location.php}   |  0
 controller/edit-prefered-location.php         | 20 --------
 controller/editsave-location.php              |  0
 css/edit-prefered-location.css                | 21 +++++++-
 css/index.css                                 |  2 +-
 js/edit-prefered-location.js                  | 22 +++++++--
 model/user.php                                | 18 +++++++
 view/dashboard.php                            |  2 +-
 view/edit-prefered-locations.php              | 48 +++++++++++--------
 9 files changed, 85 insertions(+), 48 deletions(-)
 rename controller/{edit-location.php => add-location.php} (100%)
 delete mode 100644 controller/edit-prefered-location.php
 create mode 100644 controller/editsave-location.php

diff --git a/controller/edit-location.php b/controller/add-location.php
similarity index 100%
rename from controller/edit-location.php
rename to controller/add-location.php
diff --git a/controller/edit-prefered-location.php b/controller/edit-prefered-location.php
deleted file mode 100644
index b4ae86a6..00000000
--- a/controller/edit-prefered-location.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-/* Model */
-include "../model/user.php";
-
-//TODO: tambahin redirect klo get kosong
-
-$user = new User;
-$user_id = $_GET['id'];
-$result = $user->getUserById($user_id);
-header('Content-Type: text/xml');
-$xml = '<user>';
-while($row = mysqli_fetch_array($result)){
-    $xml = $xml . '<name>' . $row['name'] . '</name><username>' . $row['username'] .
-        '</username><email>' . $row['email'] . '</email><image>' . $row['image'] .
-        '</image><phone>' . $row['phone'] . '</phone><driver>' . $row['isDriver'] . '</driver>';
-}
-$xml = $xml . '</user>';
-
-print $xml;
\ No newline at end of file
diff --git a/controller/editsave-location.php b/controller/editsave-location.php
new file mode 100644
index 00000000..e69de29b
diff --git a/css/edit-prefered-location.css b/css/edit-prefered-location.css
index 7884ac90..3a65d2e9 100644
--- a/css/edit-prefered-location.css
+++ b/css/edit-prefered-location.css
@@ -81,10 +81,29 @@ button{
 
 .submit{
 	width: 100%;
+	height: 40px;
 	background-color: #82D800;
 	border-color: #000000;
 	border-radius: 5px;
-	font-size: 130%;
+	font: 130% Serif;
 	padding: 0 0 0 0;
 	color: #000000;
+	border-width: 1px;
+}
+
+.backbutton{
+	width: 15%;
+}
+
+.back{
+	float: left;
+	background-color: #d50027;
+	color: black;
+	border-color: black;
+	border-radius: 5px;
+	padding: 5px 12px;
+	text-decoration: none;
+	border-width: 1px;
+	border-style: outset;
+	font: 130% Serif;
 }
diff --git a/css/index.css b/css/index.css
index e4d51e10..d15b6cb1 100644
--- a/css/index.css
+++ b/css/index.css
@@ -85,6 +85,6 @@
 .link{
 	text-align: left;
 	font-family: "Arial";
-	width: 69%;
+	width: 68%;
 	text-align: left;
 }
\ No newline at end of file
diff --git a/js/edit-prefered-location.js b/js/edit-prefered-location.js
index 15481ca9..76f018a3 100644
--- a/js/edit-prefered-location.js
+++ b/js/edit-prefered-location.js
@@ -1,6 +1,5 @@
-function hideMe(i){
-	var textHidden = false;
-	textHidden = !textHidden;
+function editsave(i){
+	var submitForm = document.getElementById("locationform"+i);
 	var textBox = document.getElementById("locationtext"+i);
 	var inputBox = document.getElementById("locationinput"+i);
 	var image = document.getElementById("imageeditsave"+i);
@@ -11,11 +10,24 @@ function hideMe(i){
 		inputBox.setAttribute("style","display:block");
 		inputBox.value = textBox.innerHTML;
 		image.src = "../img/floppy.png";
-	}
-	else{
+	} else {
+		var form = document.forms["locationform"+i];
 		textBox.setAttribute("style","display:block");
+		inputBox.type = "hidden";
 		inputBox.setAttribute("style","display:none");
 		textBox.innerHTML = inputBox.value;
 		image.src = "../img/pencil.png";
+		form.action = "editsave-location.php";
+		form.submit();
+	}
+}
+
+function del(i){
+	var textBox = document.getElementById("locationtext"+i);
+	var form = document.forms["locationform"+i];
+	var check = confirm ("Delete "+textBox.innerHTML+" from preferred location?");
+	if (check === true){
+        form.action = "delete-location.php";
+        form.submit();
 	}
 }
\ No newline at end of file
diff --git a/model/user.php b/model/user.php
index 779228b4..accd1cdf 100644
--- a/model/user.php
+++ b/model/user.php
@@ -119,6 +119,24 @@ class User {
         mysqli_close($con);
         return $result;
     }
+
+    function addLocation($id, $location) {
+        $con = mysqli_connect('localhost','root','','projekers');
+        mysqli_select_db($con, 'projekers');
+        $image = "pikachu.jpg";
+        $sql = "INSERT INTO pref_loc (" . $id . ", '" . (string)$location . "')";
+        mysqli_query($con,$sql);
+        mysqli_close($con);
+    }
+
+    function updateLocation($id, $location) {
+        $con = mysqli_connect('localhost','root','','projekers');
+        mysqli_select_db($con, 'projekers');
+        $image = "pikachu.jpg";
+        $sql = "INSERT INTO pref_loc (" . $id . ", '" . (string)$location . "')";
+        mysqli_query($con,$sql);
+        mysqli_close($con);
+    }
     //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/dashboard.php b/view/dashboard.php
index b38a36db..973e32c9 100644
--- a/view/dashboard.php
+++ b/view/dashboard.php
@@ -65,7 +65,7 @@
             <div class="sub-header" id="preferred-location">
                 <div class="sub-header-heading"><h1>PREFERRED LOCATION</h1></div>
                 <div class="edit-icon">
-                    <a href="../view/edit-prefered-locations.php"><img src="../img/pencil.png" /></a>
+                    <a href="../view/edit-prefered-locations.php?id=<?php echo $_GET['id'];?>"><img src="../img/pencil.png" /></a>
                 </div>
             </div>
             <div class="user-location"><ul id="user-location"></ul></div>
diff --git a/view/edit-prefered-locations.php b/view/edit-prefered-locations.php
index 739d1e3f..a9bdc479 100644
--- a/view/edit-prefered-locations.php
+++ b/view/edit-prefered-locations.php
@@ -11,7 +11,7 @@
 			<?php
 				include "../model/user.php";
 				$user = new User;
-				$user_id = 1;
+				$user_id = $_GET['id'];
 				$result = $user->getPreferredLocation($user_id);
 				if ($result->num_rows > 0) {
 					echo '
@@ -26,25 +26,29 @@
 					while($row = $result->fetch_assoc()) {
 						echo '
 							<tr>
-								<td class="tabno">'.$i.'</td>
-								<td class="tabloc">
-									<div  id="locationtext'.$i.'">'.$row["location"].'</div>
-									<input class="locationinput" id="locationinput'.$i.'" type="hidden">
-								</td>
-								<td class="tabact">
-									<div class="addset" align="center">
-										<div class="editsavebox">
-											<button onClick=hideMe('.$i.') class="editsave">
-												<img src="../img/pencil.png" class="editsavebutton" id="imageeditsave'.$i.'">
-											</button>
+								<form name="locationform'.$i.'" method="post">
+									<td class="tabno">'.$i.'</td>
+									<td class="tabloc">
+										<input class="locationinput" id="id'.$i.'" type="hidden">
+										<div  id="locationtext'.$i.'">'.$row["location"].'</div>
+										<input class="locationinput" id="locationinput'.$i.'" type="hidden">
+										<input class="oldlocation" id="oldlocation'.$i.'" type="hidden" value="'.$row["location"].'">
+									</td>
+									<td class="tabact">
+										<div class="addset" align="center">
+											<div class="editsavebox">
+												<button type="button" onClick=editsave('.$i.') class="editsave">
+													<img src="../img/pencil.png" class="editsavebutton" id="imageeditsave'.$i.'">
+												</button>
+											</div>
+											<div>
+												<button onClick=del('.$i.') class="delete">
+													<img src="../img/delete.png" class="deletebutton" id="imagedelete'.$i.'">
+												</button>
+											</div>
 										</div>
-										<div>
-											<button onClick=f() class="delete">
-												<img src="../img/delete.png" class="deletebutton" id="imagedelete'.$i.'">
-											</button>
-										</div>
-									</div>
-								</td>
+									</td>
+								</form>
 							</tr>';
 						$i++;
 					}
@@ -64,8 +68,12 @@
 					<input type="Submit" value="ADD" class="submit">
 				</div>
 			</div>
+			<br>
+			<br>
 			<div>
-				
+				<div class="backbutton">
+					<a class="back" href="dashboard.php?id=<?php echo $_GET['id'];?>">BACK</a>
+				</div>
 			</div>
 		</div>
 	</body>
-- 
GitLab