update,
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
class AddToCart extends Controller{
|
||||
public function __construct(){
|
||||
$this->processJSON();
|
||||
}
|
||||
|
||||
private function processJSON(){
|
||||
$json = file_get_contents('php://input');
|
||||
$obj = json_decode($json, true);
|
||||
if(isset($_SESSION['store']) && $_SESSION['store'] != $obj['storeId']){
|
||||
$returnJSON = new stdClass();
|
||||
$returnJSON->statue = false;
|
||||
$returnJSON->message = "Store Changed";
|
||||
echo json_encode($returnJSON);
|
||||
}else{
|
||||
$checkDup = false;
|
||||
if(isset($_SESSION['store']) && isset($_SESSION['products'])){
|
||||
foreach ($_SESSION['products'] as $key => $value) {
|
||||
if($key == $obj["productId"]){
|
||||
$_SESSION['products'][$key] += $obj["qty"];
|
||||
$checkDup = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!$checkDup){
|
||||
$_SESSION['store'] = $obj['storeId'];
|
||||
if(isset($_SESSION['products'])){
|
||||
$_SESSION['products'] += array($obj['productId']=> $obj['qty']);
|
||||
}else{
|
||||
$_SESSION['products'] = array($obj['productId']=> $obj['qty']);
|
||||
}
|
||||
}
|
||||
$returnJSON = new stdClass();
|
||||
$returnJSON ->statue = true;
|
||||
$returnJSON->itemCount = count($_SESSION['products']);
|
||||
$returnJSON->messsage = "Item saved";
|
||||
echo json_encode($returnJSON);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
class Cart extends Controller{
|
||||
|
||||
public function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
$storeCart = $this->getStoreInf($_SESSION["store"]);
|
||||
$cartItem = $this->getSessionItem($_SESSION["store"]);
|
||||
$storeInf = $this->getStoreName($_SESSION["store"]);
|
||||
$storeCart = new StoreCart($storeInf[0], $storeInf[1], $storeCart, $cartItem);
|
||||
parent::__construct("Cart", $storeCart);
|
||||
}
|
||||
|
||||
public function getStoreInf($storeId){
|
||||
$result = $this->database->query("SELECT consignmentstore.consignmentStoreID, shop.shopID, shop.address FROM consignmentstore_shop, consignmentstore, shop WHERE consignmentstore_shop.shopID = shop.shopID AND consignmentstore_shop.consignmentStoreID = consignmentstore.consignmentStoreID AND consignmentstore.consignmentStoreID = ?", "s" ,array($storeId));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getSessionItem($storeId){
|
||||
$itemList = array();
|
||||
foreach ($_SESSION["products"] as $key => $value) {
|
||||
$result = $this->database->query("SELECT * FROM Goods WHERE consignmentStoreID = ? AND goodsNumber = ?", "ss", array($storeId, $key));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
$item = new Product($row["goodsNumber"], $row["goodsName"], $row["status"], $row["stockPrice"], $row["remainingStock"], $value);
|
||||
array_push($itemList, $item);
|
||||
}
|
||||
|
||||
return $itemList;
|
||||
}
|
||||
|
||||
public function getStoreName($storeId){
|
||||
$result = $this->database->query("SELECT consignmentStoreID, ConsignmentStoreName FROM consignmentstore WHERE consignmentStoreID = ?", "i", array($storeId));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
|
||||
return array($row["consignmentStoreID"], $row["ConsignmentStoreName"]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
class Checking extends Controller{
|
||||
|
||||
public function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
$this->processJSON();
|
||||
}
|
||||
|
||||
private function processJSON(){
|
||||
$json = file_get_contents('php://input');
|
||||
$obj = json_decode($json, true);
|
||||
$returnJSON = new stdClass();
|
||||
if(isset($obj["password"])){
|
||||
$result = $this->database->query("SELECT COUNT(*) AS count FROM customer WHERE customerEmail = ? AND password = ?", "ss", array($_SESSION['customer']['customerEmail'], $obj["password"]));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
if($row["count"] > 0){
|
||||
$selectOrderResult = $this->database->query("SELECT orderID FROM orders WHERE customerEmail = ?", "s", array($_SESSION['customer']['customerEmail']));
|
||||
while($row = $selectOrderResult->fetch_array(MYSQLI_ASSOC)){
|
||||
$deleteOrderItem = $this->database->query("DELETE FROM orderitem WHERE orderID = ?", "i", array($row["orderID"]));
|
||||
}
|
||||
$deleteOrders = $this->database->query("DELETE FROM orders WHERE customerEmail = ? ", "s", array($_SESSION['customer']['customerEmail']));
|
||||
$deleteUser = $this->database->query("DELETE FROM customer WHERE customerEmail = ? ", "s", array($_SESSION['customer']['customerEmail']));
|
||||
|
||||
$returnJSON->statue = true;
|
||||
$returnJSON->message = "confirm";
|
||||
}else{
|
||||
$returnJSON->statue = false;
|
||||
$returnJSON->message = "password incorrect";
|
||||
}
|
||||
}else{
|
||||
$returnJSON->statue = false;
|
||||
$returnJSON->message = "Missing arguments";
|
||||
}
|
||||
echo json_encode($returnJSON);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
class Checkout extends Controller{
|
||||
|
||||
|
||||
function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
$this->checkout();
|
||||
}
|
||||
|
||||
public function checkout(){
|
||||
$storeId = $_POST["store"];
|
||||
$address = $_POST["address"];
|
||||
$totalPrice = 0.0;
|
||||
$itemList = array();
|
||||
|
||||
foreach ($_POST as $key => $value) {
|
||||
if($key != "address" && $key != "store" && $key > 0){
|
||||
$result = $this->database->query("SELECT stockPrice FROM goods WHERE consignmentStoreID = ? AND goodsNumber = ?", "ss", array($storeId, $key));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
$item = new OrderItem($key, $value, $row["stockPrice"]);
|
||||
$totalPrice += ($row["stockPrice"] * $value);
|
||||
array_push($itemList, $item);
|
||||
}
|
||||
}
|
||||
$result = $this->database->query("INSERT INTO orders(customerEmail, consignmentStoreID, shopID, orderDateTime, status, totalPrice) VALUES(?,?,?, NOW(),?,?)", "sssid", array($_SESSION['customer']['customerEmail'], $storeId, $address, 2, $totalPrice ));
|
||||
if($result > 0){
|
||||
print_r($itemList);
|
||||
$insert_id = $this->database->getInsertId();
|
||||
foreach ($itemList as $row) {
|
||||
echo "lopo";
|
||||
$insertItemResult = $this->database->query("INSERT INTO orderitem VALUES(?,?,?,?)", "iiid",array($insert_id, $row->getId(), $row->getQty(), $row->getPrice()));
|
||||
$updateItemStock = $this->database->query("UPDATE goods SET remainingStock = remainingStock - ? WHERE goodsNumber = ?", "ii", array($row->getQty(), $row->getId()));
|
||||
$checkItemStockResult = $this->database->qurey("SELECT remainingStock FROM goods WHERE goodsNumber = ?", "i", array($row->getId()));
|
||||
$result = $checkItemStockResult->fetch_array(MYSQLI_ASSOC);
|
||||
if($result["remainingStock"] < 1){
|
||||
$updateAvaliable = $this->database->query("UPDATE goods SET status = 2 WHERE goodsNumber = ?", "i", array($row->getId()));
|
||||
}
|
||||
}
|
||||
unset($_SESSION["products"]);
|
||||
unset($_SESSION["storeId"]);
|
||||
unset($_SESSION["store"]);
|
||||
header("location: orderhistory?checkout=". $insert_id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
class Controller {
|
||||
|
||||
private $viewName;
|
||||
private $data;
|
||||
|
||||
public function __construct($viewName, $data = null){
|
||||
$this->viewName = $viewName;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function setData($data){
|
||||
$this->data = $data;
|
||||
var_dump($this->data);
|
||||
}
|
||||
|
||||
public function render(){
|
||||
View::render($this->viewName, $this->data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
class Home extends Controller{
|
||||
|
||||
private $database;
|
||||
private $shopList;
|
||||
|
||||
function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
$this->shopList = $this->getShopList();
|
||||
parent::__construct("Home", $this->shopList);
|
||||
}
|
||||
|
||||
public function getShopList(){
|
||||
$result = $this->database->query("SELECT * FROM ConsignmentStore");
|
||||
//while($row = $result->fetch_array(MYSQLI_ASSOC)){
|
||||
// echo "$row[tenantID]";
|
||||
//}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
class Index extends Controller{
|
||||
|
||||
function __construct(){
|
||||
parent::__construct("Index");
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
class Login extends Controller {
|
||||
|
||||
private $database;
|
||||
|
||||
public function __construct(){
|
||||
$this->login();
|
||||
}
|
||||
|
||||
public function login(){
|
||||
$database = new DatabaseAccess();
|
||||
$username = $_POST['username'];
|
||||
$password = $_POST['password'];
|
||||
$role;
|
||||
|
||||
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
|
||||
// Login Customer
|
||||
$result = $database->query("SELECT * FROM Customer WHERE customerEmail = ? AND password = ?", "ss" ,array($username, $password));
|
||||
$role = 1;
|
||||
} else {
|
||||
// Login Tenant
|
||||
$result = $database->query("SELECT * FROM Tenant WHERE tenantID = ? AND password = ?", "ss" ,array($username, $password));
|
||||
$role = 0;
|
||||
}
|
||||
|
||||
if($result->num_rows == 1){
|
||||
session_start();
|
||||
$_SESSION['username'] = $username;
|
||||
$_SESSION['role'] = $role;
|
||||
header('Location: home');
|
||||
}else{
|
||||
header("Location: index?username=$username");
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
class Logout extends Controller{
|
||||
|
||||
function __construct(){
|
||||
session_destroy();
|
||||
header('Location: ../loginUI.php');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
class OrderHistory extends Controller{
|
||||
|
||||
private $database;
|
||||
private $orderHistory;
|
||||
|
||||
function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
if(isset($_GET['id'])){
|
||||
$orderDetials = $this->getOrderDetials($_GET['id']);
|
||||
parent::__construct("OrderHistoryDetail", $orderDetials);
|
||||
}else{
|
||||
$this->orderHistory = $this->getOrderHistory();
|
||||
parent::__construct("OrderHistory", $this->orderHistory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getOrderDetials($orderId){
|
||||
|
||||
$orderItemResult = $this->database->query("SELECT Goods.goodsNumber, Goods.goodsName, OrderItem.quantity, OrderItem.sellingPrice FROM Goods, OrderItem WHERE OrderItem.goodsNumber = Goods.goodsNumber AND OrderItem.orderID = ?", "s", array($orderId));
|
||||
$orderInfResult = $this->database->query("SELECT Orders.orderID, ConsignmentStore.consignmentStoreName, Shop.address, Orders.orderDateTime, Orders.status, Orders.totalPrice FROM Orders, ConsignmentStore, Shop WHERE Orders.consignmentStoreID =ConsignmentStore.consignmentStoreID
|
||||
AND Orders.shopID = Shop.ShopID AND Orders.orderID = ?", "s", array($orderId));
|
||||
|
||||
return new OrderHistoryModel($orderInfResult, $orderItemResult);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getOrderHistory(){
|
||||
$result = $this->database->query("SELECT Orders.orderID, ConsignmentStore.ConsignmentStoreName, Orders.orderDateTime, Orders.status, Orders.totalPrice FROM Orders, ConsignmentStore WHERE Orders.consignmentStoreID =ConsignmentStore.consignmentStoreID AND Orders.customerEmail = ? ORDER BY Orders.orderID DESC", "s" ,array($_SESSION['customer']['customerEmail']));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
class Profile extends Controller{
|
||||
|
||||
private $database;
|
||||
|
||||
public function __construct(){
|
||||
$this->database = new DatabaseAccess();
|
||||
if(isset($_POST["currentPassword"]) && isset($_POST["fname"])){
|
||||
$this->updateUserProfile($_SESSION['customer']['customerEmail']);
|
||||
}
|
||||
$userProfile = $this->getUserInformation($_SESSION['customer']['customerEmail']);
|
||||
parent::__construct("Profile", $userProfile);
|
||||
}
|
||||
|
||||
public function getUserInformation($userEmail){
|
||||
$result = $this->database->query("SELECT * FROM customer WHERE customerEmail = ?", "s", array($userEmail));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
return new UserProfile($row["customerEmail"], $row["firstName"], $row["lastName"], $row["phoneNumber"]);
|
||||
}
|
||||
|
||||
public function updateUserProfile($userEmail){
|
||||
$fname = $_POST["fname"];
|
||||
$lname = $_POST["lname"];
|
||||
$currentPwd = $_POST["currentPassword"];
|
||||
$newPwd = $_POST["password"];
|
||||
$tel = $_POST["tel"];
|
||||
|
||||
if($currentPwd == ""){
|
||||
$result = $this->database->query("UPDATE customer SET firstName = ?, lastName = ?, phoneNumber = ? WHERE customerEmail = ?", "ssss", array($fname, $lname, $tel, $userEmail));
|
||||
if($result > 0){
|
||||
header("location: profile?update=success");
|
||||
}else{
|
||||
header("location: profile?inf=invalid");
|
||||
}
|
||||
}else{
|
||||
$result = $this->database->query("SELECT COUNT(*) AS count FROM customer WHERE customerEmail = ? AND password = ?", "ss", array($userEmail, $currentPwd));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
if($row["count"] > 0){
|
||||
$result = $this->database->query("UPDATE customer SET firstName = ?, lastName = ?, phoneNumber = ?, password = ? WHERE customerEmail = ?", "sssss", array($fname, $lname, $tel, $newPwd, $userEmail));
|
||||
echo "test1";
|
||||
if($result > 0){
|
||||
header("location: profile?update=success");
|
||||
echo "test3";
|
||||
}else{
|
||||
header("location: profile?pwd=invalid");
|
||||
}
|
||||
}else{
|
||||
header("location: profile?pwd=invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
class RemoveInCart extends Controller{
|
||||
|
||||
public function __construct(){
|
||||
$this->processJSON();
|
||||
}
|
||||
|
||||
private function processJSON(){
|
||||
$json = file_get_contents('php://input');
|
||||
$obj = json_decode($json, true);
|
||||
$returnJSON = new stdClass();
|
||||
if(isset($obj["action"]) && $obj["action"] == "remove"){
|
||||
unset($_SESSION["products"]);
|
||||
unset($_SESSION["store"]);
|
||||
$returnJSON->statue = true;
|
||||
$returnJSON->messsage = "cart cleard";
|
||||
}elseif(isset($_SESSION["products"][$obj["productId"]])){
|
||||
unset($_SESSION["products"][$obj["productId"]]);
|
||||
if($_SESSION["products"] == null){
|
||||
unset($_SESSION["store"]);
|
||||
}
|
||||
$returnJSON->statue = true;
|
||||
$returnJSON->itemCount = count($_SESSION['products']);
|
||||
$returnJSON->messsage = "Item removed";
|
||||
}else{
|
||||
$returnJSON->statue = false;
|
||||
$returnJSON->messsage = "Error";
|
||||
}
|
||||
echo json_encode($returnJSON);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
class SignUp extends Controller {
|
||||
|
||||
private $database;
|
||||
|
||||
function __construct(){
|
||||
$this->signup();
|
||||
}
|
||||
|
||||
public function signup(){
|
||||
$database = new DatabaseAccess();
|
||||
$email = $_POST["email"];
|
||||
$fname = $_POST["fname"];
|
||||
$lname = $_POST["lname"];
|
||||
$tel = $_POST["tel"];
|
||||
$password = $_POST["password"];
|
||||
$result = $database->query("SELECT * FROM Customer WHERE customerEmail = ?", "s", array($email));
|
||||
|
||||
if($result->num_rows > 0){
|
||||
header("Location: index?fname=$fname&lname=$lname&tel=$tel");
|
||||
|
||||
}else{
|
||||
$result = $database->query("INSERT INTO Customer(customerEmail, firstName, lastName, password, phoneNumber) VALUES(?, ?, ?, ?, ?)", "sssss", array($email, $fname, $lname, $password, $tel));
|
||||
if($result == 1){
|
||||
header("Location: index?signup=true");
|
||||
}
|
||||
// echo $result + "<br>";
|
||||
// var_dump($result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
class Store extends Controller{
|
||||
|
||||
private $database;
|
||||
|
||||
function __construct(){
|
||||
if(isset($_GET['id'])){
|
||||
$this->database = new DatabaseAccess();
|
||||
$consignmentStoreID = $_GET['id'];
|
||||
$storeItemList = $this->getStoreItem($consignmentStoreID);
|
||||
$storeInf = $this->getStoreName($consignmentStoreID);
|
||||
$consignmentStore = new ConsignmentStore($storeInf[0], $storeInf[1], $storeItemList);
|
||||
parent::__construct("Store", $consignmentStore);
|
||||
}else{
|
||||
header('Location: home');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getStoreItem($consignmentStoreID){
|
||||
$result = $this->database->query("SELECT * FROM Goods WHERE consignmentStoreID = ?", "s", array($consignmentStoreID));
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getStoreName($consignmentStoreID){
|
||||
$result = $this->database->query("SELECT consignmentStoreID, ConsignmentStoreName FROM ConsignmentStore WHERE consignmentStoreID = ?", "s", array($consignmentStoreID));
|
||||
$row = $result->fetch_array(MYSQLI_ASSOC);
|
||||
return array($row["consignmentStoreID"], $row["ConsignmentStoreName"]);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
class ViewCart extends Controller{
|
||||
public function __construct(){
|
||||
$this->processJSON();
|
||||
}
|
||||
|
||||
private function processJSON(){
|
||||
$returnJSON = new stdClass();
|
||||
if(isset($_SESSION["products"]) && isset($_SESSION["store"])){
|
||||
$returnJSON->statue = true;
|
||||
$returnJSON->itemCount = count($_SESSION["products"]);
|
||||
$returnJSON->storeId = $_SESSION["store"];
|
||||
}else{
|
||||
$returnJSON->statue = true;
|
||||
$returnJSON->itemCount = 0;
|
||||
}
|
||||
echo json_encode($returnJSON);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user