Files
2026-09-01 10:46:29 -03:00

126 lines
4.1 KiB
PHP
Executable File

<?php
namespace Modules\SystemConfig\Controllers;
use App\Controllers\BaseAdminController;
use Modules\SystemConfig\Models\SystemConfig;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ThemeController extends BaseAdminController
{
public function index(Request $request): Response
{
$user = $this->getCurrentUser();
if (!$user || !$user->isSuperAdmin()) {
return new Response('Access Denied', 403);
}
$themesDir = dirname(dirname(dirname(dirname(__DIR__)))) . '/themes';
$themes = [];
if (is_dir($themesDir)) {
$folders = array_diff(scandir($themesDir), ['.', '..']);
foreach ($folders as $folder) {
if (is_dir($themesDir . '/' . $folder)) {
$themes[$folder] = $this->getThemeInfo($themesDir . '/' . $folder, $folder);
}
}
}
$activeTheme = \get_active_theme();
return $this->renderAdminView('systemconfig/views/admin/themes/index.twig', [
'themes' => $themes,
'activeTheme' => $activeTheme,
'title' => 'Gerenciar Temas'
]);
}
public function activate(Request $request, string $theme): Response
{
$user = $this->getCurrentUser();
if (!$user || !$user->isSuperAdmin()) {
return new Response('Access Denied', 403);
}
$themesDir = dirname(dirname(dirname(dirname(__DIR__)))) . '/themes';
if (!is_dir($themesDir . '/' . $theme)) {
return new Response('Tema não encontrado', 404);
}
SystemConfig::updateOrCreate(
['system_config_key' => 'active_theme'],
[
'system_config_value' => $theme,
'system_config_updated_by' => $user->users_id,
'system_config_type' => 'string'
]
);
// Clear Twig cache
$cacheDir = dirname(dirname(dirname(dirname(__DIR__)))) . '/cache/twig';
if (is_dir($cacheDir)) {
$this->rrmdir($cacheDir);
}
return new RedirectResponse(url('admin/themes'));
}
protected function getThemeInfo(string $path, string $slug): array
{
$info = [
'slug' => $slug,
'name' => $slug,
'version' => '1.0.0',
'author' => 'Desconhecido',
'description' => '',
'screenshot' => null,
'is_customizable' => file_exists($path . '/customizer.json')
];
$stylePath = $path . '/style.css';
if (file_exists($stylePath)) {
$content = file_get_contents($stylePath);
if (preg_match('/Theme Name:\s*(.*)$/mi', $content, $matches)) {
$info['name'] = trim($matches[1]);
}
if (preg_match('/Version:\s*(.*)$/mi', $content, $matches)) {
$info['version'] = trim($matches[1]);
}
if (preg_match('/Author:\s*(.*)$/mi', $content, $matches)) {
$info['author'] = trim($matches[1]);
}
if (preg_match('/Description:\s*(.*)$/mi', $content, $matches)) {
$info['description'] = trim($matches[1]);
}
}
if (file_exists($path . '/screenshot.png')) {
$info['screenshot'] = '/themes/' . $slug . '/screenshot.png';
} elseif (file_exists($path . '/screenshot.jpg')) {
$info['screenshot'] = '/themes/' . $slug . '/screenshot.jpg';
}
return $info;
}
private function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . "/" . $object)) {
$this->rrmdir($dir . DIRECTORY_SEPARATOR . $object);
} else {
unlink($dir . DIRECTORY_SEPARATOR . $object);
}
}
}
rmdir($dir);
}
}
}