Files
Woles-Framework/app/Modules/User/Controller.php

220 lines
5.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Modules\User;
use App\Core\Controller as BaseController;
/**
* User Controller
* Handles user management
*/
class Controller extends BaseController
{
private Model $model;
public function __construct()
{
parent::__construct();
$this->model = new Model();
}
/**
* List all users
*/
public function index()
{
$users = $this->model->all();
if ($this->request()->expectsJson()) {
return $this->json($users);
}
return $this->view('User.view.index', [
'title' => 'Users - NovaCore Framework',
'users' => $users
]);
}
/**
* Show user details
*/
public function show(int $id)
{
$user = $this->model->findById($id);
if (!$user) {
if ($this->request()->expectsJson()) {
return $this->error('User not found', 404);
}
http_response_code(404);
echo "<h1>404 - User Not Found</h1>";
return;
}
if ($this->request()->expectsJson()) {
return $this->json($user);
}
return $this->view('User.view.show', [
'title' => 'User Details - NovaCore Framework',
'user' => $user
]);
}
/**
* Show create user form
*/
public function create()
{
return $this->view('User.view.create', [
'title' => 'Create User - NovaCore Framework'
]);
}
/**
* Store new user
*/
public function store()
{
$data = $this->request()->all();
// Validation
$errors = $this->validate($data, [
'name' => 'required|min:2',
'email' => 'required|email',
'password' => 'required|min:6'
]);
// Check if email exists
if (empty($errors) && $this->model->emailExists($data['email'])) {
$errors['email'] = 'Email already exists.';
}
if (!empty($errors)) {
if ($this->request()->expectsJson()) {
return $this->error('Validation failed', 422);
}
return $this->view('User.view.create', [
'title' => 'Create User - NovaCore Framework',
'errors' => $errors,
'old' => $data
]);
}
// Create user
$userId = $this->model->create($data);
if ($this->request()->expectsJson()) {
return $this->success(['id' => $userId], 'User created successfully');
}
return $this->redirect('/users');
}
/**
* Show edit user form
*/
public function edit(int $id)
{
$user = $this->model->findById($id);
if (!$user) {
http_response_code(404);
echo "<h1>404 - User Not Found</h1>";
return;
}
return $this->view('User.view.edit', [
'title' => 'Edit User - NovaCore Framework',
'user' => $user
]);
}
/**
* Update user
*/
public function update(int $id)
{
$user = $this->model->findById($id);
if (!$user) {
if ($this->request()->expectsJson()) {
return $this->error('User not found', 404);
}
http_response_code(404);
echo "<h1>404 - User Not Found</h1>";
return;
}
$data = $this->request()->all();
// Validation
$errors = $this->validate($data, [
'name' => 'required|min:2',
'email' => 'required|email'
]);
// Check if email exists (excluding current user)
if (empty($errors) && $this->model->emailExists($data['email'], $id)) {
$errors['email'] = 'Email already exists.';
}
if (!empty($errors)) {
if ($this->request()->expectsJson()) {
return $this->error('Validation failed', 422);
}
return $this->view('User.view.edit', [
'title' => 'Edit User - NovaCore Framework',
'user' => array_merge($user, $data),
'errors' => $errors
]);
}
// Remove password if empty
if (empty($data['password'])) {
unset($data['password']);
} else {
$data['password'] = password_hash($data['password'], PASSWORD_ARGON2ID);
}
// Update user
$this->model->update($id, $data);
if ($this->request()->expectsJson()) {
return $this->success([], 'User updated successfully');
}
return $this->redirect('/users');
}
/**
* Delete user
*/
public function destroy(int $id)
{
$user = $this->model->findById($id);
if (!$user) {
if ($this->request()->expectsJson()) {
return $this->error('User not found', 404);
}
http_response_code(404);
echo "<h1>404 - User Not Found</h1>";
return;
}
$this->model->delete($id);
if ($this->request()->expectsJson()) {
return $this->success([], 'User deleted successfully');
}
return $this->redirect('/users');
}
}