update dashboard bug

This commit is contained in:
Flatlogic Bot 2026-03-22 03:47:43 +00:00
parent 07e733df85
commit 70cf52dd48
3 changed files with 78 additions and 31 deletions

View File

@ -1,13 +1,31 @@
<?php <?php
// Enable detailed error reporting for debugging 500 errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$section = 'dashboard'; $section = 'dashboard';
require_once __DIR__ . '/db/config.php'; require_once __DIR__ . '/db/config.php';
// Try to connect to DB first to catch connection errors early
try {
$db = db();
} catch (PDOException $e) {
die("Database Connection Error: " . $e->getMessage());
} catch (Exception $e) {
die("General Error: " . $e->getMessage());
}
// Now include helpers, which can use the existing $db connection
require_once __DIR__ . '/helpers.php'; require_once __DIR__ . '/helpers.php';
// Auth Check (Moved to top to prevent 500 error on unauthenticated access) // Auth Check
require_once __DIR__ . '/includes/auth.php'; require_once __DIR__ . '/includes/auth.php';
check_auth(); check_auth();
$db = db(); // $db is already set above, so no need to call db() again, but it's safe if we do.
// $db = db();
$lang = $_SESSION['lang']; $lang = $_SESSION['lang'];
require_once __DIR__ . '/includes/actions.php'; require_once __DIR__ . '/includes/actions.php';

View File

@ -11,7 +11,12 @@ function get_system_settings() {
if (!isset($db)) { if (!isset($db)) {
require_once __DIR__ . '/db/config.php'; require_once __DIR__ . '/db/config.php';
$local_db = db(); try {
$local_db = db();
} catch (Exception $e) {
// If DB connection fails, return empty settings instead of crashing
return [];
}
} else { } else {
$local_db = $db; $local_db = $db;
} }
@ -30,9 +35,13 @@ function get_system_settings() {
} }
function apply_timezone() { function apply_timezone() {
$s = get_system_settings(); try {
if (!empty($s['timezone'])) { $s = get_system_settings();
date_default_timezone_set($s['timezone']); if (!empty($s['timezone'])) {
date_default_timezone_set($s['timezone']);
}
} catch (Exception $e) {
// Ignore timezone errors
} }
} }
apply_timezone(); apply_timezone();
@ -45,7 +54,11 @@ function format_currency($amount) {
return $currency_symbol . ' ' . number_format((float)$amount, $decimal_digits); return $currency_symbol . ' ' . number_format((float)$amount, $decimal_digits);
} }
session_start(); // Only start session if not already started
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/lang.php'; require_once __DIR__ . '/lang.php';
if (!isset($_SESSION['lang'])) { if (!isset($_SESSION['lang'])) {
@ -56,19 +69,21 @@ if (isset($_GET['lang'])) {
if ($_GET['lang'] === 'ar' || $_GET['lang'] === 'en') { if ($_GET['lang'] === 'ar' || $_GET['lang'] === 'en') {
$_SESSION['lang'] = $_GET['lang']; $_SESSION['lang'] = $_GET['lang'];
// Redirect to remove lang param // Redirect to remove lang param
header("Location: " . strtok($_SERVER["REQUEST_URI"], '?')); if (!headers_sent()) {
exit; header("Location: " . strtok($_SERVER["REQUEST_URI"], '?'));
exit;
}
} }
} }
function __($key) { function __($key) {
global $translations; global $translations;
$lang = $_SESSION['lang']; $lang = $_SESSION['lang'] ?? 'en'; // Fallback if session is empty
return $translations[$lang][$key] ?? $key; return $translations[$lang][$key] ?? $key;
} }
function is_rtl() { function is_rtl() {
return $_SESSION['lang'] === 'ar'; return ($_SESSION['lang'] ?? 'en') === 'ar';
} }
function get_dir() { function get_dir() {
@ -76,11 +91,11 @@ function get_dir() {
} }
function get_lang_name() { function get_lang_name() {
return $_SESSION['lang'] === 'ar' ? 'English' : 'العربية'; return ($_SESSION['lang'] ?? 'en') === 'ar' ? 'English' : 'العربية';
} }
function get_lang_code() { function get_lang_code() {
return $_SESSION['lang'] === 'ar' ? 'en' : 'ar'; return $_SESSION['lang'] ?? 'en';
} }
function calculate_age($dob) { function calculate_age($dob) {

View File

@ -1,8 +1,24 @@
<?php <?php
session_start(); // Enable error reporting for debugging
require_once 'db/config.php'; ini_set('display_errors', 1);
require_once 'lang.php'; ini_set('display_startup_errors', 1);
require_once 'helpers.php'; error_reporting(E_ALL);
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/db/config.php';
// Initialize DB safely
try {
$db = db();
} catch (Exception $e) {
die("Database Connection Error: " . $e->getMessage());
}
require_once __DIR__ . '/lang.php';
require_once __DIR__ . '/helpers.php';
if (isset($_SESSION['user_id'])) { if (isset($_SESSION['user_id'])) {
header("Location: dashboard.php"); header("Location: dashboard.php");
@ -18,20 +34,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($email) || empty($password)) { if (empty($email) || empty($password)) {
$error = __('fill_all_fields'); $error = __('fill_all_fields');
} else { } else {
$db = db();
$stmt = $db->prepare("SELECT id, name, password, role_id, active FROM users WHERE email = ?"); $stmt = $db->prepare("SELECT id, name, password, role_id, active FROM users WHERE email = ?");
$stmt->execute([$email]); $stmt->execute([$email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC); $user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && $user['active']) { if ($user && $user['active']) {
// For the default seeded user, we used a specific hash.
// In a real app, use password_verify($password, $user['password'])
// For this demo/prototype environment where I manually inserted a hash:
if (password_verify($password, $user['password'])) { if (password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id']; $_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name']; $_SESSION['user_name'] = $user['name'];
// Update last login
$update = $db->prepare("UPDATE users SET last_login = NOW() WHERE id = ?"); $update = $db->prepare("UPDATE users SET last_login = NOW() WHERE id = ?");
$update->execute([$user['id']]); $update->execute([$user['id']]);
@ -47,18 +58,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
// Fetch site settings for branding // Fetch site settings for branding
$db = db(); try {
$stmt = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('company_name', 'company_logo')"); $stmt = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('company_name', 'company_logo')");
$settings = []; $settings = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$settings[$row['setting_key']] = $row['setting_value']; $settings[$row['setting_key']] = $row['setting_value'];
}
} catch (Exception $e) {
$settings = [];
} }
$site_name = !empty($settings['company_name']) ? $settings['company_name'] : 'Hospital Management'; $site_name = !empty($settings['company_name']) ? $settings['company_name'] : 'Hospital Management';
$site_logo = !empty($settings['company_logo']) ? $settings['company_logo'] : null; $site_logo = !empty($settings['company_logo']) ? $settings['company_logo'] : null;
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="<?php echo $_SESSION['lang']; ?>" dir="<?php echo get_dir(); ?>"> <html lang="<?php echo get_lang_code(); ?>" dir="<?php echo get_dir(); ?>">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">