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

Binary file not shown.

0
.idea/.gitignore generated vendored Normal file → Executable file
View File

1
.idea/.name generated
View File

@@ -1 +0,0 @@
MVCtemplate

0
.idea/Ecobuddy.iml generated Normal file → Executable file
View File

9
.idea/MVCtemplate.iml generated
View File

@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jquery" level="application" />
</component>
</module>

12
.idea/dataSources.xml generated Normal file → Executable file
View File

@@ -1,12 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="ecobuddy.sqlite" uuid="8da0d10f-48a6-4aa3-9200-f806440fb4ed">
<data-source source="LOCAL" name="ecobuddy" uuid="b932ada6-ed77-47fa-96d8-d6dfa86a6ca2">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/ecobuddy.sqlite</jdbc-url>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/Databases/ecobuddy.sqlite</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar</url>
</library>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

0
.idea/encodings.xml generated Normal file → Executable file
View File

6
.idea/jsLibraryMappings.xml generated Executable file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{jquery}" />
</component>
</project>

0
.idea/misc.xml generated Normal file → Executable file
View File

5
.idea/modules.xml generated Normal file → Executable file
View File

@@ -2,8 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/MVCtemplate.iml" filepath="$PROJECT_DIR$/.idea/MVCtemplate.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/Ecobuddy.iml" filepath="$PROJECT_DIR$/.idea/Ecobuddy.iml" />
</modules>
</component>
</project>
</project>

8
.idea/php.xml generated Normal file → Executable file
View File

@@ -10,5 +10,11 @@
<option name="highlightLevel" value="WARNING" />
<option name="transferred" value="true" />
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="5.5.0" />
<component name="PhpProjectSharedConfiguration" php_language_level="7.1" />
<component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" />
</component>
<component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" />
</component>
</project>

0
.idea/scopes/scope_settings.xml generated Normal file → Executable file
View File

6
.idea/sqldialects.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="PROJECT" dialect="SQLite" />
</component>
</project>

0
.idea/vcs.xml generated Normal file → Executable file
View File

0
.~lock.Assessment Brief Form 2024-25.docx# Normal file → Executable file
View File

0
Assessment Brief Form 2024-25.docx Normal file → Executable file
View File

BIN
ecobuddy.sqlite → Databases/ecobuddy.sqlite Normal file → Executable file

Binary file not shown.

Binary file not shown.

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;
}
}

0
README.md Normal file → Executable file
View File

View File

@@ -1,11 +1,32 @@
<?php require('template/header.phtml') ?>
<h3>Welcome to the web-site </h3>
<div class="row">
<div class="col-5">
<p><?php echo $view->dbMessage; ?></p>
</div>
<div class="col-7">
<p>Current script <?php echo $_SERVER["PHP_SELF"]; ?></p>
</div>
</div>
<h3>A template for web-site development using the <i>Model-View-Controller</i> design pattern and <a href="http://www.getbootstrap.com/css"><i>Bootstrap</i></a>.</h3>
<p>The <i>Views/template</i> directory contains a <i>header.phtl</i> and a <i>footer.phtml</i> which should be included on every new page generated.
To add additional pages just edit the file <i>header.phtml</i> to add the extra link and then add a new <i>Controller (pageN.php)</i> and a new <i>View (pageN.phtml)</i>, for each page required.</p>
<p>The <i>Model</i> code files are placed in the <i>Models</i> directory.</p>
<p>Do not change any of the css files in the <i>css</> directory!<p>
<div class="row">
<table class="table table-bordered">
<thead> <tr> <th>Facility ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>Address</th> <th>Postcode</th> <th>Lat/Long</th> <th>Contributor</th> </tr>
</thead>
<tbody>
<?php foreach ($view->facilityDataSet as $facilityData) {
echo '<tr> <td>' . $facilityData->getId() .
'</td> <td>' . $facilityData->getTitle() .
'</td> <td>' . $facilityData->getCategory() .
'</td> <td>' . $facilityData->getDescription() .
'</td> <td>' . $facilityData->getHouseNumber() . " " . $facilityData->getStreetName() . " " . $facilityData->getCounty() . " " . $facilityData->getTown() .
'</td> <td>' . $facilityData->getPostcode() .
'</td> <td>' . $facilityData->getLatitude() . " " . $facilityData->getLongitude() .
'</td> <td>' . $facilityData->getContributor() .
'</td> </td> </tr>';
} ?>
</tbody>
</table>
<?php require('template/footer.phtml') ?>
<?php require('template/footer.phtml') ?>

View File

@@ -1,5 +0,0 @@
<?php require('template/header.phtml') ?>
<h3>Welcome to Page1</h3>
<?php require('template/footer.phtml') ?>

View File

@@ -1,5 +0,0 @@
<?php require('template/header.phtml') ?>
<h3>Welcome to Page2</h3>
<?php require('template/footer.phtml') ?>

View File

@@ -3,7 +3,8 @@
<div class="row">
<div id="footer" class="col-xs-12">
<p>Web-site development using the MVC design pattern and the Bootstrap CSS framework</p>
<p>George Wilkinson @2024</p>
<p>Powered by Bootstrap</p>
</div>
</div>
<!-- Bootstrap core JavaScript

View File

@@ -14,55 +14,54 @@
<link href="/css/bootstrap-theme.css" rel="stylesheet">
<link href="/css/my-style.css" rel="stylesheet">
<title>Server-Side Programming - <?php echo $view->pageTitle; ?></title>
<title>Ecobuddy - <?php echo $view->pageTitle; ?></title>
</head>
<body role="document">
<div class="container">
<div class="row">
<div id="title" class="col-xs-12">
<img src="/images/new_uos_logo.jpg" alt="Salford University" />
<div class="pull-right"> <h1><?php echo $view->pageTitle ?> </h1></div>
</div>
</div>
<div class="row">
<div id="menu" class="col-xs-6 col-sm-3 col-md-2">
<ul class="nav navbar_default nav-stacked">
<nav class="navbar navbar-expand-lg m-2 border rounded-2">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="/index.php">Ecobuddy <?php echo $view->pageTitle?></a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn bg-light btn-outline-light text-dark" type="submit">Search</button>
</form>
<?php
if (!isset($_SESSION["login"])) {
echo '
<form method="post" action="" class="form text-primary">
<label for="username">Username</label>
<input type="text" name="username">
<label for="password">Password</label>
<input type="text" name="password">
<input type="submit" name="loginbutton" value="Login">
</form>';
if ($view->loginError) {
require_once('Views/template/loginError.phtml');
}
else
{
echo '
<form method="post" action="" class="form text-primary">
<input type="submit" name="logoutbutton" value="Logout">
</form>';
if(!$user->isLoggedIn()) {
require_once('Views/template/loginModal.phtml');
}
if($user->isLoggedIn()) {
require_once('Views/template/logoutButton.phtml');
}
?>
</div>
</div>
</nav>
<body role="document">
<li><a href="/index.php">Home</a></li>
<li><a href="/page1.php">Page1</a></li>
<li><a href="/page2.php">Page2</a></li>
</ul>
</div>
<div class="container-lg">
<div id="content" class="col-xs-6 col-sm-9 col-md-10">
<div id="content" >

View File

View File

@@ -0,0 +1,29 @@
<button type="button" class="btn bg-primary btn-outline-primary text-light m-1 ms-2" data-bs-toggle="modal" data-bs-target="#loginModal">
Login
</button>
<div class="modal fade" id="loginModal" tabindex="-1" aria-labelledby="loginModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loginModalLabel">Login</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
</div>
<button type="submit" class="btn bg-primary btn-outline-primary text-light" name="loginButton">Login</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,4 @@
<?php echo "<p class='nav-link m-2' style='color: black;'>" . $user->getUsername() . " is logged in.</p>"?>
<form class="form-inline my-2 my-lg-0" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
<input class="btn bg-danger btn-outline-danger text-light" type="submit" name="logoutButton" value="Logout">
</form>

View File

0
css/bootstrap-theme.css vendored Normal file → Executable file
View File

0
css/bootstrap-theme.css.map Normal file → Executable file
View File

0
css/bootstrap-theme.min.css vendored Normal file → Executable file
View File

16828
css/bootstrap.css vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

13
css/bootstrap.min.css vendored

File diff suppressed because one or more lines are too long

21
css/my-style.css Normal file → Executable file
View File

@@ -1,3 +1,6 @@
nav {
background-color: #3cc471;
}
#title {
margin-top: 12px;
background-color: #fff;
@@ -12,13 +15,13 @@
}
#menu a {
background-color: #f00;
/*background-color: #f00;*/
color: #fff;
text-decoration: none;
display: block;
}
#menu a:hover {
background-color: #f00;
/*background-color: #f00;*/
color: #ddd;
text-decoration:underline;
display: block;
@@ -26,13 +29,21 @@
#content {
background-color: #fff;
border-top: solid 6px #f00;
/*border-top: solid 6px #f00;*/
}
#footer {
margin-top: 20px;
text-align: center;
background-color: #000;
color: #fff;
background-color: #bbb;
color: #111;
}
.modal {
z-index: 1055
}
.modal-backdrop {
z-index: 1040;
}

View File

@@ -1,27 +0,0 @@
services:
nginx-web:
container_name: nginx-web
image: nginx
volumes:
- ./nginx/configs/:/etc/nginx/
- ./nginx/logs/:/var/log/nginx
#- /home/boris/OneDrive/CSCS-Y2/Client Server Systems/Ecobuddy/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./:/var/www/html
#- /home/boris/OneDrive/CSCS-Y2/Client Server Systems/Ecobuddy/access.log:/var/log/nginx/access.log
#- /home/boris/OneDrive/CSCS-Y2/Client Server Systems/Ecobuddy/error.log:/var/log/nginx/error.log
ports:
- "8088:80"
environment:
- NGINX_HOST=localhost
- NGINX_PORT=8088
links:
- php-fpm
depends_on:
- php-fpm
php-fpm:
container_name: php-fpm
image: php:8-fpm
volumes:
- ./:/var/www/
- /home/boris/OneDrive/CSCS-Y2/Client Server Systems/Ecobuddy/nginx/php/config/www.conf:/usr/local/etc/php-fpm.d/www.conf
- /home/boris/OneDrive/CSCS-Y2/Client Server Systems/Ecobuddy/nginx/php/fpm-php.www.log:/var/log/fpm-php.www.log

0
eco database design v3.docx Normal file → Executable file
View File

0
fonts/glyphicons-halflings-regular.eot Normal file → Executable file
View File

0
fonts/glyphicons-halflings-regular.svg Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

0
fonts/glyphicons-halflings-regular.ttf Normal file → Executable file
View File

0
fonts/glyphicons-halflings-regular.woff Normal file → Executable file
View File

0
images/new_uos_logo.jpg Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@@ -1,10 +1,24 @@
<?php
$view = new stdClass();
$view->pageTitle = 'Homepage';
// load required classes
require_once('Models/UserDataSet.php');
require_once("logincontroller.php");
// make a view class
$view = new stdClass();
$view->pageTitle = 'Home';
// include the view
require_once('Views/index.phtml');
// create a new student dataset object that we can generate data from
$facilityDataSet = new FacilityDataSet();
$view->facilityDataSet = $facilityDataSet->fetchAll();
$view->user = new User();
// send a results count to the view to show how many results were retrieved
if (count($view->facilityDataSet) == 0)
{
$view->dbMessage = "No results";
}
else
{
$view->dbMessage = count($view->facilityDataSet) . " result(s)";
}

6211
js/bootstrap.js vendored

File diff suppressed because it is too large Load Diff

11
js/bootstrap.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,33 +0,0 @@
<?php
session_start();
var_dump($_SESSION);
if (isset($_POST["loginbutton"])) {
$username = $_POST["username"];
$password = $_POST["password"];
echo $username;
echo $password;
if ($username == "a" && $password == "b") {
// better would be to check these variables against your database
// SELECT * FROM usertable WHERE username=$username AND password=$password
// if number of rows returned > 0 then the username and password matched
echo "You are logged in";
$_SESSION["login"] = $username;
}
else
{
echo "Error in username and password";
}
}
if (isset($_POST["logoutbutton"]))
{
echo "logout user";
unset($_SESSION["login"]);
session_destroy();
}

View File

@@ -1,10 +0,0 @@
<?php
$view = new stdClass();
$view->pageTitle = 'Page1';
//var_dump($_POST);
require_once("logincontroller.php");
require_once('Views/page1.phtml');

View File

@@ -1,10 +0,0 @@
<?php
$view = new stdClass();
$view->pageTitle = 'Page2';
//var_dump($_POST);
require_once("logincontroller.php");
require_once('Views/page2.phtml');