igfs/app/controllers/UserController.php

140 lines
3.9 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-16 13:37:34 +03:00
$users = User::where(['role' => 1])->get();
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");
}
$this->twig->addFunction(new TwigFunction('table', function () use ($user){
$dataProvider = new ViewJsonTableEloquentModel($user, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user",
]);
$table = new ViewJsonTable($dataProvider->getJson());
2024-07-24 17:22:59 +03:00
$table->beforeTable(function (){
return PrimaryBtn::create("Список", "/admin/user")->fetch();
});
2024-07-23 15:22:33 +03:00
$table->create();
$table->render();
}));
echo $this->twig->render('user_table.html.twig');
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-23 16:53:38 +03:00
echo $this->twig->render('user_update.html.twig');
2024-07-09 16:08:50 +03:00
}
2024-07-24 16:29:00 +03:00
public function actionEdit($id): void
2024-07-09 16:08:50 +03:00
{
2024-07-24 16:29:00 +03:00
// $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
//
// $user = User::find($_REQUEST['id']);
// $user->username = $_REQUEST['username'];
// $user->email = $_REQUEST['email'];
// $user->password_hash = $_REQUEST['password_hash'];
// $user->save();
// $user = User::find($id);
// if (!$user){
// throw new Exception(message: "The user not found");
// }
// $userForm = new CreateUserForm();
// $userService = new UserService();
// $userForm->load($_REQUEST);
//// Debug::prn($userForm->validate());
//// Debug::dd($userForm->getErrors());
// if ($userForm->validate()){
//// Debug::prn($userService);
//
// $userService->create($userForm);
// $this->redirect("/admin/user/" . User::find($id)['id']);
// }
// else
// {
// $this->redirect("/admin/user/update/" . $id);
// }
2024-07-09 16:08:50 +03:00
}
public function actionDelete($id): void
{
User::find($id)->delete();
}
2024-07-03 14:41:15 +03:00
}