Compare commits
2 Commits
paginator-
...
574dcbb119
Author | SHA1 | Date | |
---|---|---|---|
|
574dcbb119 | ||
|
f9d625e905 |
@@ -53,14 +53,52 @@ class FacilityDataSet {
|
||||
$stmt->execute();
|
||||
return !($stmt->rowCount() == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filterArray
|
||||
* @param $sortArray
|
||||
* @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
|
||||
{
|
||||
$direction = '';
|
||||
// Set direction, if not found in array, set to ascending.
|
||||
(in_array('desc', $sortArray)) ? $direction = 'DESC' : $direction = 'ASC';
|
||||
// Default to title
|
||||
// Note: I am very sorry, i am well aware this is horrible, im running out of time
|
||||
$sortBy = 1;
|
||||
switch (array_search('desc', $sortArray) ?? array_search('asc', $sortArray)) {
|
||||
case (0) :
|
||||
$sortBy = 'ecoFacilityStatus.statusComment';
|
||||
break;
|
||||
case (1) :
|
||||
$sortBy = 'ecoFacilities.title';
|
||||
break;
|
||||
case (2) :
|
||||
$sortBy = 'ecoCategories.name';
|
||||
break;
|
||||
case (3) :
|
||||
$sortBy = 'ecoFacilities.description';
|
||||
break;
|
||||
case (4) :
|
||||
$sortBy = 'ecoFacilities.streetName';
|
||||
break;
|
||||
case (5) :
|
||||
$sortBy = 'ecoFacilities.county';
|
||||
break;
|
||||
case (6) :
|
||||
$sortBy = 'ecoFacilities.town';
|
||||
break;
|
||||
case (7) :
|
||||
$sortBy = 'ecoFacilities.postcode';
|
||||
break;
|
||||
case (8) :
|
||||
$sortBy = 'ecoUser.username';
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* COUNT(DISTINCT ecoFacilities.id) required due to multiple status comments possible.
|
||||
*/
|
||||
@@ -109,12 +147,15 @@ class FacilityDataSet {
|
||||
* 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.
|
||||
* I unfortunately HAVE to do the ORDER BY statement like this, since PDO doesn't allow
|
||||
* binding of column names to placeholders for some reason. I could have used the column
|
||||
* order and passed an integer, but its too much hassle and I have enough sanitisation,
|
||||
* this is fine.
|
||||
*/
|
||||
$sqlLimits = "
|
||||
GROUP BY ecoFacilities.id
|
||||
LIMIT :limit OFFSET :offset;";
|
||||
$sqlLimits = "
|
||||
GROUP BY ecoFacilities.id
|
||||
GROUP BY ecoFacilities.id, ecoFacilities.title, ecoCategories.name, ecoFacilities.description, ecoFacilities.streetName,
|
||||
ecoFacilities.county, ecoFacilities.town, ecoFacilities.postcode, ecoUser.username
|
||||
ORDER BY {$sortBy} {$direction}
|
||||
;";
|
||||
|
||||
// Concatenate query snippets for data and row count
|
||||
@@ -122,7 +163,7 @@ class FacilityDataSet {
|
||||
$countQuery = $sqlCount . $sqlWhere . ";";
|
||||
|
||||
// Prepare, bind and execute data query
|
||||
$stmt = $this->populateFields($dataQuery, $filterArray);
|
||||
$stmt = $this->populateFields($dataQuery, $filterArray, $sortBy, $direction);
|
||||
$stmt->execute();
|
||||
|
||||
// Create data objects
|
||||
@@ -132,7 +173,7 @@ class FacilityDataSet {
|
||||
}
|
||||
|
||||
// Prepare, bind then execute count query
|
||||
$stmt = $this->populateFields($countQuery, $filterArray);
|
||||
$stmt = $this->populateFields($countQuery, $filterArray, null, null);
|
||||
$stmt->execute();
|
||||
$totalCount = $stmt->fetch()['total'];
|
||||
|
||||
@@ -145,11 +186,13 @@ class FacilityDataSet {
|
||||
/**
|
||||
* @param $sqlQuery
|
||||
* @param $filterArray
|
||||
* @param $sortBy
|
||||
* @param $direction
|
||||
* @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)
|
||||
private function populateFields($sqlQuery, $filterArray, $sortBy, $direction)
|
||||
{
|
||||
$stmt = $this->_dbHandle->prepare($sqlQuery);
|
||||
// Ensures only one value is returned per column name
|
||||
@@ -162,15 +205,24 @@ class FacilityDataSet {
|
||||
$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 ?
|
||||
// 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);
|
||||
// }
|
||||
|
||||
// Bind other filters
|
||||
for ($i = 1; $i <= 8; $i++) { // Assuming 8 other filters
|
||||
$value = !empty($filterArray[$i]) ? "%" . $filterArray[$i] . "%" : "%";
|
||||
print_r($i . " = " . $value . "|");
|
||||
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
|
||||
}
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
// UNUSED REPLACED
|
||||
public function setFilterUri($term, $category)
|
||||
{
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
|
@@ -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>
|
||||
|
@@ -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>
|
||||
|
@@ -6,6 +6,7 @@ require_once("logincontroller.php");
|
||||
$view = new stdClass();
|
||||
$view->pageTitle = 'Home';
|
||||
|
||||
|
||||
//if (isset($_POST['applyAdvFilters'])) {
|
||||
// array_push($filterArray, $_POST['titlFilter'], $_POST['cateFilter'],
|
||||
// $description = $_POST['descFilter'], $status = $_POST['statFilter'],
|
||||
|
@@ -1,9 +1,10 @@
|
||||
<?php
|
||||
require_once('Models/FacilityDataSet.php');
|
||||
require_once("Models/Paginator.php");
|
||||
|
||||
if(!isset($_GET['page'])) {
|
||||
header("location: ?page=0");
|
||||
// If page loads empty, set initial headers
|
||||
if(!isset($_GET['page']) || !(isset($_GET['dir'])) || !(isset($_GET['sort']))) {
|
||||
header("Location: ?sort=1&dir=asc&page=0");
|
||||
exit;
|
||||
}
|
||||
|
||||
$filterArray = [
|
||||
@@ -17,20 +18,44 @@ $filterArray = [
|
||||
7 => "",
|
||||
8 => ""
|
||||
];
|
||||
$sortArray = [
|
||||
0 => "",
|
||||
1 => "",
|
||||
2 => "",
|
||||
3 => "",
|
||||
4 => "",
|
||||
5 => "",
|
||||
6 => "",
|
||||
7 => "",
|
||||
8 => ""
|
||||
];
|
||||
|
||||
$rowLimit = 5; //$_POST['rowCount'];
|
||||
$rowLimit = 5;
|
||||
|
||||
$filterArray[$_GET['category'] ?? null] = $_GET['term'] ?? null;
|
||||
$sortArray[$_GET['sort'] ?? null] = $_GET['dir'] ?? null;
|
||||
|
||||
$facilityDataSet = new FacilityDataSet();
|
||||
|
||||
if (isset($_POST['applyFilters']) && isset($_POST['filter'])) {
|
||||
if(isset($_POST['paginatorButton'])) {
|
||||
var_dump($_POST);
|
||||
$applyFilters = filter_input(INPUT_POST, 'applyFilters', FILTER_SANITIZE_STRING);
|
||||
$filterKey = filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_STRING);
|
||||
$filterArray[$filterKey] = $applyFilters;
|
||||
}
|
||||
if ((isset($_POST['filter']) && isset($_POST['filterCat']) && (isset($_POST['dir'])) && (isset($_POST['sort'])))) {
|
||||
$filter = filter_input(INPUT_POST, 'filter', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? '';
|
||||
$filterKey = filter_input(INPUT_POST, 'filterCat', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? '';
|
||||
$direction = filter_input(INPUT_POST, 'dir', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? 'asc'; // Default to 'asc'
|
||||
$sortKey = filter_input(INPUT_POST, 'sort', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? '1'; // Default to 'title'
|
||||
$page = filter_input(INPUT_POST, 'paginationButton', FILTER_SANITIZE_NUMBER_INT) ?? 0; // Default page to 0 on new filter
|
||||
$filterArray[$filterKey] = $filter;
|
||||
$sortArray[$sortKey] = $direction;
|
||||
var_dump($filterArray);
|
||||
//redirect($filter, $filterArray, $direction, $sortArray, $page);
|
||||
}
|
||||
function redirect($filter, $filterArray, $direction, $sortArray, $page) : void {
|
||||
// Set the filter and generate the new URI
|
||||
$filterSet = $facilityDataSet->setFilterUri($applyFilters, array_search($applyFilters, $filterArray));
|
||||
|
||||
//$filterSet = $facilityDataSet->setFilterUri($applyFilters, array_search($applyFilters, $filterArray));
|
||||
$filterSet = setUri($filter, array_search($filter, $filterArray), $direction, array_search($direction, $sortArray), $page);
|
||||
// Parse the existing query string
|
||||
$queryParams = [];
|
||||
parse_str($filterSet["newUri"], $queryParams);
|
||||
|
||||
// Add or overwrite the 'page' parameter
|
||||
@@ -42,14 +67,13 @@ if (isset($_POST['applyFilters']) && isset($_POST['filter'])) {
|
||||
$newQueryString = http_build_query($queryParams);
|
||||
|
||||
// Redirect with the updated URI
|
||||
//var_dump("Redirecting to: {$filterSet["path"]}?{$newQueryString}");
|
||||
header("Location: {$filterSet["path"]}?{$newQueryString}");
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$filterArray[$_GET['category'] ?? null] = $_GET['term'] ?? null;
|
||||
|
||||
$view->pageData = $facilityDataSet->fetchAll($filterArray);
|
||||
$view->pageData = $facilityDataSet->fetchAll($filterArray, $sortArray);
|
||||
var_dump($filterArray, $sortArray);
|
||||
$view->paginator = new Paginator($rowLimit, $view->pageData);
|
||||
|
||||
// Initialize paginator
|
||||
@@ -60,3 +84,52 @@ $view->pageData = $view->paginator->getPage($view->pageNumber);
|
||||
$view->dbMessage = $view->paginator->countPageResults($view->pageNumber) == 0
|
||||
? "No results"
|
||||
: $view->paginator->countPageResults($view->pageNumber) . " result(s)";
|
||||
|
||||
function setUri($filter, $category, $direction, $sort, $page)
|
||||
{
|
||||
$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 sort and filter is already correct
|
||||
if (
|
||||
(isset($params['sort']) && $params['sort'] === (string)$sort && isset($params['dir']) && $params['dir'] === $direction) &&
|
||||
(isset($params['category']) && $params['category'] === (string)$category && isset($params['term']) && $params['term'] === $filter)
|
||||
) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Update parameters
|
||||
if (!empty($category)) {
|
||||
$params['category'] = $category;
|
||||
}
|
||||
if (!empty($filter)) {
|
||||
$params['term'] = $filter;
|
||||
}
|
||||
if (!empty($sort)) {
|
||||
$params['sort'] = $sort;
|
||||
}
|
||||
if (!empty($direction)) {
|
||||
$params['dir'] = $direction;
|
||||
}
|
||||
if (!empty($filter)) {
|
||||
$params['page'] = $page;
|
||||
}
|
||||
|
||||
// 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'] ?? '/'
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user