<?php
namespace App\Controller;
use App\Entity\User;
use App\Services\CiselnikObci;
use App\Services\Ciselniky;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Controller\ActivityCheckController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Skoda;
use App\Entity\Notice;
use App\Form\SkodaType;
/**
* Skoda controller.
*/
class SkodaController extends AbstractController implements ActivityCheckController {
public function checkUser(): void {
$user = $this->getUser();
if ($user instanceof User) {
$interval = $this->getParameter('password_change_interval') ?? 30;
if ($user->getPasswordChanged() < (new \DateTime("- $interval days"))) {
$this->addFlash('danger', 'security-password-expired');
}
}
}
/**
* @Route("/sk-hr/edit/{skoda_id}", name="skoda-edit", requirements={"skoda_id"="\d+"})
*/
public function skodaEditAction(
Request $request,
CiselnikObci $ciselnikObci,
Ciselniky $ciselniky,
$skoda_id
) {
$skoda = $this->getDoctrine()->getManager()->getRepository(Skoda::class)->find($skoda_id);
$form = $this->createForm(SkodaType::class, $skoda, [
// 'ciselniky' => $this->container->get('ciselniky'),
// 'ciselnikObci' => $this->container->get('ciselnikObci'),
'ciselniky' => $ciselniky,
'ciselnikObci' => $ciselnikObci
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($skoda);
$em->flush();
$this->addFlash('success', 'skoda-edit');
return $this->redirectToRoute('skoda-view', ['id' => $skoda->getId()]);
}
$this->checkUser();
return $this->render('skoda/skoda_new.html.twig', [
'skoda' => $skoda,
'form' => $form->createView(),
]);
}
/**
* @Route("/sk-hr/for/{notice_id}/{skoda_id}", name="skoda-add", requirements={"notice_id"="\d+","skoda_id"="\d+"})
*/
public function skodaAddAction(Request $request, $notice_id, $skoda_id) {
$notice = $this->getDoctrine()->getManager()->getRepository(Notice::class)->find($notice_id);
if (!$notice) {
return $this->redirectToRoute('homepage');
}
//skusi najst report z DB
if ($skoda_id) {
$skoda = $this->getDoctrine()->getRepository(Skoda::class)->find($skoda_id);
//pridelenie skody a presmerovanie
if ($skoda) {
$notice->setSkoda($skoda);
$em = $this->getDoctrine()->getManager();
$em->persist($notice);
$em->flush();
$this->addFlash('success', 'skoda-add');
return $this->redirectToRoute($notice->getType() . '-view', ['id' => $notice->getId()]);
}
}
return $this->redirectToRoute('skoda-new', ['id' => $notice->getId()]);
}
/**
* @Route("/sk-hr/for/{notice_id}", name="skoda-new", requirements={"notice_id"="\d+"})
*/
public function skodaNewAction(
Request $request,
CiselnikObci $ciselnikObci,
Ciselniky $ciselniky,
$notice_id
) {
$notice = $this->getDoctrine()->getManager()->getRepository(Notice::class)->find($notice_id);
if (!$notice) {
return $this->redirectToRoute('homepage');
}
//vytvorenie novej skody
$skoda = new Skoda($notice);
//naklonovanie pod-entit z notice do skoda
$this->noticePartsCloneAction($skoda, $notice);
$form = $this->createForm(SkodaType::class, $skoda, [
// 'ciselniky' => $this->container->get('ciselniky'),
// 'ciselnikObci' => $this->container->get('ciselnikObci'),
'ciselniky' => $ciselniky,
'ciselnikObci' => $ciselnikObci
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($notice);
$em->persist($skoda);
$em->flush();
$this->addFlash('success', 'skoda-new');
return $this->redirectToRoute('skoda-view', ['id' => $skoda->getId()]);
}
$this->checkUser();
return $this->render('skoda/skoda_new.html.twig', [
'skoda' => $skoda,
'form' => $form->createView(),
]);
}
private function noticePartsCloneAction(Skoda $skoda, Notice $notice) {
foreach ($notice->getDocs() as $old) {
$new = clone $old;
$new->setNotice(null);
$skoda->addDoc($new);
}
foreach ($notice->getLocs() as $old) {
$new = clone $old;
$new->setNotice(null);
$skoda->addLoc($new);
}
foreach ($notice->getOperators() as $old) {
$new = clone $old;
$new->setNotice(null);
$new->setType('skoda');
$skoda->addOperator($new);
}
$skoda->setNazov($notice->getNazov());
$skoda->setWGSE(($notice->getOznamenie()) ? $notice->getOznamenie()->getWGSE() : $notice->getPodnet()->getWGSE());
$skoda->setWGSN(($notice->getOznamenie()) ? $notice->getOznamenie()->getWGSN() : $notice->getPodnet()->getWGSN());
$skoda->setVznik(($notice->getOznamenie()) ? $notice->getOznamenie()->getVznik() : $notice->getPodnet()->getVznik());
$skoda->setZistenie(($notice->getOznamenie()) ? $notice->getOznamenie()->getZistenie() : null);
}
/**
* @Route("/po-oz/resolve", name="notice-resolve")
*/
public function noticeResolveAction() {
$this->checkUser();
$notices = $this->getDoctrine()
->getRepository(Notice::class)
->findBy([], ['id' => 'DESC']);
$skody = $this->getDoctrine()
->getRepository(Skoda::class)
->findBy([], ['id' => 'DESC']);
return $this->render('notice/notice_resolve.html.twig', [
'skody' => $skody,
'notices' => $notices,
]);
}
/**
* @Route("/sk-hr/resolve", name="skoda-resolve")
*/
public function skodaResolveAction() {
$this->checkUser();
$skody = $this->getDoctrine()
->getRepository(Skoda::class)
->findBy([], ['id' => 'DESC']);
return $this->render('skoda/skoda_resolve.html.twig', [
'skody' => $skody,
]);
}
/**
* @Route("/register/skody", name="register-skody")
*/
public function registerSkodyShowAction() {
$this->checkUser();
$skody = $this->getDoctrine()
->getRepository(Skoda::class)
->findPublicSkody('s');
return $this->render('skoda/register_es.html.twig', [
'skody' => $skody,
]);
}
/**
* @Route("/register/hrozby", name="register-hrozby")
*/
public function registerHrozbyShowAction() {
$this->checkUser();
$skody = $this->getDoctrine()
->getRepository(Skoda::class)
->findPublicSkody('h');
return $this->render('skoda/register_bhes.html.twig', [
'skody' => $skody,
]);
}
/**
* @Route("/register/hrozba/{id}", name="register-hrozba", requirements={"id"="\d+"})
*/
public function registerHrozbaAction(Skoda $skoda) {
if ($skoda->getDruh() === 's') {
return $this->redirectToRoute('register-skoda', ['id' => $skoda->getId()]);
}
$this->checkUser();
return $this->skodaViewAction($skoda);
}
/**
* @Route("/register/skoda/{id}", name="register-skoda", requirements={"id"="\d+"})
*/
public function registerSkodaAction(Skoda $skoda) {
if ($skoda->getDruh() != 's') {
return $this->redirectToRoute('register-hrozba', ['id' => $skoda->getId()]);
}
$this->checkUser();
return $this->skodaViewAction($skoda);
}
/**
* @Route("/sk-hr/view/{id}", name="skoda-view")
*/
public function skodaViewAction(Skoda $skoda) {
$this->checkUser();
return $this->render('skoda/skoda_view.html.twig', [
'skoda' => $skoda,
]);
}
}