Skip to content

Commit

Permalink
rearranged controller classes and manage profile done.
Browse files Browse the repository at this point in the history
  • Loading branch information
BreadGuy007 committed Jan 29, 2022
1 parent 76d6e43 commit a21860b
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 93 deletions.
20 changes: 0 additions & 20 deletions classes/Order.class.php

This file was deleted.

20 changes: 18 additions & 2 deletions classes/OrderContr.class.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
<?php

class OrderContr extends Order {

class OrderContr extends Dbhandler {
private $orderID;
private $orderItems;

function __construct($orderID) {
$this->orderID = $orderID;
$this->updateOrderItems();
}

// update order items related to this order
protected function updateOrderItems() {
$sql = "SELECT OrderItemID FROM OrderItems WHERE ORDERID = '$this->orderID'";
$result = $this->conn()->query($sql) or die($this->conn()->error);

// create multiple OrderItem instances
$this->orderItems = array();
while ($row = $result->fetch_assoc())
array_push($this->orderItems, new OrderItem($row["OrderItemID"]));
}

public function getOrderID() { return $this->orderID; }
public function getOrderItems() { return $this->orderItems; }
}
34 changes: 0 additions & 34 deletions classes/OrderItem.class.php

This file was deleted.

32 changes: 31 additions & 1 deletion classes/OrderItemContr.class.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
<?php

class OrderItemContr extends OrderItem{
class OrderItemContr extends Dbhandler{
private $orderItemID;
private $itemID;
private $price;
private $quantity;
private $addedDateTime;

function __construct($orderItemID)
{
$this->orderItemID = $orderItemID;
$this->initData();
}

protected function initData() {
$sql = "SELECT * FROM OrderItems WHERE OrderItemID = $this->orderItemID";
$result = $this->conn()->query($sql) or die($this->conn()->error);
$row = $result->fetch_assoc();
$this->itemID = $row["ItemID"];
$this->price = $row["Price"];
$this->quantity = $row["Quantity"];
$this->addedDateTime = $row["AddedDatetime"];
}

protected function DeleteOrders() {
$sql = "DELETE * FROM OrderItems WHERE OrderItemID = ?";
$stmt = $this->conn()->prepare($sql);
$stmt->execute($this->orderItemID);

mysqli_stmt_close($stmt);
return $stmt;
}

public function GetOrderItemID() { return $this->orderItemID; }
public function GetItemID() { return $this->itemID; }
public function GetPrice() { return $this->price; }
public function GetQuantity() { return $this->quantity; }
public function GetAddedDateTime() { return $this->addedDateTime; }
}
59 changes: 59 additions & 0 deletions classes/ProfileContr.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

class ProfileContr extends CommonUtil{
private $username;
private $pwd;
private $repeatPwd;
private $email;
private $memberID;

public function __construct($username, $pwd, $repeatPwd, $email, $memberID)
{
$this->username = $username;
$this->pwd = $pwd;
$this->repeatPwd = $repeatPwd;
$this->email = $email;
$this->memberID = $memberID;
}

private function setUserAccount($username, $pwd, $email, $memberID) {
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "UPDATE Members SET Username = ?, Password=?, Email = ? where MemberID = ?;";
$stmt = $this->conn()->stmt_init();
if (!$stmt->prepare($sql)) {
header("location: ../manage_profile.php?error=Statementfailed");
exit();
}

$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

$stmt->bind_param("sssi", $username, $hashedPwd, $email, $memberID);
$stmt->execute();
$stmt->close();

session_start();
/** @var Member $member */
$member = $_SESSION["Member"];
$member->setUsername($username);
$member->setEmail($email);
$_SESSION["Member"] = $member;
}

public function updateUserAccount() {
if ($this->pwdNotMatch($this->pwd, $this->repeatPwd))
{
header("location: ../manage_profile.php?error=passwords_dont_match");
exit();
}
else if ($this->emptyInput($this->username, $this->pwd, $this->repeatPwd, $this->email))
{
header("location: ../manage_profile.php?error=empty_input");
exit();
}

$this->setUserAccount($this->username, $this->pwd, $this->email, $this->memberID);

header("location: ../manage_profile.php?error=none");
exit();
}
}
33 changes: 29 additions & 4 deletions classes/commonUtil.class.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php
/**
* @param mysqli $this->connect()
*/

class CommonUtil extends Dbhandler{

function uidExists($loginName) {
public function uidExists($loginName) {
$sql = "SELECT * FROM Members where Username = ? OR Email = ?;";
$stmt = $this->conn()->stmt_init();

Expand All @@ -25,4 +22,32 @@ function uidExists($loginName) {

$stmt->close();
}

public function setUser($username, $pwd, $email, $privilegeLevel=0) {
// create member
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "INSERT INTO Members(Username, Password, Email, PrivilegeLevel)
VALUES ('$username', '$hashedPwd', '$email', $privilegeLevel);";
$this->conn()->query($sql) or die("<p>*User creation error, please try again!</p>");

// get member id
$sql = "SELECT MemberID FROM Members where Username = '$username';";
$result = $this->conn()->query($sql) or die("<p>*MemberID error, please try again!</p>");

$row = $result->fetch_assoc();
$memberID = $row["MemberID"];

// create cart
$sql = "INSERT INTO Orders(MemberID) VALUES ($memberID);";
$result = $this->conn()->query($sql) or die("<p>*Cart creation error, please try again!</p>");
}

public function emptyInput($username, $pwd, $repeatPwd, $email)
{ return empty($username) || (empty($pwd)) || (empty($repeatPwd)) || (empty($email)); }

public function invalidUid($username)
{ return !preg_match("/^[a-zA-Z0-9]*$/", $username); }

public function pwdNotMatch($pwd, $repeatPwd)
{ return $pwd !== $repeatPwd; }
}
4 changes: 2 additions & 2 deletions classes/loginContr.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct($username, $pwd)
$this->pwd = $pwd;
}

private function emptyInput() {
private function checkEmptyInput() {
if (empty($this->username) || empty($this->pwd)) {
$result = false;
}
Expand All @@ -22,7 +22,7 @@ private function emptyInput() {
}

public function LoginUser() {
if($this->emptyInput($this->username, $this->pwd) == false) {
if($this->checkEmptyInput($this->username, $this->pwd) == false) {
header("location: ../login.php?error=emptyinput");
exit();
}
Expand Down
5 changes: 1 addition & 4 deletions footer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<footer class="page-footer grey darken-4" style="margin-top: 120px">
<footer class="page-footer black" style="margin-top: 120px; box-shadow: 0px 0px 2px white;">
<div class="row wide-container">
<div class="col s3">
<h4 class="white-text bold">OG Tech PC</h4>
Expand All @@ -25,9 +25,6 @@
<a class="waves-effect waves-light pink lighten-1 btn" style="margin: 2px;">
<i class="fa fa-instagram fa-fw"></i> Instagram
</a>
<a class="waves-effect waves-light indigo lighten-1 btn" style="margin: 2px;">
<i class="fa fa-linkedin fa-fw"></i> Linkedin
</a>
</div>
</div>
<div class="footer-copyright" style="padding-bottom: 20px;">
Expand Down
20 changes: 20 additions & 0 deletions includes/manage_profile.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

include_once "class_autoloader.php";

if (isset($_POST["update"]))
{
$username = $_POST["username"];
$pwd = $_POST["pwd"];
$repeatPwd = $_POST["repeat_pwd"];
$email = $_POST["email"];
$memberID = $_POST["id"];

$setAcc = new ProfileContr($username, $pwd, $repeatPwd, $email, $memberID);
$setAcc->updateUserAccount();
}
else
{
header("location: ../manage_profile.php");
exit();
}
Loading

0 comments on commit a21860b

Please sign in to comment.