igfs/app/modules/module_shop/controllers/KernelShopController.php
2024-12-18 12:46:57 +03:00

84 lines
2.8 KiB
PHP

<?php
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\FileUpload;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\services\ModuleService;
use ZipArchive;
class KernelShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected Files $files;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/kernel/";
$this->moduleShopService = new ModuleShopService();
$this->files = new Files();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
/**
* @throws \Exception
*/
#[NoReturn] public function actionAdd(): void
{
$moduleShopForm = new CreateModuleShopForm();
$moduleShopForm->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', 'igk']);
$file->upload();
$moduleShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpKernelDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $moduleShopForm->getItem('path_to_archive'));
if ($res === TRUE) {
if (!is_dir(RESOURCES_DIR . '/tmp/ms/')) {
mkdir(RESOURCES_DIR . '/tmp/ms/');
}
$tmpModuleShopDirFull = RESOURCES_DIR . '/tmp/ms/' . $tmpKernelDir . "/";
$zip->extractTo($tmpModuleShopDirFull);
$zip->close();
if (file_exists($tmpModuleShopDirFull . "kernel/manifest.json")){
$kernelInfo = $this->moduleShopService->getModuleInfo($tmpModuleShopDirFull . "kernel");
$moduleShopForm->load($kernelInfo);
}
else {
throw new \Exception("Manifest.json file not found");
}
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
}
else {
throw new \Exception("zip not found");
}
if ($moduleShopForm->validate()) {
$kernel = $this->moduleShopService->create($moduleShopForm);
if ($kernel) {
Flash::setMessage("success", "Ядро добавлено.");
$this->redirect("/admin/module_shop/view/" . $kernel->id);
}
}
Flash::setMessage("error", "Ошибка добавления ядра: <br>" . $moduleShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/kernel/create");
}
}