Compare commits

..

No commits in common. "43960ddcb9f92f916c2ace636d2df8074a5b13c9" and "4c44dac115804376a02ba7f8462b781c0cf7085b" have entirely different histories.

9 changed files with 7 additions and 211 deletions

View File

@ -6,6 +6,4 @@
- [ ] edit user data - change password, mail... - [ ] edit user data - change password, mail...
## Core of the app ## Core of the app
- [ ] Habits list - [ ] think about it lol
- [ ] Habits create
- [ ] Habits track

View File

@ -1,58 +0,0 @@
<?php
class HabitController extends Controller {
public function index() {
// Display the list of habits (to be implemented later)
}
public function create() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$frequency = $_POST['frequency'] ?? 'Daily';
$customFrequency = null;
if (empty($name)) {
$this->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)
}
}

View File

@ -1,32 +0,0 @@
<?php
class Habit {
private $db;
public function __construct() {
$this->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;
}
}
}

View File

@ -42,7 +42,7 @@ class User {
public function login($email, $password) { public function login($email, $password) {
$hashedPassword = password_hash($password, PASSWORD_BCRYPT); $hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$stmt = $this->db->prepare("SELECT id, username, password FROM users WHERE email = ?"); $stmt = $this->db->prepare("SELECT username, password FROM users WHERE email = ?");
$stmt->bind_param("s", $email); $stmt->bind_param("s", $email);
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
@ -52,7 +52,6 @@ class User {
$user = $result->fetch_assoc(); $user = $result->fetch_assoc();
if (password_verify($password, $user['password'])) { if (password_verify($password, $user['password'])) {
$_SESSION['user'] = [ $_SESSION['user'] = [
'id' => $user['id'],
'username' => $user['username'], 'username' => $user['username'],
'email' => $email, 'email' => $email,
]; ];

View File

@ -1,3 +1 @@
<h1>Welcome <?= $_SESSION['user']['username']?>!</h1> <h1>Welcome <?= $_SESSION['user']['username']?>!</h1>
<a href="/habits/create">Create new Habit</a>

View File

@ -1,47 +0,0 @@
<section class="habit-create">
<h1><?= $this->get('title', 'Create Habit') ?></h1>
<?php if ($this->get('error')): ?>
<div class="error" style="color: red; margin-bottom: 1rem;">
<?= htmlspecialchars($this->get('error')) ?>
</div>
<?php endif; ?>
<form method="POST" action="/habits/create">
<label for="name">Habit Name:</label>
<input type="text" name="name" id="name" required value="<?= htmlspecialchars($_POST['name'] ?? '') ?>">
<label for="frequency">Frequency:</label>
<select name="frequency" id="frequency" onchange="toggleCustomFrequency(this.value)">
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Custom">Custom</option>
</select>
<div id="custom-frequency" style="display: none;">
<label>Days of the Week:</label>
<input type="checkbox" name="days_of_week[]" value="1"> Monday
<input type="checkbox" name="days_of_week[]" value="2"> Tuesday
<input type="checkbox" name="days_of_week[]" value="3"> Wednesday
<input type="checkbox" name="days_of_week[]" value="4"> Thursday
<input type="checkbox" name="days_of_week[]" value="5"> Friday
<input type="checkbox" name="days_of_week[]" value="6"> Saturday
<input type="checkbox" name="days_of_week[]" value="7"> Sunday
<label for="days_of_month">Days of the Month:</label>
<input type="text" name="days_of_month" id="days_of_month" placeholder="1,15 (comma-separated)">
<label for="months">Months:</label>
<input type="text" name="months" id="months" placeholder="1,7,12 (comma-separated)">
</div>
<button type="submit">Create Habit</button>
</form>
</section>
<script>
function toggleCustomFrequency(value) {
const customFrequencyDiv = document.getElementById('custom-frequency');
customFrequencyDiv.style.display = value === 'Custom' ? 'block' : 'none';
}
</script>

View File

@ -85,22 +85,7 @@ class Database {
die("Failed to create progress table: " . $this->connection->error); 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);
}
} }
/** /**

View File

@ -3,8 +3,6 @@
class Router { class Router {
private $routes = []; private $routes = [];
private $middlewares = []; private $middlewares = [];
private $groupPrefix = '';
private $groupMiddlewares = [];
/** /**
* Add a route with a specific action and optional middleware * Add a route with a specific action and optional middleware
@ -14,35 +12,9 @@ class Router {
* @param array $middlewares Optional middlewares for this route * @param array $middlewares Optional middlewares for this route
*/ */
public function add($route, $action, $middlewares = []) { public function add($route, $action, $middlewares = []) {
$route = $this->groupPrefix . $route;
$middlewares = array_merge($this->groupMiddlewares, $middlewares);
$this->routes[$route] = ['action' => $action, 'middlewares' => $middlewares]; $this->routes[$route] = ['action' => $action, 'middlewares' => $middlewares];
} }
/**
* Define a group of routes with shared prefix and middlewares
*
* @param string $prefix
* @param array $middlewares
* @param callable $callback
*/
public function group($prefix, $middlewares, $callback) {
// Save the current state
$previousPrefix = $this->groupPrefix;
$previousMiddlewares = $this->groupMiddlewares;
// Set new group prefix and middlewares
$this->groupPrefix = $previousPrefix . $prefix;
$this->groupMiddlewares = array_merge($this->groupMiddlewares, $middlewares);
// Execute the callback to define routes in the group
$callback($this);
// Restore the previous state
$this->groupPrefix = $previousPrefix;
$this->groupMiddlewares = $previousMiddlewares;
}
/** /**
* Dispatch the current request to the correct route and execute middlewares * Dispatch the current request to the correct route and execute middlewares
*/ */
@ -50,11 +22,6 @@ class Router {
$uri = $_SERVER['REQUEST_URI']; $uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri, PHP_URL_PATH); $uri = parse_url($uri, PHP_URL_PATH);
// Normalize the URI by removing trailing slash (except for root "/")
if ($uri !== '/' && substr($uri, -1) === '/') {
$uri = rtrim($uri, '/');
}
if (array_key_exists($uri, $this->routes)) { if (array_key_exists($uri, $this->routes)) {
$route = $this->routes[$uri]; $route = $this->routes[$uri];
$middlewares = $route['middlewares']; $middlewares = $route['middlewares'];

View File

@ -20,29 +20,15 @@ require_once '../core/Database.php';
require_once '../core/middlewares/RequireAuth.php'; require_once '../core/middlewares/RequireAuth.php';
require_once models . 'User.php'; require_once models . 'User.php';
require_once models . 'Habit.php';
// Initialize router // Initialize router
$router = new Router(); $router = new Router();
$router->add('/', 'HomeController@index'); $router->add('/', 'HomeController@index');
$router->add('/home', 'HomeController@home'); $router->add('/home', 'HomeController@home');
// auth routes
$router->group('/auth', [], function ($router) {
$router->add('/signin', 'AuthController@signin');
$router->add('/signup', 'AuthController@signup');
$router->add('/logout', 'AuthController@logout');
});
// dashboard route
$router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']); $router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']);
// habits routes // auth routes
$router->group('/habits', ['RequireAuth'], function ($router) { $router->add('/auth/signin', 'AuthController@signin');
$router->add('', 'HabitController@index'); $router->add('/auth/signup', 'AuthController@signup');
$router->add('/create', 'HabitController@create'); $router->add('/auth/logout', 'AuthController@logout');
$router->add('/edit/{id}', 'HabitController@edit');
$router->add('/delete/{id}', 'HabitController@delete');
});
$router->dispatch(); $router->dispatch();