198 lines
6.5 KiB
PHP
198 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use App\Core\Http\Request;
|
|
use App\Core\Http\Response;
|
|
use App\Core\Auth;
|
|
use App\Models\DailySession;
|
|
use App\Models\SessionMenu;
|
|
use App\Models\EmployeeOrder;
|
|
use App\Models\User;
|
|
|
|
class SessionController extends Controller
|
|
{
|
|
public function index(Request $request, Response $response): string|Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$activeSessions = DailySession::getActiveSessions();
|
|
$pastSessions = DailySession::all();
|
|
|
|
return $this->render('sessions/index', [
|
|
'title' => 'Food & Canteen Sessions Hub',
|
|
'activeSessions' => $activeSessions,
|
|
'pastSessions' => $pastSessions,
|
|
'currentUser' => Auth::user(),
|
|
]);
|
|
}
|
|
|
|
public function createForm(Request $request, Response $response): string|Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
return $this->render('sessions/create', [
|
|
'title' => 'Start New Food Session',
|
|
'currentUser' => Auth::user(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request, Response $response): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$creatorId = (int)Auth::id();
|
|
$title = trim((string)$request->post('title', ''));
|
|
$canteenName = trim((string)$request->post('canteen_name', ''));
|
|
$sessionDate = trim((string)$request->post('session_date', date('Y-m-d')));
|
|
$notes = trim((string)$request->post('notes', ''));
|
|
$menuText = trim((string)$request->post('menu_text', ''));
|
|
|
|
if (empty($title) || empty($canteenName)) {
|
|
return $this->redirect('/sessions/create', null, 'Please provide session title and canteen name.');
|
|
}
|
|
|
|
try {
|
|
$sessionId = DailySession::create($creatorId, $title, $canteenName, $sessionDate, $notes);
|
|
|
|
// Add menu items if provided
|
|
if (!empty($menuText)) {
|
|
$lines = explode("\n", $menuText);
|
|
foreach ($lines as $line) {
|
|
$clean = preg_replace('/^(\d+[\.\)]\s*|[\-\*•]\s*)/u', '', trim($line));
|
|
$clean = trim((string)$clean);
|
|
if (!empty($clean)) {
|
|
SessionMenu::add($sessionId, $clean);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->redirect("/sessions/{$sessionId}", 'Food session created! Share it with your colleagues.');
|
|
} catch (\Throwable $e) {
|
|
return $this->redirect('/sessions/create', null, 'Failed to create session: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function show(Request $request, Response $response, array $params): string|Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$id = (int)($params['id'] ?? 0);
|
|
$session = DailySession::find($id);
|
|
|
|
if (!$session) {
|
|
return $this->redirect('/', null, 'Food session not found.');
|
|
}
|
|
|
|
$currentUser = Auth::user();
|
|
$myOrder = EmployeeOrder::findUserOrderBySession($id, (int)$currentUser['id']);
|
|
|
|
return $this->render('sessions/show', [
|
|
'title' => e($session['title']),
|
|
'session' => $session,
|
|
'currentUser' => $currentUser,
|
|
'myOrder' => $myOrder,
|
|
'isCreator' => (int)$session['creator_id'] === (int)$currentUser['id'],
|
|
]);
|
|
}
|
|
|
|
public function history(Request $request, Response $response): string|Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$sessions = DailySession::all();
|
|
return $this->render('sessions/history', [
|
|
'title' => 'All Past Sessions History',
|
|
'sessions' => $sessions,
|
|
]);
|
|
}
|
|
|
|
public function updateStatus(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$id = (int)($params['id'] ?? 0);
|
|
$status = $request->post('status');
|
|
|
|
$valid = ['open', 'closed', 'arrived', 'completed'];
|
|
if (in_array($status, $valid, true)) {
|
|
DailySession::updateStatus($id, $status);
|
|
return $this->redirect("/sessions/{$id}", "Session status updated to: " . strtoupper($status));
|
|
}
|
|
|
|
return $this->redirect("/sessions/{$id}", null, 'Invalid status.');
|
|
}
|
|
|
|
public function addMenu(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$id = (int)($params['id'] ?? 0);
|
|
$rawText = trim((string)$request->post('menu_text', ''));
|
|
|
|
if (!empty($rawText)) {
|
|
$lines = explode("\n", $rawText);
|
|
$added = 0;
|
|
foreach ($lines as $line) {
|
|
$clean = preg_replace('/^(\d+[\.\)]\s*|[\-\*•]\s*)/u', '', trim($line));
|
|
$clean = trim((string)$clean);
|
|
if (!empty($clean)) {
|
|
SessionMenu::add($id, $clean);
|
|
$added++;
|
|
}
|
|
}
|
|
return $this->redirect("/sessions/{$id}", "Added {$added} menu item(s).");
|
|
}
|
|
|
|
return $this->redirect("/sessions/{$id}", null, 'Please provide menu items.');
|
|
}
|
|
|
|
public function deleteMenu(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$sessionId = (int)($params['id'] ?? 0);
|
|
$menuId = (int)$request->post('menu_id');
|
|
|
|
SessionMenu::delete($menuId);
|
|
return $this->redirect("/sessions/{$sessionId}", 'Menu item removed.');
|
|
}
|
|
|
|
public function settlePrices(Request $request, Response $response, array $params): Response
|
|
{
|
|
if (!Auth::check()) {
|
|
return $this->redirect('/login');
|
|
}
|
|
|
|
$sessionId = (int)($params['id'] ?? 0);
|
|
$prices = $request->post('prices', []);
|
|
|
|
if (is_array($prices)) {
|
|
SessionMenu::updatePricesAndRecalculate($sessionId, $prices);
|
|
DailySession::updateStatus($sessionId, 'arrived');
|
|
return $this->redirect("/sessions/{$sessionId}", 'Prices saved and personal bills computed!');
|
|
}
|
|
|
|
return $this->redirect("/sessions/{$sessionId}", null, 'Failed to update prices.');
|
|
}
|
|
}
|