Sindbad~EG File Manager
<?php
require_once '../config/config.php';
require_once '../classes/Location.php';
// Check if user is logged in and is admin or superuser
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['account_type'], ['admin', 'superuser'])) {
header('Location: ../index.php');
exit();
}
$db = new Database();
$pdo = $db->getConnection();
$location = new Location($pdo);
$message = '';
$messageType = '';
$action = $_GET['action'] ?? 'list';
$locationId = $_GET['id'] ?? null;
// Handle form submissions
if ($_POST) {
if ($action === 'create') {
$data = [
'name' => trim($_POST['name']),
'type' => $_POST['type'],
'parent_id' => !empty($_POST['parent_id']) ? $_POST['parent_id'] : null,
'address' => trim($_POST['address']),
'contact_person' => trim($_POST['contact_person']),
'contact_phone' => trim($_POST['contact_phone']),
'contact_email' => trim($_POST['contact_email']),
'description' => trim($_POST['description']),
'created_by' => $_SESSION['user_id']
];
// Validation
if (empty($data['name']) || empty($data['type'])) {
$message = 'Location name and type are required.';
$messageType = 'error';
} elseif ($location->nameExists($data['name'], $data['type'])) {
$message = 'A location with this name and type already exists.';
$messageType = 'error';
} else {
$result = $location->create($data);
if ($result) {
$message = 'Location created successfully.';
$messageType = 'success';
$action = 'list';
} else {
$message = 'Error creating location: ' . $location->getLastError();
$messageType = 'error';
}
}
} elseif ($action === 'edit' && $locationId) {
$data = [
'name' => trim($_POST['name']),
'type' => $_POST['type'],
'parent_id' => !empty($_POST['parent_id']) ? $_POST['parent_id'] : null,
'address' => trim($_POST['address']),
'contact_person' => trim($_POST['contact_person']),
'contact_phone' => trim($_POST['contact_phone']),
'contact_email' => trim($_POST['contact_email']),
'description' => trim($_POST['description']),
'status' => $_POST['status']
];
// Validation
if (empty($data['name']) || empty($data['type'])) {
$message = 'Location name and type are required.';
$messageType = 'error';
} elseif ($location->nameExists($data['name'], $data['type'], $locationId)) {
$message = 'A location with this name and type already exists.';
$messageType = 'error';
} else {
$result = $location->update($locationId, $data);
if ($result) {
$message = 'Location updated successfully.';
$messageType = 'success';
$action = 'list';
} else {
$message = 'Error updating location.';
$messageType = 'error';
}
}
}
}
// Handle delete action
if ($action === 'delete' && $locationId) {
$result = $location->delete($locationId);
if (is_array($result) && isset($result['error'])) {
$message = $result['error'];
$messageType = 'error';
} elseif ($result) {
$message = 'Location deleted successfully.';
$messageType = 'success';
} else {
$message = 'Error deleting location.';
$messageType = 'error';
}
$action = 'list';
}
// Get data based on action
$locations = [];
$currentLocation = null;
$parentLocations = [];
if ($action === 'list') {
$filters = [];
if (!empty($_GET['type'])) $filters['type'] = $_GET['type'];
if (!empty($_GET['status'])) $filters['status'] = $_GET['status'];
if (!empty($_GET['search'])) $filters['search'] = $_GET['search'];
$locations = $location->getAll($filters);
} elseif ($action === 'edit' && $locationId) {
$currentLocation = $location->getById($locationId);
if (!$currentLocation) {
$message = 'Location not found.';
$messageType = 'error';
$action = 'list';
}
}
// Get parent locations for dropdowns (areas for districts, districts for assemblies)
if (in_array($action, ['create', 'edit'])) {
$parentLocations = [
'area' => $location->getByType('area'),
'district' => $location->getByType('district')
];
}
$pageTitle = 'Location Management';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $pageTitle; ?> - COP News Portal</title>
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
<header class="header">
<nav class="navbar">
<a href="../dashboard.php" class="logo">
<i class="fas fa-church"></i>
COP News Portal
</a>
<ul class="nav-links">
<li><a href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
<li><a href="../news/index.php"><i class="fas fa-newspaper"></i> News</a></li>
<li><a href="index.php"><i class="fas fa-cog"></i> Admin</a></li>
<li><a href="../profile.php"><i class="fas fa-user"></i> Profile</a></li>
<li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
</ul>
</nav>
</header>
<main class="container" style="margin-top: 2rem;">
<?php if ($message): ?>
<div class="alert alert-<?php echo $messageType; ?>">
<i class="fas fa-info-circle"></i> <?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<?php if ($action === 'list'): ?>
<!-- Locations List -->
<div class="card">
<div class="card-header">
<div class="flex justify-between items-center">
<h1><i class="fas fa-map-marker-alt"></i> Manage Locations</h1>
<a href="?action=create" class="btn btn-primary">
<i class="fas fa-plus"></i> Add Location
</a>
</div>
</div>
<div class="card-body">
<!-- Filters -->
<div class="mb-3">
<form method="GET" class="flex gap-2 items-end">
<div class="form-group">
<label class="form-label">Type:</label>
<select name="type" class="form-control form-select">
<option value="">All Types</option>
<option value="area" <?php echo ($_GET['type'] ?? '') === 'area' ? 'selected' : ''; ?>>Area</option>
<option value="district" <?php echo ($_GET['type'] ?? '') === 'district' ? 'selected' : ''; ?>>District</option>
<option value="assembly" <?php echo ($_GET['type'] ?? '') === 'assembly' ? 'selected' : ''; ?>>Assembly</option>
</select>
</div>
<div class="form-group">
<label class="form-label">Status:</label>
<select name="status" class="form-control form-select">
<option value="">All Status</option>
<option value="active" <?php echo ($_GET['status'] ?? '') === 'active' ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo ($_GET['status'] ?? '') === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
</select>
</div>
<div class="form-group">
<label class="form-label">Search:</label>
<input type="text" name="search" class="form-control" value="<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>" placeholder="Search locations...">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Filter</button>
<a href="locations.php" class="btn btn-secondary">Clear</a>
</div>
</form>
</div>
<?php if (empty($locations)): ?>
<div class="text-center p-4">
<i class="fas fa-map-marker-alt" style="font-size: 4rem; color: var(--light-grey); margin-bottom: 1rem;"></i>
<h3>No locations found</h3>
<p style="color: var(--primary-grey);">Start by creating your first location.</p>
<a href="?action=create" class="btn btn-primary mt-2">
<i class="fas fa-plus"></i> Create Location
</a>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Parent Location</th>
<th>Contact Person</th>
<th>Contact Phone</th>
<th>Status</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($locations as $loc): ?>
<tr>
<td>
<strong><?php echo htmlspecialchars($loc['name']); ?></strong>
<?php if ($loc['address']): ?>
<br><small style="color: var(--primary-grey);"><?php echo htmlspecialchars($loc['address']); ?></small>
<?php endif; ?>
</td>
<td>
<span class="badge badge-<?php echo $loc['type']; ?>">
<?php echo ucfirst($loc['type']); ?>
</span>
</td>
<td><?php echo $loc['parent_name'] ? htmlspecialchars($loc['parent_name']) : '-'; ?></td>
<td><?php echo $loc['contact_person'] ? htmlspecialchars($loc['contact_person']) : '-'; ?></td>
<td><?php echo $loc['contact_phone'] ? htmlspecialchars($loc['contact_phone']) : '-'; ?></td>
<td>
<span class="badge badge-<?php echo $loc['status'] === 'active' ? 'active' : 'inactive'; ?>">
<?php echo ucfirst($loc['status']); ?>
</span>
</td>
<td><?php echo date('M j, Y', strtotime($loc['created_at'])); ?></td>
<td>
<div class="flex gap-1">
<a href="?action=edit&id=<?php echo $loc['id']; ?>"
class="btn btn-sm btn-secondary">
<i class="fas fa-edit"></i>
</a>
<a href="?action=delete&id=<?php echo $loc['id']; ?>"
class="btn btn-sm btn-danger"
onclick="return confirm('Are you sure you want to delete this location?')">
<i class="fas fa-trash"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<?php elseif ($action === 'create'): ?>
<!-- Create Location -->
<div class="card">
<div class="card-header">
<h1><i class="fas fa-plus"></i> Create Location</h1>
</div>
<div class="card-body">
<form method="POST" action="">
<div class="grid grid-2">
<div class="form-group">
<label for="name" class="form-label">Location Name *</label>
<input type="text" id="name" name="name" class="form-control"
value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>"
placeholder="Enter location name..." required>
</div>
<div class="form-group">
<label for="type" class="form-label">Location Type *</label>
<select id="type" name="type" class="form-control form-select" required onchange="updateParentOptions()">
<option value="">Select Type</option>
<option value="area" <?php echo ($_POST['type'] ?? '') === 'area' ? 'selected' : ''; ?>>Area</option>
<option value="district" <?php echo ($_POST['type'] ?? '') === 'district' ? 'selected' : ''; ?>>District</option>
<option value="assembly" <?php echo ($_POST['type'] ?? '') === 'assembly' ? 'selected' : ''; ?>>Assembly</option>
</select>
</div>
</div>
<div class="form-group">
<label for="parent_id" class="form-label">Parent Location</label>
<select id="parent_id" name="parent_id" class="form-control form-select">
<option value="">No Parent</option>
<!-- Options will be populated by JavaScript -->
</select>
</div>
<div class="form-group">
<label for="address" class="form-label">Address</label>
<textarea id="address" name="address" class="form-control" rows="3"
placeholder="Location address..."><?php echo htmlspecialchars($_POST['address'] ?? ''); ?></textarea>
</div>
<div class="grid grid-2">
<div class="form-group">
<label for="contact_person" class="form-label">Contact Person</label>
<input type="text" id="contact_person" name="contact_person" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_person'] ?? ''); ?>"
placeholder="Contact person name...">
</div>
<div class="form-group">
<label for="contact_phone" class="form-label">Contact Phone</label>
<input type="tel" id="contact_phone" name="contact_phone" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_phone'] ?? ''); ?>"
placeholder="Contact phone number...">
</div>
</div>
<div class="form-group">
<label for="contact_email" class="form-label">Contact Email</label>
<input type="email" id="contact_email" name="contact_email" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_email'] ?? ''); ?>"
placeholder="Contact email address...">
</div>
<div class="form-group">
<label for="description" class="form-label">Description</label>
<textarea id="description" name="description" class="form-control" rows="4"
placeholder="Location description..."><?php echo htmlspecialchars($_POST['description'] ?? ''); ?></textarea>
</div>
<div class="flex gap-2">
<button type="submit" class="btn btn-primary">
<i class="fas fa-save"></i> Create Location
</button>
<a href="locations.php" class="btn btn-secondary">
<i class="fas fa-times"></i> Cancel
</a>
</div>
</form>
</div>
</div>
<?php elseif ($action === 'edit' && $currentLocation): ?>
<!-- Edit Location -->
<div class="card">
<div class="card-header">
<h1><i class="fas fa-edit"></i> Edit Location</h1>
</div>
<div class="card-body">
<form method="POST" action="">
<div class="grid grid-2">
<div class="form-group">
<label for="name" class="form-label">Location Name *</label>
<input type="text" id="name" name="name" class="form-control"
value="<?php echo htmlspecialchars($_POST['name'] ?? $currentLocation['name']); ?>"
placeholder="Enter location name..." required>
</div>
<div class="form-group">
<label for="type" class="form-label">Location Type *</label>
<select id="type" name="type" class="form-control form-select" required onchange="updateParentOptions()">
<option value="">Select Type</option>
<option value="area" <?php echo ($_POST['type'] ?? $currentLocation['type']) === 'area' ? 'selected' : ''; ?>>Area</option>
<option value="district" <?php echo ($_POST['type'] ?? $currentLocation['type']) === 'district' ? 'selected' : ''; ?>>District</option>
<option value="assembly" <?php echo ($_POST['type'] ?? $currentLocation['type']) === 'assembly' ? 'selected' : ''; ?>>Assembly</option>
</select>
</div>
</div>
<div class="grid grid-2">
<div class="form-group">
<label for="parent_id" class="form-label">Parent Location</label>
<select id="parent_id" name="parent_id" class="form-control form-select">
<option value="">No Parent</option>
<!-- Options will be populated by JavaScript -->
</select>
</div>
<div class="form-group">
<label for="status" class="form-label">Status</label>
<select id="status" name="status" class="form-control form-select">
<option value="active" <?php echo ($_POST['status'] ?? $currentLocation['status']) === 'active' ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo ($_POST['status'] ?? $currentLocation['status']) === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
</select>
</div>
</div>
<div class="form-group">
<label for="address" class="form-label">Address</label>
<textarea id="address" name="address" class="form-control" rows="3"
placeholder="Location address..."><?php echo htmlspecialchars($_POST['address'] ?? $currentLocation['address']); ?></textarea>
</div>
<div class="grid grid-2">
<div class="form-group">
<label for="contact_person" class="form-label">Contact Person</label>
<input type="text" id="contact_person" name="contact_person" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_person'] ?? $currentLocation['contact_person']); ?>"
placeholder="Contact person name...">
</div>
<div class="form-group">
<label for="contact_phone" class="form-label">Contact Phone</label>
<input type="tel" id="contact_phone" name="contact_phone" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_phone'] ?? $currentLocation['contact_phone']); ?>"
placeholder="Contact phone number...">
</div>
</div>
<div class="form-group">
<label for="contact_email" class="form-label">Contact Email</label>
<input type="email" id="contact_email" name="contact_email" class="form-control"
value="<?php echo htmlspecialchars($_POST['contact_email'] ?? $currentLocation['contact_email']); ?>"
placeholder="Contact email address...">
</div>
<div class="form-group">
<label for="description" class="form-label">Description</label>
<textarea id="description" name="description" class="form-control" rows="4"
placeholder="Location description..."><?php echo htmlspecialchars($_POST['description'] ?? $currentLocation['description']); ?></textarea>
</div>
<div class="flex gap-2">
<button type="submit" class="btn btn-primary">
<i class="fas fa-save"></i> Update Location
</button>
<a href="locations.php" class="btn btn-secondary">
<i class="fas fa-times"></i> Cancel
</a>
</div>
</form>
</div>
</div>
<?php endif; ?>
</main>
<script>
// Parent location data from PHP
const parentLocations = <?php echo json_encode($parentLocations); ?>;
const currentParentId = <?php echo json_encode($currentLocation['parent_id'] ?? null); ?>;
function updateParentOptions() {
const typeSelect = document.getElementById('type');
const parentSelect = document.getElementById('parent_id');
const selectedType = typeSelect.value;
// Clear existing options
parentSelect.innerHTML = '<option value="">No Parent</option>';
if (selectedType === 'district' && parentLocations.area) {
parentLocations.area.forEach(area => {
const option = document.createElement('option');
option.value = area.id;
option.textContent = area.name;
if (area.id == currentParentId) option.selected = true;
parentSelect.appendChild(option);
});
} else if (selectedType === 'assembly' && parentLocations.district) {
parentLocations.district.forEach(district => {
const option = document.createElement('option');
option.value = district.id;
option.textContent = district.name;
if (district.id == currentParentId) option.selected = true;
parentSelect.appendChild(option);
});
}
}
// Initialize parent options on page load
document.addEventListener('DOMContentLoaded', function() {
updateParentOptions();
});
</script>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists