<?php
namespace App\Controller;
use App\Entity\Review;
use App\Exception\AppException;
use App\Form\ReviewType;
use App\Form\SearchFilterType;
use App\Repository\DoctorRepository;
use App\Repository\ReviewRepository;
use App\Repository\TimeSlotRepository;
use Boab\CmsBundle\Controller\BaseController;
use DateTime;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class DoctorsController extends BaseController
{
public function __construct(private LoggerInterface $logger)
{}
#[Route('/search/doctors', name: 'app_search_doctors')]
public function index(Request $request, DoctorRepository $doctorRepository): Response
{
$distance = $request->query->get("distance")??0;
$gender = null;
$latitude = 0;
$longitude = 0;
$name='';
$form = $this->createForm(SearchFilterType::class, null, [
'action' => '',
'method' => 'POST'
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$query = $form->getData();
$name = $query['name'];
$distance = $query['distance'];
$token = $this->tokenStorage->getToken();
if($token instanceof UsernamePasswordToken){
$user = $token->getUser();
$latitude = $user->getLatitude();
$longitude = $user->getLongitude();
}
}
$collection = $doctorRepository->findAllByLocation($latitude, $longitude, $distance, $name);
//dump($collection->getItems());
return $this->render('search_doctors/index.html.twig', [
'collection' => $collection->getItems(),
'showRange' => $collection->getResultsRange(),
'searchForm' => $form->createView(),
'pageTitle' => 'Search Therapist'
]);
}
public function profile(TimeSlotRepository $timeSlotRepository, DoctorRepository $doctorRepository, string $username): Response
{
$doctor = $doctorRepository->findOneBy(["username" => $username]);
if(!$doctor){
throw new NotFoundHttpException();
}
$form = $this->createForm(ReviewType::class, new Review,[
'method' => 'POST',
'action' => $this->router->generate('app.doctor_create_review', ['username'=>$username])
]);
$schedulers = $timeSlotRepository->findByUserId($doctor->getId());
$daySlots = $this->groupTimeSlots($schedulers);
return $this->render('doctor/profile_page.html.twig', [
'doctor' => $doctor,
'daySlots' => $daySlots,
'pageTitle' => sprintf('%s %s', $doctor->getFirstname(), $doctor->getLastname()),
'reviewForm' => $form->createView()
]);
}
public function review(Request $request, DoctorRepository $doctorRepository, string $_route, string $username)
{
$doctor = $doctorRepository->findOneBy(["username" => $username]);
if(!$doctor){
throw new AppException(400, 'Invalid Request! Doctor not found');
}
$form = $this->createForm(ReviewType::class, new Review, [
'method' => 'POST',
'action' => $this->router->generate($_route, ['username'=>$username])
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$review = $form->getData();
$review->setCreatedBy($this->getUser());
$review->setCreateAt(new DateTime('now'));
try{
$doctor->addReview($review);
$this->entityManager->persist($doctor);
$this->entityManager->flush();
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
//throw new AppException('400', 'Something went wrong! unable to create review');
}
/*
return new JsonResponse([
'status' => 'success',
'message' => 'Review created successfully'
]);
*/
}
return $this->render('doctor/review.html.twig',[
'form' => $form->createView()
]);
}
public function bookAppointment(DoctorRepository $doctorRepository, string $username): Response
{
}
private function groupTimeSlots($schedulers)
{
$data = [
'Monday' => [],
'Tuesday' => [],
'Wednesday' => [],
'Thursday' => [],
'Friday' => [],
'Saturday' => [],
'Sunday' => [],
];
foreach($schedulers as $schedule){
if(!isset($data[$schedule->getDay()])){
$data[$schedule->getDay()] = [];
}
$data[$schedule->getDay()][] = $schedule;
}
return $data;
}
}