mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Slim\Views\Twig;
|
|
|
|
class AdminBaseController
|
|
{
|
|
protected Twig $view;
|
|
protected \PDO $pdo;
|
|
|
|
public function __construct(\PDO $pdo, Twig $view)
|
|
{
|
|
$this->pdo = $pdo;
|
|
$this->view = $view;
|
|
}
|
|
|
|
/**
|
|
* Render a template
|
|
*/
|
|
protected function render(Response $response, string $template, array $data = []): Response
|
|
{
|
|
// Add common admin data
|
|
$data['auth'] = [
|
|
'check' => isset($_SESSION['user_id']),
|
|
'user' => [
|
|
'username' => $_SESSION['username'] ?? 'Admin',
|
|
'is_admin' => $_SESSION['is_admin'] ?? false
|
|
]
|
|
];
|
|
|
|
// Add current route for active menu highlighting
|
|
$route = $this->getCurrentRoute();
|
|
if ($route) {
|
|
$data['current_route'] = $route;
|
|
}
|
|
|
|
return $this->view->render($response, $template, $data);
|
|
}
|
|
|
|
/**
|
|
* Get current route name
|
|
*/
|
|
protected function getCurrentRoute(): ?string
|
|
{
|
|
$route = $_SERVER['REQUEST_URI'] ?? '/';
|
|
$basePath = '/admin/';
|
|
|
|
if (strpos($route, $basePath) === 0) {
|
|
$route = substr($route, strlen($basePath));
|
|
$parts = explode('/', $route);
|
|
return $parts[0] ?: 'index';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return JSON response
|
|
*/
|
|
protected function json(Response $response, $data, int $status = 200): Response
|
|
{
|
|
$response->getBody()->write(json_encode($data));
|
|
return $response
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withStatus($status);
|
|
}
|
|
}
|