34627-vm/api/get_offices.php
Flatlogic Bot 2be5f009ee dua
2025-10-03 14:19:02 +00:00

32 lines
969 B
PHP

<?php
// api/get_offices.php
header('Content-Type: application/json');
require_once '../db/config.php';
session_start();
// Authentication and Authorization check
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'super_admin') {
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
exit;
}
try {
$sql = "SELECT * FROM kantor ORDER BY nama_kantor ASC";
$params = [];
// If an ID is provided, fetch a single office
if (isset($_GET['id']) && !empty($_GET['id'])) {
$sql = "SELECT * FROM kantor WHERE id = ?";
$params[] = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
}
$stmt = db()->prepare($sql);
$stmt->execute($params);
$offices = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['success' => true, 'offices' => $offices]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>