(feat): filter and direction dropdown
This commit is contained in:
@@ -53,14 +53,53 @@ 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;
|
||||
var_dump($sortArray);
|
||||
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 +148,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 +164,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 +174,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 +187,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,6 +206,13 @@ 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
|
||||
@@ -171,6 +222,7 @@ class FacilityDataSet {
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
// UNUSED REPLACED
|
||||
public function setFilterUri($term, $category)
|
||||
{
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
|
Reference in New Issue
Block a user