diff --git a/app/modules/module_shop/controllers/ModuleShopController.php b/app/modules/module_shop/controllers/ModuleShopController.php index 69da393..54505d0 100644 --- a/app/modules/module_shop/controllers/ModuleShopController.php +++ b/app/modules/module_shop/controllers/ModuleShopController.php @@ -2,24 +2,105 @@ 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\app_modules\tag\services\TagService; use kernel\services\ModuleService; +use mysql_xdevapi\Exception; class ModuleShopController extends AdminController { - protected ModuleService $moduleService; + protected ModuleShopService $moduleShopService; protected function init(): void { parent::init(); - $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/module_shop/views/"; - $this->moduleService = new ModuleService(); + $this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/"; + $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]); } + 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); + } } \ No newline at end of file diff --git a/app/modules/module_shop/models/ModuleShop.php b/app/modules/module_shop/models/ModuleShop.php index 9eed21b..acf631d 100644 --- a/app/modules/module_shop/models/ModuleShop.php +++ b/app/modules/module_shop/models/ModuleShop.php @@ -4,6 +4,17 @@ namespace app\modules\module_shop\models; 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 { const DISABLE_STATUS = 0; diff --git a/app/modules/module_shop/models/forms/CreateModuleShopForm.php b/app/modules/module_shop/models/forms/CreateModuleShopForm.php new file mode 100644 index 0000000..43d295b --- /dev/null +++ b/app/modules/module_shop/models/forms/CreateModuleShopForm.php @@ -0,0 +1,21 @@ + 'required|min-str-len:5', + 'version' => 'required', + 'description' => 'required|min-str-len:10', + 'author' => 'required', + 'status' => 'required', + 'path_to_archive' => 'required', + 'dependence' => '', + ]; + } +} \ No newline at end of file diff --git a/app/modules/module_shop/routs/ms.php b/app/modules/module_shop/routs/ms.php index 9544ed2..e9adcab 100644 --- a/app/modules/module_shop/routs/ms.php +++ b/app/modules/module_shop/routs/ms.php @@ -7,12 +7,13 @@ App::$collector->group(["prefix" => "admin"], 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('/page/{page_number}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionIndex']); -// App::$collector->get('/create', [\kernel\modules\menu\controllers\MenuController::class, 'actionCreate']); -// App::$collector->post("/", [\kernel\modules\menu\controllers\MenuController::class, 'actionAdd']); -// App::$collector->get('/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionView']); -// App::$collector->any('/update/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionUpdate']); -// App::$collector->any("/edit/{id}", [\kernel\modules\menu\controllers\MenuController::class, 'actionEdit']); -// App::$collector->get('/delete/{id}', [\kernel\modules\menu\controllers\MenuController::class, 'actionDelete']); + App::$collector->get('/create', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionCreate']); + App::$collector->post("/", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionAdd']); + App::$collector->get('/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\modules\module_shop\controllers\ModuleShopController::class, 'actionEdit']); + 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']); }); }); diff --git a/app/modules/module_shop/services/ModuleShopService.php b/app/modules/module_shop/services/ModuleShopService.php new file mode 100644 index 0000000..ab03f9d --- /dev/null +++ b/app/modules/module_shop/services/ModuleShopService.php @@ -0,0 +1,76 @@ +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); + } +} \ No newline at end of file diff --git a/app/modules/module_shop/table/columns/ModuleShopDeleteActionColumn.php b/app/modules/module_shop/table/columns/ModuleShopDeleteActionColumn.php new file mode 100644 index 0000000..375c0c4 --- /dev/null +++ b/app/modules/module_shop/table/columns/ModuleShopDeleteActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Удалить "; + } +} \ No newline at end of file diff --git a/app/modules/module_shop/table/columns/ModuleShopEditActionColumn.php b/app/modules/module_shop/table/columns/ModuleShopEditActionColumn.php new file mode 100644 index 0000000..7a8794f --- /dev/null +++ b/app/modules/module_shop/table/columns/ModuleShopEditActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Редактировать "; + } +} \ No newline at end of file diff --git a/app/modules/module_shop/table/columns/ModuleShopPackActionColumn.php b/app/modules/module_shop/table/columns/ModuleShopPackActionColumn.php new file mode 100644 index 0000000..b76a666 --- /dev/null +++ b/app/modules/module_shop/table/columns/ModuleShopPackActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Заархивировать "; + } +} \ No newline at end of file diff --git a/app/modules/module_shop/table/columns/ModuleShopViewActionColumn.php b/app/modules/module_shop/table/columns/ModuleShopViewActionColumn.php new file mode 100644 index 0000000..3c5601d --- /dev/null +++ b/app/modules/module_shop/table/columns/ModuleShopViewActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Просмотр "; + } +} \ No newline at end of file diff --git a/app/modules/module_shop/views/form.php b/app/modules/module_shop/views/form.php new file mode 100644 index 0000000..701fc97 --- /dev/null +++ b/app/modules/module_shop/views/form.php @@ -0,0 +1,92 @@ +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(); + +?> +
+
+ field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [ + 'class' => "btn btn-primary ", + 'value' => 'Отправить', + 'typeInput' => 'submit' + ]) + ->render(); + ?> +
+
+ field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [ + 'class' => "btn btn-warning", + 'value' => 'Сбросить', + 'typeInput' => 'reset' + ]) + ->render(); + ?> +
+
+endForm(); diff --git a/app/modules/module_shop/views/index.php b/app/modules/module_shop/views/index.php index 310a41f..86e64ff 100644 --- a/app/modules/module_shop/views/index.php +++ b/app/modules/module_shop/views/index.php @@ -1,9 +1,10 @@ $page_number, 'perPage' => 8, '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 () { return PrimaryBtn::create("Создать", "/admin/module_shop/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->render(); \ No newline at end of file diff --git a/app/modules/module_shop/views/view.php b/app/modules/module_shop/views/view.php new file mode 100644 index 0000000..48960df --- /dev/null +++ b/app/modules/module_shop/views/view.php @@ -0,0 +1,31 @@ + ["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(); \ No newline at end of file diff --git a/kernel/helpers/Slug.php b/kernel/helpers/Slug.php index 0bde8c1..cfbd995 100644 --- a/kernel/helpers/Slug.php +++ b/kernel/helpers/Slug.php @@ -132,9 +132,9 @@ class Slug * @param $model * @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) { return $slug; } diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index a085533..b65c628 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -42,7 +42,7 @@ class PostController extends AdminController * @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]); } @@ -50,7 +50,7 @@ class PostController extends AdminController /** * @throws Exception */ - public function actionView($id): void + public function actionView(int $id): void { $content = Post::find($id); @@ -63,7 +63,7 @@ class PostController extends AdminController /** * @throws Exception */ - public function actionUpdate($id): void + public function actionUpdate(int $id): void { $model = Post::find($id); if (!$model){ @@ -76,17 +76,16 @@ class PostController extends AdminController /** * @throws Exception */ - public function actionEdit($id): void + public function actionEdit(int $id): void { $post = Post::find($id); if (!$post){ throw new Exception(message: "The post not found"); } $postForm = new CreatePostForm(); - $postService = new PostService(); $postForm->load($_REQUEST); if ($postForm->validate()) { - $post = $postService->update($postForm, $post); + $post = $this->postService->update($postForm, $post); if ($post) { $this->redirect("/admin/post/" . $post->id); } @@ -94,9 +93,15 @@ class PostController extends AdminController $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(); + if (!$post){ + throw new Exception(message: "The post not found"); + } $post->delete(); $this->redirect("/admin/post/"); } diff --git a/kernel/services/AdminThemeService.php b/kernel/services/AdminThemeService.php index 6bdc894..c833cbb 100644 --- a/kernel/services/AdminThemeService.php +++ b/kernel/services/AdminThemeService.php @@ -32,7 +32,6 @@ class AdminThemeService public function setActiveAdminTheme(string $theme): void { $active_admin_theme = Option::where("key", "active_admin_theme")->first(); - Debug::prn(getConst($theme)); $active_admin_theme->value = getConst($theme); $active_admin_theme->save(); }