<?php
namespace App\Controller\Admin;
use App\Entity\Menu;
use App\Entity\Restaurante;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
class DashboardController extends AbstractDashboardController
{
/**
* @Route("/admin", name="admin")
*/
public function index(): Response
{
if ($this->isGranted('ROLE_RESTAURANTE')) {
$adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);
if (count($this->getUser()->getRestaurantes()) && count($this->getUser()->getRestaurantes()[0]->getMenus())) {
//Si tienes menu
$url = $adminUrlGenerator->setController(MenuCrudController::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getMenus()[0]->getId())->generateUrl();
} elseif (count($this->getUser()->getRestaurantes())) {
//si tienes restaurante pero no menu
$url = $adminUrlGenerator->setController(MenuCrudController::class)->setAction('new')->generateUrl();
} else {
//si no tienes menu ni restaurante
$url = $adminUrlGenerator->setController(RestauranteCrudController::class)->setAction('new')->generateUrl();
}
return $this->redirect($url);
}
return $this->render('admin/dashboard.html.twig');
}
public function configureDashboard(): Dashboard
{
return Dashboard::new()
->setTitle('<img src="/img/logo.png" height="30" class="d-inline-block align-top"> Menu del día')
->setFaviconPath('favicon.ico');;
}
public function configureUserMenu(UserInterface $user): UserMenu
{
return parent::configureUserMenu($user)
->displayUserName(false)
->addMenuItems([
MenuItem::linkToCrud('Perfil', 'fa fa-id-card', User::class)->setAction('edit')->setEntityId($this->getUser()->getId()),
// MenuItem::linkToRoute('Settings', 'fa fa-user-cog', '...', ['...' => '...']),
]);;
}
public function configureMenuItems(): iterable
{
if ($this->isGranted('ROLE_ADMIN')) {
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Usuarios', 'fas fa-user', User::class);
yield MenuItem::linkToCrud('Restaurantes', 'fas fa-utensils', Restaurante::class);
yield MenuItem::linkToCrud('Menus', 'fas fa-clipboard-list', Menu::class);
}
if ($this->isGranted('ROLE_RESTAURANTE')) {
// yield MenuItem::linkToCrud('Mi Restaurante', 'fas fa-utensils', Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());
// yield MenuItem::linkToCrud('Mi Menú', 'fas fa-plus', Menu::class)->setAction('new');
if (count($this->getUser()->getRestaurantes()) && count($this->getUser()->getRestaurantes()[0]->getMenus())) {
//Si tienes menu
yield MenuItem::linkToCrud('Mi Menú', 'fas fa-file-invoice', Menu::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getMenus()[0]->getId());
yield MenuItem::linkToCrud('Mi Restaurante', 'fas fa-utensils', Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());;
} elseif (count($this->getUser()->getRestaurantes())) {
//si tienes restaurante pero no menu
yield MenuItem::linkToCrud('Mi Menú', 'fas fa-file-invoice', Menu::class)->setAction('new');
yield MenuItem::linkToCrud('Mi Restaurante', 'fas fa-utensils', Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());;
} else {
//si no tienes menu ni restaurante
yield MenuItem::linkToCrud('Mi Restaurante', 'fas fa-utensils', Restaurante::class)->setAction('new');
}
}
}
}