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

161 lines
9.5 KiB
Python

import sys
import re
with open('admin_courses.php', 'r') as f:
content = f.read()
# 1. Add post actions
content = content.replace(
" if ($post_action === 'edit' || $post_action === 'add') {",
""" 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') {"""
)
# 2. Add max_students and registration_open to post
content = content.replace(
""" $status = $_POST['status'] ?? 'active';
$price = (float)($_POST['price'] ?? 0);""",
""" $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;"""
)
# 3. Update query edit
content = content.replace(
'$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]);',
'$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]);'
)
# 4. Update query add
content = content.replace(
'$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]);',
'$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]);'
)
# 5. Add bulk buttons
content = content.replace(
""" <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\">
",
""" <div>
<h1 class=\"section-title mb-2\">?= h(t('Courses', 'الدورات')) ?></h1>
</div>
<div class=\"d-flex gap-2\">
<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-danger\">?= h(t('Close Registration (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\">?= h(t('Open Registration (All)', 'فتح التسجيل للكل')) ?></button>
</form>
<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>
<div class=\"panel-card mb-4\">
"""
)
# 6. Modal edit
content = content.replace(
""" <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']) ?>\">
</div>""",
""" <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']) ?>\">
</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-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>"""
)
# 7. Modal add
content = content.replace(
""" <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('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-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>"""
)
# 8. update list to show registration status
content = content.replace(
""" <th>?= h(t('Assigned', 'مخصص')) ?></th>
<th class=\"text-end\">?= h(t('Actions', 'الإجراءات')) ?></th>""",
""" <th>?= h(t('Assigned', 'مخصص')) ?></th>
<th>?= h(t('Reg Status', 'حالة التسجيل')) ?></th>
<th class=\"text-end\">?= h(t('Actions', 'الإجراءات')) ?></th>"""
)
content = content.replace(
""" <td>
<span class=\"badge bg-secondary\">?= h($assigned_count) ?> <?= h(t('Students', 'طلاب')) ?></span>
</td>
<td class=\"text-end\">
""",
""" <td>
<span class=\"badge bg-secondary\">?= h($assigned_count) ?> <?= h(t('Students', 'طلاب')) ?></span>
<?php if ($row['max_students']): ?>
<span class=\"badge bg-info\">/ <?= h($row['max_students']) ?></span>
<?php endif; ?>
</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 class=\"text-end\">
"""
)
with open('admin_courses.php', 'w') as f:
f.write(content)
print("Done")