completely flarched it

This commit is contained in:
boris
2024-11-29 17:19:08 +00:00
parent 4005328979
commit b7b5fb545b
44 changed files with 252 additions and 8730 deletions

View File

@@ -12,13 +12,11 @@ class FacilityDataSet {
/**
* @param $filterArray
* @param $rowCount
* @param $offset
* @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, $rowCount, $offset): array
public function fetchAll($filterArray): array
{
/**
* COUNT(DISTINCT ecoFacilities.id) required due to multiple status comments possible.
@@ -81,7 +79,7 @@ class FacilityDataSet {
$countQuery = $sqlCount . $sqlWhere . ";";
// Prepare, bind and execute data query
$stmt = $this->populateFields($dataQuery, $rowCount, $offset, $filterArray);
$stmt = $this->populateFields($dataQuery, $filterArray);
$stmt->execute();
// Create data objects
@@ -91,7 +89,7 @@ class FacilityDataSet {
}
// Prepare, bind then execute count query
$stmt = $this->populateFields($countQuery, null, null, $filterArray);
$stmt = $this->populateFields($countQuery, $filterArray);
$stmt->execute();
$totalCount = $stmt->fetch()['total'];
@@ -103,14 +101,12 @@ class FacilityDataSet {
/**
* @param $sqlQuery
* @param $rowCount
* @param $offset
* @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, $rowCount, $offset, $filterArray)
private function populateFields($sqlQuery, $filterArray)
{
$stmt = $this->_dbHandle->prepare($sqlQuery);
// Ensures only one value is returned per column name
@@ -129,13 +125,41 @@ class FacilityDataSet {
$value = !empty($filterArray[$i]) ? "%" . $filterArray[$i] . "%" : "%";
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
}
// Bind LIMIT and OFFSET
if (!$rowCount == null || !$offset == null) {
$stmt->bindValue(':limit', $rowCount, \PDO::PARAM_INT); // LIMIT ?
$stmt->bindValue(':offset', $offset, \PDO::PARAM_INT); // OFFSET ?
}
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'] ?? '/'
]; }
}

View File

@@ -1,17 +1,18 @@
<?php
require_once('FacilityDataSet.php');
class Paginator {
protected $_pages, $_totalPages, $_rowLimit, $_offset, $_pageMatrix, $_rowCount;
protected $_pages, $_totalPages, $_rowLimit, $_pageMatrix, $_rowCount;
public function __construct($rowLimit, $offset, $dataset) {
public function __construct($rowLimit, $dataset) {
$this->_rowLimit = $rowLimit;
$this->_offset = $offset;
$this->_totalPages = $this->calculateTotalPages($dataset['count']);
$this->_rowCount = $dataset['count'];
$this->_pages = $dataset['dataset'];
$this->_pageMatrix = $this->Paginate();
}
public function getTotalPages() {
return $this->_totalPages;
}
private function calculateTotalPages(int $count): int {
return $count > 0 ? ceil($count / $this->_rowLimit) : 0;
}
@@ -32,14 +33,14 @@ class Paginator {
return $pageMatrix;
}
function getPageFromUri(): int {
public function getPageFromUri(): int {
// Retrieve 'page' parameter and default to 0 if missing or invalid
return filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, [
'options' => ['default' => 0, 'min_range' => 0] // Default to 0 if invalid or missing
'options' => ['default' => 0, 'min_range' => 0] // Default to 1 if invalid or missing
]);
}
public function setPageUri(int $page): void
public function setPageUri($page)
{
$uri = $_SERVER['REQUEST_URI'];
$uriComp = parse_url($uri);
@@ -48,11 +49,13 @@ class Paginator {
// Parse existing query parameters
if (isset($uriComp['query'])) {
parse_str($uriComp['query'], $params);
} else {
$params = array();
}
// Avoid unnecessary redirection if the page is already correct
if (isset($params['page']) && (int)$params['page'] === $page) {
return; // Do nothing if already on the correct page
exit; // Do nothing if already on the correct page
}
// Update the 'page' parameter
@@ -62,9 +65,12 @@ class Paginator {
$newUri = http_build_query($params);
// Redirect to the updated URI
$path = $uriComp['path'] ?? '/'; // Use the current path or root
header("Location: $path?$newUri");
exit;
// Use the current path or root
return
[
'newUri' => $newUri,
'path' => $uriComp['path'] ?? '/'
];
}
public function getPage(int $pageNumber): array {