103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
|
// models/HospitalMap.php
|
|
require_once dirname(__DIR__) . '/config/database.php';
|
|
|
|
class HospitalMap {
|
|
private $conn;
|
|
private $table_name = "hospital_maps";
|
|
|
|
public function __construct() {
|
|
$this->conn = Database::getInstance();
|
|
}
|
|
|
|
public function getAll($status = null) {
|
|
$query = "SELECT * FROM " . $this->table_name;
|
|
if ($status) {
|
|
$query .= " WHERE status = :status";
|
|
}
|
|
$query .= " ORDER BY building ASC, floor ASC";
|
|
|
|
$stmt = $this->conn->prepare($query);
|
|
if ($status) {
|
|
$stmt->bindParam(':status', $status);
|
|
}
|
|
$stmt->execute();
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function getById($id) {
|
|
$query = "SELECT * FROM " . $this->table_name . " WHERE id = :id LIMIT 1";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':id', $id);
|
|
$stmt->execute();
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function create($data) {
|
|
$query = "INSERT INTO " . $this->table_name . "
|
|
(name, building, floor, image_path, transport_center_location, status)
|
|
VALUES (:name, :building, :floor, :image_path, :transport_center_location, :status)";
|
|
|
|
$stmt = $this->conn->prepare($query);
|
|
|
|
$stmt->bindParam(':name', $data['name']);
|
|
$stmt->bindParam(':building', $data['building']);
|
|
$stmt->bindParam(':floor', $data['floor']);
|
|
$stmt->bindParam(':image_path', $data['image_path']);
|
|
$stmt->bindParam(':transport_center_location', $data['transport_center_location']);
|
|
$stmt->bindParam(':status', $data['status']);
|
|
|
|
return $stmt->execute();
|
|
}
|
|
|
|
public function update($id, $data) {
|
|
$update_fields = [];
|
|
$params = [':id' => $id];
|
|
|
|
foreach (['name', 'building', 'floor', 'transport_center_location', 'status'] as $field) {
|
|
if (isset($data[$field])) {
|
|
$update_fields[] = "{$field} = :{$field}";
|
|
$params[":{$field}"] = $data[$field];
|
|
}
|
|
}
|
|
|
|
// Update image only if provided
|
|
if (isset($data['image_path'])) {
|
|
$update_fields[] = "image_path = :image_path";
|
|
$params[":image_path"] = $data['image_path'];
|
|
}
|
|
|
|
if (empty($update_fields)) return false;
|
|
|
|
$query = "UPDATE " . $this->table_name . " SET " . implode(", ", $update_fields) . " WHERE id = :id";
|
|
$stmt = $this->conn->prepare($query);
|
|
|
|
foreach ($params as $key => &$val) {
|
|
$stmt->bindParam($key, $val);
|
|
}
|
|
|
|
return $stmt->execute();
|
|
}
|
|
|
|
public function delete($id) {
|
|
// Find image path to delete file
|
|
$map = $this->getById($id);
|
|
if ($map && !empty($map['image_path'])) {
|
|
$file_path = dirname(__DIR__) . '/' . $map['image_path'];
|
|
if (file_exists($file_path)) {
|
|
unlink($file_path);
|
|
}
|
|
}
|
|
|
|
$query = "DELETE FROM " . $this->table_name . " WHERE id = :id";
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bindParam(':id', $id);
|
|
return $stmt->execute();
|
|
}
|
|
}
|