<?php
namespace App\Controller\Customer;
use App\Entity\Order;
use App\Form\OrderType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Psr\Log\LoggerInterface;
class FormController extends AbstractController
{
/**
* Customer Form
* @param Request $request
* @param ValidatorInterface $validator
* @param LoggerInterface $ordersLogger
* @return Response
*/
public function index(
Request $request,
ValidatorInterface $validator,
LoggerInterface $ordersLogger
): Response
{
$timestamps = $this->timestamps();
$order = new Order();
$form = $this->createForm(OrderType::class, $order);
$form->handleRequest($request);
$answer =[];
if ($form->isSubmitted() && $form->isValid()) {
$errors = $validator->validate($order);
if (count($errors) > 0) {
/*
* Uses a __toString method on the $errors variable which is a
* ConstraintViolationList object. This gives us a nice string
* for debugging.
*/
$errorsString = (string)$errors;
$ordersLogger->error($errorsString);
}
$json = json_encode($order);
$result = $this->callAPI("https://megakuryr.cz/api/v1/zalozeniObjednavky", $json);
$answer = json_decode($result, true);
if (isset($answer['orderId'])) {
$ordersLogger->info($result);
$ordersLogger->info($json);
$this->addFlash('success', 'Objednávka číslo ' .$answer['orderId']. ' byla vytvořena.');
return $this->forward('App\Controller\Customer\StaticPagesController::thankYou', [
'order' => $order,
'api' => $answer,
]);
} else {
$ordersLogger->error($result);
$this->addFlash('success', 'Něco se pokazilo');
}
}
return $this->render('customer/form.html.twig', [
'form' => $form->createView(),
'timestamps' => $timestamps,
'api' => $answer
]);
}
private function callAPI($url, $data = false)
{
$curl = curl_init();
$token = $this->getParameter("app.api_token");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Token '.$token));
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
private function timestamps(): array
{
$dateRange = [];
for (
$day = date('c', strtotime('today 8am'));
$day <= date('c', strtotime('+6 days'));
$day = date('c', strtotime($day . '+1 day'))
) {
$dateRange[] = [
'day' => $day,
];
}
$timestamps = [];
foreach ($dateRange as $date) {
$times = [];
for (
$time = date('c', strtotime($date['day']));
$time <= date('c', strtotime($date['day']. '+8 hours'));
$time = date('c', strtotime($time . '+2 hour'))
) {
$times[] = [
'timeFrom'=>$time,
'timeTo'=>date('c', strtotime($time . '+2 hour'))
];
}
$daynames = ["Neděle", "Pondělí", "Uterý", "Středa", "Čtvrtek", "Pátek", "Sobota"];
$timestamps[] = [
'name' => (date('d.m.', strtotime($date['day'])) . "(" . $daynames[date('w', strtotime($date['day']))] . ")"),
'timeRange'=>$times,
];
}
return $timestamps;
}
}