Compare commits
6 Commits
paginator-
...
captcha
Author | SHA1 | Date | |
---|---|---|---|
|
5b0d04b702 | ||
|
cafe0b58d0 | ||
|
a1711afecc | ||
|
86f778b7ac | ||
|
574dcbb119 | ||
|
f9d625e905 |
@@ -2,10 +2,12 @@
|
||||
require_once ('Database.php');
|
||||
require_once ('FacilityData.php');
|
||||
|
||||
class FacilityDataSet {
|
||||
class FacilityDataSet
|
||||
{
|
||||
protected $_dbHandle, $_dbInstance;
|
||||
|
||||
public function __construct() {
|
||||
public function __construct()
|
||||
{
|
||||
$this->_dbInstance = Database::getInstance();
|
||||
$this->_dbHandle = $this->_dbInstance->getDbConnection();
|
||||
}
|
||||
@@ -14,7 +16,8 @@ class FacilityDataSet {
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
public function addFacility($data) : bool {
|
||||
public function addFacility($data): bool
|
||||
{
|
||||
$sqlQuery = "
|
||||
INSERT INTO ecoFacilities
|
||||
(title,
|
||||
@@ -45,7 +48,8 @@ class FacilityDataSet {
|
||||
return !($stmt->rowCount());
|
||||
}
|
||||
|
||||
public function deleteFacility($id) : bool {
|
||||
public function deleteFacility($id): bool
|
||||
{
|
||||
$sqlQuery = "DELETE FROM ecoFacilities WHERE id = ?";
|
||||
$stmt = $this->_dbHandle->prepare($sqlQuery);
|
||||
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
|
||||
@@ -53,92 +57,98 @@ class FacilityDataSet {
|
||||
$stmt->execute();
|
||||
return !($stmt->rowCount() == 0);
|
||||
}
|
||||
/**
|
||||
* @param $filterArray
|
||||
* @return array
|
||||
* Function to allow fetching of facility data. Data objects are created and held in an array
|
||||
* Count of rows for pagination returned alongside data objects.
|
||||
*/
|
||||
public function fetchAll($filterArray): array
|
||||
|
||||
|
||||
public function fetchAll($filterArray, $sortArray): array
|
||||
{
|
||||
/**
|
||||
* COUNT(DISTINCT ecoFacilities.id) required due to multiple status comments possible.
|
||||
*/
|
||||
$sqlCount = "SELECT COUNT(DISTINCT ecoFacilities.id) AS total FROM ecoFacilities";
|
||||
// Define columns for filtering and sorting
|
||||
$filterColumns = [
|
||||
0 => 'ecoFacilityStatus.statusComment',
|
||||
1 => 'ecoFacilities.title',
|
||||
2 => 'ecoCategories.name',
|
||||
3 => 'ecoFacilities.description',
|
||||
4 => 'ecoFacilities.streetName',
|
||||
5 => 'ecoFacilities.county',
|
||||
6 => 'ecoFacilities.town',
|
||||
7 => 'ecoFacilities.postcode',
|
||||
8 => 'ecoUser.username'
|
||||
];
|
||||
|
||||
/**
|
||||
* DISTINCT used again for prior reasoning, although data is handled properly regardless later.
|
||||
* GROUP_CONCAT is used to handle multiple status comments under one facility. Without this, DISTINCT
|
||||
* drops the additional comment.
|
||||
*/
|
||||
$sqlData = "SELECT DISTINCT ecoFacilities.id,
|
||||
title,
|
||||
GROUP_CONCAT(ecoFacilityStatus.statusComment, ', ') AS status,
|
||||
ecoCategories.name AS category,
|
||||
description,
|
||||
houseNumber,
|
||||
streetName,
|
||||
county,
|
||||
town,
|
||||
postcode,
|
||||
lng,
|
||||
lat,
|
||||
ecoUser.username AS contributor
|
||||
FROM ecoFacilities";
|
||||
$sortColumns = [
|
||||
0 => 'ecoFacilityStatus.statusComment',
|
||||
1 => 'ecoFacilities.title',
|
||||
2 => 'ecoCategories.name',
|
||||
3 => 'ecoFacilities.description',
|
||||
4 => 'ecoFacilities.streetName',
|
||||
5 => 'ecoFacilities.county',
|
||||
6 => 'ecoFacilities.town',
|
||||
7 => 'ecoFacilities.postcode',
|
||||
8 => 'ecoUser.username'
|
||||
];
|
||||
|
||||
/**
|
||||
* ? Parameters used here over named parameters so logic can be modular, more
|
||||
* columns can be added in the future
|
||||
*/
|
||||
$sqlWhere = "
|
||||
// Validate and select the filter column
|
||||
$selectedFilterColumn = $filterColumns[$filterArray['category']] ?? 'ecoFacilities.title';
|
||||
// Validate and select the sort column
|
||||
$selectedSortColumn = $sortColumns[$sortArray['sort']] ?? 'ecoFacilities.title';
|
||||
// Validate sort direction
|
||||
$direction = strtolower($sortArray['dir']) === 'desc' ? 'DESC' : 'ASC';
|
||||
// Base query for filtering and sorting
|
||||
$baseQuery = "
|
||||
FROM ecoFacilities
|
||||
LEFT JOIN ecoCategories ON ecoCategories.id = ecoFacilities.category
|
||||
LEFT JOIN ecoUser ON ecoUser.id = ecoFacilities.contributor
|
||||
LEFT JOIN ecoFacilityStatus ON ecoFacilityStatus.facilityid = ecoFacilities.id
|
||||
WHERE (ecoFacilityStatus.statusComment LIKE ? OR ? IS NULL)
|
||||
AND ecoFacilities.title LIKE ?
|
||||
AND ecoCategories.name LIKE ?
|
||||
AND ecoFacilities.description LIKE ?
|
||||
AND ecoFacilities.streetName LIKE ?
|
||||
AND ecoFacilities.county LIKE ?
|
||||
AND ecoFacilities.town LIKE ?
|
||||
AND ecoFacilities.postcode LIKE ?
|
||||
AND ecoUser.username LIKE ?
|
||||
WHERE {$selectedFilterColumn} LIKE :term
|
||||
";
|
||||
|
||||
/**
|
||||
* GROUP BY required to ensure status comments are displayed under the same ID
|
||||
* Named parameters used here for prior reasoning, columns can be added above without
|
||||
* effecting the bindIndex.
|
||||
*/
|
||||
$sqlLimits = "
|
||||
GROUP BY ecoFacilities.id
|
||||
LIMIT :limit OFFSET :offset;";
|
||||
$sqlLimits = "
|
||||
GROUP BY ecoFacilities.id
|
||||
;";
|
||||
|
||||
// Concatenate query snippets for data and row count
|
||||
$dataQuery = $sqlData . $sqlWhere . $sqlLimits;
|
||||
$countQuery = $sqlCount . $sqlWhere . ";";
|
||||
// Query to count total results
|
||||
$countQuery = "SELECT COUNT(DISTINCT ecoFacilities.id) AS total {$baseQuery}";
|
||||
|
||||
// Prepare, bind and execute data query
|
||||
$stmt = $this->populateFields($dataQuery, $filterArray);
|
||||
$stmt->execute();
|
||||
// Query to fetch filtered and sorted results
|
||||
$dataQuery = "
|
||||
SELECT DISTINCT ecoFacilities.id,
|
||||
ecoFacilities.title,
|
||||
GROUP_CONCAT(ecoFacilityStatus.statusComment, ', ') AS status,
|
||||
ecoCategories.name AS category,
|
||||
ecoFacilities.description,
|
||||
ecoFacilities.houseNumber,
|
||||
ecoFacilities.streetName,
|
||||
ecoFacilities.county,
|
||||
ecoFacilities.town,
|
||||
ecoFacilities.postcode,
|
||||
ecoFacilities.lng,
|
||||
ecoFacilities.lat,
|
||||
ecoUser.username AS contributor
|
||||
{$baseQuery}
|
||||
GROUP BY ecoFacilities.id, ecoFacilities.title, ecoCategories.name,
|
||||
ecoFacilities.description, ecoFacilities.streetName,
|
||||
ecoFacilities.county, ecoFacilities.town, ecoFacilities.postcode,
|
||||
ecoUser.username
|
||||
ORDER BY {$selectedSortColumn} {$direction};
|
||||
";
|
||||
$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();
|
||||
$totalResults = (int)$countStmt->fetchColumn();
|
||||
|
||||
// Create data objects
|
||||
// Prepare and execute the data query
|
||||
$dataStmt = $this->_dbHandle->prepare($dataQuery);
|
||||
$dataStmt->bindValue(':term', $filterArray['term'], PDO::PARAM_STR);
|
||||
$dataStmt->execute();
|
||||
|
||||
// Fetch results into FacilityData objects
|
||||
$dataSet = [];
|
||||
while ($row = $stmt->fetch()) {
|
||||
while ($row = $dataStmt->fetch()) {
|
||||
$dataSet[] = new FacilityData($row);
|
||||
}
|
||||
|
||||
// Prepare, bind then execute count query
|
||||
$stmt = $this->populateFields($countQuery, $filterArray);
|
||||
$stmt->execute();
|
||||
$totalCount = $stmt->fetch()['total'];
|
||||
|
||||
return [
|
||||
'dataset' => $dataSet,
|
||||
'count' => $totalCount
|
||||
'count' => $totalResults
|
||||
];
|
||||
}
|
||||
|
||||
@@ -149,60 +159,57 @@ class FacilityDataSet {
|
||||
* 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);
|
||||
// Ensures only one value is returned per column name
|
||||
$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 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 <= 8; $i++) { // Assuming 8 other filters
|
||||
$value = !empty($filterArray[$i]) ? "%" . $filterArray[$i] . "%" : "%";
|
||||
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;
|
||||
}
|
||||
|
||||
public function setFilterUri($term, $category)
|
||||
{
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
$uriComp = parse_url($uri);
|
||||
$params = [];
|
||||
|
||||
// Parse existing query parameters
|
||||
if (isset($uriComp['query'])) {
|
||||
parse_str($uriComp['query'], $params);
|
||||
} else {
|
||||
$params = array();
|
||||
}
|
||||
// Avoid unnecessary redirection if the filter is already correct
|
||||
if ((isset($params['category']) && $params['category'] === $category) && (isset($params['term']) && $params['term'] === $term)) {
|
||||
exit; // Do nothing if filter already applied
|
||||
}
|
||||
|
||||
// Update the 'page' parameter
|
||||
$params['category'] = $category;
|
||||
$params['term'] = $term;
|
||||
|
||||
// Rebuild the query string
|
||||
$newUri = http_build_query($params);
|
||||
var_dump($newUri);
|
||||
|
||||
// Redirect to the updated URI
|
||||
// Use the current path or root
|
||||
return
|
||||
[
|
||||
'newUri' => $newUri,
|
||||
'path' => $uriComp['path'] ?? '/'
|
||||
]; }
|
||||
|
||||
}
|
||||
|
||||
|
32
Views/template/createModal.phtml
Normal file
32
Views/template/createModal.phtml
Normal file
@@ -0,0 +1,32 @@
|
||||
<button type="button" class="col btn bg-primary btn-outline-primary text-light" data-bs-toggle="modal" data-bs-target="#createModal">
|
||||
<span class="bi bi-pen-fill"></span>
|
||||
</button>
|
||||
<div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="updateModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="updateModalLabel">Add Facility</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']); ?>">
|
||||
<form class="form-inline" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
|
||||
<input name="titlCreate" class="form-control rounded mb-2" placeholder="Title">
|
||||
<input name="cateCreate" class="form-control rounded mb-2" placeholder="Category">
|
||||
<input name="descCreate" class="form-control rounded mb-2" placeholder="Description">
|
||||
<input name="statCreate" class="form-control rounded mb-2" placeholder="Status">
|
||||
<input name="strtCreate" class="form-control rounded mb-2" placeholder="Street Name">
|
||||
<input name="cntyCreate" class="form-control rounded mb-2" placeholder="County">
|
||||
<input name="townCreate" class="form-control rounded mb-2" placeholder="Town">
|
||||
<input name="postCreate" class="form-control rounded mb-2" placeholder="Postcode">
|
||||
<input name="contCreate" class="form-control rounded mb-2" placeholder="Contributor">
|
||||
</form>
|
||||
<button type="submit" class="btn bg-primary btn-outline-primary text-light" name="updateButton">Add</button>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-warning" data-bs-dismiss="modal">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@@ -7,8 +7,6 @@
|
||||
<meta name="description" content="">
|
||||
<meta name="author" content="">
|
||||
<link rel="icon" type="image/x-icon" href="/images/ecoBuddy_x32.png"
|
||||
|
||||
|
||||
<!-- Bootstrap core CSS -->
|
||||
<link href="/css/bootstrap.css" rel="stylesheet">
|
||||
<!-- Bootstrap theme -->
|
||||
@@ -35,32 +33,54 @@
|
||||
<a class="nav-link" href="#">Link</a>
|
||||
</li>
|
||||
</ul>
|
||||
<form class="row m-0 me-2 align-content-center align-content-center align-items-center" role="search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
|
||||
<form class="row m-0 me-2 align-content-center align-content-center align-items-center" role="search" action="" method="POST">
|
||||
<div class="col">
|
||||
<div class="form-floating input-group">
|
||||
<select name="filter" class="form-select border-3 border-success-subtle" id="filter">
|
||||
<option selected></option>
|
||||
<option value="1">Title</option>
|
||||
<option value="2">Category</option>
|
||||
<option value="0">Status</option>
|
||||
<option value="3">Description</option>
|
||||
<option value="4">Street Name</option>
|
||||
<option value="5">County</option>
|
||||
<option value="6">Town</option>
|
||||
<option value="7">Postcode</option>
|
||||
<option value="8">Contributor</option>
|
||||
<select name="sort" class="form-select border-3 border-success-subtle" id="sort">
|
||||
<option value="1" <?php if(isset($_GET['sort']) && $_GET['sort'] == '1') echo 'selected'; ?>>Title</option>
|
||||
<option value="2" <?php if(isset($_GET['sort']) && $_GET['sort'] == '2') echo 'selected'; ?>>Category</option>
|
||||
<option value="0" <?php if(isset($_GET['sort']) && $_GET['sort'] == '0') echo 'selected'; ?>>Status</option>
|
||||
<option value="3" <?php if(isset($_GET['sort']) && $_GET['sort'] == '3') echo 'selected'; ?>>Description</option>
|
||||
<option value="4" <?php if(isset($_GET['sort']) && $_GET['sort'] == '4') echo 'selected'; ?>>Street Name</option>
|
||||
<option value="5" <?php if(isset($_GET['sort']) && $_GET['sort'] == '5') echo 'selected'; ?>>County</option>
|
||||
<option value="6" <?php if(isset($_GET['sort']) && $_GET['sort'] == '6') echo 'selected'; ?>>Town</option>
|
||||
<option value="7" <?php if(isset($_GET['sort']) && $_GET['sort'] == '7') echo 'selected'; ?>>Postcode</option>
|
||||
<option value="8" <?php if(isset($_GET['sort']) && $_GET['sort'] == '8') echo 'selected'; ?>>Contributor</option>
|
||||
</select>
|
||||
<span class="input-group-text bi bi-filter-circle bg-success-subtle border-0 rounded-end" id="filter"></span>
|
||||
<label for="filter">Column Filter</label>
|
||||
<span class="form-floating input-group">
|
||||
<select class="form-select border-3 border-start-0 rounded-end border-success-subtle" name="dir" id="dir">
|
||||
<option value="asc" <?php if($_GET['dir'] == 'asc') echo 'selected'; ?>>Asc</option>
|
||||
<option value="desc" <?php if($_GET['dir'] == 'desc') echo 'selected'; ?>>Desc</option>
|
||||
</select>
|
||||
<label for="dir">Order</label>
|
||||
</span>
|
||||
<label for="sort">Sort By</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-floating input-group">
|
||||
<input class="form-control border-3 border-success-subtle" id="search" type="search" name="applyFilters" aria-label="Search">
|
||||
<select name="filterCat" class="form-select border-3 border-success-subtle" id="filterCat">
|
||||
<option value="1" <?php if(isset($_GET['category']) && $_GET['category'] == '1') echo 'selected'; ?>>Title</option>
|
||||
<option value="2" <?php if(isset($_GET['category']) && $_GET['category'] == '2') echo 'selected'; ?>>Category</option>
|
||||
<option value="0" <?php if(isset($_GET['category']) && $_GET['category'] == '0') echo 'selected'; ?>>Status</option>
|
||||
<option value="3" <?php if(isset($_GET['category']) && $_GET['category'] == '3') echo 'selected'; ?>>Description</option>
|
||||
<option value="4" <?php if(isset($_GET['category']) && $_GET['category'] == '4') echo 'selected'; ?>>Street Name</option>
|
||||
<option value="5" <?php if(isset($_GET['category']) && $_GET['category'] == '5') echo 'selected'; ?>>County</option>
|
||||
<option value="6" <?php if(isset($_GET['category']) && $_GET['category'] == '6') echo 'selected'; ?>>Town</option>
|
||||
<option value="7" <?php if(isset($_GET['category']) && $_GET['category'] == '7') echo 'selected'; ?>>Postcode</option>
|
||||
<option value="8" <?php if(isset($_GET['category']) && $_GET['category'] == '8') echo 'selected'; ?>>Contributor</option>
|
||||
</select>
|
||||
<span class="input-group-text bi bi-filter-circle bg-success-subtle border-0 rounded-end" id="filterCat"></span>
|
||||
<label for="filterCat">Column Filter</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-floating input-group">
|
||||
<label for="search"></label>
|
||||
<input placeholder="<?php if(isset($_GET['filter'])) echo $_GET['filter']; ?>" class="form-control border-3 border-success-subtle" id="search" type="search" name="filter" aria-label="Search">
|
||||
<span class="input-group-text bg-success-subtle border-0 rounded-end" id="search">
|
||||
<button class="btn bg-light bg-success-subtle" type="submit"><span class="bi bi-search"></span></button>
|
||||
</span>
|
||||
<label for="search">Search</label>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@@ -68,9 +88,6 @@
|
||||
<div class="col-sm" id="loginStatus">
|
||||
<?php
|
||||
|
||||
if ($view->loginError) {
|
||||
require_once('Views/template/loginError.phtml');
|
||||
}
|
||||
if(!$view->user->isLoggedIn()) {
|
||||
require_once('Views/template/loginModal.phtml');
|
||||
}
|
||||
|
@@ -0,0 +1,18 @@
|
||||
<span class="ms-5 me-5 row alert alert-danger" role="alert"><?= $view->loginError ?></span>
|
||||
<div class="row captcha-container">
|
||||
<!-- CAPTCHA Display -->
|
||||
<div class="form-floating mb-3 col">
|
||||
<input type="text" class="form-control" id="captchaCode" value="<?php
|
||||
// Generate a simple 5-character CAPTCHA
|
||||
$captcha = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0, 5);
|
||||
echo $captcha;
|
||||
?>" readonly>
|
||||
<label for="captchaCode">CAPTCHA Code</label>
|
||||
</div>
|
||||
|
||||
<!-- CAPTCHA Input -->
|
||||
<div class="form-floating mb-3 col">
|
||||
<input type="text" class="form-control" id="captchaInput" name="captchaInput" placeholder="Enter CAPTCHA" required>
|
||||
<label for="captchaInput">Enter CAPTCHA</label>
|
||||
</div>
|
||||
</div>
|
@@ -1,7 +1,11 @@
|
||||
<button type="button" class="btn bg-primary btn-outline-primary text-light m-auto" data-bs-toggle="modal" data-bs-target="#loginModal">
|
||||
<button type="button" class="btn bg-primary btn-outline-primary text-light m-auto" data-bs-toggle="modal"
|
||||
data-bs-target="#loginModal">
|
||||
Login
|
||||
</button>
|
||||
<div class="modal fade" id="loginModal" tabindex="-1" aria-labelledby="loginModalLabel" aria-hidden="true">
|
||||
<?= isset($view->loginError) ? '<div class="modal-backdrop fade show"></div>' : '' ?>
|
||||
<div class="modal fade <?= isset($view->loginError) ? 'show' : '' ?>" id="loginModal" tabindex="-1"
|
||||
aria-labelledby="loginModalLabel" aria-hidden="<?= isset($view->loginError) ? 'false' : 'true' ?>"
|
||||
style="<?= isset($view->loginError) ? 'display: block;' : '' ?>">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
@@ -12,17 +16,23 @@
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
<?php if (isset($view->loginError)) { include('Views/template/loginError.phtml');} ?>
|
||||
<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 btn-outline-warning text-light" data-bs-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-warning btn-outline-warning text-light" data-bs-dismiss="modal">
|
||||
Close
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@@ -2,17 +2,26 @@
|
||||
<div class="row mb-2">
|
||||
<!-- Form for Pagination -->
|
||||
<div id="paginationButtons" class="col-auto m-auto btn-group">
|
||||
|
||||
<?php
|
||||
$params = $_GET;
|
||||
unset($params['page']); // Remove the page parameter to avoid duping
|
||||
function buildUrl($page, $params): string
|
||||
{
|
||||
$params['page'] = $page;
|
||||
return '?' . http_build_query($params);
|
||||
}
|
||||
?>
|
||||
<!-- Start Button -->
|
||||
<a class="btn btn-outline-primary" href="?page=0" <?= $view->pageNumber <= 0 ? 'disabled' : '' ?>><i class="bi bi-chevron-double-left"></i> Start</a>
|
||||
<a class="btn btn-outline-primary" href="<?= buildUrl(0, $params) ?>0" <?= $view->pageNumber <= 0 ? 'disabled' : '' ?>><i class="bi bi-chevron-double-left"></i> Start</a>
|
||||
<!-- Back Button -->
|
||||
<a class="btn btn-outline-primary" href="?page=<?=max($view->pageNumber - 1, 0)?> " <?= $view->pageNumber <= 0 ? 'disabled' : '' ?>><i class="bi bi-chevron-left"></i> Back</a>
|
||||
<a class="btn btn-outline-primary" href="<?= buildUrl(max($view->pageNumber - 1, 0), $params)?> " <?= $view->pageNumber <= 0 ? 'disabled' : '' ?>><i class="bi bi-chevron-left"></i> Back</a>
|
||||
<!-- Dynamic Page Buttons -->
|
||||
<?php
|
||||
$totalPages = $view->paginator->getTotalPages();
|
||||
var_dump($totalPages);
|
||||
for ($i = $view->pageNumber - 2; $i <= $view->pageNumber + 2; $i++) {
|
||||
if ($i >= 0 && $i < $totalPages): ?>
|
||||
<a href="?page=<?= $i ?>"
|
||||
<a href="<?= buildUrl($i, $params) ?>"
|
||||
class="btn <?= $i === $view->pageNumber ? 'btn-dark' : 'btn-outline-primary' ?>"
|
||||
<?= $i === $view->pageNumber ? 'disabled' : '' ?>>
|
||||
<?= $i + 1 ?>
|
||||
@@ -20,9 +29,9 @@
|
||||
<?php endif;
|
||||
} ?>
|
||||
<!-- Forward Button -->
|
||||
<a class="btn btn-outline-primary" href="?page=<?=min($view->pageNumber + 1, $totalPages)?> " <?= $view->pageNumber >= $totalPages - 1 ? 'disabled' : '' ?>>Forward <i class="bi bi-chevron-right"></i></a>
|
||||
<a class="btn btn-outline-primary" href="<?=buildUrl(min($view->pageNumber + 1, $totalPages), $params)?>" <?= $view->pageNumber >= $totalPages - 1 ? 'disabled' : '' ?>>Forward <i class="bi bi-chevron-right"></i></a>
|
||||
<!-- End Button -->
|
||||
<a class="btn btn-outline-primary" href="?page=<?= $totalPages - 1 ?>"<?= $view->pageNumber >= $totalPages - 1 ? 'disabled' : '' ?>>End <i class="bi bi-chevron-double-right"></i></a>
|
||||
<a class="btn btn-outline-primary" href="<?= buildUrl($totalPages - 1, $params) ?>"<?= $view->pageNumber >= $totalPages - 1 ? 'disabled' : '' ?>>End <i class="bi bi-chevron-double-right"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@@ -1,10 +1,11 @@
|
||||
<?php
|
||||
// load required classes
|
||||
require_once('Models/UserDataSet.php');
|
||||
require_once("logincontroller.php");
|
||||
// make a view class
|
||||
$view = new stdClass();
|
||||
$view->pageTitle = 'Home';
|
||||
require_once("logincontroller.php");
|
||||
|
||||
|
||||
//if (isset($_POST['applyAdvFilters'])) {
|
||||
// array_push($filterArray, $_POST['titlFilter'], $_POST['cateFilter'],
|
||||
|
@@ -4,17 +4,31 @@ require_once("Models/User.php");
|
||||
|
||||
$user = new User();
|
||||
$userDataSet = new UserDataSet();
|
||||
|
||||
if (isset($_POST["loginButton"])) {
|
||||
$username = $_POST["username"];
|
||||
$password = hash("sha256", $_POST["password"]);
|
||||
if (isset($view->loginError)) {
|
||||
$generatedCaptcha = $_POST["generatedCaptcha"];
|
||||
$userCaptcha = $_POST["captcha"];
|
||||
|
||||
if ($generatedCaptcha !== $userCaptcha) {
|
||||
$view->loginError = "Incorrect CAPTCHA.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
// create a new student dataset object that we can generate data from
|
||||
// Error handling is VERY hacky, because of the lack of JS usage.
|
||||
if($userDataSet->checkUserCredentials($username, $password)) {
|
||||
$user->Authenticate($username, $password);
|
||||
}
|
||||
else {
|
||||
echo "Error in Uname / Pass";
|
||||
// Unset modal boolean to hide it's usage.
|
||||
unset($_GET['modal']);
|
||||
} else {
|
||||
// Add error message and redirect to display modal
|
||||
$view->loginError = "Invalid username or password.";
|
||||
// Set modal boolean to header to allow modal to reappear
|
||||
$queryParams = http_build_query(['modal' => 'true']);
|
||||
header("Location: {$_SERVER['PHP_SELF']}?$queryParams");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,3 +36,7 @@ if (isset($_POST["logoutButton"]))
|
||||
{
|
||||
$user->logout();
|
||||
}
|
||||
|
||||
if (isset($_GET['modal']) && $_GET['modal'] === 'true') {
|
||||
$view->loginError = $view->loginError ?? "Please solve the Captcha and try again.";
|
||||
}
|
@@ -1,58 +1,53 @@
|
||||
<?php
|
||||
require_once('Models/FacilityDataSet.php');
|
||||
require_once("Models/Paginator.php");
|
||||
// If page loads empty, set initial headers
|
||||
//if(empty($_GET)) {
|
||||
// header("Location: ?sort=1&dir=asc&category=1&term=&page=0");
|
||||
// exit;
|
||||
//}
|
||||
|
||||
if(!isset($_GET['page'])) {
|
||||
header("location: ?page=0");
|
||||
}
|
||||
|
||||
$filterArray = [
|
||||
0 => "",
|
||||
1 => "",
|
||||
2 => "",
|
||||
3 => "",
|
||||
4 => "",
|
||||
5 => "",
|
||||
6 => "",
|
||||
7 => "",
|
||||
8 => ""
|
||||
$filters = [
|
||||
'category' => $_GET['category'] ?? '1', // Default category
|
||||
'term' => $_GET['term'] ?? '', // Default term
|
||||
'sort' => $_GET['sort'] ?? '1', // Default sort
|
||||
'dir' => $_GET['dir'] ?? 'asc', // Default direction
|
||||
'page' => $_GET['page'] ?? 0 // Default to first page
|
||||
];
|
||||
|
||||
$rowLimit = 5; //$_POST['rowCount'];
|
||||
|
||||
$facilityDataSet = new FacilityDataSet();
|
||||
|
||||
if (isset($_POST['applyFilters']) && isset($_POST['filter'])) {
|
||||
var_dump($_POST);
|
||||
$applyFilters = filter_input(INPUT_POST, 'applyFilters', FILTER_SANITIZE_STRING);
|
||||
$filterKey = filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_STRING);
|
||||
$filterArray[$filterKey] = $applyFilters;
|
||||
// Set the filter and generate the new URI
|
||||
$filterSet = $facilityDataSet->setFilterUri($applyFilters, array_search($applyFilters, $filterArray));
|
||||
|
||||
// Parse the existing query string
|
||||
parse_str($filterSet["newUri"], $queryParams);
|
||||
|
||||
// Add or overwrite the 'page' parameter
|
||||
$queryParams['page'] = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, [
|
||||
'options' => ['default' => 0, 'min_range' => 0] // Default to 0 for the first page
|
||||
]);
|
||||
|
||||
// Build the updated query string
|
||||
$newQueryString = http_build_query($queryParams);
|
||||
|
||||
// Redirect with the updated URI
|
||||
header("Location: {$filterSet["path"]}?{$newQueryString}");
|
||||
exit;
|
||||
// If no query parameters exist (initial page load), redirect to set default ones
|
||||
if (empty($_GET)) {
|
||||
redirectWithFilters($filters);
|
||||
}
|
||||
|
||||
$rowLimit = 5;
|
||||
$facilityDataSet = new FacilityDataSet();
|
||||
|
||||
$filterArray[$_GET['category'] ?? null] = $_GET['term'] ?? null;
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Check if filters/sorting changed
|
||||
$filtersChanged = (
|
||||
$filters['category'] !== ($_POST['filterCat'] ?? $filters['category']) ||
|
||||
$filters['term'] !== ($_POST['filter'] ?? $filters['term']) ||
|
||||
$filters['sort'] !== ($_POST['sort'] ?? $filters['sort']) ||
|
||||
$filters['dir'] !== ($_POST['dir'] ?? $filters['dir'])
|
||||
);
|
||||
|
||||
$filters['category'] = filter_input(INPUT_POST, 'filterCat', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? $filters['category'];
|
||||
$filters['term'] = filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? $filters['term'];
|
||||
$filters['sort'] = filter_input(INPUT_POST, 'sort', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? $filters['sort'];
|
||||
$filters['dir'] = filter_input(INPUT_POST, 'dir', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? $filters['dir'];
|
||||
|
||||
// Reset page if filters changed
|
||||
$filters['page'] = $filtersChanged ? 0 : $_POST['paginationButton'] ?? $filters['page'];
|
||||
redirectWithFilters($filters);
|
||||
}
|
||||
|
||||
$view->pageData = $facilityDataSet->fetchAll(
|
||||
['category' => $filters['category'], 'term' => $filters['term']],
|
||||
['sort' => $filters['sort'], 'dir' => $filters['dir']]
|
||||
);
|
||||
|
||||
$view->pageData = $facilityDataSet->fetchAll($filterArray);
|
||||
$view->paginator = new Paginator($rowLimit, $view->pageData);
|
||||
|
||||
// Initialize paginator
|
||||
$view->pageNumber = $view->paginator->getPageFromUri();
|
||||
$view->pageData = $view->paginator->getPage($view->pageNumber);
|
||||
|
||||
@@ -60,3 +55,10 @@ $view->pageData = $view->paginator->getPage($view->pageNumber);
|
||||
$view->dbMessage = $view->paginator->countPageResults($view->pageNumber) == 0
|
||||
? "No results"
|
||||
: $view->paginator->countPageResults($view->pageNumber) . " result(s)";
|
||||
|
||||
// Redirect function
|
||||
function redirectWithFilters($filters) {
|
||||
$queryString = http_build_query($filters);
|
||||
header("Location: ?" . $queryString);
|
||||
exit;
|
||||
}
|
||||
|
Reference in New Issue
Block a user