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

Clone this wiki locally