Skip to content
Reece Edwards edited this page Mar 25, 2024 · 5 revisions

Controller.php Documentation

Overview

  • This document provides an overview of the file's structure and functionality.
  • Controller.php contains all the functions used by every other part of the website.

Table of Contents

Properties

  • Controller.PHP contains a few global properties

    • global $userInfo
    • global $pdo

Methods

Customer & Main page Functions

CheckExists()

Check if a variable is safe to evaluate

Parameters

  • $var : mixed

Return boolean

  • True if $var is safe and exists, otherwise false

escapeHTML()

Converts html chars to prevent html injection (supports up to 1d arrays)

Parameters

  • $params : any param
    • Any param to escape html injection

Return N/A

CreateSafeCustomer()

Iterates through details from db to ensure every key exists for the Customer object

Parameters

  • $details : array
    • Details array from the database

Return Customer|null

  • A customer object with all details, or null if any didn't exist

CreateSafeAdmin()

Iterates through details from db to ensure every key exists for the Admin object

Parameters

  • $details : array
    • Details array from the database

Return Admin|null

  • An admin object with all details, or null if any didn't exist

ReLogInUser()

Puts all relevent user info into the global userInfo array

Parameters N/A

Return N/A

CheckLoggedIn()

Checks if the user is logged in

Parameters N/A

Return boolean

  • True if logged in, false if not

AttemptLogin()

Attempts to log the user in using supplied credentials

Parameters

  • $user : string
    • Customer's username or email
  • $pass : string
    • Customer's password

Return boolean

  • True if login succeeded, otherwise false

RegisterUser()

Registers users to the database if supplied information passes all checks

Parameters

  • $details : array
    • Associative array with relevent info (most likely just $_POST)

Return string

  • Empty if succeeds (ie. evaluates to false), or a string to indicate where it failed

GetCustomerCount

Get count of all customers in the database

Parameters N/A

Return int|boolean

  • The count of customers if success, otherwise false

GetAllCustomers()

Get all customers in the database

Parameters N/A

Return array|boolean

  • Array of customers if success, otherwise false

UpdateCustomerDetail()

Updates a specified field in the database for a customer

Parameters

  • $details : array
    • Associative array containing field to change, new value and other relevant info

Return string

  • Empty if succeeded, or a string to indicate where it failed

DeleteCustomer()

Deletes a customer and their associated orders

Parameters

  • $customerID : int
    • The customer's ID

Return string

  • Empty if success, otherwise err message

GetCustomerByID()

Gets customer by their ID

Parameters

  • ``$customerID : int`
    • The ID of the customer

Return Customer|boolean

  • The customer if success, otherwise false

LogOut()

Unsets both global arrays and destroys the session

Parameters N/A

Return N/A

Product Functions

CreateSafeProduct()

Creates a product object with all details (minus cateogries and images) if exists

Parameters

  • $details : array
    • Details array from db

Return Product|null

  • Product with required details, or null

CreateSafeProductReview()

Creates a productReview object with all details if exists

Parameters

  • $details : array
    • Details array from db

Return ProductReview|null

  • Product with required details, or null

SortProductImages()

Sorts through the images of a product and finds the main one

Parameters

  • $images : array
    • The productImages as an array
  • $product : Product
    • The Product object to add to

Return N/A

AddProductImagesToProduct()

Adds the images to the product

Parameters

  • $product : Prodcut
    • The product

Return N/A

GetProductByID()

Gets product from the database, regardless of stock

Parameters

  • $productID : int
    • ID of the product

Return Product|boolean

  • Product if success, otherwise false

FilterStockedProducts()

--INTERNAL USE ONLY-- Filters array to only have stocked products

Parameters

  • $products : array
    • Array of products to filter (will overwrite)

Return string|boolean

  • True if success, otherwise a string for failu

GetAllProducts()

Gets every product in the database, regardless of stock

Parameters N/A

Return array|boolean

  • Array of products if succeeded, otherwise false

GetProductCount()

Get count of all products in the database

Parameters N/A

Return int|boolean

  • The count of products if success, otherwise false

GetAllStockedProducts()

Gets every product in the database where stock > 0

Parameter N/A

Return array|boolean

  • 2d array if succeeded, otherwise false

GetAllCategories()

Gets all categories from the database

Parameters N/A

Return array|boolean

  • Array of categories if succeeded, otherwise false

AddCategoryToProduct()

Adds a category to the prodcut via it's categoryID

Parameters

  • $product : Product
    • the product

Return string

  • Empty if success, otherwise indicates failure

AddCategoriesToProducts()

Adds categories to the products

Parameters

  • $products : array
    • The products array

return string

  • Empty if success, otherwise indicates failure

GetAllByCategory()

Gets all products by category, regardless of stock

Parameters

  • $category : string
    • Category of the product (component, accessory etc.)

Return array|string

  • Array of products if succeeded, otherwise a string where it failed

GetAllStockedByCategory()

Gets all products by category where stock > 0

Parameters

  • $category : string
    • Category of the product (component, accessory etc)

Return array|string

  • Array of products succeeded, otherwise string for failure

RemoveProductFromArrayByID()

--INTERNAL USE ONLY-- Removes the product from the array by PID

Parameters

  • $products : array
    • the products array
  • productID : int
    • the ID of the product to remove

Return string

  • Empty if success, otherwise indicates failure

GetRecommendedProducts()

Gets 3 random products, including from other categories if needed

Parameters

  • $productID : int
    • the ID of the product

Return array|string

  • Array with 3 products if success, otherwise indicates failure

CheckCanLeaveReview()

Checks if a customer has bought the product, and has not already left a review

Parameters

  • $customerID : int
    • the unique identifier of the customer
  • $productID : int
    • the unique identifier of the product

return boolean

  • True if they are allowed to, otherwise false

ReviewVarChecks()

--INTERNAL USE ONLY-- Checks all vars for leaving a review

Parameters

  • $productID : int
    • The product's ID
  • $customerID : int
    • The customer's ID
  • $rating : int
    • The rating value
  • $review : string
    • The review

Return string

  • Empty if ok, otherwise an error message

CreateReview()

Creates a rating for a product

Parameters

  • $productID : int
    • The product's ID
  • $customerID : int
    • The customer's ID
  • $rating : int
    • The rating value
  • $review : string
    • The review

Return string

  • Empty if success, otherwise an error message

UpdateReview()

Updates a rating for a product

Parameters

  • $productID : int
    • The product's ID
  • $customerID : int
    • The customer's ID
  • $rating : int
    • The rating value
  • $review : string
    • The review

Return string

  • Empty if success, otherwise an error message

DeleteReview()

Deletes a product review

Parameters

  • $productID : int
    • The product's ID
  • $customerID : int
    • The customer's ID

Return boolean

  • True if success, otherwise false

GetAllReviewsByCustomer()

Gets all productReviews by a customer

Parameters

  • $customerID : int
    • The customer's ID

Return array|boolean

  • Array of ProductReview objects, or false

GetAllReviewsByRating()

Gets all reviews on a product with a certain rating

Parameters

  • $productID : int
    • the product's ID
  • $rating : int
    • the rating value

return array|boolean

  • Array of ProductReview objects, or false

Order Functions

CreateSafeOrderLine()

Iterates through every detail from db to ensure every needed key exists

Parameters

  • $details : array
    • the array from the db query

Return OrderLine|null

  • OrderLine with all required info, or null if failed

CreateMultipleSafeOrderLines()

Iterates through array to make safe versions of each orderLine

Parameters

  • $details : array
    • 2d array of orderLines from db

return array|null

  • array of OrderLine objects, or null if no orderLines are "safe"

GetAllOrderStatuses()

Get all order statuses

Parameters N/A

Return array|boolean

  • Array of order statuses if success, otherwise false

CreateSafeOrder()

Iterates through every detail from db to ensure every needed key exists

Parameters

  • $details : array
    • the array from the DB query

Return Order|null

  • Order with all required info, or null if failed

CreateMultipleSafeOrders()

Iterates through array to make safe versions of each order

Parameters

  • $details : array
    • 2d array of orders from the db

return array|null

  • array of Order objects, or null if none can be made "safe"

ProductAndQuantityCheck()

--INTERNAL USE ONLY-- checks for product to make sure it's all legit

Parameters

  • $productID : int
    • the PID of product
  • $quantity : int
    • quantity of product

Return Product|boolean

  • Product if succeeded, otherwise false

AddProductToBasket()

Adds specified product to user's basket

Parameters

  • $productID : int
    • PID of the product
  • $Quantity : int
    • Quantity of the specified product to add to basket.

Return boolean

  • True if succeeded, otherwise false

ModifyProductQuantityInBasket()

Changes quantity of specified product in user's basket, a quantity of 0 will delete product from basket

Parameters

  • $productID : int
    • PID of prodcut
  • $quantity : int
    • new quantity

Return boolean

  • True if succeeded, otherwise false

CheckoutBasket()

Checks out the basket (if it exists), of the logged in customer

Parameters N/A

Return boolean

  • True if succeeded, otherwise false

AddOrderLinesToOrder

--INTERNAL USE ONLY-- Adds the orderLines to the Order

Parameters

  • $orderLines : array
    • array of OrderLines
  • $basket : Order
    • the order to attach the OrderLines to

Return boolean

  • True if succeeded, otherwise false

GetCustomerBasket()

Retrieves the customer's basket

Parameters N/A

Return Order|boolean

  • Order with every orderLine attached, or false if failed

GetPreviousOrders()

Retrieves all previous orders for a customer (not incl. basket)

Parameterrs N/A

Return array|boolean

  • array of Order objects if success, otherwise false

CancelOrReturnOrder()

Returns stock to product and sets order status to Cancelled or Returned

Parameters

  • $orderID : int
    • the unique ID of the order
  • $status : string
    • cancelled or returned

Return boolean

  • True if success, otherwise false

Admin Functions

VerfiyToken()

Check API Token validity & attempt to generate token if expired recently

Parametrs

  • $token : string
    • API token

Return string|boolean

  • Token if valid, otherwise false

GetAdminByToken()

Retrieves the Admin object related to a token

Parameters

  • $token : string
    • the token to check

Return Admin|boolean

  • Admin object if success, otherwise false

GenerateToken()

Create a token

Parameters

  • $adminID : int
    • The admin to associate to (defaults to $_SESSION["adminID"])
  • $expiry : DateTime
    • The expiry time for the token (defaults to now+20mins)
  • $name : string
    • The name for token access type

Return string

  • The token, or an empty string if failed

PruneTokens()

Checks all tokens, and deletes ones that have expired more than 5 mins ago

Parameters N/A

Return N/A

RevokeToken()

Deletes token from database

Parameters

  • $token : string
    • the token to remove

Return boolean

  • True if succeeded, otherwise false

AddAdmin()

Add an admin to the database

Parameters

  • $details : array
    • Associative array containing key as field to update and value as new value

Return string

  • Empty if success, otherwise a string to indicate where it failed

GetAdminByID()

Get Admin by their ID

Parameters

  • $adminID : int
    • ID of the admin

Return N/A

UpdateAdminByAdmin()

Update the details of an admin by an admin

Parameters

  • $details : array
    • Associative array containing key as field to update and value as new value

Return string

  • Empty if success, otherwise a string to indicate where it failed

GetAllAdmins()

Get all admins in the database

Parameters N/A

Return array|boolean

  • Array of admins if success, otherwise false

GetAllTokens()

Get all api tokens in the database

Parameters N/A

Return array|boolean

  • Array of tokens if success, otherwise false

UpdateCustomerByAdmin()

Update Customer details by admin

Parameters

  • $details : array
    • Associative array containing key as field to update and value as new value

Return string

  • Empty if success, otherwise a string to indicate where it failed

DeleteCustomerByAdmin()

Deletes a customer from the database by their ID if there are no orders associated with them

Parameters

  • $customerID : int
    • The ID of the customer

Return string

  • Empty if success, otherwise a string to indicate where it failed

DeleteAdminByAdmin()

Delete an admin from the database by their ID

Parameters

  • $adminID : int
    • The ID of the admin

Return string

  • Empty if success, otherwise a string to indicate where it failed

GetAllOrders()

Retrieves all orders, with orderLines attached

Parameters N/A

Return array|boolean

  • array of Order objects if success, otherwise false

GetOrderByID()

Retrieves an order by orderID

Parameters

  • $orderID : int
    • The ID of the order

Return Order|boolean

  • Order object if success, otherwise false

UpdateOrderStatus()

Updates the status of an order

Parameters

  • $orderID : int
    • The ID of the order
  • $newStatusID : int
    • The new status ID of the order

Return boolean

  • True if succeeded, otherwise false

AttemptAdminLogin()

Attempts to log in an admin with the supplied credentials, storing it in $_SESSION

Parameters

  • $user : string
    • Admin's username
  • $pass : string
    • Admin's password

Return string

  • Empty if success, otherwise false

CheckAdminLoggedIn()

Checks if an admin is logged in

Parameters N/A

Return boolean

  • True if logged in, otherwise false

UpdateProductDetail()

Updates the specified field of a product in db

Parameters

  • $productID : int
    • the ID of the product to update
  • $field : string
    • the field to update
  • $value : mixed
    • the value to change to

Return string

  • Empty if success, otherwise an err message

AddProduct()

Add a product to the database

Parameters

  • $details : array
    • array with all necessary info

Return string

  • empty if success, otherwise false

DeleteProduct()

Deletes the specified product from db only if no orders are associated with it

Parameters

  • $productID : int
    • the product to delete

Return string

  • empty if success, otherwise false

UpdateCustomerInfo()

Update the specified field in a customer's details

Parameter

  • $customerID : int
    • the ID of the customer
  • $field : string
    • the field to update
  • $value : mixed
    • the new value for the field

Return string

  • Empty if success, otherwise false

AddProductImage()

Add an image to the product

Parameters

  • $productID : int
    • The ID of the product
  • $fileName : string
    • The name of the file
  • $mainImage : boolean
    • Whether the image is the main image

Return string

  • Empty if success, otherwise false

UpdateProductImage()

Update Image for a product

Parameters

  • $productID : int
    • The ID of the product
  • $fileName : string
    • The name of the file
  • $mainImage : boolean
    • Whether the image is the main image

Return string

  • Empty if success, otherwise false

GetAllReviews()

Gets all productReviews

Parameters N/A

Return array|boolean

  • Array of ProductReview objects, or false

GetAllReviewsByProduct()

Gets all reviews on a product --API USE ONLY, PRODUCTS HAVE ALL REVIEWS ATTACHED--

Parameters

  • $productID : int
    • the ID of the product

Return array|boolean

  • Array of ProductReview objects, or false

DeleteReviewsByProduct()

Deletes all reviews associated with a product

Parameters

  • $productID : int
    • the products ID

Return boolean

  • True if success, otherwise false

DeleteReviewsByCustomer()

Deletes all reviews associated with a customer

Paramters

  • $customerID : int
    • The customer's ID

Return boolean

  • True if success, otherwise false

Clone this wiki locally