Files
dockerfluxodecaixapro/modules/configindex/classes/Providers/ConfigIndexServiceProvider.php
T
2026-09-01 10:46:29 -03:00

81 lines
3.4 KiB
PHP
Executable File

<?php
namespace Modules\Configindex\Providers;
use Kernel\View;
use Modules\Configindex\Models\ConfigPage;
class ConfigIndexServiceProvider
{
public function register()
{
}
public function boot()
{
try {
$busId = isset($_SESSION['current_company_id']) ? (int) $_SESSION['current_company_id'] : 1;
$visibility = [];
if (class_exists(ConfigPage::class)) {
$baseDir = dirname(dirname(dirname(dirname(__DIR__))));
$activeTheme = \Kernel\View::getActiveTheme() ?? 'default';
$indexViewsDir = $baseDir . '/modules/index/views';
$themeViewsDir = $baseDir . '/themes/' . $activeTheme . '/modules/index/views';
$pages = ConfigPage::where('config_index_bus_id', $busId)->get();
foreach ($pages as $page) {
$key = $page->config_index_key;
// Verifica se o arquivo físico existe
$fileExists = false;
if ($key === 'index') {
$fileExists = true;
} else {
$possiblePaths = [
$indexViewsDir . '/' . $key . '.twig',
$indexViewsDir . '/' . $key . '.php',
$themeViewsDir . '/' . $key . '.twig',
$themeViewsDir . '/' . $key . '.php',
$baseDir . '/themes/' . $activeTheme . '/' . $key . '.twig',
$baseDir . '/themes/' . $activeTheme . '/' . $key . '.php',
$baseDir . '/modules/' . $key . '/views/index.twig',
];
foreach ($possiblePaths as $path) {
if (is_file($path)) {
$fileExists = true;
break;
}
}
}
$visibility[$key] = [
'public' => (bool) $page->config_index_show_public_nav && $fileExists,
'dashboard' => (bool) $page->config_index_show_dashboard_nav && $fileExists,
'title' => $page->config_index_title ?: ucfirst($key)
];
}
}
// Garante que as páginas padrão tenham uma configuração inicial, mas respeita o banco de dados
foreach (['about', 'contact', 'services'] as $key) {
if (!isset($visibility[$key])) {
// Se não estiver no banco, o padrão é VISÍVEL (true) para manter compatibilidade
// Mas agora as alterações via admin/configindex PERSISTEM no banco
$visibility[$key] = ['public' => true, 'dashboard' => true, 'title' => ucfirst($key)];
}
}
$view = new View();
$view->getEngine()->addGlobal('config_pages_visibility', $visibility);
} catch (\Throwable $e) {
$view = new View();
$view->getEngine()->addGlobal('config_pages_visibility', [
'about' => ['public' => false, 'dashboard' => false, 'title' => 'About'],
'contact' => ['public' => false, 'dashboard' => false, 'title' => 'Contact'],
'services' => ['public' => false, 'dashboard' => false, 'title' => 'Services'],
]);
}
}
}