src/Controller/Admin/DashboardController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Menu;
  4. use App\Entity\Restaurante;
  5. use App\Entity\User;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
  9. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
  10. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. class DashboardController extends AbstractDashboardController
  15. {
  16.     /**
  17.      * @Route("/admin", name="admin")
  18.      */
  19.     public function index(): Response
  20.     {
  21.         if ($this->isGranted('ROLE_RESTAURANTE')) {
  22.             $adminUrlGenerator $this->container->get(AdminUrlGenerator::class);
  23.             if (count($this->getUser()->getRestaurantes()) && count($this->getUser()->getRestaurantes()[0]->getMenus())) {
  24.                 //Si tienes menu
  25.                 $url $adminUrlGenerator->setController(MenuCrudController::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getMenus()[0]->getId())->generateUrl();
  26.             } elseif (count($this->getUser()->getRestaurantes())) {
  27.                 //si tienes restaurante pero no menu
  28.                 $url $adminUrlGenerator->setController(MenuCrudController::class)->setAction('new')->generateUrl();
  29.             } else {
  30.                 //si no tienes menu ni restaurante
  31.                 $url $adminUrlGenerator->setController(RestauranteCrudController::class)->setAction('new')->generateUrl();
  32.             }
  33.             return $this->redirect($url);
  34.         }
  35.         return $this->render('admin/dashboard.html.twig');
  36.     }
  37.     public function configureDashboard(): Dashboard
  38.     {
  39.         return Dashboard::new()
  40.             ->setTitle('<img src="/img/logo.png" height="30" class="d-inline-block align-top"> Menu del día')
  41.             ->setFaviconPath('favicon.ico');;
  42.     }
  43.     public function configureUserMenu(UserInterface $user): UserMenu
  44.     {
  45.         return parent::configureUserMenu($user)
  46.             ->displayUserName(false)
  47.             ->addMenuItems([
  48.                 MenuItem::linkToCrud('Perfil''fa fa-id-card'User::class)->setAction('edit')->setEntityId($this->getUser()->getId()),
  49.                 //                MenuItem::linkToRoute('Settings', 'fa fa-user-cog', '...', ['...' => '...']),
  50.             ]);;
  51.     }
  52.     public function configureMenuItems(): iterable
  53.     {
  54.         if ($this->isGranted('ROLE_ADMIN')) {
  55.             yield MenuItem::linkToDashboard('Dashboard''fa fa-home');
  56.             yield MenuItem::linkToCrud('Usuarios''fas fa-user'User::class);
  57.             yield MenuItem::linkToCrud('Restaurantes''fas fa-utensils'Restaurante::class);
  58.             yield MenuItem::linkToCrud('Menus''fas fa-clipboard-list'Menu::class);
  59.         }
  60.         if ($this->isGranted('ROLE_RESTAURANTE')) {
  61.             // yield MenuItem::linkToCrud('Mi Restaurante', 'fas fa-utensils', Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());                        
  62.             // yield MenuItem::linkToCrud('Mi Menú', 'fas fa-plus', Menu::class)->setAction('new');
  63.             if (count($this->getUser()->getRestaurantes()) && count($this->getUser()->getRestaurantes()[0]->getMenus())) {
  64.                 //Si tienes menu
  65.                 yield MenuItem::linkToCrud('Mi Menú''fas fa-file-invoice'Menu::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getMenus()[0]->getId());
  66.                 yield MenuItem::linkToCrud('Mi Restaurante''fas fa-utensils'Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());;
  67.             } elseif (count($this->getUser()->getRestaurantes())) {
  68.                 //si tienes restaurante pero no menu
  69.                 yield MenuItem::linkToCrud('Mi Menú''fas fa-file-invoice'Menu::class)->setAction('new');
  70.                 yield MenuItem::linkToCrud('Mi Restaurante''fas fa-utensils'Restaurante::class)->setAction('edit')->setEntityId($this->getUser()->getRestaurantes()[0]->getId());;
  71.             } else {
  72.                 //si no tienes menu ni restaurante
  73.                 yield MenuItem::linkToCrud('Mi Restaurante''fas fa-utensils'Restaurante::class)->setAction('new');
  74.             }
  75.         }
  76.     }
  77. }