themes add and some fix
This commit is contained in:
84
app/modules/module_shop/controllers/ThemeShopController.php
Normal file
84
app/modules/module_shop/controllers/ThemeShopController.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\module_shop\controllers;
|
||||
|
||||
use app\modules\module_shop\models\forms\CreateAdminThemeShopForm;
|
||||
use app\modules\module_shop\services\ModuleShopService;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\FileUpload;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use ZipArchive;
|
||||
|
||||
class ThemeShopController extends AdminController
|
||||
{
|
||||
protected ModuleShopService $moduleShopService;
|
||||
protected Files $files;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/theme/";
|
||||
$this->moduleShopService = new ModuleShopService();
|
||||
$this->files = new Files();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$themeShopForm = new CreateThemeShopForm();
|
||||
$themeShopForm->load($_REQUEST);
|
||||
|
||||
if (isset($_FILES['path_to_archive']) && $_FILES['path_to_archive']['error'] === UPLOAD_ERR_OK) {
|
||||
$file = new FileUpload($_FILES['path_to_archive'], ['zip', 'rar', 'igt']);
|
||||
$file->upload();
|
||||
$themeShopForm->setItem('path_to_archive', $file->getUploadFile());
|
||||
}
|
||||
|
||||
$tmpThemeDir = md5(time());
|
||||
$zip = new ZipArchive;
|
||||
$res = $zip->open(ROOT_DIR . $themeShopForm->getItem('path_to_archive'));
|
||||
if ($res === TRUE) {
|
||||
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
|
||||
$oldMask = umask(0);
|
||||
mkdir(RESOURCES_DIR . '/tmp/ms/', 0775, true);
|
||||
umask($oldMask);
|
||||
}
|
||||
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpThemeDir . "/";
|
||||
$zip->extractTo($tmpModuleShopDirFull);
|
||||
$zip->close();
|
||||
|
||||
if (file_exists($tmpModuleShopDirFull . "meta/manifest.json")){
|
||||
$themeInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "meta");
|
||||
$themeShopForm->load($themeInfo);
|
||||
}
|
||||
else {
|
||||
throw new \Exception("Manifest.json file not found");
|
||||
}
|
||||
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
|
||||
|
||||
}
|
||||
else {
|
||||
throw new \Exception("zip not found");
|
||||
}
|
||||
|
||||
if ($themeShopForm->validate()) {
|
||||
$theme = $this->moduleShopService->create($themeShopForm);
|
||||
if ($theme) {
|
||||
Flash::setMessage("success", "Тема сайта добавлена.");
|
||||
$this->redirect("/admin/module_shop/view/" . $theme->id);
|
||||
}
|
||||
}
|
||||
Flash::setMessage("error", "Ошибка добавления темы сайта: <br>" . $themeShopForm->getErrorsStr());
|
||||
$this->redirect("/admin/module_shop/theme/create");
|
||||
}
|
||||
}
|
22
app/modules/module_shop/models/forms/CreateThemeShopForm.php
Normal file
22
app/modules/module_shop/models/forms/CreateThemeShopForm.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\module_shop\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateThemeShopForm extends FormModel
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|min-str-len:3',
|
||||
'version' => 'required',
|
||||
'description' => '',
|
||||
'author' => 'required',
|
||||
'status' => 'required',
|
||||
'slug' => 'required',
|
||||
'type' => 'required',
|
||||
'path_to_archive' => 'required',
|
||||
];
|
||||
}
|
||||
}
|
51
app/modules/module_shop/views/theme/form.php
Normal file
51
app/modules/module_shop/views/theme/form.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @var ModuleShop $model
|
||||
*/
|
||||
|
||||
use app\modules\module_shop\models\ModuleShop;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm("/admin/module_shop/theme", enctype: 'multipart/form-data');
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\File::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();
|
Reference in New Issue
Block a user