src/Controller/Admin/ServiceCrudController.php line 144

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Autres;
  4. use App\Entity\DemandeService;
  5. use App\Entity\Feedback;
  6. use App\Entity\FormationsService;
  7. use App\Entity\InterventionEntrepriseService;
  8. use App\Entity\OtherService;
  9. use App\Entity\ProgrammeFormationService;
  10. use App\Entity\Service;
  11. use App\Form\DemandeServiceFormType;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  19. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  20. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  21. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  22. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  23. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  24. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  25. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  26. use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
  27. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\TextEditorType;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. class ServiceCrudController extends AbstractCrudController
  30. {
  31.     private $request;
  32.     /**
  33.      * @param RequestStack $request
  34.      */
  35.     public function __construct(RequestStack $requestEntityManagerInterface $em)
  36.     {
  37.         $this->request $request;
  38.         $this->em $em;
  39.     }
  40.     public static function getEntityFqcn(): string
  41.     {
  42.         return Service::class;
  43.     }
  44.     public function configureFields(string $pageName): iterable
  45.     {
  46.         return [
  47.             IdField::new('id')->hideOnForm(),
  48.             TextField::new('axe'),
  49.             TextareaField::new('description''description')->formatValue(function ($value) {
  50.                 return $value ;
  51.             })->setColumns('col-sm-12 col-md-12 col-lg-12 col-xxl-12')
  52.             ->setTemplatePath('admin/fields/details.html.twig')->setRequired($pageName !== Crud::PAGE_EDIT)->hideOnIndex(),
  53. //            TextareaField::new('details', 'détails')->formatValue(function ($value) {
  54. //                return $value ;
  55. //            })->setColumns('col-sm-12 col-md-12 col-lg-12 col-xxl-12')
  56. //            ->setTemplatePath('admin/fields/details.html.twig')->setRequired($pageName !== Crud::PAGE_EDIT)->hideOnIndex(),
  57.         ];
  58.     }
  59.     public function persistEntity(EntityManagerInterface $entityManager$entityInstance): void
  60.     {
  61.         $entityInstance = new OtherService();
  62.         $entityInstance->setAxe($this->request->getCurrentRequest()->request->all()['Service']['axe']);
  63.         $entityInstance->setDescription($this->request->getCurrentRequest()->request->all()['Service']['description']);
  64.         $entityManager->persist($entityInstance);
  65.         $entityManager->flush();
  66.     }
  67.     #[Route('/show/service/detail/{id}'name'show_service_detail')]
  68.     public function showByServiceDetail(Request $requestEntityManagerInterface $emTranslatorInterface $translator): Response
  69.     {
  70.         $averageStars = [] ;
  71.         $id = (int) $request->attributes->get('id');
  72.         $service $em->getRepository(Service::class)->find($id);
  73.         $isAllowed false;
  74.         $isShowable false;
  75.         $isNotAllowedMessage $translator->trans('attention.message2');
  76.         if ( !$service ){
  77.             return $this->redirectToRoute('accueil');
  78.         }
  79.         switch ($service) {
  80.             case $service instanceof InterventionEntrepriseService:
  81.                 if( $this->isGranted('ROLE_ENTREPRISES') ){
  82.                     $isAllowed true;
  83.                 }else{
  84.                     $isNotAllowedMessage $translator->trans('attention.message3');
  85.                 }
  86.                 $isShowable true;
  87.                 break;
  88.             case $service instanceof FormationsService:
  89.                 if( $this->isGranted('ROLE_ENTREPRISES') || $this->isGranted('ROLE_PARTENAIRES_NATIONAUX') || $this->isGranted('ROLE_PARTENAIRES_REGIONAUX') ){
  90.                     $isAllowed true;
  91.                 }else{
  92.                     $isNotAllowedMessage $translator->trans('attention.message4');
  93.                 }
  94.                 $isShowable true;
  95.                 break;
  96.             case $service instanceof ProgrammeFormationService:
  97.                 if( $this->isGranted('ROLE_PARTENAIRES_NATIONAUX') || $this->isGranted('ROLE_PARTENAIRES_REGIONAUX') ){
  98.                     $isAllowed true;
  99.                 }else{
  100.                     $isNotAllowedMessage $translator->trans('attention.message5');
  101.                 }
  102.                 $isShowable true;
  103.                 break;
  104.             case $service instanceof OtherService:
  105.                 $isAllowed false;
  106.                 $isShowable false;
  107.                 break;
  108.             default:
  109.                 $isShowable true;
  110.                 $isNotAllowedMessage $translator->trans('attention.message6');
  111.         }
  112.             $averageStars[$service->getId()] = $em->getRepository(Feedback::class)->GetAverageCountServiceStars($service->getId());
  113.         return $this->render('service/detail.html.twig', [
  114.             'service' => $service,
  115.             'isShowable' => $isShowable,
  116.             'isAllowed' => $isAllowed,
  117.             'isNotAllowedMessage' => $isNotAllowedMessage,
  118.             'averageStars' => $averageStars
  119.          ]);
  120.     }
  121.     #[Route('/list/service/index/'name'index_service')]
  122.     public function listServices(Request $requestEntityManagerInterface $entityManagerPaginatorInterface $paginator): Response
  123.     {
  124.         $donnees $entityManager->getRepository(Service::class)->findAll();
  125.         $services $paginator->paginate(
  126.             $donnees,
  127.             $request->query->getInt('page'1),
  128.             10
  129.         );
  130.         foreach($services as $service){
  131.             $averageStars[$service->getId()] = $entityManager->getRepository(Feedback::class)->getAverageStarsByService($service->getId());
  132.         }
  133.         return $this->render('service/index.html.twig', [
  134.              'services'=>$services,
  135.              'averageStars'=>$averageStars
  136.          ]);
  137.     }
  138.     public function configureActions(Actions $actions): Actions
  139.     {
  140.         $actions->add(Crud::PAGE_INDEXAction::DETAIL)
  141.             ->update(Crud::PAGE_INDEXAction::NEW, function (Action $action) {
  142.                 return $action->setIcon('fa fa-plus')->setLabel('Créer un service');
  143.             })->update(Crud::PAGE_NEWAction::SAVE_AND_RETURN, function (Action $action) {
  144.                 return $action->setLabel('Enregistrer');
  145.             });
  146.         return $actions;
  147.     }
  148.     public function configureCrud(Crud $crud): Crud
  149.     {
  150.         return $crud ->setPageTitle('index''Liste des Services')
  151.             ->setPageTitle('detail''Détails de Service')
  152.             ->setPageTitle('new''Créer un service')
  153.             ->setPageTitle('edit''Modifier un service');
  154.     }
  155. }