227 lines
9.9 KiB
PHP
227 lines
9.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
$errors = [];
|
|
$flash = null;
|
|
$editCityId = isset($_GET['edit_city']) ? (int)$_GET['edit_city'] : 0;
|
|
|
|
db()->exec("
|
|
CREATE TABLE IF NOT EXISTS countries (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name_en VARCHAR(255) NOT NULL,
|
|
name_ar VARCHAR(255) DEFAULT NULL
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
");
|
|
|
|
db()->exec("
|
|
CREATE TABLE IF NOT EXISTS cities (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
country_id INT NOT NULL,
|
|
name_en VARCHAR(255) NOT NULL,
|
|
name_ar VARCHAR(255) DEFAULT NULL,
|
|
UNIQUE KEY uniq_city_country (country_id, name_en),
|
|
CONSTRAINT fk_cities_country FOREIGN KEY (country_id) REFERENCES countries(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
");
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['add_city'])) {
|
|
$countryId = (int)($_POST['country_id'] ?? 0);
|
|
$cityNameEn = trim($_POST['city_name_en'] ?? '');
|
|
$cityNameAr = trim($_POST['city_name_ar'] ?? '');
|
|
if ($countryId <= 0 || $cityNameEn === '') {
|
|
$errors[] = 'Please select a country and provide city name (English).';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO cities (country_id, name_en, name_ar) VALUES (?, ?, ?)");
|
|
$stmt->execute([$countryId, $cityNameEn, $cityNameAr !== '' ? $cityNameAr : null]);
|
|
$flash = 'City added.';
|
|
} catch (Throwable $e) {
|
|
$errors[] = 'City already exists or could not be saved.';
|
|
}
|
|
}
|
|
} elseif (isset($_POST['update_city'])) {
|
|
$cityId = (int)($_POST['city_id'] ?? 0);
|
|
$countryId = (int)($_POST['country_id'] ?? 0);
|
|
$cityNameEn = trim($_POST['city_name_en'] ?? '');
|
|
$cityNameAr = trim($_POST['city_name_ar'] ?? '');
|
|
if ($cityId <= 0 || $countryId <= 0 || $cityNameEn === '') {
|
|
$errors[] = 'City ID, country and English city name are required.';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("UPDATE cities SET country_id = ?, name_en = ?, name_ar = ? WHERE id = ?");
|
|
$stmt->execute([$countryId, $cityNameEn, $cityNameAr !== '' ? $cityNameAr : null, $cityId]);
|
|
$flash = 'City updated.';
|
|
$editCityId = 0;
|
|
} catch (Throwable $e) {
|
|
$errors[] = 'City could not be updated.';
|
|
}
|
|
}
|
|
} elseif (isset($_POST['delete_city'])) {
|
|
$cityId = (int)($_POST['city_id'] ?? 0);
|
|
if ($cityId <= 0) {
|
|
$errors[] = 'Invalid city selected.';
|
|
} else {
|
|
$stmt = db()->prepare("DELETE FROM cities WHERE id = ?");
|
|
$stmt->execute([$cityId]);
|
|
$flash = 'City deleted.';
|
|
$editCityId = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
$countryNameExpr = $lang === 'ar'
|
|
? "COALESCE(NULLIF(co.name_ar, ''), co.name_en)"
|
|
: "COALESCE(NULLIF(co.name_en, ''), co.name_ar)";
|
|
$countryNameExprNoAlias = $lang === 'ar'
|
|
? "COALESCE(NULLIF(name_ar, ''), name_en)"
|
|
: "COALESCE(NULLIF(name_en, ''), name_ar)";
|
|
$cityNameExpr = $lang === 'ar'
|
|
? "COALESCE(NULLIF(c.name_ar, ''), c.name_en)"
|
|
: "COALESCE(NULLIF(c.name_en, ''), c.name_ar)";
|
|
|
|
$countries = db()->query("SELECT id, name_en, name_ar, {$countryNameExprNoAlias} AS display_name FROM countries ORDER BY display_name ASC")->fetchAll();
|
|
$cities = db()->query(
|
|
"SELECT
|
|
c.id,
|
|
c.country_id,
|
|
c.name_en,
|
|
c.name_ar,
|
|
{$countryNameExpr} AS country_name,
|
|
{$cityNameExpr} AS city_name
|
|
FROM cities c
|
|
JOIN countries co ON co.id = c.country_id
|
|
ORDER BY country_name ASC, city_name ASC
|
|
LIMIT 200"
|
|
)->fetchAll();
|
|
|
|
$editingCity = null;
|
|
if ($editCityId > 0) {
|
|
foreach ($cities as $city) {
|
|
if ((int)$city['id'] === $editCityId) {
|
|
$editingCity = $city;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
render_header('Manage Cities', 'admin', true);
|
|
?>
|
|
|
|
<div class="row g-0">
|
|
<div class="col-md-2 bg-white border-end min-vh-100">
|
|
<?php render_admin_sidebar('cities'); ?>
|
|
</div>
|
|
<div class="col-md-10 p-4">
|
|
<div class="page-intro">
|
|
<h1 class="section-title mb-1">Cities</h1>
|
|
<p class="muted mb-0">Manage cities and map each city to its country.</p>
|
|
</div>
|
|
|
|
<?php if ($flash): ?>
|
|
<div class="alert alert-success" data-auto-dismiss="true"><?= e($flash) ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($errors): ?>
|
|
<div class="alert alert-warning"><?= e(implode(' ', $errors)) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="panel p-4">
|
|
<h2 class="h5 mb-3">Add city</h2>
|
|
<form method="post" class="row g-3">
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="country_id">Country</label>
|
|
<select id="country_id" name="country_id" class="form-select" required>
|
|
<option value="">Select country</option>
|
|
<?php foreach ($countries as $country): ?>
|
|
<option value="<?= e($country['id']) ?>"><?= e($country['display_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="city_name_en">City name (EN)</label>
|
|
<input id="city_name_en" type="text" name="city_name_en" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="city_name_ar">City name (AR)</label>
|
|
<input id="city_name_ar" type="text" name="city_name_ar" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" name="add_city" class="btn btn-primary">Add city</button>
|
|
<a class="btn btn-outline-dark" href="<?= e(url_with_lang('admin_countries.php')) ?>">Go to countries</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="panel p-4 mt-4">
|
|
<h2 class="h5 mb-2">Cities list</h2>
|
|
|
|
<?php if ($editingCity): ?>
|
|
<form method="post" class="row g-2 align-items-end mb-3">
|
|
<input type="hidden" name="city_id" value="<?= e((string)$editingCity['id']) ?>">
|
|
<div class="col-md-3">
|
|
<label class="form-label mb-1">Country</label>
|
|
<select class="form-select form-select-sm" name="country_id" required>
|
|
<?php foreach ($countries as $country): ?>
|
|
<option value="<?= e((string)$country['id']) ?>" <?= (int)$country['id'] === (int)$editingCity['country_id'] ? 'selected' : '' ?>>
|
|
<?= e($country['display_name']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label mb-1">City (EN)</label>
|
|
<input class="form-control form-control-sm" type="text" name="city_name_en" value="<?= e($editingCity['name_en']) ?>" required>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label mb-1">City (AR)</label>
|
|
<input class="form-control form-control-sm" type="text" name="city_name_ar" value="<?= e((string)($editingCity['name_ar'] ?? '')) ?>">
|
|
</div>
|
|
<div class="col-md-3 d-flex gap-2">
|
|
<button type="submit" name="update_city" class="btn btn-sm btn-primary">Save</button>
|
|
<a href="<?= e(url_with_lang('admin_cities.php')) ?>" class="btn btn-sm btn-outline-secondary">Cancel</a>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$cities): ?>
|
|
<p class="muted mb-0">No cities added yet.</p>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Country</th>
|
|
<th>City (EN)</th>
|
|
<th>City (AR)</th>
|
|
<th class="text-end">Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($cities as $city): ?>
|
|
<tr>
|
|
<td><?= e((string)$city['id']) ?></td>
|
|
<td><?= e($city['country_name']) ?></td>
|
|
<td><?= e($city['name_en']) ?></td>
|
|
<td><?= e((string)($city['name_ar'] ?? '-')) ?></td>
|
|
<td class="text-end">
|
|
<a class="btn btn-sm btn-outline-primary" href="<?= e(url_with_lang('admin_cities.php', ['edit_city' => (int)$city['id']])) ?>">Edit</a>
|
|
<form method="post" class="d-inline" onsubmit="return confirm('Delete this city?');">
|
|
<input type="hidden" name="city_id" value="<?= e((string)$city['id']) ?>">
|
|
<button type="submit" name="delete_city" class="btn btn-sm btn-outline-danger">Del</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php render_footer(); ?>
|