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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemConfig\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Modules\SystemConfig\Models\SystemConfig;
|
||||
use Kernel\View;
|
||||
|
||||
class MaintenanceMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
// Check if maintenance mode is enabled
|
||||
$maintenanceMode = false;
|
||||
try {
|
||||
if (class_exists(SystemConfig::class)) {
|
||||
$config = SystemConfig::where('system_config_key', 'maintenance_mode')->first();
|
||||
if ($config && $config->system_config_value == '1') {
|
||||
$maintenanceMode = true;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// If DB fails, assume not in maintenance or handle gracefully
|
||||
$maintenanceMode = false;
|
||||
}
|
||||
|
||||
if (!$maintenanceMode) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Inject maintenance mode variable into view
|
||||
try {
|
||||
$view = new View();
|
||||
$view->getEngine()->addGlobal('is_maintenance_mode', true);
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
|
||||
// Allow access to login route and assets
|
||||
$path = $request->getPathInfo();
|
||||
$allowedRoutes = ['/login', '/auth/login'];
|
||||
$isAsset = preg_match('/\.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|ttf|svg)$/', $path);
|
||||
|
||||
if (in_array($path, $allowedRoutes) || $isAsset) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Allow Super Admin to bypass
|
||||
if ($this->isSuperAdmin()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Render maintenance page
|
||||
try {
|
||||
$view = new View();
|
||||
return new Response($view->render('errors/maintenance.twig'), 503);
|
||||
} catch (\Throwable $e) {
|
||||
return new Response('Maintenance Mode', 503);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user is Super Admin.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isSuperAdmin(): bool
|
||||
{
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
if (class_exists('\Modules\Auth\Models\User')) {
|
||||
$user = \Modules\Auth\Models\User::find($_SESSION['user_id']);
|
||||
return $user && $user->isSuperAdmin();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemConfig\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class SystemConfig extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'system_config';
|
||||
|
||||
/**
|
||||
* The primary key associated with table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'system_config_id';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'system_config_key',
|
||||
'system_config_value',
|
||||
'system_config_type',
|
||||
'system_config_description',
|
||||
'system_config_is_public',
|
||||
'system_config_updated_by'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'system_config_is_public' => 'boolean',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* Get the user who updated the config.
|
||||
*/
|
||||
public function updatedByUser(): BelongsTo
|
||||
{
|
||||
// Check if Auth module exists before relating
|
||||
if (class_exists('\Modules\Auth\Models\User')) {
|
||||
return $this->belongsTo(\Modules\Auth\Models\User::class, 'system_config_updated_by');
|
||||
}
|
||||
// Fallback or return empty relation if Auth module is missing (unlikely but safe)
|
||||
return $this->belongsTo(self::class, 'system_config_updated_by')->whereNull('system_config_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config value by key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get(string $key, $default = null)
|
||||
{
|
||||
$config = self::where('system_config_key', $key)->first();
|
||||
|
||||
if (!$config) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
// Handle type casting if needed based on system_config_type
|
||||
// For now, return value directly
|
||||
return $config->system_config_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set config value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param string $type
|
||||
* @param string|null $description
|
||||
* @param bool $isPublic
|
||||
* @return self
|
||||
*/
|
||||
public static function set(string $key, $value, string $type = 'string', ?string $description = null, bool $isPublic = false): self
|
||||
{
|
||||
$config = self::firstOrNew(['system_config_key' => $key]);
|
||||
|
||||
$config->system_config_value = $value;
|
||||
$config->system_config_type = $type;
|
||||
|
||||
if ($description !== null) {
|
||||
$config->system_config_description = $description;
|
||||
}
|
||||
|
||||
$config->system_config_is_public = $isPublic;
|
||||
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
$config->system_config_updated_by = $_SESSION['user_id'];
|
||||
}
|
||||
|
||||
$config->save();
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\SystemConfig\Providers;
|
||||
|
||||
use Kernel\Application;
|
||||
use Modules\SystemConfig\Middleware\MaintenanceMiddleware;
|
||||
|
||||
class SystemConfigServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
$app = Application::getInstance();
|
||||
|
||||
// Register Global Middleware (runs on all requests)
|
||||
if (method_exists($app, 'addGlobalMiddleware')) {
|
||||
$app->addGlobalMiddleware(new MaintenanceMiddleware());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user