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 @@