(airhead): ive no idea no lie
Signed-off-by: boris <boris@borishub.co.uk>
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
<?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;
|
||||
//}
|
||||
|
||||
// Default Filters
|
||||
$filters = [
|
||||
'category' => $_GET['category'] ?? '1', // Default category
|
||||
'term' => $_GET['term'] ?? '', // Default term
|
||||
@@ -20,10 +16,50 @@ if (empty($_GET)) {
|
||||
redirectWithFilters($filters);
|
||||
}
|
||||
|
||||
$rowLimit = 5;
|
||||
// Set row limit
|
||||
$rowLimit = 7;
|
||||
// create dataset object
|
||||
$facilityDataSet = new FacilityDataSet();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
/**
|
||||
* Unfortunately, ZERO time to fix this, too complex.
|
||||
*/
|
||||
if(isset($_POST['updateButton'])) {
|
||||
$data = [
|
||||
'id' => $_POST['idUpdate'],
|
||||
'title' => $_POST['titlUpdate'],
|
||||
'category' => $_POST['cateUpdate'],
|
||||
'description' => $_POST['descUpdate'],
|
||||
'houseNumber' => $_POST['hnumUpdate'],
|
||||
'streetName' => $_POST['strtUpdate'],
|
||||
'county' => $_POST['cntyUpdate'],
|
||||
'town' => $_POST['townUpdate'],
|
||||
'postcode' => $_POST['postUpdate'],
|
||||
'lng' => $_POST['lngUpdate'],
|
||||
'lat' => $_POST['latUpdate'],
|
||||
'contributor' => $_POST['contUpdate'],
|
||||
];
|
||||
$facilityDataSet->addFacility($data);
|
||||
}
|
||||
if(isset($_POST['createButton'])) {
|
||||
$data = [
|
||||
'title' => $_POST['titlCreate'],
|
||||
'category' => $_POST['cateCreate'],
|
||||
'description' => $_POST['descCreate'],
|
||||
'houseNumber' => $_POST['hnumCreate'],
|
||||
'streetName' => $_POST['strtCreate'],
|
||||
'county' => $_POST['cntyCreate'],
|
||||
'town' => $_POST['townCreate'],
|
||||
'postcode' => $_POST['postCreate'],
|
||||
'contributor' => $_POST['contCreate'],
|
||||
];
|
||||
$facilityDataSet->addFacility($data);
|
||||
}
|
||||
// passes id to delete facility
|
||||
if (isset($_POST['deleteButton'])) {
|
||||
$facilityDataSet->deleteFacility($_POST['id']);
|
||||
}
|
||||
// Check if filters/sorting changed
|
||||
$filtersChanged = (
|
||||
$filters['category'] !== ($_POST['filterCat'] ?? $filters['category']) ||
|
||||
@@ -32,6 +68,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$filters['dir'] !== ($_POST['dir'] ?? $filters['dir'])
|
||||
);
|
||||
|
||||
// load from post if exists and sanitise, otherwise use defaults
|
||||
$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'];
|
||||
@@ -42,23 +79,34 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
redirectWithFilters($filters);
|
||||
}
|
||||
|
||||
$view->pageData = $facilityDataSet->fetchAll(
|
||||
// fetch page data from database
|
||||
$view->allPageData = $facilityDataSet->fetchAll(
|
||||
['category' => $filters['category'], 'term' => $filters['term']],
|
||||
['sort' => $filters['sort'], 'dir' => $filters['dir']]
|
||||
);
|
||||
|
||||
$view->paginator = new Paginator($rowLimit, $view->pageData);
|
||||
// set total facility count to view
|
||||
$view->totalResults = $view->allPageData['count'];
|
||||
// create paginator object
|
||||
$view->paginator = new Paginator($rowLimit, $view->allPageData);
|
||||
// assign page number to view
|
||||
$view->pageNumber = $view->paginator->getPageFromUri();
|
||||
// get current page
|
||||
$view->pageData = $view->paginator->getPage($view->pageNumber);
|
||||
|
||||
// Send result count to view
|
||||
// Send result count to view in format "showing x of y results"
|
||||
$view->dbMessage = $view->paginator->countPageResults($view->pageNumber) == 0
|
||||
? "No results"
|
||||
: $view->paginator->countPageResults($view->pageNumber) . " result(s)";
|
||||
: "Showing " . $view->paginator->countPageResults($view->pageNumber) . " of " . $view->totalResults . " result(s)";
|
||||
|
||||
// Redirect function
|
||||
// Redirect function, adds header parameters
|
||||
function redirectWithFilters($filters) {
|
||||
// Ensure no unintended keys are passed
|
||||
$allowedKeys = ['category', 'term', 'sort', 'dir', 'page'];
|
||||
$filters = array_filter($filters, function($key) use ($allowedKeys) {
|
||||
return in_array($key, $allowedKeys);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
|
||||
$queryString = http_build_query($filters);
|
||||
header("Location: ?" . $queryString);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user