src/Controller/NoticeController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Services\CiselnikObci;
  5. use App\Services\Ciselniky;
  6. use App\Services\MailNotificator;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use App\Controller\ActivityCheckController;
  10. use App\Entity\Notice;
  11. use App\Form\NoticeType;
  12. use App\Form\NoticeResultType;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("notice")
  16.  */
  17. class NoticeController extends AbstractController implements ActivityCheckController {
  18.     public function checkUser(): void {
  19.         $user $this->getUser();
  20.         if ($user instanceof User) {
  21.             $interval $this->getParameter('password_change_interval') ?? 30;
  22.             if ($user->getPasswordChanged() < (new \DateTime("- $interval days"))) {
  23.                 $this->addFlash('danger''security-password-expired');
  24.             }
  25.         }
  26.     }
  27.     protected $type null;
  28.     protected function newAction(
  29.         Request         $request,
  30.         MailNotificator $mailNotificator,
  31.         CiselnikObci    $ciselnikObci,
  32.         Ciselniky       $ciselniky
  33.     ) {
  34.         return $this->editAction($request$mailNotificator$ciselnikObci$ciselnikynull);
  35.     }
  36.     protected function editAction(
  37.         Request         $request,
  38.         MailNotificator $mailNotificator,
  39.         CiselnikObci    $ciselnikObci,
  40.         Ciselniky       $ciselniky,
  41.                         $notice_id
  42.     ) {
  43.         $noticeRep $this->getDoctrine()->getManager()->getRepository(Notice::class);
  44.         if ($notice_id) {
  45.             $notice $noticeRep->find($notice_id);
  46.             if (!$notice) {
  47.                 return $this->redirectToRoute($this->type '-new');
  48.             }
  49.         } else {
  50.             $notice null;
  51.         }
  52.         if (!$notice) {
  53.             $notice = new Notice();
  54.             $notice->setType($this->type);
  55.             if ($this->getUser()) {
  56.                 $notice->setUserId($this->getUser()->getId());
  57.             }
  58.         }
  59.         if ($this->type != $notice->getType()) {
  60.             return $this->redirectToRoute($notice->getType() . '-edit', ['id' => $notice->getId()]);
  61.         }
  62.         $form $this->createForm(NoticeType::class, $notice, [
  63. //            'ciselniky' => $this->container->get('ciselniky'),
  64. //            'ciselnikObci' => $this->container->get('ciselnikObci'),
  65.             'ciselniky' => $ciselniky,
  66.             'ciselnikObci' => $ciselnikObci
  67.         ]);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $new = ($notice->getId()) ? 'edit' 'new';
  71.             if (!$notice->getId()) {
  72. //                $this->container->get('mailNotificator')->sendMail('Nové podanie: ' . $notice->getType() . ' - názov: ' . $notice->getNazov(), $notice->getType());
  73.                 $mailNotificator->sendMail('Nové podanie: ' $notice->getType() . ' - názov: ' $notice->getNazov(), $notice->getType());
  74.             }
  75.             $em $this->getDoctrine()->getManager();
  76.             $em->persist($notice);
  77.             $em->flush();
  78.             $this->addFlash('success'$this->type '-' $new);
  79.             //return $this->redirectToRoute($this->type . '-view', ['id' => $notice->getId()]);
  80.             return $this->redirectToRoute($this->type '-new');
  81.         }
  82.         $notices = (($this->type == 'oznamenie') ? $noticeRep->findPublicOznamenia() : $noticeRep->findPublicPodnets());
  83.         $this->checkUser();
  84.         return $this->render('notice/notice_new_' $this->type '.html.twig', [
  85.             'notices' => $notices,
  86.             'notice' => $notice,
  87.             'form' => $form->createView(),
  88.         ]);
  89.     }
  90.     public function viewAction(Notice $notice) {
  91.         if ($this->type != $notice->getType()) {
  92.             return $this->redirectToRoute($notice->getType() . '-view', ['id' => $notice->getId()]);
  93.         }
  94.         $this->checkUser();
  95.         return $this->render('notice/notice_view.html.twig', [
  96.             'notice' => $notice,
  97.         ]);
  98.     }
  99.     public function resultAction(
  100.         Request   $request,
  101.         Ciselniky $ciselniky,
  102.                   $notice_id
  103.     ) {
  104.         $notice $this->getDoctrine()->getManager()->getRepository(Notice::class)->find($notice_id);
  105.         if ($this->type != $notice->getType()) {
  106.             return $this->redirectToRoute($notice->getType() . '-result', ['id' => $notice->getId()]);
  107.         }
  108.         $form $this->createForm(NoticeResultType::class, $notice, [
  109. //            'ciselniky' => $this->container->get('ciselniky'),
  110.             'ciselniky' => $ciselniky
  111.         ]);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) {
  114.             $em $this->getDoctrine()->getManager();
  115.             $em->persist($notice);
  116.             $em->flush();
  117.             $this->addFlash('success'$this->type '-result');
  118.             return $this->redirectToRoute($this->type '-view', ['id' => $notice->getId()]);
  119.         }
  120.         $this->checkUser();
  121.         return $this->render('notice/notice_result.html.twig', [
  122.             'notice' => $notice,
  123.             'form' => $form->createView(),
  124.         ]);
  125.     }
  126. }