268 lines
16 KiB
PHP
268 lines
16 KiB
PHP
<?php
|
|
// admin_plans.php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
|
|
$action = $_GET['action'] ?? 'list';
|
|
$id = (int)($_GET['id'] ?? 0);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$post_action = $_POST['action'] ?? $action;
|
|
$post_id = (int)($_POST['id'] ?? $id);
|
|
|
|
if ($post_action === 'delete' && $post_id > 0) {
|
|
$stmt = db()->prepare("DELETE FROM plans WHERE id = ?");
|
|
$stmt->execute([$post_id]);
|
|
header('Location: ' . app_url('admin.php', ['page' => 'plans']));
|
|
exit;
|
|
}
|
|
|
|
if ($post_action === 'edit' || $post_action === 'add') {
|
|
$plan_key = $_POST['plan_key'] ?? '';
|
|
$name_en = $_POST['name_en'] ?? '';
|
|
$name_ar = $_POST['name_ar'] ?? '';
|
|
$price_monthly = (float)($_POST['price_monthly'] ?? 0);
|
|
$price_yearly = (float)($_POST['price_yearly'] ?? 0);
|
|
$subjects_limit = (int)($_POST['subjects_limit'] ?? 1);
|
|
|
|
$features_en_raw = $_POST['features_en'] ?? '';
|
|
$features_ar_raw = $_POST['features_ar'] ?? '';
|
|
|
|
// Convert multiline text into JSON array
|
|
$features_en = array_values(array_filter(array_map('trim', explode("\n", $features_en_raw))));
|
|
$features_ar = array_values(array_filter(array_map('trim', explode("\n", $features_ar_raw))));
|
|
|
|
$features_en_json = json_encode($features_en, JSON_UNESCAPED_UNICODE);
|
|
$features_ar_json = json_encode($features_ar, JSON_UNESCAPED_UNICODE);
|
|
|
|
if ($post_action === 'edit' && $post_id > 0) {
|
|
$stmt = db()->prepare("UPDATE plans SET plan_key=?, name_en=?, name_ar=?, price_monthly=?, price_yearly=?, subjects_limit=?, features_en=?, features_ar=? WHERE id=?");
|
|
$stmt->execute([$plan_key, $name_en, $name_ar, $price_monthly, $price_yearly, $subjects_limit, $features_en_json, $features_ar_json, $post_id]);
|
|
} else {
|
|
$stmt = db()->prepare("INSERT INTO plans (plan_key, name_en, name_ar, price_monthly, price_yearly, subjects_limit, features_en, features_ar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$plan_key, $name_en, $name_ar, $price_monthly, $price_yearly, $subjects_limit, $features_en_json, $features_ar_json]);
|
|
}
|
|
header('Location: ' . app_url('admin.php', ['page' => 'plans']));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// list view
|
|
$search = $_GET['search'] ?? '';
|
|
$page_num = max(1, (int)($_GET['p'] ?? 1));
|
|
$limit = 10;
|
|
$offset = ($page_num - 1) * $limit;
|
|
|
|
$where = "";
|
|
$params = [];
|
|
if ($search !== '') {
|
|
$where = "WHERE name_en LIKE ? OR name_ar LIKE ? OR plan_key LIKE ?";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
}
|
|
|
|
$total_stmt = db()->prepare("SELECT COUNT(*) FROM plans $where");
|
|
$total_stmt->execute($params);
|
|
$total = $total_stmt->fetchColumn();
|
|
$pages = ceil($total / $limit);
|
|
|
|
$stmt = db()->prepare("SELECT * FROM plans $where ORDER BY id DESC LIMIT $limit OFFSET $offset");
|
|
$stmt->execute($params);
|
|
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
<div class="section-header mb-4 d-flex justify-content-between align-items-center">
|
|
<div>
|
|
<h1 class="section-title mb-2"><?= h(t('Plans', 'الخطط')) ?></h1>
|
|
</div>
|
|
<div class="d-flex flex-wrap gap-2 justify-content-end">
|
|
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addPlanModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Plan', 'إضافة خطة')) ?></button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="panel-card mb-4">
|
|
<form method="get" class="d-flex gap-2 align-items-center">
|
|
<input type="hidden" name="page" value="plans">
|
|
<input type="text" name="search" class="form-control w-auto" placeholder="<?= h(t('Search...', 'بحث...')) ?>" value="<?= h($search) ?>">
|
|
<button type="submit" class="btn btn-outline-secondary"><?= h(t('Filter', 'تصفية')) ?></button>
|
|
<?php if ($search): ?>
|
|
<a href="<?= h(app_url('admin.php', ['page'=>'plans'])) ?>" class="btn btn-link text-secondary text-decoration-none"><?= h(t('Clear', 'مسح')) ?></a>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="panel-card">
|
|
<div class="table-responsive">
|
|
<table class="table align-middle dashboard-table mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th><?= h(t('Key', 'المفتاح')) ?></th>
|
|
<th><?= h(t('Name', 'الاسم')) ?></th>
|
|
<th><?= h(t('Monthly', 'شهري')) ?></th>
|
|
<th><?= h(t('Yearly', 'سنوي')) ?></th>
|
|
<th><?= h(t('Subjects', 'المواد')) ?></th>
|
|
<th class="text-end"><?= h(t('Actions', 'إجراءات')) ?></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach($items as $row):
|
|
$f_en = json_decode($row['features_en'] ?: '[]', true);
|
|
$f_ar = json_decode($row['features_ar'] ?: '[]', true);
|
|
$f_en_text = is_array($f_en) ? implode("\n", $f_en) : '';
|
|
$f_ar_text = is_array($f_ar) ? implode("\n", $f_ar) : '';
|
|
?>
|
|
<tr>
|
|
<td><?= h((string)$row['id']) ?></td>
|
|
<td><span class="badge bg-light text-dark border"><?= h($row['plan_key']) ?></span></td>
|
|
<td>
|
|
<div class="fw-semibold"><?= h(current_lang() === 'ar' ? $row['name_ar'] : $row['name_en']) ?></div>
|
|
</td>
|
|
<td><?= h(format_price((float)$row['price_monthly'])) ?></td>
|
|
<td><?= h(format_price((float)$row['price_yearly'])) ?></td>
|
|
<td><?= h((string)$row['subjects_limit']) ?></td>
|
|
<td class="text-end">
|
|
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editPlanModal<?= $row['id'] ?>" title="<?= h(t('Edit', 'تعديل')) ?>">
|
|
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/></svg>
|
|
</button>
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'plans', 'action'=>'delete', 'id'=>$row['id']])) ?>" class="d-inline" onsubmit="return confirm('<?= h(t('Are you sure?', 'هل أنت متأكد؟')) ?>');">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger" title="<?= h(t('Delete', 'حذف')) ?>"><svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/></svg></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- Edit Modal for Row <?= $row['id'] ?> -->
|
|
<div class="modal fade" id="editPlanModal<?= $row['id'] ?>" tabindex="-1" aria-labelledby="editPlanModalLabel<?= $row['id'] ?>" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-lg">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header border-0 bg-dark-blue">
|
|
<h5 class="modal-title section-title" id="editPlanModalLabel<?= $row['id'] ?>"><?= h(t('Edit Plan', 'تعديل الخطة')) ?></h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'plans'])) ?>">
|
|
<input type="hidden" name="action" value="edit">
|
|
<input type="hidden" name="id" value="<?= $row['id'] ?>">
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Plan Key (Unique identifier e.g. plus, basic)', 'مفتاح الخطة (معرف فريد مثل plus, basic)')) ?></label>
|
|
<input type="text" name="plan_key" class="form-control" value="<?= h($row['plan_key']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (EN)', 'الاسم (إنجليزي)')) ?></label>
|
|
<input type="text" name="name_en" class="form-control" value="<?= h($row['name_en']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (AR)', 'الاسم (عربي)')) ?></label>
|
|
<input type="text" name="name_ar" class="form-control" value="<?= h($row['name_ar']) ?>" required>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Monthly Price', 'السعر الشهري')) ?></label>
|
|
<input type="number" step="0.001" name="price_monthly" class="form-control" value="<?= h($row['price_monthly']) ?>">
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Yearly Price', 'السعر السنوي')) ?></label>
|
|
<input type="number" step="0.001" name="price_yearly" class="form-control" value="<?= h($row['price_yearly']) ?>">
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Subjects Limit', 'حد المواد')) ?></label>
|
|
<input type="number" name="subjects_limit" class="form-control" value="<?= h($row['subjects_limit']) ?>">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Features (EN) - One per line', 'الميزات (EN) - ميزة في كل سطر')) ?></label>
|
|
<textarea name="features_en" class="form-control" rows="5"><?= h($f_en_text) ?></textarea>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Features (AR) - One per line', 'الميزات (AR) - ميزة في كل سطر')) ?></label>
|
|
<textarea name="features_ar" class="form-control" rows="5"><?= h($f_ar_text) ?></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="d-flex justify-content-end gap-2 mt-4">
|
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
|
|
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save Changes', 'حفظ التغييرات')) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- End Edit Modal -->
|
|
|
|
<?php endforeach; ?>
|
|
<?php if(!$items): ?>
|
|
<tr><td colspan="7" class="text-center text-secondary py-3"><?= h(t('No plans found.', 'لا توجد خطط.')) ?></td></tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($pages > 1): ?>
|
|
<nav class="mt-4">
|
|
<ul class="pagination justify-content-center">
|
|
<?php for($i=1; $i<=$pages; $i++):
|
|
$active_class = ($i === $page_num) ? ' active' : '';
|
|
?>
|
|
<li class="page-item<?= $active_class ?>">
|
|
<a class="page-link" href="<?= h(app_url('admin.php', ['page'=>'plans', 'p'=>$i, 'search'=>$search])) ?>"><?= $i ?></a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
</ul>
|
|
</nav>
|
|
<?php endif; ?>
|
|
|
|
<!-- Add Plan Modal -->
|
|
<div class="modal fade" id="addPlanModal" tabindex="-1" aria-labelledby="addPlanModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-lg">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header border-0 bg-dark-blue">
|
|
<h5 class="modal-title section-title" id="addPlanModalLabel"><?= h(t('Add Plan', 'إضافة خطة')) ?></h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'plans'])) ?>">
|
|
<input type="hidden" name="action" value="add">
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Plan Key (Unique identifier e.g. plus, basic)', 'مفتاح الخطة (معرف فريد مثل plus, basic)')) ?></label>
|
|
<input type="text" name="plan_key" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (EN)', 'الاسم (إنجليزي)')) ?></label>
|
|
<input type="text" name="name_en" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (AR)', 'الاسم (عربي)')) ?></label>
|
|
<input type="text" name="name_ar" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Monthly Price', 'السعر الشهري')) ?></label>
|
|
<input type="number" step="0.001" name="price_monthly" class="form-control" value="0.000">
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Yearly Price', 'السعر السنوي')) ?></label>
|
|
<input type="number" step="0.001" name="price_yearly" class="form-control" value="0.000">
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label"><?= h(t('Subjects Limit', 'حد المواد')) ?></label>
|
|
<input type="number" name="subjects_limit" class="form-control" value="1">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Features (EN) - One per line', 'الميزات (EN) - ميزة في كل سطر')) ?></label>
|
|
<textarea name="features_en" class="form-control" rows="5"></textarea>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Features (AR) - One per line', 'الميزات (AR) - ميزة في كل سطر')) ?></label>
|
|
<textarea name="features_ar" class="form-control" rows="5"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="d-flex justify-content-end gap-2 mt-4">
|
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
|
|
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save', 'حفظ')) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- End Add Modal -->
|