From 43960ddcb9f92f916c2ace636d2df8074a5b13c9 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Thu, 26 Dec 2024 18:47:00 +0100 Subject: [PATCH] Added: Habit creation logic --- app/controllers/HabitController.php | 58 +++++++++++++++++++++++++++++ app/models/Habit.php | 32 ++++++++++++++++ app/models/User.php | 3 +- app/views/dashboard/index.php | 2 + app/views/habits/create.php | 47 +++++++++++++++++++++++ core/Database.php | 17 ++++++++- public/index.php | 1 + 7 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 app/controllers/HabitController.php create mode 100644 app/models/Habit.php diff --git a/app/controllers/HabitController.php b/app/controllers/HabitController.php new file mode 100644 index 0000000..9f7f6ce --- /dev/null +++ b/app/controllers/HabitController.php @@ -0,0 +1,58 @@ +view('habits/create', ['error' => 'Habit name is required.']); + return; + } + + if ($frequency === 'Custom') { + var_dump($_POST); + $daysOfWeek = $_POST['days_of_week'] ?? []; + $daysOfMonth = $_POST['days_of_month'] ?? '*'; + $months = $_POST['months'] ?? '*'; + + // Combine into crontab-like string + $customFrequency = implode(',', $daysOfWeek) . " $daysOfMonth $months"; + var_dump($customFrequency); + + } + + $habit = new Habit(); + $result = $habit->create([ + 'name' => $name, + 'frequency' => $frequency, + 'custom_frequency' => $customFrequency, + 'reward_points' => intval($_POST['difficulty'] ?? 1), + 'user_id' => $_SESSION['user']['id'], + ]); + + if ($result) { + //$this->redirect('/habits'); + } else { + $this->view('habits/create', ['error' => 'Failed to create habit.']); + } + } else { + $this->view('habits/create', ['title' => 'Create Habit']); + } + } + + + public function edit() { + // Edit habit (to be implemented later) + } + + public function delete() { + // Delete habit (to be implemented later) + } +} diff --git a/app/models/Habit.php b/app/models/Habit.php new file mode 100644 index 0000000..26d0df5 --- /dev/null +++ b/app/models/Habit.php @@ -0,0 +1,32 @@ +db = Database::getInstance()->getConnection(); + } + + public function create($data) { + $stmt = $this->db->prepare(" + INSERT INTO habits (user_id, title, frequency, custom_frequency, reward_points, created_at) + VALUES (?, ?, ?, ?, ?, NOW()) + "); + + $stmt->bind_param( + "isssi", // Bind types: int, string, string, string, int + $data['user_id'], + $data['name'], + $data['frequency'], + $data['custom_frequency'], // Bind the custom_frequency field + $data['reward_points'] + ); + + if ($stmt->execute()) { + return true; + } else { + error_log("Failed to create habit: " . $stmt->error); + return false; + } + } +} diff --git a/app/models/User.php b/app/models/User.php index e6ffffc..ee40aec 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -42,7 +42,7 @@ class User { public function login($email, $password) { $hashedPassword = password_hash($password, PASSWORD_BCRYPT); - $stmt = $this->db->prepare("SELECT username, password FROM users WHERE email = ?"); + $stmt = $this->db->prepare("SELECT id, username, password FROM users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $result = $stmt->get_result(); @@ -52,6 +52,7 @@ class User { $user = $result->fetch_assoc(); if (password_verify($password, $user['password'])) { $_SESSION['user'] = [ + 'id' => $user['id'], 'username' => $user['username'], 'email' => $email, ]; diff --git a/app/views/dashboard/index.php b/app/views/dashboard/index.php index 40f76c1..80ff303 100644 --- a/app/views/dashboard/index.php +++ b/app/views/dashboard/index.php @@ -1 +1,3 @@

Welcome !

+ +Create new Habit diff --git a/app/views/habits/create.php b/app/views/habits/create.php index e69de29..70c0935 100644 --- a/app/views/habits/create.php +++ b/app/views/habits/create.php @@ -0,0 +1,47 @@ +
+

get('title', 'Create Habit') ?>

+ + get('error')): ?> +
+ get('error')) ?> +
+ + +
+ + + + + + + + + +
+
+ + diff --git a/core/Database.php b/core/Database.php index 8bbde03..fbc6ec9 100644 --- a/core/Database.php +++ b/core/Database.php @@ -85,7 +85,22 @@ class Database { die("Failed to create progress table: " . $this->connection->error); } - // Add more table creation logic as needed + + // Create habits table + $habitsTableQuery = "CREATE TABLE IF NOT EXISTS habits ( + id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT NOT NULL, + title VARCHAR(100) NOT NULL, + frequency ENUM('Daily', 'Weekly', 'Custom') NOT NULL, + custom_frequency VARCHAR(255) DEFAULT NULL, -- Store crontab-like string + reward_points INT DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + ) ENGINE=InnoDB;"; + + if (!$this->connection->query($habitsTableQuery)) { + die("Failed to create habits table: " . $this->connection->error); + } } /** diff --git a/public/index.php b/public/index.php index f2293f3..ceeb96b 100644 --- a/public/index.php +++ b/public/index.php @@ -20,6 +20,7 @@ require_once '../core/Database.php'; require_once '../core/middlewares/RequireAuth.php'; require_once models . 'User.php'; +require_once models . 'Habit.php'; // Initialize router $router = new Router();