<?php declare(strict_types=1);
namespace PPAdmin\Controller;
use PlnaPenezenka\PPSDKBundle\Doctrine\Repository\WebsRepository;
use PPAdmin\Security\ManagedWebsResolver;
use PPAdmin\UI\DashboardTilesControllersCollection;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractAdminController
{
#[Route("/", name: "dashboard")]
function dashboard(DashboardTilesControllersCollection $controllers): Response
{
$tiles_methods = [];
foreach ($controllers as $controller) {
$controller_name = get_class($controller);
$method = $controller->getDashboardTileMethodName();
if($method === null){
continue;
}
if(!method_exists($controller, $method)){
throw new \RuntimeException("Controller {$controller_name} does not have method '{$method}' implemented for rendering dashboard tile");
}
$tiles_methods[$controller_name] = $method;
}
return $this->render('dashboard.html.twig', [
'tiles_methods' => $tiles_methods
]);
}
#[Route("/manage-web/{identifier}", name: "manage-web")]
function manageWeb(string $identifier, ManagedWebsResolver $resolver, WebsRepository $webs_repo, Request $request): RedirectResponse
{
$web = $webs_repo->findByIdentifier($identifier);
if(!$web){
throw $this->createNotFoundException("Web not found");
}
if(!in_array($web, $resolver->getWebsManagedByCurrentAdmin())){
throw $this->createAccessDeniedException("Cannot manage given web");
}
$resolver->setCurrentManagedWeb($web);
$referer = $request->headers->get('referer');
return $referer ?
$this->redirect($referer) :
$this->redirectToRoute('dashboard');
}
}