From 911c42ef034a5628a07063dfc9cc179f4ec44744 Mon Sep 17 00:00:00 2001
From: aldrichvalentino <aldrich.vh97@gmail.com>
Date: Sat, 30 Sep 2017 21:52:46 +0700
Subject: [PATCH] transaction model and controller

---
 controller/transactionController.php | 21 +++++++++++
 js/history.js                        | 53 ++++++++++++++++++++++++++++
 model/order.php                      | 12 +++++++
 3 files changed, 86 insertions(+)
 create mode 100644 controller/transactionController.php
 create mode 100644 js/history.js
 create mode 100644 model/order.php

diff --git a/controller/transactionController.php b/controller/transactionController.php
new file mode 100644
index 00000000..b8508456
--- /dev/null
+++ b/controller/transactionController.php
@@ -0,0 +1,21 @@
+<?php
+
+/* Model */
+include "../model/order.php";
+
+//TODO: tambahin redirect klo get kosong
+
+$user = new Order();
+$result = $user->getAllTransaction();
+header('Content-Type: text/xml');
+$xml = '<transaction>';
+while($row = mysqli_fetch_array($result)){
+    $xml = $xml . '<transactionid>' . $row['transaction_id'] . '</transactionid><driverid>' . $row['driver_id']
+        . '</driverid><userid>' . $row['user_id']. '</userid><pickup>' . $row['pickup'] . '</pickup><destination>'.
+        $row['destination'] .'</destination><rating>' . $row['rating'] . '</rating><comment>'. $row['comment'].
+        '</comment><date>'. $row['date'] .'</date>';
+}
+$xml = $xml . '</transaction>';
+
+echo $xml;
+
diff --git a/js/history.js b/js/history.js
new file mode 100644
index 00000000..7d5dee1d
--- /dev/null
+++ b/js/history.js
@@ -0,0 +1,53 @@
+function getAllTransaction(){
+    //TODO: minta id dari session!!
+    var id = 2;
+    var xmlhttp = new XMLHttpRequest();
+    xmlhttp.onreadystatechange = function () {
+        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
+            var get = xmlhttp.responseXML;
+            var transactions = get.getElementsByTagName('transaction');
+            for(i = 0; i < transactions.length; i++){
+                if(transactions[i].getElementsByTagName('driverid')[0].childNodes[0].nodeValue == id){
+                    var dateElement = document.createElement('div');
+                    dateElement.classList.add('transaction-date');
+                    var txt = transactions[i].getElementsByTagName('date')[0].childNodes[0].nodeValue;
+                    var temp = txt.split("-");
+                    var date = new Date(temp[0], txt[1], txt[2]);
+                    dateElement.innerHTML = date.toDateString();
+
+                    var cityElement = document.createElement('div');
+                    cityElement.classList.add('transaction-city');
+                    txt = transactions[i].getElementsByTagName('pickup')[0].childNodes[0].nodeValue;
+                    txt = txt + '->';
+                    txt = txt + transactions[i].getElementsByTagName('destination')[0].childNodes[0].nodeValue;
+                    cityElement.innerHTML = txt;
+
+                    var ratingElement = document.createElement('div');
+                    ratingElement.classList.add('transaction-rating');
+                    txt = 'gave ';
+                    txt = txt + transactions[i].getElementsByTagName('rating')[0].childNodes[0].nodeValue;
+                    txt = txt + ' star(s) for this order';
+                    ratingElement.innerHTML = txt;
+
+                    var commentElement = document.createElement('div');
+                    commentElement.classList.add('transaction-comment');
+                    txt = 'and left a comment <br />';
+                    txt = txt + transactions[i].getElementsByTagName('comment')[0].childNodes[0].nodeValue;
+                    commentElement.innerHTML = txt;
+
+                    var singleTransaction = document.createElement('div');
+                    singleTransaction.classList.add('single-transaction');
+                    singleTransaction.appendChild(dateElement);
+                    singleTransaction.appendChild(cityElement);
+                    singleTransaction.appendChild(ratingElement);
+                    singleTransaction.appendChild(commentElement);
+                    singleTransaction.appendChild(document.createElement('button'));
+
+                    document.getElementById('driver-info').appendChild(singleTransaction);
+                }
+            }
+        }
+    };
+    xmlhttp.open('GET', '../controller/transactionController.php',true);
+    xmlhttp.send();
+}
diff --git a/model/order.php b/model/order.php
new file mode 100644
index 00000000..920e3c59
--- /dev/null
+++ b/model/order.php
@@ -0,0 +1,12 @@
+<?php
+
+class Order {
+    function getAllTransaction(){
+        $con = mysqli_connect('localhost','root', '', 'projekers');
+        mysqli_select_db($con, 'projekers');
+        $sql = "SELECT * FROM transaction";
+        $result = mysqli_query($con, $sql);
+        mysqli_close($con);
+        return $result;
+    }
+}
\ No newline at end of file
-- 
GitLab