Lokasi:
/
home
/
u847703118
/
domains
/
alphamed.cloud
/
public_html
/
Ubah: units.php
<?php require('includes/auth_session.php'); require('db_connect.php'); // Access Control - matching sidebar logic (admin, med_tech, chief_med_tech) if (!userHasModule('system_units') && !userHasModule('system_import_tests') && !userCanAccess('tests', ['admin', 'med_tech', 'chief_med_tech'])) { header("Location: dashboard.php"); exit(); } $action = $_GET['action'] ?? 'list'; $message = ''; $error = ''; // Handle Form Submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = trim($_POST['name']); $description = trim($_POST['description']); if (empty($name)) { $error = "Unit Name is required."; } else { if ($action == 'add') { $stmt = $pdo->prepare("INSERT INTO units (name, description) VALUES (?, ?)"); try { if ($stmt->execute([$name, $description])) { if (function_exists('logAction')) { logAction($pdo, $_SESSION['user_id'], 'Add Unit', "Added unit: $name"); } header("Location: units.php?msg=added"); exit(); } else { $error = "Error adding unit."; } } catch (PDOException $e) { if ($e->getCode() == 23000) { $error = "Unit already exists."; } else { $error = "Database error: " . $e->getMessage(); } } } elseif ($action == 'edit') { $id = $_POST['id']; $stmt = $pdo->prepare("UPDATE units SET name=?, description=? WHERE id=?"); try { if ($stmt->execute([$name, $description, $id])) { if (function_exists('logAction')) { logAction($pdo, $_SESSION['user_id'], 'Edit Unit', "Updated unit: $name"); } header("Location: units.php?msg=updated"); exit(); } else { $error = "Error updating unit."; } } catch (PDOException $e) { if ($e->getCode() == 23000) { $error = "Unit name already exists."; } else { $error = "Database error: " . $e->getMessage(); } } } } } // Handle Delete if ($action == 'delete' && isset($_GET['id'])) { $id = $_GET['id']; // Get name for log $stmt = $pdo->prepare("SELECT name FROM units WHERE id = ?"); $stmt->execute([$id]); $u_name = $stmt->fetchColumn(); $stmt = $pdo->prepare("DELETE FROM units WHERE id = ?"); try { if ($stmt->execute([$id])) { if (function_exists('logAction')) { logAction($pdo, $_SESSION['user_id'], 'Delete Unit', "Deleted unit: " . ($u_name ?: $id)); } header("Location: units.php?msg=deleted"); exit(); } } catch (PDOException $e) { $error = "Cannot delete unit. It may be in use."; } } // Fetch for Edit $edit_item = null; if ($action == 'edit' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM units WHERE id = ?"); $stmt->execute([$_GET['id']]); $edit_item = $stmt->fetch(); } // Handle Seed Defaults if ($action == 'seed') { $standard_units = [ ['name' => 'g/dL', 'description' => 'Grams per deciliter (Hemoglobin)'], ['name' => '10^9/L', 'description' => 'Billions per liter (WBC, Platelets)'], ['name' => '10^12/L', 'description' => 'Trillions per liter (RBC)'], ['name' => '%', 'description' => 'Percentage'], ['name' => 'fL', 'description' => 'Femtoliter (MCV)'], ['name' => 'pg', 'description' => 'Picogram (MCH)'], ['name' => 'g/L', 'description' => 'Grams per liter'], ['name' => 'mg/dL', 'description' => 'Milligrams per deciliter (Chemistry)'], ['name' => 'mmol/L', 'description' => 'Millimoles per liter (Electrolytes)'], ['name' => 'U/L', 'description' => 'Units per liter (Enzymes)'], ['name' => 'ng/mL', 'description' => 'Nanograms per milliliter'], ['name' => 'μIU/mL', 'description' => 'Micro-International Units per milliliter (TSH)'], ['name' => 'μg/dL', 'description' => 'Micrograms per deciliter'], ['name' => 'sg', 'description' => 'Specific Gravity'], ['name' => 'pH', 'description' => 'pH Value'], ['name' => '/HPF', 'description' => 'Per High Power Field (Microscopy)'], ['name' => '/LPF', 'description' => 'Per Low Power Field (Microscopy)'], ['name' => 'seconds', 'description' => 'Time in seconds'], ['name' => 'INR', 'description' => 'International Normalized Ratio'], ['name' => 'mm/hr', 'description' => 'Millimeters per hour (ESR)'], ['name' => 'mL/min', 'description' => 'Milliliters per minute (GFR)'], ['name' => 'mEq/L', 'description' => 'Milliequivalents per liter'], ['name' => 'cells/uL', 'description' => 'Cells per microliter'], ['name' => 'IU/L', 'description' => 'International Units per liter'], ['name' => 'pg/mL', 'description' => 'Picograms per milliliter'], ['name' => 'ng/dL', 'description' => 'Nanograms per deciliter'] ]; $count = 0; // Use INSERT IGNORE or check existence to avoid duplicates if name is unique // Assuming name has a unique constraint. If not, we should check first. // Let's check if we can rely on unique constraint or just check manually. // To be safe, we'll check manually for each. $checkStmt = $pdo->prepare("SELECT COUNT(*) FROM units WHERE name = ?"); $insertStmt = $pdo->prepare("INSERT INTO units (name, description) VALUES (?, ?)"); foreach ($standard_units as $unit) { $checkStmt->execute([$unit['name']]); if ($checkStmt->fetchColumn() == 0) { try { $insertStmt->execute([$unit['name'], $unit['description']]); $count++; } catch (Exception $e) { // Ignore errors } } } if (function_exists('logAction')) { logAction($pdo, $_SESSION['user_id'], 'Seed Units', "Seeded $count standard units"); } header("Location: units.php?msg=seeded&count=$count"); exit(); } // Fetch All $stmt = $pdo->query("SELECT * FROM units ORDER BY name"); $units = $stmt->fetchAll(); ?> <?php include('includes/header.php'); ?> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>Units Management</h2> <?php if ($action == 'list'): ?> <div class="d-flex gap-2"> <a href="units.php?action=add" class="btn btn-primary"><i class="bi bi-plus"></i> Add Unit</a> <a href="units.php?action=seed" class="btn btn-outline-secondary" onclick="return confirm('Add standard medical units? Existing units will not be overwritten.')"><i class="bi bi-collection"></i> Populate Defaults</a> </div> <?php endif; ?> </div> <?php if (isset($_GET['msg'])): ?> <div class="alert alert-success"> <?php if ($_GET['msg'] == 'added') echo "Unit added successfully."; if ($_GET['msg'] == 'updated') echo "Unit updated successfully."; if ($_GET['msg'] == 'deleted') echo "Unit deleted successfully."; if ($_GET['msg'] == 'seeded') echo "Standard units seeded. Added " . (int)$_GET['count'] . " new units."; ?> </div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php endif; ?> <?php if ($action == 'add' || $action == 'edit'): ?> <div class="card"> <div class="card-header"><?php echo ucfirst($action); ?> Unit</div> <div class="card-body"> <form method="POST"> <?php if ($action == 'edit'): ?> <input type="hidden" name="id" value="<?php echo $edit_item['id']; ?>"> <?php endif; ?> <div class="mb-3"> <label class="form-label">Unit Name / Symbol</label> <input type="text" name="name" class="form-control" required value="<?php echo $edit_item['name'] ?? ''; ?>" placeholder="e.g. mg/dL"> </div> <div class="mb-3"> <label class="form-label">Description (Optional)</label> <textarea name="description" class="form-control" rows="3"><?php echo $edit_item['description'] ?? ''; ?></textarea> </div> <button type="submit" class="btn btn-success">Save</button> <a href="units.php" class="btn btn-secondary">Cancel</a> </form> </div> </div> <?php else: ?> <div class="card"> <div class="card-body"> <table class="table table-striped"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Description</th> <th>Actions</th> </tr> </thead> <tbody> <?php if (count($units) > 0): ?> <?php foreach ($units as $item): ?> <tr> <td><?php echo $item['id']; ?></td> <td><?php echo htmlspecialchars($item['name']); ?></td> <td><?php echo htmlspecialchars($item['description'] ?? ''); ?></td> <td> <a href="units.php?action=edit&id=<?php echo $item['id']; ?>" class="btn btn-sm btn-info text-white"><i class="bi bi-pencil-square"></i></a> <a href="units.php?action=delete&id=<?php echo $item['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure? This might affect tests using this unit.')"><i class="bi bi-trash"></i></a> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="4" class="text-center">No units found.</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> <?php endif; ?> <?php include('includes/footer.php'); ?>
Batal