MicroFrameWork/app/controllers/UserController.php

141 lines
4.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-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);
2024-07-24 16:29:00 +03:00
// Debug::prn($userForm->validate());
// Debug::dd($userForm->getErrors());
2024-07-24 14:07:45 +03:00
if ($userForm->validate()){
2024-07-24 16:29:00 +03:00
// Debug::prn($userService);
2024-07-24 14:07:45 +03:00
$userService->create($userForm);
2024-07-24 16:29:00 +03:00
// Debug::dd($userService->create($userForm));
$this->redirect("/admin/user/" . User::latest()->first()['id']);
}
else
{
$this->redirect("/admin/user/create");
2024-07-24 12:50:07 +03:00
}
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);
2024-07-24 16:29:00 +03:00
$table->addAction(UserEditActionColumn::class);
2024-07-16 13:37:34 +03:00
$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
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
}