95 lines
3.0 KiB
PHP
Raw Normal View History

2025-01-15 16:57:03 +03:00
<?php
namespace kernel\modules\themes\controllers;
use DirectoryIterator;
2025-01-23 16:57:03 +03:00
use GuzzleHttp\Exception\GuzzleException;
2025-01-15 16:57:03 +03:00
use JetBrains\PhpStorm\NoReturn;
2025-01-23 16:57:03 +03:00
use Josantonius\Session\Exceptions\HeadersSentException;
use Josantonius\Session\Exceptions\SessionNotStartedException;
use Josantonius\Session\Exceptions\SessionStartedException;
use Josantonius\Session\Exceptions\WrongSessionOptionException;
use Josantonius\Session\Facades\Session;
2025-01-15 16:57:03 +03:00
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\Request;
2025-01-23 16:57:03 +03:00
use kernel\services\ModuleService;
use kernel\services\ThemeService;
2025-01-15 16:57:03 +03:00
class ThemeController extends AdminController
{
2025-01-23 16:57:03 +03:00
public ThemeService $themeService;
public ModuleService $moduleService;
2025-01-15 16:57:03 +03:00
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/themes/views/";
2025-01-23 16:57:03 +03:00
$this->themeService = new ThemeService();
$this->moduleService = new ModuleService();
2025-01-15 16:57:03 +03:00
}
public function actionIndex(): void
{
$themePaths = Option::where("key", "theme_paths")->first();
$dirs = [];
if ($themePaths){
$path = json_decode($themePaths->value);
foreach ($path->paths as $p){
$dirs[] = getConst($p);
}
}
$infoToTable = [];
$meta = [];
$meta['columns'] = [
"preview" => "Превью",
"name" => "Название",
"author" => "Автор",
"version" => "Версия",
"description" => "Описание"
];
$meta['params'] = ["class" => "table table-bordered"];
$meta['perPage'] = 10;
$meta['baseUrl'] = "/admin/settings/themes";
$meta['currentPage'] = 1;
$infoToTable['meta'] = $meta;
$themesInfo = [];
foreach ($dirs as $dir){
$i = 1;
foreach (new DirectoryIterator($dir) as $fileInfo) {
$info = [];
if($fileInfo->isDot()) continue;
$info['id'] = $i;
$themesInfo[] = array_merge($info, $this->themeService->getThemeInfo($fileInfo->getPathname()));
$i++;
}
}
$infoToTable['meta']['total'] = count($themesInfo);
$infoToTable['data'] = $themesInfo;
$this->cgView->render("index.php", ['json' => json_encode($infoToTable, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)]);
}
2025-01-23 16:57:03 +03:00
/**
* @throws HeadersSentException
* @throws WrongSessionOptionException
* @throws SessionStartedException
* @throws GuzzleException
* @throws SessionNotStartedException
*/
2025-01-15 16:57:03 +03:00
#[NoReturn] public function actionActivate(): void
{
$request = new Request();
2025-01-23 16:57:03 +03:00
if(!$this->themeService->setActiveTheme($request->get("p"))){
$this->redirect("/admin/settings/themes/", 302);
}
2025-01-15 16:57:03 +03:00
$this->cgView->render("view.php", ['data' => $this->themeService->getThemeInfo($request->get("p"))]);
}
}