first commit
This commit is contained in:
104
models/CalendarEvent.php
Normal file
104
models/CalendarEvent.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
class CalendarEvent
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
$this->initTable();
|
||||
}
|
||||
|
||||
private function initTable()
|
||||
{
|
||||
$sql = "CREATE TABLE IF NOT EXISTS calendar_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT 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)
|
||||
)";
|
||||
|
||||
try {
|
||||
$this->db->exec($sql);
|
||||
} catch (PDOException $e) {
|
||||
// Tabela już istnieje
|
||||
}
|
||||
}
|
||||
|
||||
public function getByMonth($userId, $month)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM calendar_events
|
||||
WHERE user_id = ? AND strftime('%Y-%m', event_date) = ?
|
||||
ORDER BY event_date ASC, id ASC
|
||||
");
|
||||
$stmt->execute([$userId, $month]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function getByDate($userId, $date)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM calendar_events
|
||||
WHERE user_id = ? AND event_date = ?
|
||||
ORDER BY id ASC
|
||||
");
|
||||
$stmt->execute([$userId, $date]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function getById($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM calendar_events
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
$stmt->execute([$id, $userId]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function create($userId, $title, $content, $eventDate)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
INSERT INTO calendar_events (user_id, title, content, event_date)
|
||||
VALUES (?, ?, ?, ?)
|
||||
");
|
||||
return $stmt->execute([$userId, $title, $content, $eventDate]);
|
||||
}
|
||||
|
||||
public function update($id, $userId, $title, $content, $eventDate)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
UPDATE calendar_events
|
||||
SET title = ?, content = ?, event_date = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$title, $content, $eventDate, $id, $userId]);
|
||||
}
|
||||
|
||||
public function delete($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
DELETE FROM calendar_events
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$id, $userId]);
|
||||
}
|
||||
|
||||
public function countByDateRange($userId, $startDate, $endDate)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT COUNT(*) AS count
|
||||
FROM calendar_events
|
||||
WHERE user_id = ? AND event_date BETWEEN ? AND ?
|
||||
");
|
||||
$stmt->execute([$userId, $startDate, $endDate]);
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return (int)$result['count'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user