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'];
|
||||
}
|
||||
}
|
||||
73
models/Database.php
Normal file
73
models/Database.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
class Database
|
||||
{
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
private $dbPath;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->dbPath = __DIR__ . '/../database/database.db';
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
try {
|
||||
$this->connection = new PDO('sqlite:' . $this->dbPath);
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
} catch (PDOException $e) {
|
||||
die('Connection failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function initDatabase()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
|
||||
// Tworzenie tabeli użytkowników
|
||||
$sql = "CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)";
|
||||
$db->exec($sql);
|
||||
|
||||
// Tworzenie tabeli kodów weryfikacyjnych
|
||||
$sql = "CREATE TABLE IF NOT EXISTS verification_codes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
code TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at DATETIME NOT NULL,
|
||||
used INTEGER DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
)";
|
||||
$db->exec($sql);
|
||||
|
||||
// Dodawanie testowego użytkownika
|
||||
$username = 'projectpro';
|
||||
$password = password_hash('testowehaslo', PASSWORD_DEFAULT);
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
||||
$stmt->execute([$username, $password]);
|
||||
} catch (PDOException $e) {
|
||||
// Użytkownik już istnieje
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
92
models/Notes.php
Normal file
92
models/Notes.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
class Notes
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
$this->initTable();
|
||||
}
|
||||
|
||||
private function initTable()
|
||||
{
|
||||
// Tworzenie tabeli notatek jeśli nie istnieje
|
||||
$sql = "CREATE TABLE IF NOT EXISTS notes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT,
|
||||
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 getAllByUser($userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM notes
|
||||
WHERE user_id = ?
|
||||
ORDER BY updated_at DESC
|
||||
");
|
||||
$stmt->execute([$userId]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function getById($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM notes
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
$stmt->execute([$id, $userId]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function create($userId, $title, $content)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
INSERT INTO notes (user_id, title, content)
|
||||
VALUES (?, ?, ?)
|
||||
");
|
||||
return $stmt->execute([$userId, $title, $content]);
|
||||
}
|
||||
|
||||
public function update($id, $userId, $title, $content)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
UPDATE notes
|
||||
SET title = ?, content = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$title, $content, $id, $userId]);
|
||||
}
|
||||
|
||||
public function delete($id, $userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
DELETE FROM notes
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([$id, $userId]);
|
||||
}
|
||||
|
||||
public function getCount($userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT COUNT(*) as count FROM notes
|
||||
WHERE user_id = ?
|
||||
");
|
||||
$stmt->execute([$userId]);
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
return $result['count'];
|
||||
}
|
||||
}
|
||||
70
models/User.php
Normal file
70
models/User.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
class User
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Database::getInstance()->getConnection();
|
||||
}
|
||||
|
||||
public function authenticate($username, $password)
|
||||
{
|
||||
$stmt = $this->db->prepare("SELECT * FROM users WHERE username = ?");
|
||||
$stmt->execute([$username]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($user && password_verify($password, $user['password'])) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function generateVerificationCode($userId)
|
||||
{
|
||||
// Generowanie 6-cyfrowego kodu
|
||||
$code = sprintf('%06d', random_int(0, 999999));
|
||||
|
||||
// Ustawienie czasu wygaśnięcia (15 minut)
|
||||
$expiresAt = date('Y-m-d H:i:s', strtotime('+15 minutes'));
|
||||
|
||||
// Usuwanie starych nieużytych kodów dla tego użytkownika
|
||||
$stmt = $this->db->prepare("DELETE FROM verification_codes WHERE user_id = ? AND used = 0");
|
||||
$stmt->execute([$userId]);
|
||||
|
||||
// Zapisywanie nowego kodu
|
||||
$stmt = $this->db->prepare("INSERT INTO verification_codes (user_id, code, expires_at) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$userId, $code, $expiresAt]);
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function verifyCode($userId, $code)
|
||||
{
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT * FROM verification_codes
|
||||
WHERE user_id = ? AND code = ? AND used = 0 AND expires_at > datetime('now')
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
");
|
||||
$stmt->execute([$userId, $code]);
|
||||
$verification = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($verification) {
|
||||
// Oznaczenie kodu jako użyty
|
||||
$stmt = $this->db->prepare("UPDATE verification_codes SET used = 1 WHERE id = ?");
|
||||
$stmt->execute([$verification['id']]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getUserById($userId)
|
||||
{
|
||||
$stmt = $this->db->prepare("SELECT * FROM users WHERE id = ?");
|
||||
$stmt->execute([$userId]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user