Added filter removing deleted objects from paginated object lists

This commit is contained in:
Jepp9350
2024-12-19 12:08:33 +01:00
parent a9565bfb29
commit e9a3029628
+50 -47
View File
@@ -7,30 +7,14 @@ use Exception;
trait db_object_t
{
private string $table; // The table of the objects in the database (e.g. users)
public int $id; // The id of the object in the database
private string $table; // The table of the objects in the database (e.g. users)
public function __construct()
{
$this->structure();
}
public function __toString(): string
{
// Return the object as a string
return json_encode($this->getArray());
}
/**
* Set the table of the objects in the database
* @param string $table The table of the objects in the database
*/
public function setTable(string $table): void
{
$this->table = $table;
}
/**
* Structure: Define the table and fields of the objects in the database
*/
@@ -39,6 +23,12 @@ trait db_object_t
// Define the table and fields of the objects in the database
}
public function __toString(): string
{
// Return the object as a string
return json_encode($this->getArray());
}
/**
* Get current object row as an array
* @return array The current object row as an array
@@ -59,15 +49,39 @@ trait db_object_t
}
/**
* List ALL objects in the table
* Set the table of the objects in the database
* @param string $table The table of the objects in the database
*/
public function setTable(string $table): void
{
$this->table = $table;
}
/**
* List objects with pagination (if set)
* @return array The list of objects in the table
*/
public function listObjects(): array
public function listObjectsWithPaginationIfSet(): array
{
global $db;
$sql = "SELECT * FROM $this->table";
$result = $db->query($sql);
return $db->fetch_all($result);
global $response;
$page = ((int)$response->getRequestParameter('page')) ?? null; // Get the page number
$limit = ((int)$response->getRequestParameter('limit')) ?? null; // Get the number of objects per page
$search = $response->getRequestParameter('search') ?? null; // Get the search query
$filters = $response->getRequestParameter('filters') ?? null; // Get the filters ( Eg. department_id:1,role_id:2 OR department_id:1 )
// Make the filters an array
if ($filters) {
$filters = explode(',', $filters);
$temp = [];
foreach ( $filters as $filter ) {
$filter = explode(':', $filter);
$temp[$filter[0]] = $filter[1];
}
$filters = $temp;
}
if ($page && $limit) {
return $this->listObjectsWithPagination($page, $limit, $search, $filters);
}
return $this->listObjects();
}
/**
@@ -86,7 +100,7 @@ trait db_object_t
$result = $db->query($sql);
$fields = $db->fetch_all($result);
$searchfields = [];
foreach ($fields as $field) {
foreach ( $fields as $field ) {
$searchfields[] = $field['Field'];
}
// Search for any similarities to the search query (Not case sensitive)
@@ -95,7 +109,7 @@ trait db_object_t
$searchQuery = 'WHERE ';
$search = strtolower($search);
$searchQuery .= '(';
foreach ($searchfields as $field) {
foreach ( $searchfields as $field ) {
$searchQuery .= "LOWER($field) LIKE '%$search%' OR ";
}
$searchQuery = substr($searchQuery, 0, -4);
@@ -108,9 +122,13 @@ trait db_object_t
} else {
$searchQuery = 'WHERE ';
}
foreach ($filters as $field => $value) {
foreach ( $filters as $field => $value ) {
$searchQuery .= "$field = $value AND ";
}
// If the deleted_at column exists, filter out the deleted objects
if (in_array('deleted_at', $searchfields)) {
$searchQuery .= "deleted_at IS NULL AND ";
}
$searchQuery = substr($searchQuery, 0, -5);
}
// Get the objects with pagination
@@ -126,30 +144,15 @@ trait db_object_t
}
/**
* List objects with pagination (if set)
* List ALL objects in the table (THIS INCLUDES DELETED OBJECTS)
* @return array The list of objects in the table
*/
public function listObjectsWithPaginationIfSet(): array
public function listObjects(): array
{
global $response;
$page = ((int) $response->getRequestParameter('page')) ?? null; // Get the page number
$limit = ((int) $response->getRequestParameter('limit')) ?? null; // Get the number of objects per page
$search = $response->getRequestParameter('search') ?? null; // Get the search query
$filters = $response->getRequestParameter('filters') ?? null; // Get the filters ( Eg. department_id:1,role_id:2 OR department_id:1 )
// Make the filters an array
if ($filters) {
$filters = explode(',', $filters);
$temp = [];
foreach ($filters as $filter) {
$filter = explode(':', $filter);
$temp[$filter[0]] = $filter[1];
}
$filters = $temp;
}
if ($page && $limit) {
return $this->listObjectsWithPagination($page, $limit, $search, $filters);
}
return $this->listObjects();
global $db;
$sql = "SELECT * FROM $this->table";
$result = $db->query($sql);
return $db->fetch_all($result);
}
/**