<?php
namespace App\Controller;
use App\Entity\User;
use App\Services\CiselnikObci;
use App\Services\Ciselniky;
use App\Services\MailNotificator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Controller\ActivityCheckController;
use App\Entity\Notice;
use App\Form\NoticeType;
use App\Form\NoticeResultType;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("notice")
*/
class NoticeController 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');
}
}
}
protected $type = null;
protected function newAction(
Request $request,
MailNotificator $mailNotificator,
CiselnikObci $ciselnikObci,
Ciselniky $ciselniky
) {
return $this->editAction($request, $mailNotificator, $ciselnikObci, $ciselniky, null);
}
protected function editAction(
Request $request,
MailNotificator $mailNotificator,
CiselnikObci $ciselnikObci,
Ciselniky $ciselniky,
$notice_id
) {
$noticeRep = $this->getDoctrine()->getManager()->getRepository(Notice::class);
if ($notice_id) {
$notice = $noticeRep->find($notice_id);
if (!$notice) {
return $this->redirectToRoute($this->type . '-new');
}
} else {
$notice = null;
}
if (!$notice) {
$notice = new Notice();
$notice->setType($this->type);
if ($this->getUser()) {
$notice->setUserId($this->getUser()->getId());
}
}
if ($this->type != $notice->getType()) {
return $this->redirectToRoute($notice->getType() . '-edit', ['id' => $notice->getId()]);
}
$form = $this->createForm(NoticeType::class, $notice, [
// 'ciselniky' => $this->container->get('ciselniky'),
// 'ciselnikObci' => $this->container->get('ciselnikObci'),
'ciselniky' => $ciselniky,
'ciselnikObci' => $ciselnikObci
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$new = ($notice->getId()) ? 'edit' : 'new';
if (!$notice->getId()) {
// $this->container->get('mailNotificator')->sendMail('Nové podanie: ' . $notice->getType() . ' - názov: ' . $notice->getNazov(), $notice->getType());
$mailNotificator->sendMail('Nové podanie: ' . $notice->getType() . ' - názov: ' . $notice->getNazov(), $notice->getType());
}
$em = $this->getDoctrine()->getManager();
$em->persist($notice);
$em->flush();
$this->addFlash('success', $this->type . '-' . $new);
//return $this->redirectToRoute($this->type . '-view', ['id' => $notice->getId()]);
return $this->redirectToRoute($this->type . '-new');
}
$notices = (($this->type == 'oznamenie') ? $noticeRep->findPublicOznamenia() : $noticeRep->findPublicPodnets());
$this->checkUser();
return $this->render('notice/notice_new_' . $this->type . '.html.twig', [
'notices' => $notices,
'notice' => $notice,
'form' => $form->createView(),
]);
}
public function viewAction(Notice $notice) {
if ($this->type != $notice->getType()) {
return $this->redirectToRoute($notice->getType() . '-view', ['id' => $notice->getId()]);
}
$this->checkUser();
return $this->render('notice/notice_view.html.twig', [
'notice' => $notice,
]);
}
public function resultAction(
Request $request,
Ciselniky $ciselniky,
$notice_id
) {
$notice = $this->getDoctrine()->getManager()->getRepository(Notice::class)->find($notice_id);
if ($this->type != $notice->getType()) {
return $this->redirectToRoute($notice->getType() . '-result', ['id' => $notice->getId()]);
}
$form = $this->createForm(NoticeResultType::class, $notice, [
// 'ciselniky' => $this->container->get('ciselniky'),
'ciselniky' => $ciselniky
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($notice);
$em->flush();
$this->addFlash('success', $this->type . '-result');
return $this->redirectToRoute($this->type . '-view', ['id' => $notice->getId()]);
}
$this->checkUser();
return $this->render('notice/notice_result.html.twig', [
'notice' => $notice,
'form' => $form->createView(),
]);
}
}