Compare commits

..

2 Commits

Author SHA1 Message Date
43960ddcb9 Added: Habit creation logic 2024-12-26 18:47:00 +01:00
85209ff134 Added: Router route groups 2024-12-26 17:47:42 +01:00
9 changed files with 211 additions and 7 deletions

View File

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

View File

@ -0,0 +1,58 @@
<?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)
}
}

32
app/models/Habit.php Normal file
View File

@ -0,0 +1,32 @@
<?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) {
$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,
];

View File

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

View File

@ -0,0 +1,47 @@
<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,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);
}
}
/**

View File

@ -3,6 +3,8 @@
class Router {
private $routes = [];
private $middlewares = [];
private $groupPrefix = '';
private $groupMiddlewares = [];
/**
* Add a route with a specific action and optional middleware
@ -12,9 +14,35 @@ class Router {
* @param array $middlewares Optional middlewares for this route
*/
public function add($route, $action, $middlewares = []) {
$route = $this->groupPrefix . $route;
$middlewares = array_merge($this->groupMiddlewares, $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
*/
@ -22,6 +50,11 @@ class Router {
$uri = $_SERVER['REQUEST_URI'];
$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)) {
$route = $this->routes[$uri];
$middlewares = $route['middlewares'];

View File

@ -20,15 +20,29 @@ 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();
$router->add('/', 'HomeController@index');
$router->add('/home', 'HomeController@home');
$router->add('/dashboard', 'HomeController@dashboard', ['RequireAuth']);
// auth routes
$router->add('/auth/signin', 'AuthController@signin');
$router->add('/auth/signup', 'AuthController@signup');
$router->add('/auth/logout', 'AuthController@logout');
$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']);
// habits routes
$router->group('/habits', ['RequireAuth'], function ($router) {
$router->add('', 'HabitController@index');
$router->add('/create', 'HabitController@create');
$router->add('/edit/{id}', 'HabitController@edit');
$router->add('/delete/{id}', 'HabitController@delete');
});
$router->dispatch();