<?php
namespace App\Controller\BugReport;
use App\Entity\BugReport;
use App\Entity\Child;
use App\Form\BugReportForm;
use App\Form\ChildType;
use App\Repository\ChildRepository;
use App\Utils\HelperUtils;
use Doctrine\DBAL\Exception\ConstraintViolationException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @Route("/bug-report-form")
*/
class BugReportController extends AbstractController
{
/**
* @Route("/", name="bug_report_form", methods={"GET","POST"})
* @Security("is_granted('ROLE_ADMIN') and is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function form(Request $request, EntityManagerInterface $em)
{
$bugReport = new BugReport();
$user = $this->getUser();
$bugReport->setAdmin($user);
$form = $this->createForm(BugReportForm::class, $bugReport, ['required' => false]);
$form->handleRequest($request);
if ($form->isSubmitted()/* && $form->isValid()*/) {
try {
$em->persist($bugReport);
$em->flush();
return $this->json(['success' => true]);
} catch (ConstraintViolationException $e) {
return $this->json(['errors' => 'Visi laukai privalo buti uzpildyti'], Response::HTTP_BAD_REQUEST);
} catch (\Exception $e) {
return $this->json(['errors' => 'Visi laukai privalo buti uzpildyti'], Response::HTTP_BAD_REQUEST);
}
}
$errorMessages = [];
if ( $form->getErrors()->count() !== 0 ) {
foreach ($form->getErrors() as $error) {
$errorMessages[] = $error->getMessage();
}
return $this->json(['errors' => $errorMessages], Response::HTTP_BAD_REQUEST);
}
return $this->renderForm(
'bug_report/bug_modal.html.twig',
[
'form' => $form,
'loggedUser' => $this->getUser()
]
);
}
}