-
Notifications
You must be signed in to change notification settings - Fork 8
Controller.php
- 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.
-
Controller.phpDocumentation- Overview
- Table of Contents
- Properties
-
Methods
- Customer & Main page Functions
-
Product Functions
CreateSafeProduct()CreateSafeProductReview()SortProductImages()AddProductImagesToProduct()GetProductByID()FilterStockedProducts()GetAllProducts()GetProductCount()GetAllStockedProducts()GetAllCategories()AddCategoryToProduct()AddCategoriesToProducts()GetAllByCategory()GetAllStockedByCategory()RemoveProductFromArrayByID()GetRecommendedProducts()CheckCanLeaveReview()ReviewVarChecks()CreateReview()UpdateReview()DeleteReview()GetAllReviewsByCustomer()GetAllReviewsByRating()
- Order Functions
-
Admin Functions
VerfiyToken()GetAdminByToken()GenerateToken()PruneTokens()RevokeToken()AddAdmin()GetAdminByID()UpdateAdminByAdmin()GetAllAdmins()GetAllTokens()UpdateCustomerByAdmin()DeleteCustomerByAdmin()DeleteAdminByAdmin()GetAllOrders()GetOrderByID()UpdateOrderStatus()AttemptAdminLogin()CheckAdminLoggedIn()UpdateProductDetail()AddProduct()DeleteProduct()UpdateCustomerInfo()AddProductImage()UpdateProductImage()GetAllReviews()GetAllReviewsByProduct()DeleteReviewsByProduct()DeleteReviewsByCustomer()
-
Controller.PHP contains a few global properties
global $userInfoglobal $pdo
Check if a variable is safe to evaluate
Parameters
$var : mixed
Return boolean
- True if $var is safe and exists, otherwise false
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
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
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
Puts all relevent user info into the global userInfo array
Parameters N/A
Return N/A
Checks if the user is logged in
Parameters N/A
Return boolean
- True if logged in, false if not
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
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
Get count of all customers in the database
Parameters N/A
Return int|boolean
- The count of customers if success, otherwise false
Get all customers in the database
Parameters N/A
Return array|boolean
- Array of customers if success, otherwise false
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
Deletes a customer and their associated orders
Parameters
-
$customerID : int- The customer's ID
Return string
- Empty if success, otherwise err message
Gets customer by their ID
Parameters
- ``$customerID : int`
- The ID of the customer
Return Customer|boolean
- The customer if success, otherwise false
Unsets both global arrays and destroys the session
Parameters N/A
Return N/A
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
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
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
Adds the images to the product
Parameters
-
$product : Prodcut- The product
Return N/A
Gets product from the database, regardless of stock
Parameters
-
$productID : int- ID of the product
Return Product|boolean
- Product if success, otherwise false
--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
Gets every product in the database, regardless of stock
Parameters N/A
Return array|boolean
- Array of products if succeeded, otherwise false
Get count of all products in the database
Parameters N/A
Return int|boolean
- The count of products if success, otherwise false
Gets every product in the database where stock > 0
Parameter N/A
Return array|boolean
- 2d array if succeeded, otherwise false
Gets all categories from the database
Parameters N/A
Return array|boolean
- Array of categories if succeeded, otherwise false
Adds a category to the prodcut via it's categoryID
Parameters
-
$product : Product- the product
Return string
- Empty if success, otherwise indicates failure
Adds categories to the products
Parameters
-
$products : array- The products array
return string
- Empty if success, otherwise indicates failure
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
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
--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
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
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
--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
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
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
Deletes a product review
Parameters
-
$productID : int- The product's ID
-
$customerID : int- The customer's ID
Return boolean
- True if success, otherwise false
Gets all productReviews by a customer
Parameters
-
$customerID : int- The customer's ID
Return array|boolean
- Array of ProductReview objects, or false
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
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
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"
Get all order statuses
Parameters N/A
Return array|boolean
- Array of order statuses if success, otherwise false
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
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"
--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
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
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
Checks out the basket (if it exists), of the logged in customer
Parameters N/A
Return boolean
- True if succeeded, otherwise false
--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
Retrieves the customer's basket
Parameters N/A
Return Order|boolean
- Order with every orderLine attached, or false if failed
Retrieves all previous orders for a customer (not incl. basket)
Parameterrs N/A
Return array|boolean
- array of Order objects if success, otherwise false
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
Check API Token validity & attempt to generate token if expired recently
Parametrs
-
$token : string- API token
Return string|boolean
- Token if valid, otherwise false
Retrieves the Admin object related to a token
Parameters
-
$token : string- the token to check
Return Admin|boolean
- Admin object if success, otherwise false
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
Checks all tokens, and deletes ones that have expired more than 5 mins ago
Parameters N/A
Return N/A
Deletes token from database
Parameters
-
$token : string- the token to remove
Return boolean
- True if succeeded, otherwise false
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
Get Admin by their ID
Parameters
-
$adminID : int- ID of the admin
Return N/A
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
Get all admins in the database
Parameters N/A
Return array|boolean
- Array of admins if success, otherwise false
Get all api tokens in the database
Parameters N/A
Return array|boolean
- Array of tokens if success, otherwise false
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
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
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
Retrieves all orders, with orderLines attached
Parameters N/A
Return array|boolean
- array of Order objects if success, otherwise false
Retrieves an order by orderID
Parameters
-
$orderID : int- The ID of the order
Return Order|boolean
- Order object if success, otherwise false
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
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
Checks if an admin is logged in
Parameters N/A
Return boolean
- True if logged in, otherwise false
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
Add a product to the database
Parameters
-
$details : array- array with all necessary info
Return string
- empty if success, otherwise false
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
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
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
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
Gets all productReviews
Parameters N/A
Return array|boolean
- Array of ProductReview objects, or false
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
Deletes all reviews associated with a product
Parameters
-
$productID : int- the products ID
Return boolean
- True if success, otherwise false
Deletes all reviews associated with a customer
Paramters
-
$customerID : int- The customer's ID
Return boolean
- True if success, otherwise false