216 lines
7.8 KiB
PHP
216 lines
7.8 KiB
PHP
<?php
|
|
require_once ('Database.php');
|
|
require_once ('FacilityData.php');
|
|
|
|
class FacilityDataSet
|
|
{
|
|
protected $_dbHandle, $_dbInstance;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->_dbInstance = Database::getInstance();
|
|
$this->_dbHandle = $this->_dbInstance->getDbConnection();
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return bool
|
|
*/
|
|
public function addFacility($data): bool
|
|
{
|
|
$sqlQuery = "
|
|
INSERT INTO ecoFacilities
|
|
(title,
|
|
category,
|
|
description,
|
|
houseNumber,
|
|
streetName,
|
|
county,
|
|
town,
|
|
postcode,
|
|
lng,
|
|
lat,
|
|
contributor)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
;";
|
|
$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 other filters
|
|
for ($i = 1; $i <= 8; $i++) { // Assuming 8 other filters
|
|
$value = !empty($data[$i]) ? "%" . $data[$i] . "%" : "%";
|
|
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
|
|
}
|
|
return !($stmt->rowCount());
|
|
}
|
|
|
|
public function deleteFacility($id): bool
|
|
{
|
|
$sqlQuery = "DELETE FROM ecoFacilities WHERE id = ?";
|
|
$stmt = $this->_dbHandle->prepare($sqlQuery);
|
|
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
|
|
$stmt->bindValue(1, $id, \PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
return !($stmt->rowCount() == 0);
|
|
}
|
|
|
|
|
|
public function fetchAll($filterArray, $sortArray): array
|
|
{
|
|
// 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'
|
|
];
|
|
|
|
$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'
|
|
];
|
|
|
|
// 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 {$selectedFilterColumn} LIKE :term
|
|
";
|
|
|
|
|
|
// Query to count total results
|
|
$countQuery = "SELECT COUNT(DISTINCT ecoFacilities.id) AS total {$baseQuery}";
|
|
|
|
// 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();
|
|
|
|
// 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 = $dataStmt->fetch()) {
|
|
$dataSet[] = new FacilityData($row);
|
|
}
|
|
|
|
return [
|
|
'dataset' => $dataSet,
|
|
'count' => $totalResults
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $sqlQuery
|
|
* @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, $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);
|
|
$stmt->setFetchMode(\PDO::FETCH_ASSOC);
|
|
|
|
$bindIndex = 1;
|
|
|
|
// 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 < count($filterArray); $i++) {
|
|
$value = $filterArray[$i] ?? '%';
|
|
print_r($i . ":" . $value . "||\n");
|
|
$stmt->bindValue($bindIndex++, $value, \PDO::PARAM_STR);
|
|
}
|
|
|
|
// Debugging
|
|
//$stmt->debugDumpParams();
|
|
return $stmt;
|
|
}
|
|
|
|
}
|
|
|