First Commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemConfig\Controllers;
|
||||
|
||||
use App\Controllers\BaseAdminController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Modules\SystemConfig\Models\SystemConfig;
|
||||
use Kernel\View;
|
||||
|
||||
class CustomizerController extends BaseAdminController
|
||||
{
|
||||
/**
|
||||
* Show customizer for a specific theme
|
||||
*/
|
||||
public function index(Request $request, string $themeSlug): Response
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return new RedirectResponse(url('login'));
|
||||
}
|
||||
|
||||
$baseDir = dirname(dirname(dirname(dirname(__DIR__))));
|
||||
$themePath = $baseDir . '/themes/' . $themeSlug;
|
||||
$customizerFile = $themePath . '/customizer.json';
|
||||
|
||||
if (!is_file($customizerFile)) {
|
||||
return new Response("O tema " . ucfirst($themeSlug) . " não possui opções de personalização.", 404);
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($customizerFile), true);
|
||||
|
||||
// Get current values from DB
|
||||
$dbKey = 'theme_settings_' . $themeSlug;
|
||||
$savedSettings = json_decode(SystemConfig::get($dbKey, '{}'), true);
|
||||
|
||||
// Merge defaults with saved values
|
||||
foreach ($config['sections'] as &$section) {
|
||||
foreach ($section['fields'] as &$field) {
|
||||
$field['value'] = $savedSettings[$field['id']] ?? $field['default'];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->renderAdminView('systemconfig/views/admin/customizer/index.twig', [
|
||||
'title' => 'Personalizar Tema: ' . ucfirst($themeSlug),
|
||||
'theme' => $themeSlug,
|
||||
'config' => $config,
|
||||
'saved' => $savedSettings
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save customizer settings
|
||||
*/
|
||||
public function save(Request $request, string $themeSlug): Response
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return new RedirectResponse(url('login'));
|
||||
}
|
||||
|
||||
$settings = $request->request->all();
|
||||
// Remove CSRF or other non-setting fields if present
|
||||
unset($settings['_token']);
|
||||
|
||||
$dbKey = 'theme_settings_' . $themeSlug;
|
||||
|
||||
$config = SystemConfig::where('system_config_key', $dbKey)->first();
|
||||
if (!$config) {
|
||||
$config = new SystemConfig();
|
||||
$config->system_config_key = $dbKey;
|
||||
$config->system_config_type = 'json';
|
||||
$config->system_config_is_public = true;
|
||||
}
|
||||
|
||||
$config->system_config_value = json_encode($settings, JSON_UNESCAPED_UNICODE);
|
||||
$config->system_config_updated_by = $user->users_id ?? 1;
|
||||
$config->save();
|
||||
|
||||
return new RedirectResponse(url('admin/themes/' . $themeSlug . '/customize?success=1'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemConfig\Controllers;
|
||||
|
||||
use App\Controllers\BaseAdminController;
|
||||
use Modules\SystemConfig\Models\SystemConfig;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SystemConfigController extends BaseAdminController
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
|
||||
if (!$user || !$user->isSuperAdmin()) {
|
||||
return new Response('Access Denied', 403);
|
||||
}
|
||||
|
||||
$configs = SystemConfig::all();
|
||||
|
||||
return $this->renderAdminView('systemconfig/views/admin/index.twig', [
|
||||
'configs' => $configs,
|
||||
'title' => 'Configurações do Sistema'
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
|
||||
if (!$user || !$user->isSuperAdmin()) {
|
||||
return new Response('Access Denied', 403);
|
||||
}
|
||||
|
||||
$data = $request->request->all();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if ($key === 'csrf_token') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$config = SystemConfig::where('system_config_key', $key)->first();
|
||||
|
||||
if ($config) {
|
||||
$config->system_config_value = $value;
|
||||
$config->system_config_updated_by = $user->users_id;
|
||||
$config->save();
|
||||
} else {
|
||||
SystemConfig::create([
|
||||
'system_config_key' => $key,
|
||||
'system_config_value' => $value,
|
||||
'system_config_updated_by' => $user->users_id
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('', 302, ['Location' => url('admin/systemconfig')]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user