igfs/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php

241 lines
8.6 KiB
PHP

<?php
namespace kernel\modules\module_shop_client\controllers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\RESTClient;
use kernel\helpers\SMTP;
use kernel\Mailing;
use kernel\Request;
use kernel\services\AdminThemeService;
use kernel\services\KernelService;
use kernel\services\ModuleService;
use kernel\services\ModuleShopService;
use kernel\services\TokenService;
use PHPMailer\PHPMailer\Exception;
class ModuleShopClientController extends AdminController
{
protected Client $client;
protected ModuleService $moduleService;
protected KernelService $kernelService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/module_shop_client/views/";
$this->client = new Client();
$this->moduleService = new ModuleService();
$this->kernelService = new KernelService();
}
/**
* @throws GuzzleException
*/
public function actionIndex(int $page_number = 1): void
{
if ($this->moduleService->issetModuleShopToken()) {
if ($this->moduleService->isServerAvailable()) {
$per_page = 8;
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$module_count = count($modules_info);
$modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page);
$this->cgView->render("index.php", [
'modules_info' => $modules_info,
'moduleService' => $this->moduleService,
'page_number' => $page_number,
'module_count' => $module_count,
'per_page' => $per_page,
'kernelService' => new KernelService(),
'adminThemeService' => new AdminThemeService(),
]);
} else {
$this->cgView->render("module_shop_error_connection.php");
}
} else {
$this->cgView->render("login_at_module_shop.php");
}
}
public function actionView(int $id): void
{
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id);
$module_info = json_decode($module_info->getBody()->getContents(), true);
$this->cgView->render("view.php", ['data' => $module_info]);
}
/**
* @throws GuzzleException
*/
#[NoReturn] public function actionInstall(): void
{
$request = new Request();
$id = $request->get("id");
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id);
$module_info = json_decode($module_info->getBody()->getContents(), true);
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
$this->moduleService->installModule('/resources/tmp/modules/' . basename($module_info['path_to_archive']));
Flash::setMessage("success", "Модуль успешно установлен.");
$this->redirect('/admin/module_shop_client', 302);
}
#[NoReturn] public function actionUpdate(): void
{
$request = new Request();
$slug = $request->get("slug");
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
foreach ($modules_info as $module) {
if ($module['slug'] === $slug) {
$path = $module['path_to_archive'];
}
}
if (isset($path)) {
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $path, RESOURCES_DIR . "/tmp/modules");
$this->moduleService->updateModule('/resources/tmp/modules/' . basename($path));
Flash::setMessage("success", "Модуль успешно обновлен.");
} else {
Flash::setMessage("error", "Ошибка обновления модуля.");
}
$this->redirect('/admin/module_shop_client', 302);
}
public function actionRenderKernelUpdateForm(): void
{
$this->cgView->render("kernel_update.php");
}
#[NoReturn] public function actionKernelUpdate(): void
{
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
foreach ($modules_info as $module) {
if ($module['slug'] === 'kernel') {
$path = $module['path_to_archive'];
}
}
if (isset($path)) {
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $path, RESOURCES_DIR . "/tmp/kernel");
if ($this->kernelService->updateKernel('/resources/tmp/kernel/' . basename($path))) {
Flash::setMessage("success", "Ядро успешно обновлено.");
} else {
Flash::setMessage("error", "Ошибка обновления ядра.");
}
} else {
Flash::setMessage("error", "Ошибка обновления ядра.");
}
$this->redirect('/admin/module_shop_client', 302);
}
#[NoReturn] public function actionDelete(): void
{
$request = new Request();
$slug = $request->get("slug");
$module_info = $this->moduleService->getModuleInfoBySlug($slug);
$this->moduleService->uninstallModule($module_info['app_module_path']);
Flash::setMessage("success", "Модуль успешно удален.");
$this->redirect('/admin/module_shop_client', 302);
}
public function actionSearch(int $page_number = 1): void
{
$request = new Request();
$filters = $request->get();
if ($this->moduleService->issetModuleShopToken()) {
if ($this->moduleService->isServerAvailable()) {
$modules_info = [];
$per_page = 8;
$modules = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules = json_decode($modules->getBody()->getContents(), true);
foreach ($modules as $module) {
foreach ($filters as $key => $value) {
if ($value === '') continue;
if ($module[$key] !== $value) {
break;
}
$modules_info[] = $module;
}
}
$module_count = count($modules_info);
$modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page);
$this->cgView->render("index.php", [
'modules_info' => $modules_info,
'moduleService' => $this->moduleService,
'page_number' => $page_number,
'module_count' => $module_count,
'per_page' => $per_page,
'kernelService' => new KernelService(),
'adminThemeService' => new AdminThemeService(),
]);
} else {
$this->cgView->render("module_shop_error_connection.php");
}
} else {
$this->cgView->render("login_at_module_shop.php");
}
}
/**
* @throws Exception
*/
public function actionAuth(): void
{
$request = new Request();
$address = $request->post("email");
$moduleShopService = new ModuleShopService();
$result = $moduleShopService->email_auth($address);
if ($result['status'] == 'success'){
$this->cgView->render('enter_code.php', ['email' => $address]);
}
$this->cgView->render('module_shop_error_connection.php', ['email' => $address]);
}
public function actionCodeCheck(): void
{
$request = new Request();
$code = $request->post("code");
$moduleShopService = new ModuleShopService();
$result = $moduleShopService->code_check($code);
if (isset($result['access_token'])){
$envFile = \EnvEditor\EnvFile::loadFrom(ROOT_DIR . "/.env");
$envFile->setValue("MODULE_SHOP_TOKEN", $result['access_token']);
$envFile->saveTo(ROOT_DIR . "/.env");
$this->cgView->render('success_login.php');
}
$this->cgView->render('module_shop_error_connection.php');
}
}