MicroFrameWork/app/controllers/UserController.php

124 lines
3.1 KiB
PHP
Raw Normal View History

2024-07-03 14:41:15 +03:00
<?php
2024-07-05 13:49:04 +03:00
namespace app\controllers;
2024-07-12 13:46:44 +03:00
use app\foo;
2024-07-16 13:37:34 +03:00
use app\helpers\Debug;
2024-07-24 14:07:45 +03:00
use app\models\forms\CreateUserForm;
2024-07-05 13:49:04 +03:00
use app\models\Question;
use app\models\User;
2024-07-24 14:07:45 +03:00
use app\services\UserService;
2024-07-24 16:29:00 +03:00
use app\tables\columns\UserEditActionColumn;
2024-07-16 13:37:34 +03:00
use app\tables\columns\UserViewActionColumn;
2024-07-23 15:22:33 +03:00
use Exception;
use http\Message;
2024-07-12 13:46:44 +03:00
use Itguild\Tables\ListJsonTable;
2024-07-23 15:22:33 +03:00
use Itguild\Tables\ViewJsonTable;
2024-07-24 17:22:59 +03:00
use JetBrains\PhpStorm\NoReturn;
2024-07-12 11:31:04 +03:00
use kernel\Controller;
2024-07-24 17:22:59 +03:00
use kernel\IGTabel\btn\PrimaryBtn;
2024-07-16 13:37:34 +03:00
use kernel\IGTabel\ListJsonTableEloquentCollection;
2024-07-23 15:22:33 +03:00
use kernel\IGTabel\ViewJsonTableEloquentModel;
2024-07-24 16:31:07 +03:00
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
2024-07-16 13:37:34 +03:00
use Twig\TwigFunction;
2024-07-03 14:41:15 +03:00
2024-07-11 16:16:36 +03:00
class UserController extends Controller{
2024-07-24 17:22:59 +03:00
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
2024-07-08 16:20:25 +03:00
public function actionCreate(): void
2024-07-03 14:41:15 +03:00
{
2024-07-24 17:22:59 +03:00
$this->cgView->render("user/form.php");
2024-07-09 16:08:50 +03:00
}
2024-07-24 17:22:59 +03:00
#[NoReturn] public function actionAdd(): void
2024-07-09 16:08:50 +03:00
{
2024-07-24 14:07:45 +03:00
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
2024-07-24 17:22:59 +03:00
$user = $userService->create($userForm);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
2024-07-24 12:50:07 +03:00
}
2024-07-24 14:07:45 +03:00
$this->redirect("/admin/user/create");
2024-07-03 14:41:15 +03:00
}
2024-07-03 15:15:59 +03:00
public function actionQuestionCount($user_id)
2024-07-03 14:41:15 +03:00
{
return Question::where('user_id', $user_id)->count();
}
2024-07-05 13:49:04 +03:00
2024-07-16 13:37:34 +03:00
/**
* @throws \Exception
*/
2024-07-08 16:20:25 +03:00
public function actionIndex(): void
2024-07-05 13:49:04 +03:00
{
2024-07-25 16:15:18 +03:00
// $users = User::where(['role' => 1])->get();
$users = User::all();
2024-07-16 13:37:34 +03:00
2024-07-24 17:22:59 +03:00
$this->cgView->render("user/index.php", ['users' => $users]);
2024-07-05 13:49:04 +03:00
}
2024-07-23 15:22:33 +03:00
/**
* @throws Exception
*/
2024-07-08 16:20:25 +03:00
public function actionView($id): void
2024-07-05 13:49:04 +03:00
{
2024-07-09 16:08:50 +03:00
$user = User::find($id);
2024-07-24 12:50:07 +03:00
2024-07-23 15:22:33 +03:00
if (!$user){
throw new Exception(message: "The user not found");
}
2024-07-25 11:55:31 +03:00
$this->cgView->render("user/view.php", ['user' => $user]);
2024-07-05 13:49:04 +03:00
}
2024-07-09 16:08:50 +03:00
2024-07-24 16:31:07 +03:00
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
2024-07-24 16:29:00 +03:00
public function actionUpdate($id): void
2024-07-09 16:08:50 +03:00
{
2024-07-25 13:23:50 +03:00
$model = User::find($id);
if (!$model){
throw new Exception(message: "The user not found");
}
$this->cgView->render("user/form.php", ['model' => $model]);
2024-07-09 16:08:50 +03:00
}
2024-07-25 13:23:50 +03:00
/**
* @throws Exception
*/
2024-07-24 16:29:00 +03:00
public function actionEdit($id): void
2024-07-09 16:08:50 +03:00
{
2024-07-25 13:23:50 +03:00
$user = User::find($id);
if (!$user){
throw new Exception(message: "The user not found");
}
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
$user = $userService->update($userForm, $user);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/update/" . $id);
2024-07-09 16:08:50 +03:00
}
2024-07-25 13:23:50 +03:00
#[NoReturn] public function actionDelete($id): void
2024-07-09 16:08:50 +03:00
{
User::find($id)->delete();
2024-07-25 11:55:31 +03:00
$this->redirect("/admin/user/");
2024-07-09 16:08:50 +03:00
}
2024-07-03 14:41:15 +03:00
}