igfs/app/modules/module_shop/controllers/ThemeShopController.php
2025-01-19 21:22:27 +03:00

85 lines
2.9 KiB
PHP

<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\forms\CreateAdminThemeShopForm;
use app\modules\module_shop\models\forms\CreateThemeShopForm;
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");
}
}