src/Controller/Uniqskills/OrderErrorController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Uniqskills;
  3. use App\Entity\Gos\Cart;
  4. use App\Entity\Gos\Country;
  5. use App\Entity\Gos\ProductVariant;
  6. use App\Entity\Gos\Uniqskills\Course;
  7. use App\Entity\Gos\Uniqskills\OrderError;
  8. use App\Entity\Gos\User;
  9. use App\Form\Frontend\Uniqskills\OrderErrorType;
  10. use App\Form\Frontend\Uniqskills\OrderErrorWithUserType;
  11. use App\Utils\Uniqskills\OrderErrorUtils;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("/{_locale}")
  21.  */
  22. class OrderErrorController extends AbstractController
  23. {
  24.     private EntityManagerInterface $em;
  25.     private OrderErrorUtils $orderErrorUtils;
  26.     public function __construct(EntityManagerInterface $_emOrderErrorUtils $_orderErrorUtils)
  27.     {
  28.         $this->em $_em;
  29.         $this->orderErrorUtils $_orderErrorUtils;
  30.     }
  31.     /**
  32.      * @Route("/order/country-not-found/thank", name="fmUniqskillsOrderErrorThankYouMessage")
  33.      */
  34.     public function orderErrorThankYouMessageAction(): Response
  35.     {
  36.         return $this->render('uniqskills/orderError/orderErrorThankYouMessage.html.twig');
  37.     }
  38.     /**
  39.      * @Route("/order/country-not-found/check/{slugCourse}/{countryCode}", name="fmUniqskillsOrderErrorCheck")
  40.      */
  41.     public function orderErrorCheckAction($slugCourse$countryCode): JsonResponse
  42.     {
  43.         $course $this->em->getRepository(Course::class)->findOneBySlug($slugCourse);
  44.         $productVariants $this->em->getRepository(ProductVariant::class)
  45.             ->findAllByCourseAndCountryCode($course->getSlug(), $countryCode);
  46.         if (empty($productVariants))
  47.         {
  48.             return $this->json(['code' => 200'status' => 'EMPTY']);
  49.         }
  50.         return $this->json(['code' => 200'status' => 'OK']);
  51.     }
  52.     /**
  53.      * @Route("/order/country-not-found/{slugCourse}/{country}", name="fmUniqskillsOrderError")
  54.      */
  55.     public function orderErrorAction(Request $request$slugCourse$country null)
  56.     {
  57.         $user $this->getUser();
  58.         $course $this->em->getRepository(Course::class)->findOneBySlug($slugCourse);
  59.         $idMessage $request->request->get('idMessage');
  60.         $orderError $idMessage
  61.             $this->em->getRepository(OrderError::class)->find($idMessage)
  62.             : (new OrderError())
  63.                 ->setUser($this->getUser())
  64.                 ->setCourse($course)
  65.                 ->setMessageTitle('Brak odpowiedniej subskrypcji');
  66.         $form $this->createOrderErrorForm($request$user$orderError);
  67.         if ($form->isSubmitted() && $form->isValid())
  68.         {
  69.             $this->em->persist($orderError);
  70.             $this->em->flush();
  71.             $this->orderErrorUtils->sendOrderErrorMail($course$orderError$country);
  72.             return $this->redirectToRoute('fmUniqskillsOrderErrorThankYouMessage');
  73.         }
  74.         $this->em->persist($orderError);
  75.         $this->em->flush();
  76.         if ($user)
  77.         {
  78.             if ($country)
  79.             {
  80.                 $countryEntity $this->em->getRepository(Country::class)->findOneBySlug($country);
  81.                 $country $countryEntity $countryEntity->getName() : $country;
  82.             }
  83.             elseif ($user->getCountry())
  84.             {
  85.                 $country $user->getCountry()->getName();
  86.             }
  87.             $country $country === 'Poland' && $request->getSession()->get('userLocale''pl') === 'pl' 'Polska' $country;
  88.         }
  89.         $parameters = [
  90.             'course'    => $course,
  91.             'form'      => $form->createView(),
  92.             'idMessage' => $orderError->getId(),
  93.             'countries' => $this->em->getRepository(Country::class)->findBy([], ['name' => 'ASC']),
  94.             'country'   => $country
  95.         ];
  96.         if ($form->getName() === 'order_error_with_user')
  97.         {
  98.             return $this->render('uniqskills/orderError/orderErrorUser.html.twig'$parameters);
  99.         }
  100.         return $this->render('uniqskills/orderError/orderError.html.twig'$parameters);
  101.     }
  102.     private function createOrderErrorForm(Request $request, ?User $user, ?OrderError $ordersError): FormInterface
  103.     {
  104.         if ($user !== null)
  105.         {
  106.             return $this->createForm(OrderErrorWithUserType::class, $ordersError)->handleRequest($request);
  107.         }
  108.         return $this->createForm(OrderErrorType::class, $ordersError)->handleRequest($request);
  109.     }
  110. }