187 lines
8.4 KiB
PHP
187 lines
8.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/layout.php'; require_role('admin');
|
|
|
|
$errors = [];
|
|
$flash = null;
|
|
$editCountryId = isset($_GET['edit_country']) ? (int)$_GET['edit_country'] : 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') { validate_csrf_token();
|
|
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['update_country'])) {
|
|
$countryId = (int)($_POST['country_id'] ?? 0);
|
|
$countryNameEn = trim($_POST['country_name_en'] ?? '');
|
|
$countryNameAr = trim($_POST['country_name_ar'] ?? '');
|
|
if ($countryId <= 0 || $countryNameEn === '') {
|
|
$errors[] = 'Country ID and English name are required.';
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("UPDATE countries SET name_en = ?, name_ar = ? WHERE id = ?");
|
|
$stmt->execute([$countryNameEn, $countryNameAr !== '' ? $countryNameAr : null, $countryId]);
|
|
$flash = 'Country updated.';
|
|
$editCountryId = 0;
|
|
} catch (Throwable $e) {
|
|
$errors[] = 'Country could not be updated.';
|
|
}
|
|
}
|
|
} elseif (isset($_POST['delete_country'])) {
|
|
$countryId = (int)($_POST['country_id'] ?? 0);
|
|
if ($countryId <= 0) {
|
|
$errors[] = 'Invalid country selected.';
|
|
} else {
|
|
$stmt = db()->prepare("DELETE FROM countries WHERE id = ?");
|
|
$stmt->execute([$countryId]);
|
|
$flash = 'Country deleted.';
|
|
$editCountryId = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
$countryNameExprNoAlias = $lang === 'ar'
|
|
? "COALESCE(NULLIF(name_ar, ''), name_en)"
|
|
: "COALESCE(NULLIF(name_en, ''), name_ar)";
|
|
$countries = db()->query("SELECT id, name_en, name_ar, {$countryNameExprNoAlias} AS display_name FROM countries ORDER BY display_name ASC")->fetchAll();
|
|
|
|
$editingCountry = null;
|
|
if ($editCountryId > 0) {
|
|
foreach ($countries as $country) {
|
|
if ((int)$country['id'] === $editCountryId) {
|
|
$editingCountry = $country;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
render_header('Manage Countries', 'admin', true);
|
|
?>
|
|
|
|
<div class="row g-0">
|
|
<div class="col-md-2 bg-white border-end min-vh-100">
|
|
<?php render_admin_sidebar('countries'); ?>
|
|
</div>
|
|
<div class="col-md-10 p-4">
|
|
<div class="page-intro">
|
|
<h1 class="section-title mb-1">Countries</h1>
|
|
<p class="muted mb-0">Manage the list of allowed countries for shipment routes.</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 country</h2>
|
|
<form method="post" class="row g-3"> <?= csrf_field() ?>
|
|
<div class="col-md-6">
|
|
<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="col-md-6">
|
|
<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>
|
|
<div class="col-12">
|
|
<button type="submit" name="add_country" class="btn btn-primary">Add country</button>
|
|
<a class="btn btn-outline-dark" href="<?= e(url_with_lang('admin_cities.php')) ?>">Go to cities</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="panel p-4 mt-4">
|
|
<h2 class="h5 mb-2">Countries list</h2>
|
|
|
|
<?php if ($editingCountry): ?>
|
|
<form method="post" class="row g-2 align-items-end mb-3"> <?= csrf_field() ?>
|
|
<input type="hidden" name="country_id" value="<?= e((string)$editingCountry['id']) ?>">
|
|
<div class="col-md-4">
|
|
<label class="form-label mb-1">Country (EN)</label>
|
|
<input class="form-control form-control-sm" type="text" name="country_name_en" value="<?= e($editingCountry['name_en']) ?>" required>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label mb-1">Country (AR)</label>
|
|
<input class="form-control form-control-sm" type="text" name="country_name_ar" value="<?= e((string)($editingCountry['name_ar'] ?? '')) ?>">
|
|
</div>
|
|
<div class="col-md-4 d-flex gap-2">
|
|
<button type="submit" name="update_country" class="btn btn-sm btn-primary">Save</button>
|
|
<a href="<?= e(url_with_lang('admin_countries.php')) ?>" class="btn btn-sm btn-outline-secondary">Cancel</a>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$countries): ?>
|
|
<p class="muted mb-0">No countries added yet.</p>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Country (EN)</th>
|
|
<th>Country (AR)</th>
|
|
<th class="text-end">Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($countries as $country): ?>
|
|
<tr>
|
|
<td><?= e((string)$country['id']) ?></td>
|
|
<td><?= e($country['name_en']) ?></td>
|
|
<td><?= e((string)($country['name_ar'] ?? '-')) ?></td>
|
|
<td class="text-end">
|
|
<a class="btn btn-sm p-1 border-0 bg-transparent text-primary" href="<?= e(url_with_lang('admin_countries.php', ['edit_country' => (int)$country['id']])) ?>">
|
|
<i class="bi bi-pencil"></i>
|
|
</a>
|
|
<form method="post" class="d-inline m-0 p-0" onsubmit="return confirm('Delete this country and its cities?');"> <?= csrf_field() ?>
|
|
<input type="hidden" name="country_id" value="<?= e((string)$country['id']) ?>">
|
|
<button type="submit" name="delete_country" class="btn btn-sm p-1 border-0 bg-transparent text-danger">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php render_footer(); ?>
|