| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-11-29 16:38:49 +00:00
										 |  |  |  * class Platform | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Handles platform management in the database, including retrieving, adding, editing, and deleting platforms. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  | class Platform { | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var PDO|null $db The database connection instance. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     private $db; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Platform constructor. | 
					
						
							| 
									
										
										
										
											2024-11-29 16:47:18 +00:00
										 |  |  |      * Initializes the database connection. | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2024-11-29 16:47:18 +00:00
										 |  |  |      * @param object $database The database object to initialize the connection. | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     public function __construct($database) { | 
					
						
							|  |  |  |         $this->db = $database->getConnection(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 17:06:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Retrieve details of a specific platform or all platforms. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $platform_id The ID of the platform to retrieve details for (optional). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array An associative array containing platform details. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     public function getPlatformDetails($platform_id = '') { | 
					
						
							|  |  |  |         $sql = 'SELECT * FROM platforms'; | 
					
						
							|  |  |  |         if ($platform_id !== '') { | 
					
						
							|  |  |  |             $sql .= ' WHERE id = :platform_id'; | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->bindParam(':platform_id', $platform_id); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $query->execute(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $query->fetchAll(PDO::FETCH_ASSOC); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 17:06:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Add a new platform to the database. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $newPlatform An associative array containing the details of the new platform: | 
					
						
							|  |  |  |      *                           - `name` (string): The name of the platform. | 
					
						
							|  |  |  |      *                           - `jitsi_url` (string): The URL for the Jitsi integration. | 
					
						
							|  |  |  |      *                           - `jilo_database` (string): The database name for Jilo integration. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|string True if the platform was added successfully, or an error message on failure. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     public function addPlatform($newPlatform) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $sql = 'INSERT INTO platforms | 
					
						
							|  |  |  |                     (name, jitsi_url, jilo_database) | 
					
						
							|  |  |  |                     VALUES | 
					
						
							|  |  |  |                     (:name, :jitsi_url, :jilo_database)'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->execute([ | 
					
						
							|  |  |  |                 ':name'			=> $newPlatform['name'], | 
					
						
							|  |  |  |                 ':jitsi_url'		=> $newPlatform['jitsi_url'], | 
					
						
							|  |  |  |                 ':jilo_database'	=> $newPlatform['jilo_database'], | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return $e->getMessage(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 17:06:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Edit an existing platform in the database. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param int $platform_id The ID of the platform to update. | 
					
						
							|  |  |  |      * @param array $updatedPlatform An associative array containing the updated platform details: | 
					
						
							|  |  |  |      *                               - `name` (string): The updated name of the platform. | 
					
						
							|  |  |  |      *                               - `jitsi_url` (string): The updated Jitsi URL. | 
					
						
							|  |  |  |      *                               - `jilo_database` (string): The updated Jilo database name. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|string True if the platform was updated successfully, or an error message on failure. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     public function editPlatform($platform_id, $updatedPlatform) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $sql = 'UPDATE platforms SET | 
					
						
							|  |  |  |                         name = :name, | 
					
						
							|  |  |  |                         jitsi_url = :jitsi_url, | 
					
						
							|  |  |  |                         jilo_database = :jilo_database | 
					
						
							|  |  |  |                     WHERE | 
					
						
							|  |  |  |                         id = :platform_id'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->execute([ | 
					
						
							|  |  |  |                 ':name'			=> $updatedPlatform['name'], | 
					
						
							|  |  |  |                 ':jitsi_url'		=> $updatedPlatform['jitsi_url'], | 
					
						
							|  |  |  |                 ':jilo_database'	=> $updatedPlatform['jilo_database'], | 
					
						
							|  |  |  |                 ':platform_id'		=> $platform_id, | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return $e->getMessage(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 17:06:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-26 14:16:16 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Delete a platform from the database. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param int $platform_id The ID of the platform to delete. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool|string True if the platform was deleted successfully, or an error message on failure. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |     public function deletePlatform($platform_id) { | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  |             $this->db->beginTransaction(); | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  |             // First, get all hosts in this platform
 | 
					
						
							|  |  |  |             $sql = 'SELECT id FROM hosts WHERE platform_id = :platform_id'; | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->bindParam(':platform_id', $platform_id); | 
					
						
							|  |  |  |             $query->execute(); | 
					
						
							|  |  |  |             $hosts = $query->fetchAll(PDO::FETCH_ASSOC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Delete all agents for each host
 | 
					
						
							|  |  |  |             foreach ($hosts as $host) { | 
					
						
							|  |  |  |                 $sql = 'DELETE FROM jilo_agents WHERE host_id = :host_id'; | 
					
						
							|  |  |  |                 $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |                 $query->bindParam(':host_id', $host['id']); | 
					
						
							|  |  |  |                 $query->execute(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Delete all hosts in this platform
 | 
					
						
							|  |  |  |             $sql = 'DELETE FROM hosts WHERE platform_id = :platform_id'; | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->bindParam(':platform_id', $platform_id); | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  |             $query->execute(); | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  |             // Finally, delete the platform
 | 
					
						
							|  |  |  |             $sql = 'DELETE FROM platforms WHERE id = :platform_id'; | 
					
						
							|  |  |  |             $query = $this->db->prepare($sql); | 
					
						
							|  |  |  |             $query->bindParam(':platform_id', $platform_id); | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |             $query->execute(); | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $this->db->commit(); | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |             return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-01-22 22:26:40 +00:00
										 |  |  |             $this->db->rollBack(); | 
					
						
							| 
									
										
										
										
											2024-09-04 09:53:02 +00:00
										 |  |  |             return $e->getMessage(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |