module shop crud

This commit is contained in:
Билай Станислав 2024-10-16 16:06:25 +03:00
parent b7ac923261
commit 209b1e3f29
15 changed files with 414 additions and 22 deletions

View File

@ -2,24 +2,105 @@
namespace app\modules\module_shop\controllers; namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\forms\CreateModuleShopForm;
use app\modules\module_shop\models\ModuleShop;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController; use kernel\AdminController;
use kernel\app_modules\tag\services\TagService; use kernel\app_modules\tag\services\TagService;
use kernel\services\ModuleService; use kernel\services\ModuleService;
use mysql_xdevapi\Exception;
class ModuleShopController extends AdminController class ModuleShopController extends AdminController
{ {
protected ModuleService $moduleService; protected ModuleShopService $moduleShopService;
protected function init(): void protected function init(): void
{ {
parent::init(); parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/module_shop/views/"; $this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/";
$this->moduleService = new ModuleService(); $this->moduleShopService = new ModuleShopService();
} }
public function actionIndex($page_number): void public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$moduleShopForm = new CreateModuleShopForm();
$moduleShopForm->load($_REQUEST);
if ($moduleShopForm->validate()) {
$module = $this->moduleShopService->create($moduleShopForm);
if ($module) {
$this->redirect("/admin/module_shop/" . $module->id);
}
}
$this->redirect("/admin/module_shop/create");
}
public function actionIndex(int $page_number = 1): void
{ {
$this->cgView->render("index.php", ['page_number' => $page_number]); $this->cgView->render("index.php", ['page_number' => $page_number]);
} }
public function actionView(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$this->cgView->render("view.php", ['module' => $module]);
}
public function actionUpdate(int $id): void
{
$model = ModuleShop::find($id);
if (!$model) {
throw new Exception("The module not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
public function actionEdit(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$moduleForm = new CreateModuleShopForm();
$moduleForm->load($_REQUEST);
if ($moduleForm->validate()) {
$module = $this->moduleShopService->update($moduleForm, $module);
if ($module) {
$this->redirect("/admin/module_shop/" . $module->id);
}
}
$this->redirect("/admin/module_shop/update/" . $id);
}
public function actionDelete(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$module->delete();
$this->redirect("/admin/module_shop");
}
public function actionPack(int $id): void
{
$module = ModuleShop::find($id);
if (!$module) {
throw new Exception("The module not found");
}
$this->moduleShopService->packModule($module);
}
} }

View File

@ -4,6 +4,17 @@ namespace app\modules\module_shop\models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $name
* @property string $slug
* @property string $version
* @property string $description
* @property string $author
* @property int $status
* @property string $path_to_archive
* @property string $dependence
*/
class ModuleShop extends Model class ModuleShop extends Model
{ {
const DISABLE_STATUS = 0; const DISABLE_STATUS = 0;

View File

@ -0,0 +1,21 @@
<?php
namespace app\modules\module_shop\models\forms;
use kernel\FormModel;
class CreateModuleShopForm extends FormModel
{
public function rules(): array
{
return [
'name' => 'required|min-str-len:5',
'version' => 'required',
'description' => 'required|min-str-len:10',
'author' => 'required',
'status' => 'required',
'path_to_archive' => 'required',
'dependence' => '',
];
}
}

View File

@ -7,12 +7,13 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router){ App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router){
App::$collector->get('/', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']); App::$collector->get('/', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']); App::$collector->get('/page/{page_number}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']);
// App::$collector->get('/create', [\kernel\modules\menu\controllers\MenuController::class, 'actionCreate']); App::$collector->get('/create', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionCreate']);
// App::$collector->post("/", [\kernel\modules\menu\controllers\MenuController::class, 'actionAdd']); App::$collector->post("/", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionAdd']);
// App::$collector->get('/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionView']); App::$collector->get('/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionView']);
// App::$collector->any('/update/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionUpdate']); App::$collector->any('/update/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionUpdate']);
// App::$collector->any("/edit/{id}", [\kernel\modules\menu\controllers\MenuController::class, 'actionEdit']); App::$collector->any("/edit/{id}", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionEdit']);
// App::$collector->get('/delete/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionDelete']); App::$collector->get('/delete/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionDelete']);
App::$collector->get('/pack/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionPack']);
}); });
}); });

View File

@ -0,0 +1,76 @@
<?php
namespace app\modules\module_shop\services;
use app\modules\module_shop\models\ModuleShop;
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\Slug;
use kernel\services\ModuleService;
class ModuleShopService extends ModuleService
{
public function create(FormModel $form_model): false|ModuleShop
{
$model = new ModuleShop();
$model->name = $form_model->getItem("name");
$model->version = $form_model->getItem("version");
$model->description = $form_model->getItem("description");
$model->author = $form_model->getItem("author");
$model->status = $form_model->getItem("status");
$model->path_to_archive = $form_model->getItem("path_to_archive");
$model->dependence = $form_model->getItem("dependence");
$model->slug = Slug::createSlug($model->name, ModuleShop::class);
if ($model->save()) {
return $model;
}
return false;
}
public function update(FormModel $form_model, ModuleShop $model): false|ModuleShop
{
if ($model->name !== $form_model->getItem("name")) {
$model->name = $form_model->getItem("name");
$model->slug = Slug::createSlug($model->name, ModuleShop::class);
}
$model->version = $form_model->getItem("version");
$model->description = $form_model->getItem("description");
$model->author = $form_model->getItem("author");
$model->status = $form_model->getItem("status");
$model->path_to_archive = $form_model->getItem("path_to_archive");
$model->dependence = $form_model->getItem("dependence");
if ($model->save()) {
return $model;
}
return false;
}
public function packModule($module): void
{
$tmpModuleDir = md5(time());
$authorSlug = Slug::createSlug($module->author);
$nameSlug = Slug::createSlug($module->name);
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . '/';
$fileHelper = new Files();
Debug::dd(APP_DIR . '/modules/' . $nameSlug);
$fileHelper->copy_folder(APP_DIR . '/modules/' . $nameSlug, $tmpModuleDirFull . 'app/');
// $fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $nameSlug, $tmpModuleDirFull . 'kernel/');
$fileHelper->pack(
$tmpModuleDirFull,
RESOURCES_DIR . '/module_shop/' .
$authorSlug . '/' .
$module->slug . '/' .
$module->version . '/' .
$nameSlug . '.itguild'
);
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\modules\module_shop\table\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class ModuleShopDeleteActionColumn extends ActionColumn
{
protected string $prefix = "/delete/";
public function fetch()
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-danger'>Удалить</a> ";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\modules\module_shop\table\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class ModuleShopEditActionColumn 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> ";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\modules\module_shop\table\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class ModuleShopPackActionColumn extends ActionColumn
{
protected string $prefix = "/pack/";
public function fetch()
{
$link = $this->baseUrl . $this->prefix . $this->id;
return " <a href='$link' class='btn btn-info'>Заархивировать</a> ";
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\modules\module_shop\table\columns;
use Itguild\Tables\ActionColumn\ActionColumn;
class ModuleShopViewActionColumn 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> ";
}
}

View File

@ -0,0 +1,92 @@
<?php
/**
* @var ModuleShop $model
*/
use app\modules\module_shop\models\ModuleShop;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/module_shop/edit/" . $model->id : "/admin/module_shop");
$form->field(\itguild\forms\inputs\TextInput::class, 'name', [
'class' => "form-control",
'placeholder' => 'Название',
'value' => $model->name ?? ''
])
->setLabel("Название")
->render();
$form->field(\itguild\forms\inputs\TextInput::class, 'author', [
'class' => "form-control",
'placeholder' => 'Автор',
'value' => $model->author ?? ''
])
->setLabel("Автор")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "version", params: [
'class' => "form-control",
'placeholder' => 'Версия',
'value' => $model->version ?? ''
])
->setLabel("Версия")
->render();
$form->field(class: \itguild\forms\inputs\TextArea::class, name: "description", params: [
'class' => "form-control",
'placeholder' => 'Описание',
'rows' => '10',
'value' => $model->description ?? ''
])
->setLabel("Описание")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "dependence", params: [
'class' => "form-control",
'placeholder' => 'Зависимости',
'value' => $model->dependence ?? ''
])
->setLabel("Зависимости")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "path_to_archive", params: [
'class' => "form-control",
'placeholder' => 'Путь к файлу модуля',
'value' => $model->path_to_archive ?? ''
])
->setLabel("Путь к файлу модуля")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [
'class' => "form-control",
'value' => $model->status ?? ''
])
->setLabel("Статус")
->setOptions(ModuleShop::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -1,9 +1,10 @@
<?php <?php
/** /**
* @var \Illuminate\Database\Eloquent\Collection $menuItem * @var \Illuminate\Database\Eloquent\Collection $module
* @var int $page_number * @var int $page_number
*/ */
use app\modules\module_shop\models\ModuleShop;
use Itguild\EloquentTable\EloquentDataProvider; use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable; use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
@ -16,11 +17,21 @@ $table = new ListEloquentTable(new EloquentDataProvider(\app\modules\module_shop
'currentPage' => $page_number, 'currentPage' => $page_number,
'perPage' => 8, 'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/settings/menu", 'baseUrl' => "/admin/module_shop",
])); ]));
$table->columns([
'status' => function ($data) {
return ModuleShop::getStatus()[$data];
}
]);
$table->beforePrint(function () { $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/module_shop/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/module_shop/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
}); });
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopViewActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopEditActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopDeleteActionColumn::class);
$table->addAction(\app\modules\module_shop\table\columns\ModuleShopPackActionColumn::class);
$table->create(); $table->create();
$table->render(); $table->render();

View File

@ -0,0 +1,31 @@
<?php
/**
* @var Collection $module
*/
use Illuminate\Database\Eloquent\Collection;
use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($module, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/module_shop",
]));
$table->beforePrint(function () use ($module) {
$btn = PrimaryBtn::create("Список", "/admin/module_shop")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/module_shop/update/" . $module->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/module_shop/delete/" . $module->id)->fetch();
return $btn;
});
$table->rows([
'status' => (function ($data) {
return \app\modules\module_shop\models\ModuleShop::getStatus()[$data];
})
]);
$table->create();
$table->render();

View File

@ -132,9 +132,9 @@ class Slug
* @param $model * @param $model
* @return string * @return string
*/ */
public static function createSlug(string $title, $model = null): string public static function createSlug(string $data, $model = null): string
{ {
$slug = Slug::url_slug($title, ['transliterate' => true, 'lowercase' => true]); $slug = Slug::url_slug($data, ['transliterate' => true, 'lowercase' => true]);
if ($model === null) { if ($model === null) {
return $slug; return $slug;
} }

View File

@ -42,7 +42,7 @@ class PostController extends AdminController
* @throws Exception * @throws Exception
*/ */
public function actionIndex($page_number = 1): void public function actionIndex(int $page_number = 1): void
{ {
$this->cgView->render("index.php", ['page_number' => $page_number]); $this->cgView->render("index.php", ['page_number' => $page_number]);
} }
@ -50,7 +50,7 @@ class PostController extends AdminController
/** /**
* @throws Exception * @throws Exception
*/ */
public function actionView($id): void public function actionView(int $id): void
{ {
$content = Post::find($id); $content = Post::find($id);
@ -63,7 +63,7 @@ class PostController extends AdminController
/** /**
* @throws Exception * @throws Exception
*/ */
public function actionUpdate($id): void public function actionUpdate(int $id): void
{ {
$model = Post::find($id); $model = Post::find($id);
if (!$model){ if (!$model){
@ -76,17 +76,16 @@ class PostController extends AdminController
/** /**
* @throws Exception * @throws Exception
*/ */
public function actionEdit($id): void public function actionEdit(int $id): void
{ {
$post = Post::find($id); $post = Post::find($id);
if (!$post){ if (!$post){
throw new Exception(message: "The post not found"); throw new Exception(message: "The post not found");
} }
$postForm = new CreatePostForm(); $postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST); $postForm->load($_REQUEST);
if ($postForm->validate()) { if ($postForm->validate()) {
$post = $postService->update($postForm, $post); $post = $this->postService->update($postForm, $post);
if ($post) { if ($post) {
$this->redirect("/admin/post/" . $post->id); $this->redirect("/admin/post/" . $post->id);
} }
@ -94,9 +93,15 @@ class PostController extends AdminController
$this->redirect("/admin/post/update/" . $id); $this->redirect("/admin/post/update/" . $id);
} }
#[NoReturn] public function actionDelete($id): void /**
* @throws Exception
*/
#[NoReturn] public function actionDelete(int $id): void
{ {
$post = Post::find($id)->first(); $post = Post::find($id)->first();
if (!$post){
throw new Exception(message: "The post not found");
}
$post->delete(); $post->delete();
$this->redirect("/admin/post/"); $this->redirect("/admin/post/");
} }

View File

@ -32,7 +32,6 @@ class AdminThemeService
public function setActiveAdminTheme(string $theme): void public function setActiveAdminTheme(string $theme): void
{ {
$active_admin_theme = Option::where("key", "active_admin_theme")->first(); $active_admin_theme = Option::where("key", "active_admin_theme")->first();
Debug::prn(getConst($theme));
$active_admin_theme->value = getConst($theme); $active_admin_theme->value = getConst($theme);
$active_admin_theme->save(); $active_admin_theme->save();
} }