(airhead): ive no idea no lie

Signed-off-by: boris <boris@borishub.co.uk>
This commit is contained in:
boris
2024-12-04 21:13:46 +00:00
parent 5b0d04b702
commit 8de2b7f29e
32 changed files with 233 additions and 1103 deletions

View File

@@ -23,8 +23,8 @@ class Database {
private function __construct() {
try {
$this->_dbHandle = new PDO("sqlite:Databases/ecobuddy.sqlite");
$this->_dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_dbHandle = new PDO("sqlite:Databases/ecobuddynew.sqlite");
$this->_dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$this->_dbHandle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (PDOException $e) {

View File

@@ -15,50 +15,96 @@ class FacilityDataSet
/**
* @param $data
* @return bool
* Broken last minute, dont have time to fix.
* add / update facility to database from array of columns
*/
public function addFacility($data): bool
{
$userQuery = "
SELECT ecoUser.id FROM ecoUser
WHERE ecoUser.username = :contributor;
";
$catQuery = "
SELECT ecoCategories.id FROM ecoCategories
WHERE ecoCategories.name = :category;
";
$sqlQuery = "
INSERT INTO ecoFacilities
(title,
category,
description,
houseNumber,
streetName,
county,
town,
postcode,
lng,
lat,
INSERT OR REPLACE INTO ecoFacilities
(id,
title,
category,
description,
houseNumber,
streetName,
county,
town,
postcode,
lng,
lat,
contributor)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, -1, -1, ?)
;";
// gets contributor name
$stmt = $this->_dbHandle->prepare($userQuery);
$stmt->bindParam(':contributor', $data->contributor, PDO::PARAM_STR);
$stmt = $this->_dbHandle->prepare($userQuery);
$stmt->execute();
$data['contributor'] = (int)$stmt->fetch(PDO::FETCH_ASSOC);
// gets category ID
$stmt = $this->_dbHandle->prepare($catQuery);
$stmt->bindParam(':category', $data->category, PDO::PARAM_STR);
$stmt = $this->_dbHandle->prepare($catQuery);
$stmt->execute();
$data['category'] = (int)$stmt->fetch(PDO::FETCH_ASSOC);
// run main query and bind updated parameters
$stmt = $this->_dbHandle->prepare($sqlQuery);
// Ensures only one value is returned per column name
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
// Initialize index for binding
$bindIndex = 1;
// Bind other filters
for ($i = 1; $i <= 8; $i++) { // Assuming 8 other filters
$value = !empty($data[$i]) ? "%" . $data[$i] . "%" : "%";
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
if (isset($data['id'])) {
$stmt->bindParam(1, $data['id']);
}
$stmt->bindParam(2, $data['title'], PDO::PARAM_STR);
$stmt->bindParam(3, $data['category'], PDO::PARAM_INT);
$stmt->bindParam(4, $data['description'], PDO::PARAM_STR);
$stmt->bindParam(5, $data['houseNumber'], PDO::PARAM_STR);
$stmt->bindParam(6, $data['streetName'], PDO::PARAM_STR);
$stmt->bindParam(7, $data['county'], PDO::PARAM_STR);
$stmt->bindParam(8, $data['town'], PDO::PARAM_STR);
$stmt->bindParam(9, $data['postcode'], PDO::PARAM_STR);
$stmt->bindParam(10, $data['contributor'], PDO::PARAM_INT);
$stmt->execute();
// var_dump($stmt);
// var_dump($this->_dbHandle->errorInfo());
return !($stmt->rowCount());
}
/**
* @param $id
* @return bool
* Deletes Facility Records being passed a facility id.
*/
public function deleteFacility($id): bool
{
$sqlQuery = "DELETE FROM ecoFacilities WHERE id = ?";
$sqlQuery = "DELETE FROM ecoFacilities WHERE ecoFacilities.id = :id;";
$stmt = $this->_dbHandle->prepare($sqlQuery);
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
$stmt->bindValue(1, $id, \PDO::PARAM_INT);
$stmt->bindValue(':id', (int)$id, \PDO::PARAM_INT);
$stmt->execute();
var_dump($stmt);
echo $stmt->rowCount();
return !($stmt->rowCount() == 0);
}
/**
* @param $filterArray
* @param $sortArray
* @return array
* Fetch all records depending on filters, and sort by defined column
*/
public function fetchAll($filterArray, $sortArray): array
{
// Define columns for filtering and sorting
@@ -127,12 +173,13 @@ class FacilityDataSet
ecoUser.username
ORDER BY {$selectedSortColumn} {$direction};
";
// Surround 'term' with % to allow usage with LIKE
$filterArray['term'] = '%' . $filterArray['term'] . '%' ?? '%';
var_dump($filterArray);
// Prepare and execute the count query
$countStmt = $this->_dbHandle->prepare($countQuery);
$countStmt->bindValue(':term', $filterArray['term'], PDO::PARAM_STR);
$countStmt->execute();
// Set total results to output of count statement
$totalResults = (int)$countStmt->fetchColumn();
// Prepare and execute the data query
@@ -151,65 +198,5 @@ class FacilityDataSet
'count' => $totalResults
];
}
/**
* @param $sqlQuery
* @param $filterArray
* @return false|PDOStatement
* Function for fetchAll() to de-dupe code. Performs binding on PDO statements to facilitate
* filtering of facilities. Returns a bound PDO statement.
*/
// private function populateFields($sqlQuery, $filterArray, $sortBy, $direction)
// {
// $stmt = $this->_dbHandle->prepare($sqlQuery);
// $stmt->setFetchMode(\PDO::FETCH_ASSOC);
//
// // Initialize index for binding
// $bindIndex = 1;
//
// // Bind statusComment filter, required due to comments not being so.
// $statusComment = !empty($filterArray[0]) ? "%" . $filterArray[0] . "%" : null;
// $stmt->bindValue($bindIndex++, $statusComment ?? "%", \PDO::PARAM_STR); // First ?
// $stmt->bindValue($bindIndex++, $statusComment, $statusComment === null ? \PDO::PARAM_NULL : \PDO::PARAM_STR); // Second ?
//
// // Bind other filters
// for ($i = 1; $i <= 8; $i++) { // Assuming 8 other filters
// $value = !empty($filterArray[$i]) ? "%" . $filterArray[$i] . "%" : "%";
// $stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
// }
// return $stmt;
// }
// So i worked on trying to get this to work for 30 minutes and it turns out you
// can never bind column name values to placeholders, and must use column orders
// as integers..... what
// if(isset($sortBy) && isset($direction)) {
// $stmt->bindValue(':sortBy', $sortBy, \PDO::PARAM_STR);
// $stmt->bindValue(':direction', $direction, \PDO::PARAM_STR);
// }
private function populateFields($sqlQuery, $filterArray)
{
$stmt = $this->_dbHandle->prepare($sqlQuery);
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
$bindIndex = 1;
// Bind statusComment (two placeholders required)
$statusComment = $filterArray[0] ?? '%';
$stmt->bindValue($bindIndex++, $statusComment, \PDO::PARAM_STR);
$stmt->bindValue($bindIndex++, $statusComment, \PDO::PARAM_STR);
// Bind other filters
for ($i = 1; $i < count($filterArray); $i++) {
$value = $filterArray[$i] ?? '%';
print_r($i . ":" . $value . "||\n");
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
}
// Debugging
//$stmt->debugDumpParams();
return $stmt;
}
}

View File

@@ -10,6 +10,10 @@ class User {
public function getUserId() {
return $this->_userId;
}
/**
* Open session, set field variables
*/
public function __construct() {
session_start();
@@ -17,7 +21,7 @@ class User {
$this->_loggedIn = false;
$this->_userId = "0";
$this->_accessLevel = null;
// if user logged in, set variables.
if(isset($_SESSION['login'])) {
$this->_username = $_SESSION['login'];
$this->_userId = $_SESSION['uid'];
@@ -26,17 +30,6 @@ class User {
}
}
public function init() {
$this->_username = "None";
$this->_userId = "0";
$this->_loggedIn = false;
if(isset($_SESSION['login'])) {
$this->_username = $_SESSION['login'];
$this->_userId = $_SESSION['uid'];
$this->_loggedIn = true;
}
}
private function setAccessLevel($level) {
$this->_accessLevel = $level;
$_SESSION['accessLevel'] = $level;
@@ -44,6 +37,13 @@ class User {
public function getAccessLevel() {
return $this->_accessLevel;
}
/**
* @param $username
* @param $password
* @return bool
* Using a username and password, authenticate a user and assign variables from query
*/
public function Authenticate($username, $password): bool
{
$users = new UserDataSet();
@@ -64,6 +64,10 @@ class User {
}
}
/**
* @return void
* Unset user variables from session, and set variables to default values - destroying session.
*/
public function logout() {
unset($_SESSION['login']);
unset($_SESSION['uid']);

View File

@@ -9,6 +9,12 @@ class UserDataSet {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getDbConnection();
}
/**
* @param $username
* @return mixed
* Query access level of a username, and return their usertype
*/
public function checkAccessLevel($username) {
$sqlQuery = "SELECT ecoUser.userType FROM ecoUser
LEFT JOIN ecoUsertypes ON ecoUser.userType = ecoUsertypes.userType
@@ -18,26 +24,12 @@ class UserDataSet {
$statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC)['userType'];
}
public function fetchAll(): array
{
$sqlQuery = 'SELECT * FROM ecoUser;';
$statement = $this->_dbHandle->prepare($sqlQuery); // prepare a PDO statement
$statement->execute(); // execute the PDO statement
$dataSet = [];
// loop through and read the results of the query and cast
// them into a matching object
while ($row = $statement->fetch()) {
$dataSet[] = new UserData($row);
}
return $dataSet;
}
/**
* @param $username
* @param $password
* @return array
* Authenticate user with query, and return their details
*/
public function checkUserCredentials($username, $password): array
{
@@ -52,16 +44,4 @@ class UserDataSet {
}
return $dataSet;
}
public function fetchUser($username): array
{
$sqlQuery = 'SELECT * FROM ecoUser WHERE username = ?';
$statement = $this->_dbHandle->prepare($sqlQuery);
$statement->execute([$username]);
$dataSet = [];
while ($row = $statement->fetch()) {
$dataSet[] = new UserData($row);
}
return $dataSet;
}
}