MicroFrameWork/app/controllers/UserController.php
2024-07-24 16:29:00 +03:00

141 lines
4.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 kernel\Controller;
use kernel\IGTabel\ListJsonTableEloquentCollection;
use kernel\IGTabel\ViewJsonTableEloquentModel;
use Twig\TwigFunction;
class UserController extends Controller{
public function actionCreate(): void
{
echo $this->twig->render('user_create.html.twig');
}
public function actionAdd(): void
{
$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);
// Debug::dd($userService->create($userForm));
$this->redirect("/admin/user/" . User::latest()->first()['id']);
}
else
{
$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::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->addAction(UserEditActionColumn::class);
$table->create();
$table->render();
}));
echo $this->twig->render('user_table.html.twig');
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$user = User::find($id);
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');
}
public function actionUpdate($id): void
{
echo $this->twig->render('user_update.html.twig');
}
public function actionEdit($id): void
{
// $_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);
// }
}
public function actionDelete($id): void
{
User::find($id)->delete();
}
}