query("SELECT * FROM resources_table ORDER BY id DESC"); return is_array($rows) ? $rows : []; } catch (Throwable $e) { error_log('get_resources attempt failed: ' . $e->getMessage()); return []; } } } if (!function_exists('add_resource')) { function add_resource(array $payload): void { $pdo = db(); $table = resource_table_name($pdo); $pdo->insert($table, [ 'title' => trim($payload['title'] ?? ''), 'summary' => trim($payload['summary'] ?? ''), 'label' => trim($payload['label'] ?? ''), 'file_path' => $payload['file_path'] ?? null, 'video_url' => trim($payload['video_url'] ?? ''), ]); } } if (!function_exists('delete_resource')) { function delete_resource(int $id): void { $pdo = db(); $table = resource_table_name($pdo); $filePath = null; $row = $pdo->getOne("SELECT file_path FROM `$table` WHERE id = ?", [$id]); if ($row && isset($row['file_path'])) { $filePath = $row['file_path']; } $pdo->deleteById($table, $id); if ($filePath && is_string($filePath)) { $full = __DIR__ . '/../' . ltrim($filePath, '/'); if (is_file($full)) { @unlink($full); } } } } if (!function_exists('get_sessions')) { function get_sessions(): array { $pdo = db(); $rows = $pdo->query(" SELECT id, number, start_date, status, join_link, code, venue FROM sessions WHERE (status IN ('current','upcoming') OR start_date >= NOW()) AND (deleted_at IS NULL OR deleted_at = '0000-00-00 00:00:00') ORDER BY start_date ASC "); return is_array($rows) ? $rows : []; } } if (!function_exists('add_session')) { function add_session(array $payload): void { $pdo = db(); $pdo->insert('sessions', [ 'title' => trim($payload['title'] ?? ''), 'description' => trim($payload['description'] ?? ''), 'tag' => trim($payload['tag'] ?? ''), 'cta_label' => trim($payload['cta_label'] ?? 'Register'), 'start_date' => $payload['start_date'] ?? null, ]); } } if (!function_exists('update_session')) { function update_session(int $id, array $payload): void { $pdo = db(); $pdo->update('sessions', [ 'title' => trim($payload['title'] ?? ''), 'description' => trim($payload['description'] ?? ''), 'tag' => trim($payload['tag'] ?? ''), 'cta_label' => trim($payload['cta_label'] ?? 'Register'), 'start_date' => $payload['start_date'] ?? null, ], "id = ?", [$id]); } } if (!function_exists('delete_session')) { function delete_session(int $id): void { $pdo = db(); $pdo->deleteById('sessions', $id); } } if (!function_exists('save_contact')) { function save_contact(array $payload): void { $pdo = db(); $pdo->insert('contacts', [ 'name' => trim($payload['name'] ?? ''), 'email' => trim($payload['email'] ?? ''), 'organization' => trim($payload['organization'] ?? ''), 'message' => trim($payload['message'] ?? ''), ]); } } if (!function_exists('get_contacts')) { function get_contacts(): array { $pdo = db(); return $pdo->query("SELECT id, name, email, organization, message, created_at FROM contacts ORDER BY id DESC"); } } if (!function_exists('get_tutorials')) { function get_tutorials(bool $onlyActive = true): array { $pdo = db(); $sql = "SELECT id, title, description, status, file_path, video_url, created_at FROM tutorials"; $params = []; if ($onlyActive) { $sql .= " WHERE status = ?"; $params[] = 'active'; } $sql .= " ORDER BY id DESC"; $rows = $pdo->query($sql, $params); return is_array($rows) ? $rows : []; } } if (!function_exists('add_tutorial')) { function add_tutorial(array $payload): void { $pdo = db(); $pdo->insert('tutorials', [ 'title' => trim($payload['title'] ?? ''), 'description' => trim($payload['description'] ?? ''), 'status' => trim($payload['status'] ?? 'active'), 'file_path' => $payload['file_path'] ?? null, 'video_url' => trim($payload['video_url'] ?? ''), ]); } } if (!function_exists('update_tutorial')) { function update_tutorial(int $id, array $payload): void { $pdo = db(); $data = [ 'title' => trim($payload['title'] ?? ''), 'description' => trim($payload['description'] ?? ''), 'status' => trim($payload['status'] ?? 'active'), 'video_url' => trim($payload['video_url'] ?? ''), ]; if (!empty($payload['file_path'])) { $data['file_path'] = $payload['file_path']; } $pdo->update('tutorials', $data, "id = ?", [$id]); } } if (!function_exists('delete_tutorial')) { function delete_tutorial(int $id): void { $pdo = db(); $row = $pdo->getOne("SELECT file_path FROM tutorials WHERE id = ?", [$id]); $filePath = $row['file_path'] ?? null; $pdo->deleteById('tutorials', $id); if ($filePath && is_string($filePath)) { $full = __DIR__ . '/../' . ltrim($filePath, '/'); if (is_file($full)) { @unlink($full); } } } } if (!function_exists('save_subscription')) { function save_subscription(array $payload): void { $pdo = db(); $pdo->rawQuery( "INSERT INTO newsletter_subscriptions (first_name, email) VALUES (?, ?) ON DUPLICATE KEY UPDATE first_name = VALUES(first_name)", [ trim($payload['first_name'] ?? ''), trim($payload['email'] ?? ''), ] ); } } if (!function_exists('get_subscriptions')) { function get_subscriptions(): array { $pdo = db(); return $pdo->query("SELECT id, first_name, email, created_at FROM newsletter_subscriptions ORDER BY id DESC"); } } if (!function_exists('get_admin_by_email')) { function get_admin_by_email(string $email): ?array { $pdo = db(); $row = $pdo->getOne("SELECT * FROM admins WHERE email = ?", [$email]); return $row ?: null; } } if (!function_exists('create_admin')) { function create_admin(string $email, string $password): void { $pdo = db(); $hash = password_hash($password, PASSWORD_DEFAULT); $pdo->insert('admins', ['email' => $email, 'password_hash' => $hash]); } }