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(); + +?> +