src/Controller/Frontend/MySpaceController.php line 251

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Controller\BaseController;
  4. use App\Entity\Contact;
  5. use App\Entity\EntityStatus;
  6. use App\Entity\Etablissement;
  7. use App\Entity\Order;
  8. use App\Entity\UserQuestion;
  9. use App\Entity\UserResponse;
  10. use App\Entity\User;
  11. use App\Form\Common\ContactCompleteType;
  12. use App\Form\Common\EtablissementType;
  13. use App\Form\Frontend\EntrepriseType;
  14. use App\Repository\CotisationRepository;
  15. use App\Repository\OrderRepository;
  16. use App\Repository\ParameterRepository;
  17. use App\Repository\UserQuestionRepository;
  18. use App\Repository\UserResponseRepository;
  19. use App\Service\IsMemberService;
  20. use App\Service\MailerService;
  21. use App\Service\OrderPdfExport;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use Knp\Component\Pager\PaginatorInterface;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. class MySpaceController extends BaseController
  29. {
  30.     #[Route('/my-space/profile/show'name'customer_show_profile'methods: ['GET'])]
  31.     public function showProfile(
  32.         CotisationRepository $cotisationRepository,
  33.         IsMemberService $isMemberService
  34.     ): Response
  35.     {
  36.         $entreprise $this->getUser()->getContact()->getFirm();
  37.         $isMemberService->checkIsMember($entreprise);
  38.         return $this->render('frontend/my_space/show_profile.html.twig', [
  39.             'controller_name' => 'MySpaceController',
  40.             'menu' => 'my-space',
  41.             'submenu' => 'my-profile',
  42.             'entreprise' => $entreprise,
  43.         ]);
  44.     }
  45.     #[Route('/my-space/profile/edit'name'customer_edit_profile'methods: ['GET''POST'])]
  46.     public function editProfile(
  47.         Request $request,
  48.         EntityManagerInterface $em,
  49.         IsMemberService $isMemberService
  50.     ): Response
  51.     {
  52.         $entreprise $this->getUser()->getContact()->getFirm();
  53.         $isMemberService->checkIsMember($entreprise);
  54.         $form $this->createForm(EntrepriseType::class, $entreprise);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             $em->flush();
  58.             $this->addFlash(
  59.                 'success',
  60.                 'Enregistrement effectué avec succès'
  61.             );
  62.             return $this->redirectToRoute('customer_show_profile', ['id' => $entreprise->getId()]);
  63.         }
  64.         return $this->renderForm('frontend/my_space/edit_profile.html.twig', [
  65.             'controller_name' => 'MySpaceController',
  66.             'menu' => 'my-space',
  67.             'submenu' => 'my-profile',
  68.             'entreprise' => $entreprise,
  69.             'form' => $form
  70.         ]);
  71.     }
  72.     #[Route('/my-space/profile/contact/{id}/show'name'customer_show_contact'methods: ['GET'])]
  73.     public function showContact(Contact $contact): Response
  74.     {
  75.         return $this->renderForm('frontend/my_space/show_contact.html.twig', [
  76.             'controller_name' => 'MySpaceController',
  77.             'menu' => 'my-space',
  78.             'submenu' => 'my-profile',
  79.             'contact' => $contact
  80.         ]);
  81.     }
  82.     #[Route('/my-space/profile/contact/add'name'customer_add_contact'methods: ['GET''POST'])]
  83.     public function addContact(
  84.         Request $request,
  85.         EntityManagerInterface $em
  86.     ): Response
  87.     {
  88.         $contact = new Contact();
  89.         $contact
  90.             ->setFirm($this->getUser()->getContact()->getFirm())
  91.             ->setStatus(Contact::STATUS_ENABLED)
  92.         ;
  93.         $form $this->createForm(ContactCompleteType::class, $contact, [
  94.             'create_from_dashboard' => false
  95.         ]);
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             $contact
  99.                 ->setFirm($this->getUser()->getContact()->getFirm())
  100.                 ->setStatus(Contact::STATUS_ENABLED)
  101.             ;
  102.             $em->persist($contact);
  103.             $em->flush();
  104.             $this->addFlash(
  105.                 'success',
  106.                 'Enregistrement effectué avec succès'
  107.             );
  108.             return $this->redirectToRoute('customer_show_profile', [
  109.                 'id' => $contact->getFirm()->getId()
  110.             ]);
  111.         }
  112.         return $this->renderForm('frontend/my_space/edit_contact.html.twig', [
  113.             'controller_name' => 'MySpaceController',
  114.             'menu' => 'my-space',
  115.             'submenu' => 'my-profile',
  116.             'form' => $form,
  117.             'contact' => $contact
  118.         ]);
  119.     }
  120.     #[Route('/my-space/profile/contact/{id}/edit'name'customer_edit_contact'methods: ['GET''POST'])]
  121.     public function editContact(
  122.         Contact $contact,
  123.         Request $request,
  124.         EntityManagerInterface $em
  125.     )
  126.     {
  127.         $form $this->createForm(ContactCompleteType::class, $contact, [
  128.             'create_from_dashboard' => false
  129.         ]);
  130.         $form->handleRequest($request);
  131.         if ($form->isSubmitted() && $form->isValid()) {
  132.             $em->flush();
  133.             $this->addFlash(
  134.                 'success',
  135.                 'Enregistrement effectué avec succès'
  136.             );
  137.             return $this->redirectToRoute('customer_show_profile');
  138.         }
  139.         return $this->renderForm('frontend/my_space/edit_contact.html.twig', [
  140.             'controller_name' => 'MySpaceController',
  141.             'menu' => 'my-space',
  142.             'submenu' => 'my-profile',
  143.             'form' => $form,
  144.             'contact' => $contact
  145.         ]);
  146.     }
  147.     #[Route('/my-space/profile/contact/{id}/delete'name'customer_archive_contact'methods: ['DELETE''POST'])]
  148.     public function archiveContact(Contact $contactRequest $requestEntityManagerInterface $em): JsonResponse
  149.     {
  150.         $data json_decode($request->getContent(), true);
  151.         if ($this->isCsrfTokenValid('contact_'.$contact->getId(), $data['_token'])) {
  152.             $contact->setStatus(EntityStatus::STATUS_ARCHIVED);
  153.             if ($contact->getUser() instanceof User) {
  154.                 $contact->getUser()->setStatus(EntityStatus::STATUS_ARCHIVED);
  155.             }
  156.             $em->persist($contact);
  157.             $em->flush();
  158.             if ($contact->getUser() instanceof User) {
  159.                 $em->remove($contact->getUser());
  160.             }
  161.             $em->flush();
  162.             return new JsonResponse([
  163.                 'status' => 'ok',
  164.                 'message' => 'Suppression effectuée avec succès'
  165.             ]);
  166.         }
  167.         return new JsonResponse([
  168.             'status' => 'nok',
  169.             'message' => 'Erreur lors de la suppression. Merci de contacter un administrateur.'
  170.         ]);
  171.     }
  172.     #[Route('/my-space/orders'name'customer_list_order'methods: ['GET'])]
  173.     public function order(OrderRepository $orderRepositoryPaginatorInterface $paginatorRequest $request): Response
  174.     {
  175.         $query $orderRepository->findCustomerOrderQuery($this->getUser());
  176.         $orders $paginator->paginate(
  177.             $query,
  178.             $request->query->getInt('page'1),
  179.             self::ITEM_PER_PAGE
  180.         );
  181.         return $this->render('frontend/my_space/list_order.html.twig', [
  182.             'controller_name' => 'MySpaceController',
  183.             'menu' => 'my-space',
  184.             'submenu' => 'my-order',
  185.             'orders' => $orders
  186.         ]);
  187.     }
  188.     #[Route('/my-space/orders/{id}'name'customer_show_order'methods: ['GET'])]
  189.     public function showOrder(Order $orderParameterRepository $parameterRepository): Response
  190.     {
  191.         $rib $parameterRepository->findOneBy([
  192.             'subtype' => 'banque',
  193.             'tag' => 'sale.bank.rib',
  194.         ]);
  195.         return $this->render('frontend/my_space/show_order.html.twig', [
  196.             'controller_name' => 'MySpaceController',
  197.             'menu' => 'my-space',
  198.             'submenu' => 'my-order',
  199.             'order' => $order,
  200.             'rib' => $rib,
  201.         ]);
  202.     }
  203.     #[Route('/my-space/orders/{id}/download-order'name'customer_download_order'methods: ['GET'])]
  204.     public function downloadBill(Order $orderOrderPdfExport $orderPdfExport): Response
  205.     {
  206.         $pdfPath $orderPdfExport->exportOrder($order);
  207.         return $this->file($pdfPathbasename($pdfPath));
  208.     }
  209.     #[Route('/my-space/orders/{id}/download-invoice'name'customer_download_invoice'methods: ['GET'])]
  210.     public function downloadInvoice(Order $orderOrderPdfExport $orderPdfExport): Response
  211.     {
  212.         $pdfPath $orderPdfExport->exportInvoice($order);
  213.         return $this->file($pdfPathbasename($pdfPath));
  214.     }
  215.     #[Route('/my-space/establishments/{id}/show'name'customer_show_establishment'methods: ['GET'])]
  216.     public function showEtablissement(Etablissement $etablissement): Response
  217.     {
  218.         return $this->render('frontend/my_space/show_etablissement.html.twig', [
  219.             'controller_name' => 'MySpaceController',
  220.             'menu' => 'my-space',
  221.             'submenu' => 'my-profile',
  222.             'etablissement' => $etablissement
  223.         ]);
  224.     }
  225.     #[Route('/my-space/establishments/{id}/add'name'customer_add_establishment'methods: ['GET''POST'])]
  226.     public function addEtablissement(Request $requestEntityManagerInterface $em): Response
  227.     {
  228.         $entreprise $this->getUser()->getContact()->getFirm();
  229.         $etablissement = new Etablissement();
  230.         $etablissement->setEntreprise($entreprise);
  231.         $form $this->createForm(EtablissementType::class, $etablissement);
  232.         $form->handleRequest($request);
  233.         if ($form->isSubmitted() && $form->isValid()) {
  234.             $etablissement->setEntreprise($entreprise);
  235.             $em->persist($etablissement);
  236.             $em->flush();
  237.             $this->addFlash(
  238.                 'success',
  239.                 'Enregistrement effectué avec succès'
  240.             );
  241.             return $this->redirectToRoute('customer_show_profile');
  242.         }
  243.         return $this->renderForm('frontend/my_space/edit_etablissement.html.twig', [
  244.             'controller_name' => 'MySpaceController',
  245.             'menu' => 'my-space',
  246.             'submenu' => 'my-profile',
  247.             'entreprise' => $entreprise,
  248.             'form' => $form
  249.         ]);
  250.     }
  251.     #[Route('/my-space/establishments/{id}/edit'name'customer_edit_establishment'methods: ['GET''POST'])]
  252.     public function editEtablissement(Etablissement $etablissementRequest $requestEntityManagerInterface $em): Response
  253.     {
  254.         $form $this->createForm(EtablissementType::class, $etablissement);
  255.         $form->handleRequest($request);
  256.         if ($form->isSubmitted() && $form->isValid()) {
  257.             $etablissement->setEntreprise($this->getUser()->getContact()->getFirm());
  258.             $em->persist($etablissement);
  259.             $em->flush();
  260.             $this->addFlash(
  261.                 'success',
  262.                 'Enregistrement effectué avec succès'
  263.             );
  264.             return $this->redirectToRoute('customer_show_profile');
  265.         }
  266.         return $this->renderForm('frontend/my_space/edit_etablissement.html.twig', [
  267.             'controller_name' => 'MySpaceController',
  268.             'menu' => 'my-space',
  269.             'submenu' => 'my-profile',
  270.             'etablissement' => $etablissement,
  271.             'form' => $form
  272.         ]);
  273.     }
  274.     #[Route('/my-space/establishments/{id}/delete'name'customer_delete_establishment'methods: ['DELETE''POST'])]
  275.     public function delete(Etablissement $etablissementRequest $requestEntityManagerInterface $em): JsonResponse
  276.     {
  277.         $data json_decode($request->getContent(), true);
  278.         if ($this->isCsrfTokenValid('etablissement_'.$etablissement->getId(), $data['_token'])) {
  279.             $em->remove($etablissement);
  280.             $em->flush();
  281.             return new JsonResponse([
  282.                 'status' => 'ok',
  283.                 'message' => 'Suppression effectuée avec succès'
  284.             ]);
  285.         }
  286.         return new JsonResponse([
  287.             'status' => 'nok',
  288.             'message' => 'Erreur lors de la suppression. Merci de contacter un administrateur.'
  289.         ]);
  290.     }
  291.     #[Route('/questions'name'customer_list_question'methods: ['GET'])]
  292.     public function listQuestion(UserQuestionRepository $repositoryPaginatorInterface $paginatorRequest $request): Response
  293.     {
  294.         $query $repository->findAllQuery($this->getUser());
  295.         $questions $paginator->paginate(
  296.             $query,
  297.             $request->query->getInt('page'1),
  298.             self::ITEM_PER_PAGE
  299.         );
  300.         return $this->render('frontend/my_space/list_question.html.twig', [
  301.             'controller_name' => 'UserQuestionController',
  302.             'menu' => 'my-space',
  303.             'submenu' => 'my-question',
  304.             'questions' => $questions
  305.         ]);
  306.     }
  307.     #[Route('/questions/{id}/show'name'customer_show_question'methods: ['GET'])]
  308.     public function showQuestion(UserQuestion $questionUserQuestionRepository $repository): Response
  309.     {
  310.         $question->setIsNew(false);
  311.         /** @var UserResponse $response */
  312.         foreach ($question->getResponses() as $response) {
  313.             $response->setIsNew(false);
  314.         }
  315.         $repository->save($questiontrue);
  316.         return $this->render('frontend/my_space/show_question.html.twig', [
  317.             'controller_name' => 'UserQuestionController',
  318.             'menu' => 'my-space',
  319.             'submenu' => 'my-question',
  320.             'question' => $question
  321.         ]);
  322.     }
  323.     #[Route('/questions/{id}/send-message'name'customer_send_message_question'methods: ['POST'])]
  324.     public function sendQuestionMessage(
  325.         UserQuestion $question,
  326.         Request $request,
  327.         UserResponseRepository $repository,
  328.         MailerService $mailerService
  329.     ): JsonResponse
  330.     {
  331.         $datas json_decode($request->getContent(), true);
  332.         $response = new UserResponse();
  333.         $response
  334.             ->setAuthor($this->getUser())
  335.             ->setMessage($datas['message'])
  336.             ->setQuestion($question)
  337.             ->setIsNew(true)
  338.         ;
  339.         $repository->save($responsetrue);
  340.         $mailerService->sendQuestionNotification($question$this->getUser());
  341.         return new JsonResponse([
  342.             'status' => 'ok',
  343.             'message' => $response->getMessage(),
  344.             'time' => $response->getCreatedAt()->format('H:i')
  345.         ]);
  346.     }
  347. }