crud menu
This commit is contained in:
106
app/controllers/MenuController.php
Normal file
106
app/controllers/MenuController.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
|
||||
use app\helpers\Debug;
|
||||
use app\models\forms\CreateMenuForm;
|
||||
use app\services\MenuService;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\Controller;
|
||||
use kernel\models\Menu;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
|
||||
class MenuController extends Controller
|
||||
{
|
||||
protected function init(): void
|
||||
{
|
||||
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
|
||||
$this->cgView->layout = "layouts/main.php";
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("menu/form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuService = new MenuService();
|
||||
$menuForm->load($_REQUEST);
|
||||
if ($menuForm->validate()){
|
||||
$menuItem = $menuService->create($menuForm);
|
||||
if ($menuItem){
|
||||
$this->redirect("/admin/menu/" . $menuItem->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/menu/create");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("menu/index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$menuItem = Menu::find($id);
|
||||
|
||||
if (!$menuItem){
|
||||
throw new Exception(message: "The menu item not found");
|
||||
}
|
||||
$this->cgView->render("menu/view.php", ['menu' => $menuItem]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeError
|
||||
* @throws SyntaxError
|
||||
* @throws LoaderError|Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = Menu::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The menu item not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("menu/form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$menuItem = Menu::find($id);
|
||||
if (!$menuItem){
|
||||
throw new Exception(message: "The menu item not found");
|
||||
}
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuService = new MenuService();
|
||||
$menuForm->load($_REQUEST);
|
||||
if ($menuForm->validate()){
|
||||
$menuItem = $menuService->update($menuForm, $menuItem);
|
||||
if ($menuItem){
|
||||
$this->redirect("/admin/menu/" . $menuItem->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/menu/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
Menu::find($id)->delete();
|
||||
$this->redirect("/admin/menu/");
|
||||
}
|
||||
|
||||
}
|
@ -30,16 +30,10 @@ class PostController extends Controller
|
||||
$postForm = new CreatePostForm();
|
||||
$postService = new PostService();
|
||||
$postForm->load($_REQUEST);
|
||||
// Debug::dd($_REQUEST);
|
||||
if(UserService::check($_REQUEST['user_id'])) {
|
||||
// Debug::dd(123);
|
||||
if ($postForm->validate()) {
|
||||
|
||||
// Debug::dd($postForm);
|
||||
$post = $postService->create($postForm);
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/" . $post->id);
|
||||
}
|
||||
if ($postForm->validate()) {
|
||||
$post = $postService->create($postForm);
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/" . $post->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/post/create");
|
||||
@ -49,11 +43,9 @@ class PostController extends Controller
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public function actionIndex(): void
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$contents = Post::all();
|
||||
|
||||
$this->cgView->render("post/index.php", ['contents' => $contents]);
|
||||
$this->cgView->render("post/index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,31 +2,16 @@
|
||||
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 Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
use Itguild\Tables\ViewJsonTable;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\App;
|
||||
use kernel\Controller;
|
||||
use kernel\IGTabel\btn\PrimaryBtn;
|
||||
use kernel\IGTabel\EloquentDataProvider;
|
||||
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{
|
||||
protected function init(): void
|
||||
|
47
app/foo.php
47
app/foo.php
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app;
|
||||
class foo
|
||||
{
|
||||
public array $informationArray;
|
||||
|
||||
/**
|
||||
* @param array $columns
|
||||
* @param array $data
|
||||
* @param string $title
|
||||
* @return string|null
|
||||
*/
|
||||
public function createJsonArray(array $columns, array $data, string $title): ?string
|
||||
{
|
||||
if ($columns && $data) {
|
||||
$this->informationArray = [
|
||||
"meta" => [
|
||||
"title" => $title,
|
||||
"columns" => $columns,
|
||||
"perPage" => 10,
|
||||
"currentPage" => 1,
|
||||
"params" =>
|
||||
[
|
||||
"class" => "table table-bordered",
|
||||
"border" => "1"
|
||||
]
|
||||
],
|
||||
"data" => $data
|
||||
];
|
||||
return $this->toJson($this->informationArray);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $infArr
|
||||
* @return string|null
|
||||
*/
|
||||
protected function toJson(array $infArr): ?string
|
||||
{
|
||||
if ($infArr)
|
||||
return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
return null;
|
||||
}
|
||||
}
|
20
app/models/forms/CreateMenuForm.php
Normal file
20
app/models/forms/CreateMenuForm.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace app\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateMenuForm extends FormModel
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'parent_id' => '',
|
||||
'icon_file' => '',
|
||||
'icon_font' => '',
|
||||
'label' => 'required|min-str-len:1|max-str-len:50',
|
||||
'url' => 'required|min-str-len:1',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
}
|
54
app/services/MenuService.php
Normal file
54
app/services/MenuService.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace app\services;
|
||||
|
||||
use app\helpers\Debug;
|
||||
use kernel\FormModel;
|
||||
use kernel\models\Menu;
|
||||
|
||||
class MenuService
|
||||
{
|
||||
|
||||
public function create(FormModel $form_model): false|Menu
|
||||
{
|
||||
$model = new Menu();
|
||||
$model->parent_id = $form_model->getItem('parent_id');
|
||||
$model->icon_file = $form_model->getItem('icon_file');
|
||||
$model->icon_font = $form_model->getItem('icon_font');
|
||||
$model->label = $form_model->getItem('label');
|
||||
$model->url = $form_model->getItem('url');
|
||||
$model->status = $form_model->getItem('status');
|
||||
if ($model->save()){
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, Menu $menuItem): false|Menu
|
||||
{
|
||||
$menuItem->parent_id = $form_model->getItem('parent_id');
|
||||
$menuItem->icon_file = $form_model->getItem('icon_file');
|
||||
$menuItem->icon_font = $form_model->getItem('icon_font');
|
||||
$menuItem->label = $form_model->getItem('label');
|
||||
$menuItem->url = $form_model->getItem('url');
|
||||
$menuItem->status = $form_model->getItem('status');
|
||||
if ($menuItem->save()){
|
||||
return $menuItem;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function createLabelArr(): array
|
||||
{
|
||||
foreach (Menu::all()->toArray() as $menuItem) {
|
||||
$labelArr[$menuItem['id']] = $menuItem['label'];
|
||||
}
|
||||
if (!empty($labelArr)) {
|
||||
return $labelArr;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
@ -34,16 +34,6 @@ class UserService
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function check(int $user_id): bool
|
||||
{
|
||||
|
||||
if (User::where(['id' => $user_id])->first())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function createUsernameArr(): array
|
||||
{
|
||||
foreach (User::all()->toArray() as $user) {
|
||||
|
17
app/tables/columns/menu/MenuDeleteActionColumn.php
Normal file
17
app/tables/columns/menu/MenuDeleteActionColumn.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns\menu;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
||||
class MenuDeleteActionColumn extends ActionColumn
|
||||
{
|
||||
|
||||
protected string $prefix = "/delete/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
|
||||
}
|
||||
}
|
17
app/tables/columns/menu/MenuEditActionColumn.php
Normal file
17
app/tables/columns/menu/MenuEditActionColumn.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns\menu;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
||||
class MenuEditActionColumn extends ActionColumn
|
||||
{
|
||||
|
||||
protected string $prefix = "/update/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-success'>Редактировать</a> ";
|
||||
}
|
||||
}
|
17
app/tables/columns/menu/MenuViewActionColumn.php
Normal file
17
app/tables/columns/menu/MenuViewActionColumn.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns\menu;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
||||
class MenuViewActionColumn extends ActionColumn
|
||||
{
|
||||
|
||||
protected string $prefix = "/";
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-primary'>Просмотр</a> ";
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\post;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\post;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\post;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
@ -1,8 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\user;
|
||||
|
||||
use app\helpers\Debug;
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
||||
class UserDeleteActionColumn extends ActionColumn
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\user;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
||||
@ -10,7 +10,6 @@ class UserEditActionColumn extends ActionColumn
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
// $link = $this->baseUrl . $this->prefix . $this->id . $this->prefix . "update";
|
||||
$link = $this->baseUrl . $this->prefix . $this->id;
|
||||
return " <a href='$link' class='btn btn-success'>Редактировать</a> ";
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\tables\columns;
|
||||
namespace app\tables\columns\user;
|
||||
|
||||
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||
|
Reference in New Issue
Block a user