src/Controller/SpeakerController.php line 232

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Dto\SpeakerSearchDto;
  4. use App\Entity\Category;
  5. use App\Entity\PromoCode;
  6. use App\Entity\Speaker;
  7. use App\Entity\SpeakerConsultation;
  8. use App\Entity\SpeakerCourse;
  9. use App\Form\RequestConsultationType;
  10. use App\Form\SpeakerOrderType;
  11. use App\Service\CartService;
  12. use App\Service\DiscountService;
  13. use App\Service\Email;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  19. use Symfony\Component\Mailer\MailerInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. use Twig\Error\LoaderError;
  23. use Twig\Error\RuntimeError;
  24. use Twig\Error\SyntaxError;
  25. use Twig\Extra\Intl\IntlExtension;
  26. use App\Repository\ReviewRepository;
  27. use App\Dto\Cabinet\ReviewSearchDto;
  28. class SpeakerController extends AbstractController
  29. {
  30.     private TranslatorInterface $translator;
  31.     public function __construct(TranslatorInterface $translator)
  32.     {
  33.         $this->translator $translator;
  34.     }
  35.     /**
  36.      * @Route("/speakers/", name="speaker.list")
  37.      */
  38.     public function index(): Response
  39.     {
  40.         return $this->render('speaker/index.html.twig', [
  41.             'categories' => $this->getDoctrine()->getRepository(Category::class)->findBy([], ['name' => 'asc']),
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/speakers/searh.json", name="speaker.search.json")
  46.      * @param Request $request
  47.      * @return JsonResponse
  48.      */
  49.     public function search(Request $request): JsonResponse
  50.     {
  51.         $searchDto = new SpeakerSearchDto((string)$request->get('search'));
  52.         if ($request->get('category')) {
  53.             $searchDto->setCategory($this->getDoctrine()->getRepository(Category::class)->find((int)$request->get('category')));
  54.         }
  55.         if ($request->get('page')) {
  56.             $searchDto->setPage((int)$request->get('page'));
  57.         }
  58.         if ($request->get('order_by')) {
  59.             $searchDto->setOrderBy($request->get('order_by'));
  60.         }
  61.         $speakerRep $this->getDoctrine()->getRepository(Speaker::class);
  62.         return $this->json([
  63.             'data' => $this->renderView('speaker/search.html.twig', [
  64.                 'speakers' => $speakerRep->search($searchDto),
  65.             ]),
  66.             'left' => $speakerRep->searchCount($searchDto) - $searchDto->getPage() * $searchDto->getPerPage(),
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/{uri}", name="speaker.detail", requirements={"uri"=".*"}, priority=-100)
  71.      * @param $uri
  72.      * @param Request $request
  73.      * @return Response
  74.      */
  75.     public function detail($uriRequest $requestCartService $cartServiceReviewRepository $reviewRepository): Response
  76.     {
  77.         $speaker $this->getDoctrine()->getRepository(Speaker::class)->findOneBy([
  78.             'uri'     => $uri,
  79.             'enabled' => true,
  80.             'status' => Speaker::STATUS_APPROVE
  81.         ]);
  82.         if (!$speaker) {
  83.             throw $this->createNotFoundException();
  84.         }
  85.         $promoCodes $cartService->getPromoCodes();
  86.         $dto = new ReviewSearchDto();
  87.         $dto->setSortBy('date')->setOrderBy('desc');
  88.         $reviews $reviewRepository->findBySpeaker($speaker$dto);
  89.         return $this->render('speaker/detail.html.twig', [
  90.             'speaker'   => $speaker,
  91.             'reviews'   => $reviews,
  92.             'category'  => $this->getDoctrine()->getRepository(Category::class)->find((int)$request->get('category')),
  93.             'orderForm' => $this->createForm(SpeakerOrderType::class)->createView(),
  94.             'promoVal'  => isset($promoCodes[$speaker->getId()])
  95.                 ? $this->getDoctrine()->getRepository(PromoCode::class)->find($promoCodes[$speaker->getId()])
  96.                 : '',
  97.             'courses'   => $this->getDoctrine()->getRepository(SpeakerCourse::class)->findBy(['enabled' => true'speaker' => $speaker]),
  98.         ]);
  99.     }
  100.     /**
  101.      * @Route("/speakers/subscribe/{speaker}", name="speaker.subscribe")
  102.      * @param Speaker $speaker
  103.      * @return JsonResponse
  104.      */
  105.     public function subscribe(Speaker $speaker): JsonResponse
  106.     {
  107.         $res = [
  108.             'success' => false,
  109.         ];
  110.         if (!is_null($this->getUser())) {
  111.             $speaker->addSubscriber($this->getUser());
  112.             $em $this->getDoctrine()->getManager();
  113.             $em->persist($speaker);
  114.             $em->flush();
  115.             $res['success'] = true;
  116.             $res['message'] = $this->translator->trans('message.subscribed_to_speaker');
  117.         }
  118.         return $this->json($res);
  119.     }
  120.     /**
  121.      * @Route("/speakers/timeslots/{speaker}/{fromDate}/{toDate}", name="speaker.timeslots")
  122.      * @param Speaker $speaker
  123.      * @param $fromDate
  124.      * @param $toDate
  125.      * @param Request $request
  126.      * @return JsonResponse
  127.      * @throws \Exception
  128.      */
  129.     public function timeslots(Speaker $speaker$fromDate$toDateRequest $requestCartService $cartService): JsonResponse
  130.     {
  131.         try {
  132.             $timezone = new \DateTimeZone($request->get('timezone'));
  133.             $utcTimezone = new \DateTimeZone('UTC');
  134.         } catch (\Exception $e) {
  135.             return $this->json([]);
  136.         }
  137.         $fromDateUTC = (new \DateTime($fromDate$timezone))->setTimezone($utcTimezone);
  138.         $toDateUTC = (new \DateTime($toDate$timezone))->setTimezone($utcTimezone);
  139.         $cartService->setTimeZone($timezone->getName());
  140.         $now = (new \DateTime())->setTimezone($timezone);
  141.         $data = [];
  142.         foreach ($this->getDoctrine()
  143.                      ->getRepository(SpeakerConsultation::class)
  144.                      ->findSpeakerSlots($speaker$fromDateUTC$toDateUTC) as $v) {
  145.             if ($v['status'] == SpeakerConsultation::STATUS_CANCELED) {
  146.                 continue;
  147.             }
  148.             if ($v['startDate'] < $now) {
  149.                 continue;
  150.             }
  151.             $data[] = [
  152.                 'id'     => $v['id'],
  153.                 'date'   => $v['startDate']->setTimezone($timezone)->format('Y-m-d'),
  154.                 'label'  => $v['startDate']->setTimezone($timezone)->format('H:i') . ' - ' $v['endDate']->setTimezone($timezone)->format('H:i'),
  155.                 'freeze' => ((empty($v['order_type']) || !empty($v['order_refunded'])) && (is_null($v['freezeDate']) || $v['freezeDate'] < (new \DateTime()))) ? 1,
  156.             ];
  157.         }
  158.         return $this->json($data);
  159.     }
  160.     /**
  161.      * @Route("/speakers/timeslot/{consultation}", name="speaker.timeslot")
  162.      * @param SpeakerConsultation $consultation
  163.      * @return JsonResponse
  164.      */
  165.     public function timeslot(SpeakerConsultation $consultationDiscountService $discountCartService $cartService): JsonResponse
  166.     {
  167.         $promoCodes $cartService->getPromoCodes();
  168.         if (!empty($promoCodes[$consultation->getSpeaker()->getId()])) {
  169.             $discount->setPromoCode($this->getDoctrine()
  170.                 ->getRepository(PromoCode::class)
  171.                 ->find($promoCodes[$consultation->getSpeaker()->getId()])
  172.             );
  173.         }
  174.         $price $consultation->getPrice();
  175.         $groupPrice $consultation->getGroupPrice();
  176.         $priceTotal $discount->getConsultationPrice($consultation);
  177.         $groupPriceTotal $discount->getConsultationGroupPrice($consultation);
  178.         $intl $this->container->get('twig')->getExtension(IntlExtension::class);
  179.         $formatCurrency = function ($price) use ($intl) {
  180.             return $intl->formatCurrency($price 100'RUB', ['fraction_digit' => 0]);
  181.         };
  182.         return $this->json([
  183.             'price'              => $formatCurrency($price),
  184.             'groupPrice'         => $formatCurrency($groupPrice),
  185.             'priceTotal'         => $formatCurrency($priceTotal),
  186.             'groupPriceTotal'    => $formatCurrency($groupPriceTotal),
  187.             'priceDiscount'      => $formatCurrency($price $priceTotal),
  188.             'groupPriceDiscount' => $formatCurrency($groupPrice $groupPriceTotal),
  189.             'discountDesc'       => $discount->getInfo(),
  190.         ]);
  191.     }
  192.     /**
  193.      * @Route("/speakers/{uri}/request-consultation", name="speaker.request-consultation"), requirements={"uri"=".*"}, priority=-100)
  194.      * @param $uri
  195.      * @param Request $request
  196.      * @param Email $email
  197.      * @param MailerInterface $mailer
  198.      * @return Response
  199.      * @throws TransportExceptionInterface
  200.      * @throws LoaderError
  201.      * @throws RuntimeError
  202.      * @throws SyntaxError
  203.      */
  204.     public function requestConsultation($uriRequest $requestEmail $emailMailerInterface $mailer): Response
  205.     {
  206.         $speaker $this->getDoctrine()->getRepository(Speaker::class)->findOneBy([
  207.             'uri' => $uri,
  208.             'enabled' => true,
  209.             'status' => Speaker::STATUS_APPROVE
  210.         ]);
  211.         $speakerName "{$speaker->getFirstName()} {$speaker->getLastName()}";
  212.         $form $this->createForm(RequestConsultationType::class, null, [
  213.             'speaker_name' => $speakerName
  214.         ]);
  215.         $form->handleRequest($request);
  216.         if ($form->isSubmitted() and $form->isValid()) {
  217.             $message $email->create('request_consultation', [
  218.                 'name' => strip_tags($form->get('name')->getData()),
  219.                 'email' => strip_tags($form->get('email')->getData()),
  220.                 'description' => strip_tags($form->get('description')->getData()),
  221.             ])->to($this->getParameter('manager_email'));
  222.             $mailer->send($message);
  223.             return $this->redirectToRoute('speaker.detail', [
  224.                 'uri' => $uri
  225.             ]);
  226.         }
  227.         return $this->render('speaker/request-consultation.twig', [
  228.             'form' => $form->createView(),
  229.             'speaker_name' => $speakerName
  230.         ]);
  231.     }
  232. }