igfs/app/modules/module_shop/controllers/AdminThemeShopController.php

84 lines
2.9 KiB
PHP
Raw Normal View History

2024-12-23 14:16:00 +03:00
<?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 AdminThemeShopController extends AdminController
{
protected ModuleShopService $moduleShopService;
protected Files $files;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = APP_DIR . "/modules/module_shop/views/admin_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
{
$adminThemeShopForm = new CreateAdminThemeShopForm();
$adminThemeShopForm->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();
$adminThemeShopForm->setItem('path_to_archive', $file->getUploadFile());
}
$tmpThemeDir = md5(time());
$zip = new ZipArchive;
$res = $zip->open(ROOT_DIR . $adminThemeShopForm->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");
$adminThemeShopForm->load($themeInfo);
}
else {
throw new \Exception("Manifest.json file not found");
}
$this->files->recursiveRemoveDir($tmpModuleShopDirFull);
}
else {
throw new \Exception("zip not found");
}
if ($adminThemeShopForm->validate()) {
$theme = $this->moduleShopService->create($adminThemeShopForm);
if ($theme) {
Flash::setMessage("success", "Тема админ-панели добавлена.");
$this->redirect("/admin/module_shop/view/" . $theme->id);
}
}
Flash::setMessage("error", "Ошибка добавления темы админ-панели: <br>" . $adminThemeShopForm->getErrorsStr());
$this->redirect("/admin/module_shop/admin_theme/create");
}
}