src/Controller/Customer/FormController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Entity\Order;
  4. use App\Form\OrderType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Validator\Validator\ValidatorInterface;
  9. use Psr\Log\LoggerInterface;
  10. class FormController extends AbstractController
  11. {
  12.     /**
  13.      * Customer Form
  14.      * @param Request $request
  15.      * @param ValidatorInterface $validator
  16.      * @param LoggerInterface $ordersLogger
  17.      * @return Response
  18.      */
  19.     public function index(
  20.         Request $request,
  21.         ValidatorInterface $validator,
  22.         LoggerInterface $ordersLogger
  23.     ): Response
  24.     {
  25.         $timestamps $this->timestamps();
  26.         $order = new Order();
  27.         $form $this->createForm(OrderType::class, $order);
  28.         $form->handleRequest($request);
  29.         $answer =[];
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             $errors $validator->validate($order);
  32.             if (count($errors) > 0) {
  33.                 /*
  34.                  * Uses a __toString method on the $errors variable which is a
  35.                  * ConstraintViolationList object. This gives us a nice string
  36.                  * for debugging.
  37.                  */
  38.                 $errorsString = (string)$errors;
  39.                 $ordersLogger->error($errorsString);
  40.             }
  41.             $json json_encode($order);
  42.             $result $this->callAPI("https://megakuryr.cz/api/v1/zalozeniObjednavky"$json);
  43.             $answer json_decode($resulttrue);
  44.             if (isset($answer['orderId'])) {
  45.                 $ordersLogger->info($result);
  46.                 $ordersLogger->info($json);
  47.                 $this->addFlash('success''Objednávka číslo ' .$answer['orderId']. ' byla vytvořena.');
  48.                 return $this->forward('App\Controller\Customer\StaticPagesController::thankYou', [
  49.                     'order'  => $order,
  50.                     'api' => $answer,
  51.                 ]);
  52.             } else {
  53.                 $ordersLogger->error($result);
  54.                 $this->addFlash('success''Něco se pokazilo');
  55.             }
  56.         }
  57.         return $this->render('customer/form.html.twig', [
  58.             'form' => $form->createView(),
  59.             'timestamps' => $timestamps,
  60.             'api' => $answer
  61.         ]);
  62.     }
  63.     private function callAPI($url$data false)
  64.     {
  65.         $curl curl_init();
  66.         $token $this->getParameter("app.api_token");
  67.         curl_setopt($curlCURLOPT_HTTPHEADER, array('Authorization: Token '.$token));
  68.         curl_setopt($curlCURLOPT_POST1);
  69.         if ($data) {
  70.             curl_setopt($curlCURLOPT_POSTFIELDS$data);
  71.         }
  72.         // Optional Authentication:
  73.         curl_setopt($curlCURLOPT_URL$url);
  74.         curl_setopt($curlCURLOPT_RETURNTRANSFER1);
  75.         $result curl_exec($curl);
  76.         curl_close($curl);
  77.         return $result;
  78.     }
  79.     private function timestamps(): array
  80.     {
  81.         $dateRange = [];
  82.         for (
  83.             $day date('c'strtotime('today 8am'));
  84.             $day <= date('c'strtotime('+6 days'));
  85.             $day date('c'strtotime($day '+1 day'))
  86.         ) {
  87.             $dateRange[] = [
  88.                 'day' => $day,
  89.             ];
  90.         }
  91.         $timestamps = [];
  92.         foreach ($dateRange as $date) {
  93.             $times = [];
  94.             for (
  95.                 $time date('c'strtotime($date['day']));
  96.                 $time <= date('c'strtotime($date['day']. '+8 hours'));
  97.                 $time date('c'strtotime($time '+2 hour'))
  98.             ) {
  99.                 $times[] = [
  100.                     'timeFrom'=>$time,
  101.                     'timeTo'=>date('c'strtotime($time '+2 hour'))
  102.                 ];
  103.             }
  104.             $daynames = ["Neděle""Pondělí""Uterý""Středa""Čtvrtek""Pátek""Sobota"];
  105.             $timestamps[] = [
  106.                 'name' => (date('d.m.'strtotime($date['day'])) . "(" $daynames[date('w'strtotime($date['day']))] . ")"),
  107.                 'timeRange'=>$times,
  108.             ];
  109.         }
  110.         return $timestamps;
  111.     }
  112. }