This commit is contained in:
Kavalar 2024-07-24 17:22:59 +03:00
commit f005cb357b
13 changed files with 258 additions and 50 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace app\action_btn;
use Itguild\Tables\actionBtn\ActionBtn;
class CreateUserBtn extends ActionBtn
{
protected string $prefix = "/admin/user/create";
public function fetch(): string
{
return "<a class='btn btn-primary' href='$this->baseUrl$this->prefix' style='margin: 3px'>Создать</a>";
}
}

View File

@ -8,12 +8,15 @@ 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\Controller;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection;
use kernel\IGTabel\ViewJsonTableEloquentModel;
use Twig\Error\LoaderError;
@ -22,24 +25,27 @@ use Twig\Error\SyntaxError;
use Twig\TwigFunction;
class UserController extends Controller{
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function actionCreate(): void
protected function init(): void
{
echo $this->twig->render('user_create.html.twig');
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionAdd(): void
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()){
$userService->create($userForm);
$this->redirect("/admin/user");
$user = $userService->create($userForm);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/create");
}
@ -56,20 +62,7 @@ class UserController extends Controller{
{
$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');
$this->cgView->render("user/index.php", ['users' => $users]);
}
/**
@ -88,6 +81,9 @@ class UserController extends Controller{
'baseUrl' => "/admin/user",
]);
$table = new ViewJsonTable($dataProvider->getJson());
$table->beforeTable(function (){
return PrimaryBtn::create("Список", "/admin/user")->fetch();
});
$table->create();
$table->render();
}));
@ -100,22 +96,40 @@ class UserController extends Controller{
* @throws SyntaxError
* @throws LoaderError
*/
public function actionUpdate(): void
public function actionUpdate($id): void
{
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
echo $this->twig->render('user_update.html.twig');
}
public function actionEdit(): void
public function actionEdit($id): void
{
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
// $_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($_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

View File

@ -8,13 +8,17 @@ use kernel\FormModel;
class UserService
{
public function create(FormModel $form_model): bool
public function create(FormModel $form_model): false|User
{
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');
$model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
return $model->save();
if ($model->save()){
return $model;
}
return false;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace app\tables\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class UserEditActionColumn extends ActionColumn
{
protected string $prefix = "/";
public function fetch(): string
{
// $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update";
$link = $this->baseUrl . $this->prefix . "update" . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-primary'>Редактировать</a> ";
}
}

View File

@ -19,13 +19,14 @@ $router->get('/', [MainController::class, 'actionIndex']);
$router->get('/example', [MainController::class, 'actionExample']);
$router->group(["prefix" => "admin"], function (RouteCollector $router){
$router->group(["prefix" => "user"], function (RouteCollector $router){
$router->get('/create', [\app\controllers\UserController::class, 'actionCreate']);
$router->get('/update', [\app\controllers\UserController::class, 'actionUpdate']);
$router->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']);
$router->get('/', [\app\controllers\UserController::class, 'actionIndex']);
$router->get('/{id}', [\app\controllers\UserController::class, 'actionView']);
$router->get('/create', [\app\controllers\UserController::class, 'actionCreate']);
$router->post("/", [\app\controllers\UserController::class, 'actionAdd']);
$router->post("/edit", [\app\controllers\UserController::class, 'actionEdit']);
$router->get('/{id}', [\app\controllers\UserController::class, 'actionView']);
// $router->get('/{id}/update', [\app\controllers\UserController::class, 'actionUpdate']);
$router->any('/update/{id}', [\app\controllers\UserController::class, 'actionUpdate']);
$router->any("/edit/{id}", [\app\controllers\UserController::class, 'actionEdit']);
$router->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']);
});
$router->group(["prefix" => "question"], function (RouteCollector $router){
$router->get('/create', [QuestionController::class, 'actionCreate']);

56
kernel/CgView.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace kernel;
class CgView
{
public string $viewPath = '';
public bool|string $layout = false;
public function __construct()
{
}
public function render(string $view, array $data = []): void
{
$content = $this->createContent($view, $data);
echo $content;
}
public function fetch(string $view, array $data = []): false|string
{
$content = $this->createContent($view, $data);
return $content;
}
private function createContent(string $view, array $data = []): false|string
{
ob_start();
foreach ($data as $key => $datum){
${"$key"} = $datum;
}
include ($this->viewPath . $view);
$content = ob_get_contents();
ob_end_clean ();
ob_start();
$file_content = $content;
if ($this->layout){
if (file_exists($this->viewPath . $this->layout)){
include ($this->viewPath . $this->layout);
$file_content = ob_get_contents();
}
}
ob_end_clean ();
return $file_content;
}
}

View File

@ -2,19 +2,30 @@
namespace kernel;
use JetBrains\PhpStorm\NoReturn;
class Controller
{
protected \Twig\Loader\FilesystemLoader $loader;
protected \Twig\Environment $twig;
protected CgView $cgView;
public function __construct()
{
$this->loader = new \Twig\Loader\FilesystemLoader(ROOT_DIR . $_ENV['VIEWS_PATH']);
$this->twig = new \Twig\Environment($this->loader, ['cache' => ROOT_DIR . $_ENV['VIEWS_CACHE_PATH']]);
$this->cgView = new CgView();
$this->cgView->viewPath = ROOT_DIR . "/views";
$this->init();
}
public function redirect(string $url): void
#[NoReturn] protected function redirect(string $url, int $code = 301): void
{
header("Location: " . $url);
header('Location: ' . $url, true, $code);
exit;
}
protected function init(){}
}

View File

@ -0,0 +1,24 @@
<?php
namespace kernel\IGTabel\btn;
class PrimaryBtn
{
protected string $btn = '';
public function __construct(string $title, string $url)
{
$this->btn = "<a class='btn btn-primary' href='$url' style='margin: 3px; width: 100px;' >$title</a>";
}
public function fetch(): string
{
return $this->btn;
}
public static function create(string $title, string $url): PrimaryBtn
{
return new self($title, $url);
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* @var $content
*/
?>
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="UTF-8">
<title>Примеры шаблонизатора Twig</title>
<link rel="stylesheet" href="/resources/css/bootstrap.css">
</head>
<body>
<div class="container">
<div class="row">
<h1>HEADER</h1>
<?= $content ?>
<h1>FOOTER</h1>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1 @@
<?php

19
views/admin/user/form.php Normal file
View File

@ -0,0 +1,19 @@
<form action="/admin/user/" method="post">
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
</label> <br> <br>
Пароль:<br>
<label>
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
<label>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>

View File

@ -0,0 +1,28 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $users
*/
use app\models\User;
use app\tables\columns\UserEditActionColumn;
use app\tables\columns\UserViewActionColumn;
use Itguild\Tables\ListJsonTable;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection;
$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->beforePrint(function (){
return PrimaryBtn::create("Создать", "/admin/user/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->addAction(UserViewActionColumn::class);
$table->addAction(UserEditActionColumn::class);
$table->create();
$table->render();

View File

@ -2,11 +2,6 @@
{% block content %}
<form action="/admin/user/edit" method="post">
id пользователя:<br>
<label>
<input type = "text" name = "id" required size="50" autofocus placeholder="id">
</label> <br> <br>
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
@ -14,12 +9,12 @@
Пароль:<br>
<label>
<input type = "text" name = "password_hash" required size="50" placeholder="Пароль">
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
<label>
<input type="Email" name="email" required>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">