298 lines
12 KiB
PHP
298 lines
12 KiB
PHP
<?php
|
|
|
|
namespace kernel\modules\module_shop_client\controllers;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
use Josantonius\Session\Facades\Session;
|
|
use kernel\AdminController;
|
|
use kernel\Flash;
|
|
use kernel\helpers\Debug;
|
|
use kernel\helpers\Files;
|
|
use kernel\helpers\RESTClient;
|
|
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
|
use kernel\Request;
|
|
use kernel\services\AdminThemeService;
|
|
use kernel\services\KernelService;
|
|
use kernel\services\ModuleService;
|
|
use kernel\services\ModuleShopService;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
class ModuleShopClientController extends AdminController
|
|
{
|
|
|
|
protected Client $client;
|
|
protected ModuleService $moduleService;
|
|
protected KernelService $kernelService;
|
|
protected ModuleShopClientService $moduleShopClientService;
|
|
|
|
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();
|
|
$this->moduleShopClientService = new ModuleShopClientService();
|
|
}
|
|
|
|
/**
|
|
* @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_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 $key => $module) {
|
|
foreach ($filters as $column => $value) {
|
|
if ($value === '') continue;
|
|
if (is_numeric($value)) {
|
|
if ($module[$column] !== $value) {
|
|
unset($modules_info[$key]);
|
|
}
|
|
} elseif (is_string($value)) {
|
|
if (!str_contains($module[$column], $value)) {
|
|
unset($modules_info[$key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$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(),
|
|
'filterValues' => $filters
|
|
]);
|
|
} 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');
|
|
}
|
|
|
|
#[NoReturn] public function actionAdminThemeInstall(): void
|
|
{
|
|
$request = new Request();
|
|
$id = $request->get("id");
|
|
$adminThemeInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id);
|
|
|
|
$adminThemeInfo = json_decode($adminThemeInfo->getBody()->getContents(), true);
|
|
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $adminThemeInfo['path_to_archive'], RESOURCES_DIR . "/tmp/admin_themes");
|
|
if ($this->adminThemeService->install('/resources/tmp/admin_themes/' . basename($adminThemeInfo['path_to_archive']))) {
|
|
Flash::setMessage("success", "Тема админ-панели успешно установлена.");
|
|
} else {
|
|
Session::start();
|
|
Session::set("error", implode(";", $this->adminThemeService->getErrors()));
|
|
}
|
|
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
#[NoReturn] public function actionAdminThemeUpdate(): 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/admin_themes");
|
|
$this->adminThemeService->update('/resources/tmp/admin_themes/' . basename($path));
|
|
Flash::setMessage("success", "Тема админ-панели успешно обновлена.");
|
|
} else {
|
|
Flash::setMessage("error", "Ошибка обновления темы админ-панели.");
|
|
}
|
|
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
#[NoReturn] public function actionAdminThemeDelete(): void
|
|
{
|
|
$request = new Request();
|
|
$slug = $request->get("slug");
|
|
$adminThemeInfo = $this->adminThemeService->getAdminThemeInfoBySlug($slug);
|
|
$this->adminThemeService->uninstall($adminThemeInfo['path']);
|
|
|
|
Flash::setMessage("success", "Тема админ-панели успешно удалена.");
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
} |