MicroFrameWork/app/controllers/UserController.php

113 lines
3.3 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-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-12 11:31:04 +03:00
use kernel\Controller;
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-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-08 16:20:25 +03:00
public function actionCreate(): void
2024-07-03 14:41:15 +03:00
{
2024-07-23 16:53:38 +03:00
echo $this->twig->render('user_create.html.twig');
2024-07-09 16:08:50 +03:00
}
public function actionAdd(): void
{
2024-07-24 14:07:45 +03:00
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
$userService->create($userForm);
$this->redirect("/admin/user");
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();
$this->twig->addFunction(new TwigFunction('table', function () use ($users){
$dataProvider = new ListJsonTableEloquentCollection($users, [
'model' => User::class,
'perPage' => 5,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user",
]);
$table = new ListJsonTable($dataProvider->getJson());
$table->addAction(UserViewActionColumn::class);
$table->create();
$table->render();
}));
echo $this->twig->render('user_table.html.twig');
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());
$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
public function actionUpdate(): void
{
2024-07-11 16:16:36 +03:00
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
2024-07-23 16:53:38 +03:00
echo $this->twig->render('user_update.html.twig');
2024-07-09 16:08:50 +03:00
}
public function actionEdit(): void
{
2024-07-11 16:16:36 +03:00
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
2024-07-09 16:08:50 +03:00
$user = User::find($_REQUEST['id']);
$user->username = $_REQUEST['username'];
$user->email = $_REQUEST['email'];
2024-07-11 16:16:36 +03:00
$user->password_hash = $_REQUEST['password_hash'];
2024-07-09 16:08:50 +03:00
$user->save();
}
public function actionDelete($id): void
{
User::find($id)->delete();
}
2024-07-03 14:41:15 +03:00
}