Compare commits

..

No commits in common. "f005cb357ba4c2c11ff59001bff10d6b3d6ef31f" and "0e13d2c418328d433a8ada341ec5f371239c6732" have entirely different histories.

12 changed files with 32 additions and 217 deletions

View File

@ -1,15 +0,0 @@
<?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,40 +14,35 @@ use Exception;
use http\Message; use http\Message;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
use Itguild\Tables\ViewJsonTable; use Itguild\Tables\ViewJsonTable;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller; use kernel\Controller;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection; use kernel\IGTabel\ListJsonTableEloquentCollection;
use kernel\IGTabel\ViewJsonTableEloquentModel; use kernel\IGTabel\ViewJsonTableEloquentModel;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\TwigFunction; use Twig\TwigFunction;
class UserController extends Controller{ class UserController extends Controller{
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionCreate(): void public function actionCreate(): void
{ {
$this->cgView->render("user/form.php"); echo $this->twig->render('user_create.html.twig');
} }
#[NoReturn] public function actionAdd(): void public function actionAdd(): void
{ {
$userForm = new CreateUserForm(); $userForm = new CreateUserForm();
$userService = new UserService(); $userService = new UserService();
$userForm->load($_REQUEST); $userForm->load($_REQUEST);
// Debug::prn($userForm->validate());
// Debug::dd($userForm->getErrors());
if ($userForm->validate()){ if ($userForm->validate()){
$user = $userService->create($userForm); // Debug::prn($userService);
if ($user){
$this->redirect("/admin/user/" . $user->id); $userService->create($userForm);
} // Debug::dd($userService->create($userForm));
$this->redirect("/admin/user/" . User::latest()->first()['id']);
}
else
{
$this->redirect("/admin/user/create");
} }
$this->redirect("/admin/user/create");
} }
public function actionQuestionCount($user_id) public function actionQuestionCount($user_id)
@ -62,7 +57,21 @@ class UserController extends Controller{
{ {
$users = User::where(['role' => 1])->get(); $users = User::where(['role' => 1])->get();
$this->cgView->render("user/index.php", ['users' => $users]); $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');
} }
/** /**
@ -81,9 +90,6 @@ class UserController extends Controller{
'baseUrl' => "/admin/user", 'baseUrl' => "/admin/user",
]); ]);
$table = new ViewJsonTable($dataProvider->getJson()); $table = new ViewJsonTable($dataProvider->getJson());
$table->beforeTable(function (){
return PrimaryBtn::create("Список", "/admin/user")->fetch();
});
$table->create(); $table->create();
$table->render(); $table->render();
})); }));
@ -91,11 +97,6 @@ class UserController extends Controller{
echo $this->twig->render('user_table.html.twig'); echo $this->twig->render('user_table.html.twig');
} }
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function actionUpdate($id): void public function actionUpdate($id): void
{ {
echo $this->twig->render('user_update.html.twig'); echo $this->twig->render('user_update.html.twig');

View File

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

View File

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

View File

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

View File

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

View File

@ -1,24 +0,0 @@
<?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

@ -1,23 +0,0 @@
<?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

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

View File

@ -1,19 +0,0 @@
<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

@ -1,28 +0,0 @@
<?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();