import json with open("includes/app.php", "r") as f: c = f.read() c = c.replace( "'notes' => '',", "'notes' => '',\n 'subjects' => []," ) old_loop = """ foreach ($data as $key => $_value) { $data[$key] = clean_text((string) ($input[$key] ?? ''), $key === 'notes' ? 1000 : 190); }""" new_loop = """ foreach ($data as $key => $_value) { if ($key === 'subjects') { $data[$key] = is_array($input[$key] ?? []) ? $input[$key] : []; continue; } $data[$key] = clean_text((string) ($input[$key] ?? ''), $key === 'notes' ? 1000 : 190); }""" c = c.replace(old_loop, new_loop) old_val = """ if ($startDate !== '' && $endDate !== '' && strtotime($endDate) < strtotime($startDate)) { $errors['end_date'] = 'يجب أن يكون تاريخ النهاية بعد البداية.'; }""" new_val = old_val + "\n\n if (empty($data['subjects'])) { $errors['subjects'] = 'يرجى اختيار مادة واحدة على الأقل.'; } else { $data['subjects'] = array_map('intval', $data['subjects']); }" c = c.replace(old_val, new_val) old_insert_sql = " 'INSERT INTO center_applications ( center_name, city, center_type, gender_scope, director_name, phone, email, expected_students, start_date, end_date, notes, status, submitted_at, updated_at ) VALUES ( :center_name, :city, :center_type, :gender_scope, :director_name, :phone, :email, :expected_students, :start_date, :end_date, :notes, :status, NOW(), NOW() )"" new_insert_sql = " 'INSERT INTO center_applications ( center_name, city, center_type, gender_scope, director_name, phone, email, expected_students, start_date, end_date, notes, subjects, status, submitted_at, updated_at ) VALUES ( :center_name, :city, :center_type, :gender_scope, :director_name, :phone, :email, :expected_students, :start_date, :end_date, :notes, :subjects, :status, NOW(), NOW() )"" c = c.replace(old_insert_sql, new_insert_sql) old_execute = """ ':notes' => $data['notes'], ':status' => 'submitted', ]);""" new_execute = """ ':notes' => $data['notes'], ':subjects' => json_encode($data['subjects']), ':status' => 'submitted', ]);""" c = c.replace(old_execute, new_execute) if "function get_enabled_subjects" not in c: c += "\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" with open("includes/app.php", "w") as f: f.write(c) print("Patch applied.")