jilo-web/app/classes/host.php

153 lines
4.2 KiB
PHP
Raw Normal View History

2024-10-30 17:11:23 +00:00
<?php
2024-11-25 14:09:47 +00:00
/**
2024-11-29 16:38:49 +00:00
* class Host
2024-11-25 14:09:47 +00:00
*
* Manages the hosts in the database, providing methods to retrieve, add, edit, and delete host entries.
*/
2024-10-30 17:11:23 +00:00
class Host {
2024-11-25 14:09:47 +00:00
/**
* @var PDO|null $db The database connection instance.
*/
2024-10-30 17:11:23 +00:00
private $db;
2024-11-25 14:09:47 +00:00
/**
* Host constructor.
*
* @param Database $database The database connection object.
*/
2024-10-30 17:11:23 +00:00
public function __construct($database) {
$this->db = $database->getConnection();
}
2024-11-25 14:09:47 +00:00
/**
* Get details of a specified host ID (or all hosts) in a specified platform ID.
*
* @param string $platform_id The platform ID to filter the hosts by (optional).
* @param string $host_id The host ID to filter the details (optional).
2024-11-26 14:17:41 +00:00
*
2024-11-25 14:09:47 +00:00
* @return array The details of the host(s) in the form of an associative array.
*/
2024-10-30 17:11:23 +00:00
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);
}
2024-11-25 14:09:47 +00:00
/**
* Add a new host to the database.
*
* @param array $newHost An associative array containing the details of the host to be added.
2024-11-26 14:17:41 +00:00
*
2024-11-25 14:09:47 +00:00
* @return bool True if the host was added successfully, otherwise false.
*/
2024-10-30 17:11:23 +00:00
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();
}
}
2024-11-25 14:09:47 +00:00
/**
* Edit an existing host in the database.
*
* @param string $platform_id The platform ID to which the host belongs.
* @param array $updatedHost An associative array containing the updated details of the host.
2024-11-26 14:17:41 +00:00
*
2024-11-25 14:09:47 +00:00
* @return bool True if the host was updated successfully, otherwise false.
*/
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,
2024-10-31 09:51:16 +00:00
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:51:16 +00:00
':id' => $updatedHost['id'],
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();
}
}
2024-11-25 14:09:47 +00:00
/**
* Delete a host from the database.
*
* @param int $host_id The ID of the host to be deleted.
2024-11-26 14:17:41 +00:00
*
2024-11-25 14:09:47 +00:00
* @return bool True if the host was deleted successfully, otherwise false.
*/
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();
}
}
}
?>