39496-vm/patch_admin.php
2026-04-06 17:25:15 +00:00

238 lines
14 KiB
PHP

<?php
$content = file_get_contents('admin_courses.php');
// 1. Add post actions
$post_actions_orig = " if (\$post_action === 'edit' || \$post_action === 'add') {";
$post_actions_new = <<<PHP
if (\$post_action === 'close_all_registration') {
db()->query("UPDATE courses SET registration_open = 0");
header('Location: ' . app_url('admin.php', ['page' => 'courses']));
exit;
}
if (\$post_action === 'open_all_registration') {
db()->query("UPDATE courses SET registration_open = 1");
header('Location: ' . app_url('admin.php', ['page' => 'courses']));
exit;
}
if (\$post_action === 'reset_all_students') {
db()->query("TRUNCATE TABLE course_students");
header('Location: ' . app_url('admin.php', ['page' => 'courses']));
exit;
}
if (\$post_action === 'edit' || \$post_action === 'add') {
PHP;
$content = str_replace($post_actions_orig, $post_actions_new, $content);
// 2. Add max_students and registration_open to post
$vars_orig = <<<PHP
$status = $_POST['status'] ?? 'active';
$price = (float)($_POST['price'] ?? 0);
PHP;
$vars_new = <<<PHP
$status = $_POST['status'] ?? 'active';
$price = (float)($_POST['price'] ?? 0);
$max_students = (isset($_POST['max_students']) && $_POST['max_students'] !== '') ? (int)$_POST['max_students'] : null;
$registration_open = isset($_POST['registration_open']) ? 1 : 0;
PHP;
$content = str_replace($vars_orig, $vars_new, $content);
// 3. Update query edit
$edit_orig = '$stmt = db()->prepare("UPDATE courses SET name_en=?, name_ar=?, description_en=?, description_ar=?, status=?, price=?, picture=? WHERE id=?");' . "\n" .
' $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture, $post_id]);';
$edit_new = '$stmt = db()->prepare("UPDATE courses SET name_en=?, name_ar=?, description_en=?, description_ar=?, status=?, price=?, picture=?, max_students=?, registration_open=? WHERE id=?");' . "\n" .
' $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture, $max_students, $registration_open, $post_id]);';
$content = str_replace($edit_orig, $edit_new, $content);
// 4. Update query add
$add_orig = '$stmt = db()->prepare("INSERT INTO courses (name_en, name_ar, description_en, description_ar, status, price, picture) VALUES (?, ?, ?, ?, ?, ?, ?)");' . "\n" .
' $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture]);';
$add_new = '$stmt = db()->prepare("INSERT INTO courses (name_en, name_ar, description_en, description_ar, status, price, picture, max_students, registration_open) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");' . "\n" .
' $stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $price, $picture, $max_students, $registration_open]);';
$content = str_replace($add_orig, $add_new, $content);
// 5. Add bulk buttons
$buttons_orig = <<<PHP
<div>
<h1 class="section-title mb-2"><?= h(t('Courses', 'الدورات')) ?></h1>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCourseModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Course', 'إضافة دورة')) ?></button>
</div>
<div class="panel-card mb-4">
PHP;
$buttons_new = <<<PHP
<div>
<h1 class="section-title mb-2"><?= h(t('Courses', 'الدورات')) ?></h1>
</div>
<div class="d-flex flex-wrap gap-2 justify-content-end">
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'courses'])) ?>" class="m-0" onsubmit="return confirm('<?= h(t('Close registration for all courses?', 'هل أنت متأكد من إغلاق التسجيل لجميع الدورات؟')) ?>');">
<input type="hidden" name="action" value="close_all_registration">
<button type="submit" class="btn btn-outline-warning btn-sm"><?= h(t('Close All', 'إغلاق الكل')) ?></button>
</form>
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'courses'])) ?>" class="m-0" onsubmit="return confirm('<?= h(t('Open registration for all courses?', 'هل أنت متأكد من فتح التسجيل لجميع الدورات؟')) ?>');">
<input type="hidden" name="action" value="open_all_registration">
<button type="submit" class="btn btn-outline-success btn-sm"><?= h(t('Open All', 'فتح الكل')) ?></button>
</form>
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'courses'])) ?>" class="m-0" onsubmit="return confirm('<?= h(t('Are you absolutely sure you want to remove ALL students from ALL courses to start a new batch?', 'هل أنت متأكد تماماً أنك تريد إزالة جميع الطلاب من جميع الدورات لبدء دفعة جديدة؟')) ?>');">
<input type="hidden" name="action" value="reset_all_students">
<button type="submit" class="btn btn-outline-danger btn-sm"><?= h(t('Reset Batch', 'إعادة ضبط الدفعة')) ?></button>
</form>
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addCourseModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Course', 'إضافة دورة')) ?></button>
</div>
</div>
<div class="panel-card mb-4">
PHP;
$content = str_replace($buttons_orig, $buttons_new, $content);
// 6. List view columns
$th_orig = <<<PHP
<th><?= h(t('Assigned', 'مخصص')) ?></th>
<th class="text-end"><?= h(t('Actions', 'إجراءات')) ?></th>
PHP;
$th_new = <<<PHP
<th><?= h(t('Assigned', 'مخصص')) ?></th>
<th><?= h(t('Reg.', 'التسجيل')) ?></th>
<th class="text-end"><?= h(t('Actions', 'إجراءات')) ?></th>
PHP;
$content = str_replace($th_orig, $th_new, $content);
$td_orig = <<<PHP
<td>
<span class="badge bg-info text-dark"><?= $assigned_count ?></span>
</td>
<td class="text-truncate" style="max-width: 200px;">
<?= h(current_lang() === 'ar' ? $row['description_ar'] : $row['description_en']) ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#manageStudentsModal<?= $row['id'] ?>" title="<?= h(t('Manage Students', 'إدارة الطلاب')) ?>">
PHP;
$td_new = <<<PHP
<td>
<span class="badge bg-info text-dark"><?= $assigned_count ?></span>
<?php if ($row['max_students']): ?>
<span class="badge bg-secondary">/ <?= h($row['max_students']) ?></span>
<?php endif; ?>
</td>
<td class="text-truncate" style="max-width: 200px;">
<?= h(current_lang() === 'ar' ? $row['description_ar'] : $row['description_en']) ?>
</td>
<td>
<?php if ($row['registration_open']): ?>
<span class="badge bg-success"><?= h(t('Open', 'مفتوح')) ?></span>
<?php else: ?>
<span class="badge bg-danger"><?= h(t('Closed', 'مغلق')) ?></span>
<?php endif; ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#manageStudentsModal<?= $row['id'] ?>" title="<?= h(t('Manage Students', 'إدارة الطلاب')) ?>">
PHP;
$content = str_replace($td_orig, $td_new, $content);
// 7. Modals
$edit_modal_orig = <<<PHP
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Price', 'السعر')) ?></label>
<input type="number" step="0.01" name="price" class="form-control" value="<?= h($row['price'] ?? 0) ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'صورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
PHP;
$edit_modal_new = <<<PHP
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Price', 'السعر')) ?></label>
<input type="number" step="0.01" name="price" class="form-control" value="<?= h($row['price'] ?? 0) ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Max Students', 'الحد الأقصى للطلاب')) ?></label>
<input type="number" name="max_students" class="form-control" placeholder="<?= h(t('Empty = unlimited', 'فارغ = غير محدود')) ?>" value="<?= h($row['max_students']) ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'صورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
PHP;
$content = str_replace($edit_modal_orig, $edit_modal_new, $content);
$edit_modal_orig2 = <<<PHP
<div class="col-12 mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active" <?= ($row['status'] ?? 'active') === 'active' ? 'selected' : '' ?>><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive" <?= ($row['status'] ?? 'active') === 'inactive' ? 'selected' : '' ?>><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
PHP;
$edit_modal_new2 = <<<PHP
<div class="col-12 mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active" <?= ($row['status'] ?? 'active') === 'active' ? 'selected' : '' ?>><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive" <?= ($row['status'] ?? 'active') === 'inactive' ? 'selected' : '' ?>><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
<div class="col-12 mb-3">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="registration_open" id="regSwitch<?= $row['id'] ?>" value="1" <?= $row['registration_open'] ? 'checked' : '' ?>/>
<label class="form-check-label" for="regSwitch<?= $row['id'] ?>"><?= h(t('Registration Open', 'التسجيل مفتوح')) ?></label>
</div>
</div>
PHP;
$content = str_replace($edit_modal_orig2, $edit_modal_new2, $content);
$add_modal_orig = <<<PHP
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Price', 'السعر')) ?></label>
<input type="number" step="0.01" name="price" class="form-control" value="0.00">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'صورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
PHP;
$add_modal_new = <<<PHP
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Price', 'السعر')) ?></label>
<input type="number" step="0.01" name="price" class="form-control" value="0.00">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Max Students', 'الحد الأقصى للطلاب')) ?></label>
<input type="number" name="max_students" class="form-control" placeholder="<?= h(t('Empty = unlimited', 'فارغ = غير محدود')) ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(t('Picture', 'صورة')) ?></label>
<input type="file" name="picture" class="form-control" accept="image/*">
PHP;
$content = str_replace($add_modal_orig, $add_modal_new, $content);
$add_modal_orig2 = <<<PHP
<div class="col-12 mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active"><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive"><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
PHP;
$add_modal_new2 = <<<PHP
<div class="col-12 mb-3">
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
<select name="status" class="form-select">
<option value="active"><?= h(t('Active', 'نشط')) ?></option>
<option value="inactive"><?= h(t('Inactive', 'غير نشط')) ?></option>
</select>
</div>
<div class="col-12 mb-3">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="registration_open" id="regSwitchAdd" value="1" checked>
<label class="form-check-label" for="regSwitchAdd"><?= h(t('Registration Open', 'التسجيل مفتوح')) ?></label>
</div>
</div>
PHP;
$content = str_replace($add_modal_orig2, $add_modal_new2, $content);
file_put_contents('admin_courses.php', $content);
echo "Done";
?>