First Commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\PageBuilder\Controllers;
|
||||
|
||||
use Kernel\View;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Modules\PageBuilder\Models\Page;
|
||||
use Modules\Auth\Models\User;
|
||||
use Twig\Error\LoaderError;
|
||||
|
||||
class PageController
|
||||
{
|
||||
protected View $view;
|
||||
|
||||
public function __construct(View $view)
|
||||
{
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a custom page by slug.
|
||||
*/
|
||||
public function show(Request $request, string $slug): Response
|
||||
{
|
||||
$query = Page::where('pages_slug', $slug);
|
||||
|
||||
// If not admin, display only published pages
|
||||
if (!$this->isAdmin()) {
|
||||
$query->published();
|
||||
}
|
||||
|
||||
$page = $query->first();
|
||||
|
||||
if (!$page) {
|
||||
return new Response('Página não encontrada', 404);
|
||||
}
|
||||
|
||||
return $this->renderPage($page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the page with error handling.
|
||||
*/
|
||||
private function renderPage(Page $page): Response
|
||||
{
|
||||
$data = [
|
||||
'page' => $page,
|
||||
'title' => $page->meta_title ?: $page->title,
|
||||
'meta_description' => $page->meta_description,
|
||||
'custom_css' => $page->getCustomCss(),
|
||||
'custom_js' => $page->getCustomJs(),
|
||||
'css_classes' => $page->getCssClasses()
|
||||
];
|
||||
|
||||
try {
|
||||
return new Response($this->view->render($page->getLayoutPath(), $data));
|
||||
} catch (LoaderError $e) {
|
||||
// If the template is not found, try the default layout
|
||||
error_log("Template error for page {$page->pages_slug}: " . $e->getMessage());
|
||||
return new Response($this->view->render('default.twig', $data));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current user is admin.
|
||||
*/
|
||||
private function isAdmin(): bool
|
||||
{
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$user = User::find($_SESSION['user_id']);
|
||||
return $user && ($user->isSuperAdmin() || $user->isAdmin());
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user