39669-vm/patch_app2.php
2026-04-16 13:19:21 +00:00

35 lines
3.0 KiB
PHP

<?php
$content = file_get_contents('includes/app.php');
$content = str_replace(
'\'notes\' => \'',
'\'notes\' => \'',
'\'subjects\' => [],
$content
);
$old_loop = " foreach (\$data as \$key => \$_value) {\n \$data[\$key] = clean_text((string) (\$input[\$key] ?? \'\'), \$key === 'notes' ? 1000 : 190);\n }";
$new_loop = " foreach (\$data as \$key => \$_value) {\n if (\$key === 'subjects') {\n \$data[\$key] = is_array(\$input[\$key] ?? []) ? \$input[\$key] : [];\n continue;\n }\n \$data[\$key] = clean_text((string) (\$input[\$key] ?? \'\'), \$key === 'notes' ? 1000 : 190);\n }";
$content = str_replace($old_loop, $new_loop, $content);
$old_val = " if (\$startDate !== '' && \$endDate !== '' && strtotime(\$endDate) < strtotime(\$startDate)) {\n \$errors['end_date'] = 'يجب أن يكون تاريخ النهاية بعد البداية.';\n }";
$new_val = $old_val . "\n\n if (empty(\$data['subjects'])) {\n \$errors['subjects'] = 'يرجى اختيار مادة واحدة على الأقل.';\n } else {\n \$data['subjects'] = array_map('intval', \$data['subjects']);\n }";
$content = str_replace($old_val, $new_val, $content);
$old_insert_sql = " 'INSERT INTO center_applications (\n center_name, city, center_type, gender_scope, director_name, phone, email,\n expected_students, start_date, end_date, notes, status, submitted_at, updated_at\n ) VALUES (\n :center_name, :city, :center_type, :gender_scope, :director_name, :phone, :email,\n :expected_students, :start_date, :end_date, :notes, :status, NOW(), NOW()\n )'";
$new_insert_sql = " 'INSERT INTO center_applications (\n center_name, city, center_type, gender_scope, director_name, phone, email,\n expected_students, start_date, end_date, notes, subjects, status, submitted_at, updated_at\n ) VALUES (\n :center_name, :city, :center_type, :gender_scope, :director_name, :phone, :email,\n :expected_students, :start_date, :end_date, :notes, :subjects, :status, NOW(), NOW()\n )'";
$content = str_replace($old_insert_sql, $new_insert_sql, $content);
$old_execute = " ':notes' => \$data['notes'],\n ':status' => 'submitted',\n ]);";
$new_execute = " ':notes' => \$data['notes'],\n ':subjects' => json_encode(\$data['subjects']),
':status' => 'submitted',\n ]);";
$content = str_replace($old_execute, $new_execute, $content);
if (strpos($content, "function get_enabled_subjects") === false) {
$content .= "\nfunction get_enabled_subjects(): array\n{\n \$pdo = db_connection();\n \$stmt = \$pdo->query(\"SELECT id, name, description FROM subjects WHERE status = 'enabled' ORDER BY name ASC\");\n \$result = \$stmt->fetchAll(PDO::FETCH_ASSOC);\n return \$result !== false ? \$result : [];\n}\n";
}
file_put_contents('includes/app.php', $content);
echo "Patch applied.\n";