diff --git a/app/action_btn/CreateUserBtn.php b/app/action_btn/CreateUserBtn.php new file mode 100644 index 0000000..c42427c --- /dev/null +++ b/app/action_btn/CreateUserBtn.php @@ -0,0 +1,15 @@ +baseUrl$this->prefix' style='margin: 3px'>Создать"; + } +} \ No newline at end of file diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index f3ab12b..e97aabd 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -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 diff --git a/app/services/UserService.php b/app/services/UserService.php index 53b82ba..3ed5c67 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -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; } } \ No newline at end of file diff --git a/app/tables/columns/UserEditActionColumn.php b/app/tables/columns/UserEditActionColumn.php new file mode 100644 index 0000000..b2ff3b3 --- /dev/null +++ b/app/tables/columns/UserEditActionColumn.php @@ -0,0 +1,17 @@ +baseUrl . $this->prefix . $this->id . $this->prefix . "update"; + $link = $this->baseUrl . $this->prefix . "update" . $this->prefix . $this->id; + return " Редактировать "; + } +} \ No newline at end of file diff --git a/index.php b/index.php index d3873c3..2fe36dd 100644 --- a/index.php +++ b/index.php @@ -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']); diff --git a/kernel/CgView.php b/kernel/CgView.php new file mode 100644 index 0000000..88d8540 --- /dev/null +++ b/kernel/CgView.php @@ -0,0 +1,56 @@ +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; + } + +} \ No newline at end of file diff --git a/kernel/Controller.php b/kernel/Controller.php index cdfb5ca..ba849f6 100644 --- a/kernel/Controller.php +++ b/kernel/Controller.php @@ -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(){} } \ No newline at end of file diff --git a/kernel/IGTabel/btn/PrimaryBtn.php b/kernel/IGTabel/btn/PrimaryBtn.php new file mode 100644 index 0000000..f4312c4 --- /dev/null +++ b/kernel/IGTabel/btn/PrimaryBtn.php @@ -0,0 +1,24 @@ +btn = "$title"; + } + + public function fetch(): string + { + return $this->btn; + } + + public static function create(string $title, string $url): PrimaryBtn + { + return new self($title, $url); + } + +} \ No newline at end of file diff --git a/views/admin/layouts/main.php b/views/admin/layouts/main.php new file mode 100644 index 0000000..e5d6dae --- /dev/null +++ b/views/admin/layouts/main.php @@ -0,0 +1,23 @@ + + + + + + Примеры шаблонизатора Twig + + + +
+
+

HEADER

+ +

FOOTER

+
+
+ + + \ No newline at end of file diff --git a/views/admin/user/create.php b/views/admin/user/create.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/views/admin/user/create.php @@ -0,0 +1 @@ + + Логин:
+

+ + Пароль:
+

+ + Email адрес:
+

+ + + + \ No newline at end of file diff --git a/views/admin/user/index.php b/views/admin/user/index.php new file mode 100644 index 0000000..b94727c --- /dev/null +++ b/views/admin/user/index.php @@ -0,0 +1,28 @@ + 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(); \ No newline at end of file diff --git a/views/user_update.html.twig b/views/user_update.html.twig index 4ea5ed3..65a46f6 100644 --- a/views/user_update.html.twig +++ b/views/user_update.html.twig @@ -2,11 +2,6 @@ {% block content %}
- id пользователя:
-

- Логин: