178 lines
7.4 KiB
PHP
178 lines
7.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
$errors = [];
|
|
$flash = null;
|
|
|
|
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_country'])) {
|
|
$countryNameEn = trim($_POST['country_name_en'] ?? '');
|
|
$countryNameAr = trim($_POST['country_name_ar'] ?? '');
|
|
if ($countryNameEn === '') {
|
|
$errors[] = 'Country name (English) is required.';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO countries (name_en, name_ar) VALUES (?, ?)");
|
|
$stmt->execute([$countryNameEn, $countryNameAr !== '' ? $countryNameAr : null]);
|
|
$flash = 'Country added.';
|
|
} catch (Throwable $e) {
|
|
$errors[] = 'Country already exists or could not be saved.';
|
|
}
|
|
}
|
|
} elseif (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.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$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, {$countryNameExprNoAlias} AS display_name FROM countries ORDER BY display_name ASC")->fetchAll();
|
|
$cities = db()->query(
|
|
"SELECT
|
|
{$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 30"
|
|
)->fetchAll();
|
|
|
|
render_header('Manage Locations', 'admin');
|
|
?>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-3">
|
|
<?php render_admin_sidebar('locations'); ?>
|
|
</div>
|
|
<div class="col-lg-9">
|
|
<div class="page-intro">
|
|
<h1 class="section-title mb-1">Country & city setup</h1>
|
|
<p class="muted mb-0">Define allowed origin and destination options for shipments.</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="row g-4">
|
|
<div class="col-lg-6">
|
|
<div class="panel p-4 h-100">
|
|
<h2 class="h5 mb-3">Add country</h2>
|
|
<form method="post">
|
|
<div class="mb-3">
|
|
<label class="form-label" for="country_name_en">Country name (EN)</label>
|
|
<input id="country_name_en" type="text" name="country_name_en" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label" for="country_name_ar">Country name (AR)</label>
|
|
<input id="country_name_ar" type="text" name="country_name_ar" class="form-control">
|
|
</div>
|
|
<button type="submit" name="add_country" class="btn btn-primary">Add country</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-6">
|
|
<div class="panel p-4 h-100">
|
|
<h2 class="h5 mb-3">Add city</h2>
|
|
<form method="post">
|
|
<div class="mb-3">
|
|
<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="mb-3">
|
|
<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="mb-3">
|
|
<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>
|
|
<button type="submit" name="add_city" class="btn btn-primary">Add city</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="panel p-4 mt-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<h2 class="h5 mb-0">Recently added cities</h2>
|
|
<a class="btn btn-sm btn-outline-dark" href="<?= e(url_with_lang('admin_dashboard.php')) ?>">Back to admin</a>
|
|
</div>
|
|
<?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>Country</th>
|
|
<th>City</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($cities as $city): ?>
|
|
<tr>
|
|
<td><?= e($city['country_name']) ?></td>
|
|
<td><?= e($city['city_name']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php render_footer(); ?>
|