124 lines
3.1 KiB
PHP
124 lines
3.1 KiB
PHP
<?php
|
|
namespace app\controllers;
|
|
|
|
|
|
use app\foo;
|
|
use app\helpers\Debug;
|
|
use app\models\forms\CreateUserForm;
|
|
use app\models\Question;
|
|
use app\models\User;
|
|
use app\services\UserService;
|
|
use app\tables\columns\UserEditActionColumn;
|
|
use app\tables\columns\UserViewActionColumn;
|
|
use Exception;
|
|
use http\Message;
|
|
use Itguild\Tables\ListJsonTable;
|
|
use Itguild\Tables\ViewJsonTable;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
use kernel\App;
|
|
use kernel\Controller;
|
|
use kernel\IGTabel\btn\PrimaryBtn;
|
|
use kernel\IGTabel\ListJsonTableEloquentCollection;
|
|
use kernel\IGTabel\ViewJsonTableEloquentModel;
|
|
use Twig\Error\LoaderError;
|
|
use Twig\Error\RuntimeError;
|
|
use Twig\Error\SyntaxError;
|
|
use Twig\TwigFunction;
|
|
|
|
class UserController extends Controller{
|
|
protected function init(): void
|
|
{
|
|
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
|
|
$this->cgView->layout = "layouts/main.php";
|
|
}
|
|
|
|
public function actionCreate(): void
|
|
{
|
|
$this->cgView->render("user/form.php");
|
|
}
|
|
|
|
#[NoReturn] public function actionAdd(): void
|
|
{
|
|
$userForm = new CreateUserForm();
|
|
$userService = new UserService();
|
|
$userForm->load($_REQUEST);
|
|
if ($userForm->validate()){
|
|
$user = $userService->create($userForm);
|
|
if ($user){
|
|
$this->redirect("/admin/user/" . $user->id);
|
|
}
|
|
}
|
|
$this->redirect("/admin/user/create");
|
|
}
|
|
|
|
public function actionQuestionCount($user_id)
|
|
{
|
|
return Question::where('user_id', $user_id)->count();
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception
|
|
*/
|
|
public function actionIndex(): void
|
|
{
|
|
$users = User::all();
|
|
|
|
$this->cgView->render("user/index.php", ['users' => $users]);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function actionView($id): void
|
|
{
|
|
$user = User::find($id);
|
|
|
|
if (!$user){
|
|
throw new Exception(message: "The user not found");
|
|
}
|
|
$this->cgView->render("user/view.php", ['user' => $user]);
|
|
}
|
|
|
|
/**
|
|
* @throws RuntimeError
|
|
* @throws SyntaxError
|
|
* @throws LoaderError
|
|
*/
|
|
public function actionUpdate($id): void
|
|
{
|
|
$model = User::find($id);
|
|
if (!$model){
|
|
throw new Exception(message: "The user not found");
|
|
}
|
|
|
|
$this->cgView->render("user/form.php", ['model' => $model]);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function actionEdit($id): void
|
|
{
|
|
$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);
|
|
}
|
|
|
|
#[NoReturn] public function actionDelete($id): void
|
|
{
|
|
User::find($id)->delete();
|
|
$this->redirect("/admin/user/");
|
|
}
|
|
|
|
} |