exec(' CREATE TABLE IF NOT EXISTS events ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, title VARCHAR(255) NOT NULL, content TEXT, event_date DATE NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ) '); } public static function getAllByUser(int $userId): array { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare(' SELECT * FROM events WHERE user_id = :user_id ORDER BY event_date ASC '); $stmt->execute(['user_id' => $userId]); return $stmt->fetchAll(); } public static function getByMonth(int $userId, int $year, int $month): array { self::ensureTable(); $db = Database::getInstance(); $startDate = sprintf('%04d-%02d-01', $year, $month); $endDate = sprintf('%04d-%02d-%02d', $year, $month, cal_days_in_month(CAL_GREGORIAN, $month, $year)); $stmt = $db->prepare(' SELECT * FROM events WHERE user_id = :user_id AND event_date BETWEEN :start_date AND :end_date ORDER BY event_date ASC '); $stmt->execute([ 'user_id' => $userId, 'start_date' => $startDate, 'end_date' => $endDate ]); return $stmt->fetchAll(); } public static function getByDate(int $userId, string $date): array { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare(' SELECT * FROM events WHERE user_id = :user_id AND event_date = :event_date ORDER BY created_at ASC '); $stmt->execute(['user_id' => $userId, 'event_date' => $date]); return $stmt->fetchAll(); } public static function getById(int $id, int $userId): ?array { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare(' SELECT * FROM events WHERE id = :id AND user_id = :user_id '); $stmt->execute(['id' => $id, 'user_id' => $userId]); $event = $stmt->fetch(); return $event ?: null; } public static function create(int $userId, string $title, string $content, string $eventDate): int { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare(' INSERT INTO events (user_id, title, content, event_date, created_at, updated_at) VALUES (:user_id, :title, :content, :event_date, datetime("now"), datetime("now")) '); $stmt->execute([ 'user_id' => $userId, 'title' => $title, 'content' => $content, 'event_date' => $eventDate ]); return (int) $db->lastInsertId(); } public static function update(int $id, int $userId, string $title, string $content, string $eventDate): bool { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare(' UPDATE events SET title = :title, content = :content, event_date = :event_date, updated_at = datetime("now") WHERE id = :id AND user_id = :user_id '); return $stmt->execute([ 'id' => $id, 'user_id' => $userId, 'title' => $title, 'content' => $content, 'event_date' => $eventDate ]); } public static function delete(int $id, int $userId): bool { $db = Database::getInstance(); $stmt = $db->prepare(' DELETE FROM events WHERE id = :id AND user_id = :user_id '); return $stmt->execute(['id' => $id, 'user_id' => $userId]); } public static function countByUser(int $userId): int { self::ensureTable(); $db = Database::getInstance(); $stmt = $db->prepare('SELECT COUNT(*) as count FROM events WHERE user_id = :user_id'); $stmt->execute(['user_id' => $userId]); return (int) $stmt->fetch()['count']; } public static function countThisWeek(int $userId): int { self::ensureTable(); $db = Database::getInstance(); // PoniedziaƂek tego tygodnia $monday = date('Y-m-d', strtotime('monday this week')); // Niedziela tego tygodnia $sunday = date('Y-m-d', strtotime('sunday this week')); $stmt = $db->prepare(' SELECT COUNT(*) as count FROM events WHERE user_id = :user_id AND event_date BETWEEN :monday AND :sunday '); $stmt->execute([ 'user_id' => $userId, 'monday' => $monday, 'sunday' => $sunday ]); return (int) $stmt->fetch()['count']; } public static function getThisWeek(int $userId): array { self::ensureTable(); $db = Database::getInstance(); $monday = date('Y-m-d', strtotime('monday this week')); $sunday = date('Y-m-d', strtotime('sunday this week')); $stmt = $db->prepare(' SELECT * FROM events WHERE user_id = :user_id AND event_date BETWEEN :monday AND :sunday ORDER BY event_date ASC '); $stmt->execute([ 'user_id' => $userId, 'monday' => $monday, 'sunday' => $sunday ]); return $stmt->fetchAll(); } }