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

180 lines
6.2 KiB
PHP
Raw Normal View History

2024-11-18 16:04:57 +03:00
<?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;
2024-12-16 14:26:13 +03:00
use kernel\helpers\SMTP;
use kernel\Mailing;
2024-11-18 16:04:57 +03:00
use kernel\modules\module_shop_client\services\ModuleShopClientService;
use kernel\Request;
use kernel\services\ModuleService;
2024-12-16 14:26:13 +03:00
use kernel\services\ModuleShopService;
use kernel\services\TokenService;
use PHPMailer\PHPMailer\Exception;
2024-11-18 16:04:57 +03:00
class ModuleShopClientController extends AdminController
{
protected Client $client;
protected ModuleService $moduleService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/module_shop_client/views/";
$this->client = new Client();
$this->moduleService = new ModuleService();
}
/**
* @throws GuzzleException
*/
public function actionIndex(int $page_number = 1): void
{
2024-12-16 14:26:13 +03:00
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,
]);
} else {
$this->cgView->render("module_shop_error_connection.php");
}
} else {
$this->cgView->render("login_at_module_shop.php");
}
2024-11-18 16:04:57 +03:00
}
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);
}
#[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);
}
2024-12-16 14:26:13 +03:00
/**
* @throws Exception
*/
public function actionAuth(): void
{
$request = new Request();
$address = $request->post("email");
// $mailing = new Mailing();
// $mailing->send_html("login_by_code.php", ['code' => mt_rand(100000, 999999)], [
// 'address' => $address,
// 'subject' => "Код авторизации",
// "from_name" => $_ENV['APP_NAME']
// ]);
$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");
// $mailing = new Mailing();
// $mailing->send_html("login_by_code.php", ['code' => mt_rand(100000, 999999)], [
// 'address' => $address,
// 'subject' => "Код авторизации",
// "from_name" => $_ENV['APP_NAME']
// ]);
$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');
}
2024-11-18 16:04:57 +03:00
}