The habitat management system handles both the creation and maintenance of animal habitats, as well as a suggestion system where veterinarians can propose improvements that require admin approval.
public function save(){ $id = $_POST['id_habitat'] ?? null; if ($id) { // UPDATE if (!hasPermission('habitats-edit')) { header('Location: /habitats/gest/start?msg=error'); exit; } $habitatModel->update($id, $name, $description); // Handle new images if ($urlMobile || $urlTablet || $urlDesktop) { // Unlink old media $mediaModel->unlink('habitats', $id); // Link new media $mediaModel->link($mediaId, 'habitats', $id); } }}
public function delete(){ if (!hasPermission('habitats-delete')) { header('Location: /habitats/gest/start?msg=error'); exit; } $id = $_GET['id'] ?? null; if ($id) { // Delete media relation first $mediaModel->unlink('habitats', $id); // Delete habitat $habitatModel->delete($id); } header('Location: /habitats/gest/start?msg=deleted'); exit;}
Deleting a habitat may affect animals assigned to that habitat. Ensure animals are reassigned before deletion or handle orphaned animals appropriately.
// In Habitat modelpublic function getAnimalsByHabitatId($habitatId){ $sql = "SELECT af.*, ag.animal_name, ag.gender, s.specie_name, c.category_name, n.nutrition_type FROM animal_full af JOIN animal_general ag ON af.animal_g_id = ag.id_animal_g LEFT JOIN specie s ON ag.specie_id = s.id_specie LEFT JOIN category c ON s.category_id = c.id_category LEFT JOIN nutrition n ON af.nutrition_id = n.id_nutrition WHERE af.habitat_id = :habitat_id ORDER BY ag.animal_name ASC"; $stmt = $this->db->prepare($sql); $stmt->execute([':habitat_id' => $habitatId]); return $stmt->fetchAll(PDO::FETCH_OBJ);}
Public page at /habitats/pages/habitat1?id=X displays:
public function habitat1() { $id = $_GET['id'] ?? null; // Get habitat details $habitat = $habitatModel->getById($id); // Get animals in this habitat $animals = $habitatModel->getAnimalsByHabitatId($id); // Get latest health state for each animal $healthReportModel = new HealthStateReport(); foreach ($animals as $animal) { $latestReport = $healthReportModel->getLatestByAnimalId( $animal->id_full_animal ); $animal->latest_health_state = $latestReport->hsr_state ?? null; } // Load view include_once __DIR__ . '/../views/pages/habitat1.php';}
Only users with the Veterinary role can create suggestions:
public function create(){ $userRoleName = $_SESSION['user']['role_name'] ?? null; if ($userRoleName !== 'Veterinary') { header('Location: /habitats/suggestion/start?msg=error'); exit; } // Show creation form include_once __DIR__ . '/../views/suggestion/create.php';}
2
Save suggestion as pending
public function save(){ // Verify CSRF token if (!csrf_verify('habitat_suggestion_save')) { header('Location: /habitats/suggestion/create?msg=error'); exit; } $habitatId = $_POST['habitat_id'] ?? null; $details = trim($_POST['details'] ?? ''); $userId = $_SESSION['user']['id_user'] ?? null; // Create with status 'pending' $suggestionModel->create($habitatId, $userId, $details);}
3
Admin reviews suggestion
Admins can accept or reject:
public function review(){ $userRoleName = $_SESSION['user']['role_name'] ?? null; if ($userRoleName !== 'Admin') { header('Location: /habitats/suggestion/start?msg=error'); exit; } // Verify CSRF if (!csrf_verify('habitat_suggestion_review')) { header('Location: /habitats/suggestion/start?msg=error'); exit; } $id = $_POST['id_hab_suggestion'] ?? null; $status = $_POST['status'] ?? null; // 'accepted' or 'rejected' $userId = $_SESSION['user']['id_user'] ?? null; $suggestionModel->review($id, $status, $userId);}