jilo-web/app/classes/host.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2024-10-30 17:11:23 +00:00
<?php
class Host {
private $db;
public function __construct($database) {
$this->db = $database->getConnection();
}
// get details of a specified host ID (or all) in a specified platform ID
public function getHostDetails($platform_id = '', $host_id = '') {
$sql = 'SELECT
id,
address,
port,
platform_id,
name
FROM
hosts';
if ($platform_id !== '' && $host_id !== '') {
$sql .= ' WHERE platform_id = :platform_id AND id = :host_id';
} elseif ($platform_id !== '') {
$sql .= ' WHERE platform_id = :platform_id';
} elseif ($host_id !== '') {
$sql .= ' WHERE id = :host_id';
}
$query = $this->db->prepare($sql);
if ($platform_id !== '') {
$query->bindParam(':platform_id', $platform_id);
}
if ($host_id !== '') {
$query->bindParam(':host_id', $host_id);
}
$query->execute();
return $query->fetchAll(PDO::FETCH_ASSOC);
}
// add new host
public function addHost($newHost) {
try {
$sql = 'INSERT INTO hosts
(address, port, platform_id, name)
VALUES
(:address, :port, :platform_id, :name)';
$query = $this->db->prepare($sql);
$query->execute([
':address' => $newHost['address'],
':port' => $newHost['port'],
':platform_id' => $newHost['platform_id'],
':name' => $newHost['name'],
]);
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
// edit an existing host
2024-10-31 09:25:37 +00:00
public function editHost($platform_id, $updatedHost) {
2024-10-30 17:11:23 +00:00
try {
2024-10-31 09:25:37 +00:00
$sql = 'UPDATE hosts SET
address = :address,
port = :port,
name = :name,
2024-10-30 17:11:23 +00:00
WHERE
2024-10-31 09:25:37 +00:00
id = :id';
2024-10-30 17:11:23 +00:00
$query = $this->db->prepare($sql);
$query->execute([
2024-10-31 09:25:37 +00:00
':address' => $updatedHost['address'],
':port' => $updatedHost['port'],
':name' => $updatedHost['name'],
2024-10-30 17:11:23 +00:00
]);
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
// delete a host
2024-10-30 20:14:54 +00:00
public function deleteHost($host_id) {
2024-10-30 17:11:23 +00:00
try {
2024-10-30 20:14:54 +00:00
$sql = 'DELETE FROM hosts
2024-10-30 17:11:23 +00:00
WHERE
2024-10-30 20:14:54 +00:00
id = :host_id';
2024-10-30 17:11:23 +00:00
$query = $this->db->prepare($sql);
2024-10-30 20:14:54 +00:00
$query->bindParam(':host_id', $host_id);
2024-10-30 17:11:23 +00:00
$query->execute();
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
}
?>