src/Controller/Admin/FaqCrudController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Faq;
  4. use App\Entity\FaqTheme;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  7. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  8. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
  11. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  18. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. class FaqCrudController extends AbstractCrudController
  21. {
  22.     public static function getEntityFqcn(): string
  23.     {
  24.         return Faq::class;
  25.     }
  26.     public function configureActions(Actions $actions): Actions
  27.     {
  28.         $actions->update(Crud::PAGE_NEWAction::SAVE_AND_RETURN, function (Action $action) {
  29.             return $action->setLabel('Enregistrer');
  30.         });
  31.         return $actions;
  32.     }
  33.     public function configureCrud(Crud $crud): Crud
  34.     {
  35.         return $crud
  36.         ->setPageTitle('index''Gestion des FAQ')
  37.             // ->setPageTitle('detail', )
  38.             // ->setPageTitle('edit', fn (Category $category) => sprintf('Editing <b>%s</b>', $category->getName()))
  39.         ;
  40.     }
  41.     public function configureFields(string $pageName): iterable
  42.     {
  43.         return [
  44.             IdField::new('id')->hideOnForm(),
  45.             AssociationField::new('faqTheme') ->setFormTypeOption('choice_label''title'),
  46.             TextEditorField::new('question')->formatValue(function ($value) {
  47.                 return $value ;
  48.             }),
  49.             TextEditorField::new('reponse''RĂ©ponse')->formatValue(function ($value) {
  50.                 return $value ;
  51.             }),
  52.         ];
  53.     }
  54.     #[Route('/show/faq'name'show_faq')]
  55.     public function showByFaq(Request $requestEntityManagerInterface $entityManagerPaginatorInterface $paginator): Response
  56.     {
  57.         //$donnees = $entityManager->getRepository(FaqTheme::class)->findAll();
  58.         $donnees $entityManager->getRepository(FaqTheme::class)->getLastsFaqTheme(02);
  59.         // $faq = $paginator->paginate(
  60.         //     $donnees,
  61.         //     $request->query->getInt('page', 1),
  62.         //     10
  63.         // );
  64.         return $this->render('faq/index.html.twig', [
  65.             'faqs'=>$donnees
  66.         ]);
  67.     }
  68.     #[Route('ajax/get/faqtheme'name'ajax_get_faqtheme')]
  69.     public function getFaqTheme(Request $requestEntityManagerInterface $em): JsonResponse
  70.     {
  71.         /** @var  $result */
  72.         $result = [];
  73.         try {
  74.             
  75.             $donnees $em->getRepository(FaqTheme::class)->getLastsFaqTheme($request->query->get('offset'), 3);
  76.             foreach ($donnees as $key => $obj) {
  77.                 $faqs=[];
  78.                 foreach ($donnees[$key]->getFaqs() as $key2 => $obj2) {
  79.                     $faqs[$key2] = ['id' => $obj2->getId(),'question' => $obj2->getQuestion(), 'reponse' => $obj2->getReponse()];
  80.                 }
  81.                 $result[$key] = ['id' => $obj->getId(),'title' => $obj->getTitle(), 'faqs' => $faqs];
  82.             }
  83.         } catch (\Exception $e) {
  84.             echo $e->getMessage();
  85.         }
  86.         return new JsonResponse(json_encode($result));
  87.     }
  88. }