From d63bbcb29afe273b5803e2a466892924b4613295 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Tue, 25 Aug 2026 19:06:01 +0200 Subject: [PATCH] feat(admin): add dashboard, analytics and user controllers --- src/Controllers/Admin/AnalyticsController.php | 22 ++++++++++++ src/Controllers/Admin/DashboardController.php | 23 ++++++++++++ src/Controllers/Admin/UserController.php | 36 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/Controllers/Admin/AnalyticsController.php create mode 100644 src/Controllers/Admin/DashboardController.php create mode 100644 src/Controllers/Admin/UserController.php diff --git a/src/Controllers/Admin/AnalyticsController.php b/src/Controllers/Admin/AnalyticsController.php new file mode 100644 index 0000000..e81884d --- /dev/null +++ b/src/Controllers/Admin/AnalyticsController.php @@ -0,0 +1,22 @@ +view('admin.analytics', [ + 'salesByDay' => $this->ctx->analytics->salesByDay(14), + 'topProducts' => $this->ctx->analytics->topProducts(5), + 'averageOrderCents' => $this->ctx->analytics->averageOrderValueCents(), + 'unitsSold' => $this->ctx->analytics->unitsSold(), + 'couponUsage' => $this->ctx->analytics->couponUsage(), + 'revenueCents' => $this->ctx->orders->revenueCents(), + 'lowStock' => $this->ctx->products->lowStock(5), + ], 'admin.layout'); + } +} diff --git a/src/Controllers/Admin/DashboardController.php b/src/Controllers/Admin/DashboardController.php new file mode 100644 index 0000000..000f0c8 --- /dev/null +++ b/src/Controllers/Admin/DashboardController.php @@ -0,0 +1,23 @@ +view('admin.dashboard', [ + 'productCount' => $this->ctx->products->count(), + 'pendingCount' => $this->ctx->orders->countByStatus('pending'), + 'paidCount' => $this->ctx->orders->countByStatus('paid'), + 'fulfilledCount' => $this->ctx->orders->countByStatus('fulfilled'), + 'orderCount' => $this->ctx->orders->count(), + 'revenueCents' => $this->ctx->orders->revenueCents(), + 'lowStock' => $this->ctx->products->lowStock(5), + 'recentOrders' => $this->ctx->orders->recent(6), + ], 'admin.layout'); + } +} diff --git a/src/Controllers/Admin/UserController.php b/src/Controllers/Admin/UserController.php new file mode 100644 index 0000000..ea1b12d --- /dev/null +++ b/src/Controllers/Admin/UserController.php @@ -0,0 +1,36 @@ +view('admin.users.index', [ + 'users' => $this->ctx->users->all(), + 'roles' => Role::all(), + ], 'admin.layout'); + } + + public function updateRole(string $id): Response + { + $userId = (int) $id; + $role = (string) $this->ctx->request->input('role', Role::customer); + if (!in_array($role, Role::all(), true)) { + return $this->redirect('/admin/users'); + } + + if ($userId === (int) $this->ctx->auth->id()) { + $this->ctx->session->flash('error', 'You cannot change your own role.'); + return $this->redirect('/admin/users'); + } + + $this->ctx->users->setRole($userId, $role); + $this->ctx->session->flash('success', 'Role updated.'); + return $this->redirect('/admin/users'); + } +}