add template files and compose file, fix syntax errors in template

This commit is contained in:
boris
2024-11-21 12:33:19 +00:00
parent f9d57038bf
commit b64c6d835e
55 changed files with 16384 additions and 7288 deletions

35
Models/Database.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
class Database {
/**
* @var Database
*/
protected static $_dbInstance = null;
/**
* @var PDO
*/
protected $_dbHandle;
public function getDbConnection(): PDO
{
return $this->_dbHandle;
}
public static function getInstance(): ?Database
{
if(self::$_dbInstance == null) {
self::$_dbInstance = new self();
}
return self::$_dbInstance;
}
private function __construct() {
try {
$this->_dbHandle = new PDO("sqlite:Databases/ecobuddy.sqlite");
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
public function __destruct() {
$this->_dbHandle = null; // destroys the PDO handle when no longer needed
}
}

52
Models/FacilityData.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
class FacilityData {
protected $_id, $_title, $_category, $_description, $_houseNumber, $_streetName, $_county, $_town, $_postcode, $_lng, $_lat;
public function __construct($dbRow) {
$this->_id = $dbRow['_id'];
$this->_title = $dbRow['title'];
$this->_category = $dbRow['category'];
$this->_description = $dbRow['description'];
$this->_houseNumber = $dbRow['houseNumber'];
$this->_streetName = $dbRow['streetName'];
$this->_county = $dbRow['county'];
$this->_town = $dbRow['town'];
$this->_postcode = $dbRow['postcode'];
$this->_lng = $dbRow['lng'];
$this->_lat = $dbRow['lat'];
}
public function getId() {
return $this->_id;
}
public function getTitle() {
return $this->_title;
}
public function getCategory() {
return $this->_category;
}
public function getDescription() {
return $this->_description;
}
public function getHouseNumber() {
return $this->_houseNumber;
}
public function getStreetName() {
return $this->_streetName;
}
public function getCounty() {
return $this->_county;
}
public function getTown() {
return $this->_town;
}
public function getPostcode() {
return $this->_postcode;
}
public function getLng() {
return $this->_lng;
}
public function getLat() {
return $this->_lat;
}
}

View File

@@ -0,0 +1,30 @@
<?php
require_once ('Database.php');
require_once ('FacilityData.php');
class FacilityDataSet {
protected $_dbHandle, $_dbInstance;
public function __construct() {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getDbConnection();
}
public function fetchAll(): array
{
$sqlQuery = 'SELECT * FROM ecoFacilities;';
$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 FacilityData($row);
}
return $dataSet;
}
}

1
Models/Paginator.php Normal file
View File

@@ -0,0 +1 @@
<?php

75
Models/User.php Normal file
View File

@@ -0,0 +1,75 @@
<?php
require_once('UserDataSet.php');
class User {
protected $_username, $_loggedIn, $_userId;
public function getUsername() {
return $this->_username;
}
public function getUserId() {
return $this->_userId;
}
public function __construct() {
session_start();
$this->_username = "None";
$this->_loggedIn = false;
$this->_userId = "0";
if(isset($_SESSION['login'])) {
$this->_username = $_SESSION['login'];
$this->_userId = $_SESSION['uid'];
$this->_loggedIn = true;
}
}
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;
}
}
public function Authenticate($username, $password): bool
{
$users = new UserDataSet();
$userDataSet = $users->checkUserCredentials($username, $password);
if(count($userDataSet) > 0) {
$_SESSION['login'] = $username;
$_SESSION['uid'] = $userDataSet[0]->getId();
$this->_loggedIn = true;
$this->_username = $username;
$this->_userId = $userDataSet[0]->getId();
return true;
}
else {
$this->_loggedIn = false;
return false;
}
}
public function logout() {
unset($_SESSION['login']);
unset($_SESSION['uid']);
$this->_loggedIn = false;
$this->_username = "None";
$this->_userId = "0";
session_destroy();
}
public function isLoggedIn(): bool
{
return $this->_loggedIn;
}
public function __destruct()
{
}
}

25
Models/UserData.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
class UserData {
protected $_id, $_username, $_name, $_password, $_usertype;
public function __construct($dbRow) {
$this->_id = $dbRow['_id'];
$this->_username = $dbRow['username'];
$this->_name = $dbRow['name'];
$this->_password = $dbRow['password'];
$this->_usertype = $dbRow['usertype'];
}
public function getId() {
return $this->_id;
}
public function getUsername() {
return $this->_username;
}
public function getName() {
return $this->_name;
}
}

59
Models/UserDataSet.php Normal file
View File

@@ -0,0 +1,59 @@
<?php
require_once ('Database.php');
require_once ('UserData.php');
class UserDataSet {
protected $_dbHandle, $_dbInstance;
public function __construct() {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getDbConnection();
}
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
*/
public function checkUserCredentials($username, $password): array
{
$sqlQuery = 'SELECT * FROM ecoUser WHERE username = ? AND password = ?;';
$statement = $this->_dbHandle->prepare($sqlQuery);
$statement->bindParam(1, $username);
$statement->bindParam(2, $password);
$statement->execute();
$dataSet = [];
while ($row = $statement->fetch()) {
$dataSet[] = new UserData($row);
}
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;
}
}