Files
api/services/nginx/app/objects/plate_scans_o.php
T
Jeppe Bundgaard 5a71889b26 Add support for optional bayId in license plate scans
- Updated `plateScansRoute` to handle an optional `bay_id` parameter in plate scan requests.
- Extended `plate_scans_o` object to include `bay_id` property and related database operations.
- Enhanced `add` method to validate and process `bayId` if provided.
2025-12-15 10:14:00 +01:00

99 lines
3.3 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class plate_scans_o extends db
{
use db_object_t;
public object_property $plate_scanner_id;
public object_property $plate;
public object_property $bay_id;
public function structure(): void
{
$this->setTable('plate_scans');
}
public function objectChanged(): void
{
// No need to invalidate the cache, since the plate_scans object is not cached
}
public function getPlateScanById(int $id): plate_scans_o
{
global $db;
// Get the record from the database
$sql = "SELECT * FROM $this->table WHERE id = $id";
$result = $db->query($sql);
if ($result->num_rows > 0) {
$this->id = $id;
$this->getObjectProperties();
}
return $this;
}
public function getObjectProperties(): void
{
$this->plate_scanner_id = new object_property($this->table, $this->id, 'plate_scanner_id', 'int');
$this->plate = new object_property($this->table, $this->id, 'plate', 'string');
$this->bay_id = new object_property($this->table, $this->id, 'bay_id', 'string');
}
public function add(int $plate_scanner_id, string $plate, string $bayId = null): void
{
global $db, $response;
try {
// Avoid SQL injection
$plate = $db->escape_string($plate);
// If the bayId is set, validate it
if ($bayId !== null) {
$bayId = $db->escape_string($bayId);
}
// Get the department id from the plate scanner id
$sql = "SELECT department_id FROM plate_scanners WHERE id = $plate_scanner_id";
$result = $db->query($sql);
$row = $db->fetch_assoc($result);
$department_id = $row['department_id'];
// Create a new record in the database
$sql = "INSERT INTO $this->table (plate_scanner_id, plate, department_id) VALUES ($plate_scanner_id, '$plate', $department_id)";
$db->query($sql);
// Get the id of the new record
$this->id = $db->insert_id();
// Set the values of the object properties
$this->getObjectProperties();
// Set the bayId if provided
if ($bayId !== null) {
$this->bay_id->set((string)$bayId);
}
} catch (\Exception $e) {
$response->error($e->getMessage());
}
}
public function getPlateScansByDepartment(int $department_id, int $page, int $limit): array
{
global $db;
// Avoid SQL injection
$department_id = $db->escape_string($department_id);
$page = $db->escape_string($page);
$limit = $db->escape_string($limit);
// Calculate the offset
$offset = ($page - 1) * $limit;
$sql = "SELECT * FROM $this->table WHERE department_id = $department_id ORDER BY id DESC LIMIT $limit OFFSET $offset";
$result = $db->query($sql);
$plate_scans = [];
if ($result->num_rows > 0) {
// Return the records as an array raw data
while ($row = $result->fetch_assoc()) {
$plate_scans[] = $row;
}
}
return $plate_scans;
}
}