diff --git a/add_visit.php b/add_visit.php index 66abc4b..90d2922 100644 --- a/add_visit.php +++ b/add_visit.php @@ -17,8 +17,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } else { try { $pdo = db(); - $stmt = $pdo->prepare('INSERT INTO visits (client_name, latitude, longitude) VALUES (?, ?, ?)'); - $stmt->execute([$client_name, $latitude, $longitude]); + $stmt = $pdo->prepare('INSERT INTO visits (client_name, latitude, longitude, user_id) VALUES (?, ?, ?, ?)'); + $stmt->execute([$client_name, $latitude, $longitude, $_SESSION['user_id']]); $success_message = 'Visit captured successfully!'; } catch (PDOException $e) { $error_message = 'Database error: ' . $e->getMessage(); diff --git a/admin/create_user.php b/admin/create_user.php new file mode 100644 index 0000000..3d5f5e9 --- /dev/null +++ b/admin/create_user.php @@ -0,0 +1,91 @@ +prepare("SELECT id FROM users WHERE username = ?"); + $stmt->execute([$username]); + if ($stmt->fetch()) { + $error_message = 'Username already taken. Please choose another.'; + } else { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + $insert_stmt = $pdo->prepare("INSERT INTO users (username, password, role_id, branch) VALUES (?, ?, ?, ?)"); + if ($insert_stmt->execute([$username, $hashed_password, $role_id, $branch])) { + header('Location: index.php?success=create'); + exit; + } else { + $error_message = 'Failed to create user.'; + } + } + } +} + +// Fetch all roles +$roles_stmt = $pdo->query("SELECT id, name FROM roles"); +$roles = $roles_stmt->fetchAll(PDO::FETCH_ASSOC); + +?> + + + + + + Create User + + + +
+

Create New User

+ + +
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + Cancel +
+
+
+
+ + diff --git a/admin/delete_user.php b/admin/delete_user.php new file mode 100644 index 0000000..cd4dda2 --- /dev/null +++ b/admin/delete_user.php @@ -0,0 +1,26 @@ +prepare("DELETE FROM users WHERE id = ?"); + $stmt->execute([$user_id]); +} + +header('Location: index.php?success=delete'); +exit; +?> diff --git a/admin/edit_user.php b/admin/edit_user.php new file mode 100644 index 0000000..d6d5be2 --- /dev/null +++ b/admin/edit_user.php @@ -0,0 +1,86 @@ +prepare("UPDATE users SET username = ?, role_id = ?, branch = ? WHERE id = ?"); + if ($update_stmt->execute([$username, $role_id, $branch, $user_id])) { + header('Location: index.php?success=1'); + exit; + } else { + $error_message = "Failed to update user."; + } +} + +// Fetch user data +$stmt = $pdo->prepare("SELECT id, username, role_id, branch FROM users WHERE id = ?"); +$stmt->execute([$user_id]); +$user = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$user) { + header('Location: admin/index.php'); + exit; +} + +// Fetch all roles +$roles_stmt = $pdo->query("SELECT id, name FROM roles"); +$roles = $roles_stmt->fetchAll(PDO::FETCH_ASSOC); + +?> + + + + + + Edit User + + + +
+

Edit User:

+ +
+
+
+
+ + +
+
+ + +
+
+ + +
+ + Cancel +
+
+
+
+ + diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..effa37f --- /dev/null +++ b/admin/index.php @@ -0,0 +1,130 @@ +prepare($sql); +$stmt->execute($params); +$users = $stmt->fetchAll(PDO::FETCH_ASSOC); + +// Fetch all unique roles for the filter dropdown +$rolesStmt = db()->query("SELECT name FROM roles ORDER BY name"); +$roles = $rolesStmt->fetchAll(PDO::FETCH_COLUMN); + +?> + + + + + + Admin - User Management + + + +
+
+

User Management

+ Create User +
+ + +
+
+ Search & Filter +
+
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ + +
+
+ All Users +
+
+ + + + + + + + + + + + + + + + + + + + + + + + +
UsernameRoleBranchActions
No users found.
+ Edit + Delete +
+
+
+ Back to Dashboard +
+ + diff --git a/auth.php b/auth.php index c350c04..b87614d 100644 --- a/auth.php +++ b/auth.php @@ -45,3 +45,7 @@ function require_role($role_names) { exit(); } } + +function isAdmin() { + return has_role('Admin'); +} diff --git a/dashboard.php b/dashboard.php new file mode 100644 index 0000000..4ec6939 --- /dev/null +++ b/dashboard.php @@ -0,0 +1,26 @@ +prepare('SELECT status, COUNT(*) as count FROM visits GROUP BY status'); + $stmt->execute(); + $results = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); + + $labels = array_keys($results); + $data = array_values($results); + + return [ + 'labels' => $labels, + 'data' => $data, + ]; +} + +$response = [ + 'visitsByStatus' => getVisitsByStatus($pdo), +]; + +echo json_encode($response); diff --git a/db/migrations/004_add_branch_and_user_to_visits.sql b/db/migrations/004_add_branch_and_user_to_visits.sql new file mode 100644 index 0000000..8217684 --- /dev/null +++ b/db/migrations/004_add_branch_and_user_to_visits.sql @@ -0,0 +1,3 @@ +ALTER TABLE `users` ADD `branch` VARCHAR(255) NULL; +ALTER TABLE `visits` ADD `user_id` INT NULL; +ALTER TABLE `visits` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`); diff --git a/index.php b/index.php index c22f16e..2719f96 100644 --- a/index.php +++ b/index.php @@ -2,13 +2,31 @@ require_once 'auth.php'; require_login(); -$current_role = current_user_role(); - -// Get username for display $pdo = db(); -$stmt = $pdo->prepare('SELECT username FROM users WHERE id = ?'); +$branch_update_message = ''; + +// Handle branch update +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['branch'])) { + $branch = trim($_POST['branch']); + if (!empty($branch)) { + $stmt = $pdo->prepare('UPDATE users SET branch = ? WHERE id = ?'); + if ($stmt->execute([$branch, $_SESSION['user_id']])) { + $branch_update_message = 'Branch updated successfully! Refreshing...'; + // Redirect to refresh the page and clear POST data + header("Refresh: 2; url=index.php"); + } else { + $branch_update_message = 'Error updating branch.'; + } + } +} + +// Get user info +$stmt = $pdo->prepare('SELECT username, branch FROM users WHERE id = ?'); $stmt->execute([$_SESSION['user_id']]); -$username = $stmt->fetchColumn(); +$user = $stmt->fetch(PDO::FETCH_ASSOC); +$username = $user['username'] ?? 'User'; +$branch = $user['branch'] ?? null; +$current_role = current_user_role(); ?> @@ -18,6 +36,7 @@ $username = $stmt->fetchColumn(); <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'GeoVerify'); ?> +
Go » +
+
+
+
+

Visits by Status

+
+
+ +
- - -
-
-
- -
Review Visits
-

View, verify, or reject recorded client visits.

- Go » + + + + + prepare('SELECT status, COUNT(*) as count FROM visits WHERE user_id = ? GROUP BY status'); + $stmt->execute([$_SESSION['user_id']]); + ?> + query("SELECT u.branch, u.username, COUNT(v.id) AS visit_count FROM visits v JOIN users u ON v.user_id = u.id WHERE u.branch IS NOT NULL AND u.role_id = (SELECT id FROM roles WHERE name = 'Loan Officer') GROUP BY u.branch, u.username ORDER BY u.branch, u.username"); + $visits_by_officer = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC); + ?> +
+
+
+

Visits by Branch and Loan Officer

+
+ +

No visits recorded yet.

+ +
+ $officers): ?> +
+

+ +

+
+
+
    + +
  • + + visits +
  • + +
+
+
+
+ +
+ +
+
-
- -
+ +