Compare commits

...

2 Commits

Author SHA1 Message Date
f005cb357b cg view 2024-07-24 17:22:59 +03:00
01ea22d089 some fix 2024-07-24 16:31:07 +03:00
12 changed files with 218 additions and 33 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

@ -14,35 +14,40 @@ 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;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\TwigFunction;
class UserController extends Controller{
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);
// 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");
$user = $userService->create($userForm);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/create");
}
public function actionQuestionCount($user_id)
@ -57,21 +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->addAction(UserEditActionColumn::class);
$table->create();
$table->render();
}));
echo $this->twig->render('user_table.html.twig');
$this->cgView->render("user/index.php", ['users' => $users]);
}
/**
@ -90,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();
}));
@ -97,6 +91,11 @@ class UserController extends Controller{
echo $this->twig->render('user_table.html.twig');
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function actionUpdate($id): void
{
echo $this->twig->render('user_update.html.twig');

View File

@ -3,6 +3,9 @@ namespace app\models;
use \Illuminate\Database\Eloquent\Model;
/**
* @method static where(string $string, $user_id)
*/
class Question extends Model {
protected $table = 'question';
protected $fillable = ['question','user_id'];

View File

@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
* @property string $username
* @property string $email
* @property string $password_hash
* @method static where(int[] $array)
* @method static find($id)
*/
class User extends Model {
protected $table = 'user';

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;
}
}

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();