Added: Signin Up, connection to the DB, validator, base controller, base view, environment file, User model

This commit is contained in:
2024-12-26 02:00:33 +01:00
parent d7b8aba072
commit 5bca9b12aa
9 changed files with 338 additions and 27 deletions

View File

@ -0,0 +1,23 @@
<?php
class Controller {
/**
* Redirect to a given URL
*
* @param string $url
*/
public function redirect($url) {
header("Location: $url");
exit();
}
/**
* Render a view
*
* @param string $viewName
* @param array $data
*/
public function view($viewName, $data = []) {
$view = new View();
$view->render($viewName, $data);
}
}

101
core/Database.php Normal file
View File

@ -0,0 +1,101 @@
<?php
class Database {
private static $instance = null;
private $connection;
private function __construct() {
$this->initializeConnection();
$this->checkAndCreateDatabase();
$this->checkAndCreateTables();
}
/**
* Get the singleton instance of the Database
* @return Database
*/
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Database();
}
return self::$instance;
}
/**
* Get the active database connection
* @return mysqli
*/
public function getConnection() {
return $this->connection;
}
/**
* Initialize the connection to the database
*/
private function initializeConnection() {
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS);
if ($this->connection->connect_error) {
die("Database connection failed: " . $this->connection->connect_error);
}
}
/**
* Check and create the database if it doesn't exist
*/
private function checkAndCreateDatabase() {
$query = "CREATE DATABASE IF NOT EXISTS `" . DB_NAME . "`";
if (!$this->connection->query($query)) {
die("Failed to create or access database: " . $this->connection->error);
}
$this->connection->select_db(DB_NAME);
}
/**
* Check and create required tables if they don't exist
*/
private function checkAndCreateTables() {
// Create users table
$usersTableQuery = "CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
points INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;";
if (!$this->connection->query($usersTableQuery)) {
die("Failed to create users table: " . $this->connection->error);
}
// Create progress table
$progressTableQuery = "CREATE TABLE IF NOT EXISTS progress (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
habit_id INT NOT NULL,
date DATE NOT NULL,
status ENUM('Done', 'Pending') DEFAULT 'Pending',
FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB;";
if (!$this->connection->query($progressTableQuery)) {
die("Failed to create progress table: " . $this->connection->error);
}
// Add more table creation logic as needed
}
/**
* Prevent cloning of the singleton instance
*/
private function __clone() {}
/**
* Prevent unserialization of the singleton instance
*/
public function __wakeup() {}
}

55
core/Validator.php Normal file
View File

@ -0,0 +1,55 @@
<?php
class Validator {
private $errors = [];
/**
* Check if a field is not empty
*/
public function required($field, $value, $message = null) {
if (empty(trim($value))) {
$this->errors[$field] = $message ?? "$field is required.";
}
}
/**
* Check if a field meets minimum length
*/
public function minLength($field, $value, $length, $message = null) {
if (strlen($value) < $length) {
$this->errors[$field] = $message ?? "$field must be at least $length characters.";
}
}
/**
* Check if a field contains letters and numbers
*/
public function alphanumeric($field, $value, $message = null) {
if (!preg_match('/^(?=.*[a-zA-Z])(?=.*\d).+$/', $value)) {
$this->errors[$field] = $message ?? "$field must contain letters and numbers.";
}
}
/**
* Validate an email
*/
public function email($field, $value, $message = null) {
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->errors[$field] = $message ?? "Invalid email format.";
}
}
/**
* Get validation errors
*/
public function errors() {
return $this->errors;
}
/**
* Check if validation passed
*/
public function passes() {
return empty($this->errors);
}
}

View File

@ -1,10 +1,11 @@
<?php
class View
{
public function render($view, $data = [], $layout = 'base')
{
// Extract variables to be accessible in the view
extract($data);
private $data = [];
public function render($view, $data = [], $layout = 'base') {
// Store the data
$this->data = $data;
// Capture the view content
ob_start();
@ -12,6 +13,13 @@ class View
$content = ob_get_clean();
// Include the base layout and inject the view content
require_once views . "layouts/$layout.php";
require_once views . "layouts/$layout.php";
}
/**
* Safely get a value from the data array
*/
public function get($key) {
return $this->data[$key] ?? null;
}
}