src/Controller/DoctorsController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Review;
  4. use App\Exception\AppException;
  5. use App\Form\ReviewType;
  6. use App\Form\SearchFilterType;
  7. use App\Repository\DoctorRepository;
  8. use App\Repository\ReviewRepository;
  9. use App\Repository\TimeSlotRepository;
  10. use Boab\CmsBundle\Controller\BaseController;
  11. use DateTime;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  20. class DoctorsController extends BaseController
  21. {
  22.     public function __construct(private LoggerInterface $logger)
  23.     {}
  24.     #[Route('/search/doctors'name'app_search_doctors')]
  25.     public function index(Request $requestDoctorRepository $doctorRepository): Response
  26.     {
  27.         $distance $request->query->get("distance")??0;
  28.         $gender null;
  29.         $latitude 0;
  30.         $longitude 0;
  31.         $name='';
  32.         $form $this->createForm(SearchFilterType::class, null, [
  33.             'action' => '',
  34.             'method' => 'POST'
  35.         ]);
  36.         $form->handleRequest($request);
  37.         if($form->isSubmitted() && $form->isValid()){
  38.             $query $form->getData();
  39.             $name $query['name'];
  40.             $distance $query['distance'];
  41.             $token $this->tokenStorage->getToken();
  42.             if($token instanceof UsernamePasswordToken){
  43.                 $user $token->getUser();
  44.                 $latitude $user->getLatitude();
  45.                 $longitude $user->getLongitude();
  46.             }
  47.         }
  48.         $collection $doctorRepository->findAllByLocation($latitude$longitude$distance$name);
  49.         //dump($collection->getItems());
  50.         return $this->render('search_doctors/index.html.twig', [
  51.             'collection' => $collection->getItems(),
  52.             'showRange' => $collection->getResultsRange(),
  53.             'searchForm' => $form->createView(),
  54.             'pageTitle' => 'Search Therapist'
  55.         ]);
  56.     }
  57.     public function profile(TimeSlotRepository $timeSlotRepositoryDoctorRepository $doctorRepositorystring $username): Response
  58.     {
  59.         $doctor $doctorRepository->findOneBy(["username" => $username]);
  60.         if(!$doctor){
  61.             throw new NotFoundHttpException();
  62.         }
  63.         $form $this->createForm(ReviewType::class, new Review,[
  64.             'method' => 'POST',
  65.             'action' => $this->router->generate('app.doctor_create_review', ['username'=>$username])
  66.         ]);
  67.         $schedulers $timeSlotRepository->findByUserId($doctor->getId());
  68.         $daySlots $this->groupTimeSlots($schedulers);
  69.         return $this->render('doctor/profile_page.html.twig', [
  70.             'doctor' => $doctor,
  71.             'daySlots' => $daySlots,
  72.             'pageTitle' => sprintf('%s %s'$doctor->getFirstname(), $doctor->getLastname()),
  73.             'reviewForm' => $form->createView()
  74.         ]);
  75.     }  
  76.     public function review(Request $requestDoctorRepository $doctorRepositorystring $_routestring $username)
  77.     {
  78.         $doctor $doctorRepository->findOneBy(["username" => $username]);
  79.         if(!$doctor){
  80.             throw new AppException(400'Invalid Request! Doctor not found');
  81.         }
  82.         $form $this->createForm(ReviewType::class, new Review, [
  83.             'method' => 'POST',
  84.             'action' => $this->router->generate($_route, ['username'=>$username])
  85.         ]);
  86.         $form->handleRequest($request);
  87.         if($form->isSubmitted() && $form->isValid()){
  88.             $review $form->getData();
  89.             $review->setCreatedBy($this->getUser());
  90.             $review->setCreateAt(new DateTime('now'));
  91.             try{
  92.                 $doctor->addReview($review);
  93.                 $this->entityManager->persist($doctor);
  94.                 $this->entityManager->flush();
  95.             }catch(\Exception $e){
  96.                 $this->logger->error($e->getMessage(), ['exception'=>$e]);
  97.                 //throw new AppException('400', 'Something went wrong! unable to create review');
  98.             }
  99.             /*
  100.             return new JsonResponse([
  101.                 'status' => 'success',
  102.                 'message' => 'Review created successfully'
  103.             ]);
  104.             */
  105.         }
  106.         return $this->render('doctor/review.html.twig',[
  107.             'form' => $form->createView()
  108.         ]);
  109.     }
  110.     
  111.     public function bookAppointment(DoctorRepository $doctorRepositorystring $username): Response
  112.     {
  113.         
  114.     }
  115.     private function groupTimeSlots($schedulers)
  116.     {
  117.         $data = [
  118.             'Monday' => [],
  119.             'Tuesday' => [],
  120.             'Wednesday' => [],
  121.             'Thursday' => [],
  122.             'Friday' => [],
  123.             'Saturday' => [],
  124.             'Sunday' => [],
  125.         ];
  126.         foreach($schedulers as $schedule){
  127.             if(!isset($data[$schedule->getDay()])){
  128.                 $data[$schedule->getDay()] = [];
  129.             }
  130.             $data[$schedule->getDay()][] = $schedule;
  131.         }
  132.         return $data;
  133.     }    
  134. }