diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 678a2c5..6ae98ec 100755 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -16,5 +16,12 @@ + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:Databases/ecobuddynew.sqlite + $ProjectFileDir$ + \ No newline at end of file diff --git a/Assessment Brief Form 2024-25.docx b/Assessment Brief Form 2024-25.docx deleted file mode 100755 index 999b5bf..0000000 Binary files a/Assessment Brief Form 2024-25.docx and /dev/null differ diff --git a/Databases/ecobuddy.sqlite b/Databases/ecobuddy.sqlite index f3dede0..ada988e 100755 Binary files a/Databases/ecobuddy.sqlite and b/Databases/ecobuddy.sqlite differ diff --git a/Databases/ecobuddynew.sqlite b/Databases/ecobuddynew.sqlite new file mode 100644 index 0000000..ff58f7d Binary files /dev/null and b/Databases/ecobuddynew.sqlite differ diff --git a/Models/Database.php b/Models/Database.php index 47b9365..793c9ca 100644 --- a/Models/Database.php +++ b/Models/Database.php @@ -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) { diff --git a/Models/FacilityDataSet.php b/Models/FacilityDataSet.php index cdbdb06..389a394 100644 --- a/Models/FacilityDataSet.php +++ b/Models/FacilityDataSet.php @@ -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; - } - } diff --git a/Models/User.php b/Models/User.php index 2b12d62..9c67259 100644 --- a/Models/User.php +++ b/Models/User.php @@ -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']); diff --git a/Models/UserDataSet.php b/Models/UserDataSet.php index f8cfa95..22715dc 100644 --- a/Models/UserDataSet.php +++ b/Models/UserDataSet.php @@ -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; - } - } \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100755 index e69de29..0000000 diff --git a/Views/index.phtml b/Views/index.phtml index 7a8f433..562160c 100644 --- a/Views/index.phtml +++ b/Views/index.phtml @@ -1,15 +1,15 @@
-
+

dbMessage; ?>

-
-

Current script

-
+
+ +
-
+
@@ -28,7 +28,7 @@ - pageData as $facilityData): ?> + pageData as $facilityData): ?> diff --git a/Views/template/createModal.phtml b/Views/template/createModal.phtml index ef8fef7..c99b758 100644 --- a/Views/template/createModal.phtml +++ b/Views/template/createModal.phtml @@ -14,14 +14,14 @@ - + - + diff --git a/Views/template/footer.phtml b/Views/template/footer.phtml index 82cd1b2..60d676d 100644 --- a/Views/template/footer.phtml +++ b/Views/template/footer.phtml @@ -1,5 +1,5 @@ -
+
getId() ?? 'N/A') ?> getTitle() ?? 'N/A') ?>