<?php
namespace App\Controller;
use App\Entity\ReunionAvis;
use App\Entity\Processus;
use App\Entity\User;
use App\Form\ReunionAvisType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Utils\Utility;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\Json;
use App\Repository\ReunionAvisRepository;
use App\Notification\SendNotification;
use App\Security\ReunionVoter;
/**
* @Route("/reunion-avis")
*/
class ReunionAvisController extends AbstractController
{
/**
* @var SendNotification
*/
private $notifyOdj;
private $notifyOdjMeeting;
public function __construct(SendNotification $notifyOdj, SendNotification $notifyOdjMeeting)
{
$this->notifyOdj = $notifyOdj;
$this->notifyOdjMeeting = $notifyOdjMeeting;
}
/**
* @Route("/", name="reunion_avis_index", methods={"GET"})
*/
public function index(): Response
{
$reunionAvis = $this->getDoctrine()
->getRepository(ReunionAvis::class)
->findAll();
//menu
$menu = "processus";
$left_menu = "list_reunions_avis";
return $this->render('reunion_avis/index.html.twig', [
'reunion_avis' => $reunionAvis,
'menu' => $menu,
'left_menu' => $left_menu,
]);
}
/**
* @Route("/listODJ", name="odj_list", methods={"GET"})
*/
public function list(Request $request, ReunionAvisRepository $reunionAvisRepository)
{
$odjData = $reunionAvisRepository->transformAll();
return new JsonResponse($odjData);
}
/**
* @Route("/new", name="reunion_avis_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$this->denyAccessUnlessGranted(ReunionVoter::REUNION_CRUD);
$reunionAvi = new ReunionAvis();
$user = $this->getUser();
/* genretate ref */
$lastId = 0;
$lastreunionAvi = $this->getDoctrine()->getRepository(ReunionAvis::class)->findOneBy([], ['id' => 'desc']);
if (is_object($lastreunionAvi)) {
$lastId = $lastreunionAvi->getId();
}
$utility = new Utility;
$ref = $utility->generateRef($lastId, ReunionAvis::PREFIX);
$reunionAvi->setRef($ref);
/* end genretate ref*/
$reunionAvi->setCreatedBy($user);
$form = $this->createForm(ReunionAvisType::class, $reunionAvi);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$reunionAvi->setCreatedAt(new \DateTime('now'));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($reunionAvi);
$date = $reunionAvi->getDatePlanification();
$datePlanification = date_format($date, 'd-m-Y');
$Hour = $reunionAvi->getHourPlanification();
$hourPlanification = date_format($Hour, 'H:i');
foreach ($reunionAvi->getInvited() as $invited) {
$this->notifyOdj->notifyOdj(
$reunionAvi->getCreatedBy()->getEmail(),
$invited->getEmail(),
$reunionAvi->getRef(),
$datePlanification,
$hourPlanification
);
}
$entityManager->flush();
return $this->redirectToRoute('reunion_avis_index');
}
//menu
$menu = "processus";
$left_menu = "new_odj";
return $this->render('reunion_avis/new.html.twig', [
'reunion_avi' => $reunionAvi,
'form' => $form->createView(),
'menu' => $menu,
'left_menu' => $left_menu
]);
}
/**
* @Route("/{id}", name="reunion_avis_show", methods={"GET"})
*/
public function show(ReunionAvis $reunionAvi): Response
{
return $this->render('reunion_avis/show.html.twig', [
'reunion_avi' => $reunionAvi,
]);
}
/**
* @Route("/{id}/edit", name="reunion_avis_edit", methods={"GET","POST"})
*/
public function edit(Request $request, ReunionAvis $reunionAvi): Response
{
$oldDate = $reunionAvi->getDatePlanification();
$nbrReport = $reunionAvi->getnbrReport();
$form = $this->createForm(ReunionAvisType::class, $reunionAvi);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->denyAccessUnlessGranted(ReunionVoter::REUNION_CRUD);
if (!empty($reunionAvi->getLastDatePlanification())) {
$newDate = $reunionAvi->getLastDatePlanification();
if ($newDate !== $oldDate) {
$reunionAvi->setNbrReport($nbrReport + 1);
}
}
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('reunion_avis_index');
}
//menu
$menu = "processus";
$left_menu = "list_reunions";
return $this->render('reunion_avis/edit.html.twig', [
'reunion_avi' => $reunionAvi,
'form' => $form->createView(),
'menu' => $menu,
'left_menu' => $left_menu
]);
}
/**
* @Route("/delete/{id}", name="reunion_avis_delete", methods={"GET"})
*/
public function delete(Request $request, ReunionAvis $reunionAvi): Response
{
$this->denyAccessUnlessGranted(ReunionVoter::REUNION_CRUD);
//if ($this->isCsrfTokenValid('delete'.$reunionAvi->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($reunionAvi);
$entityManager->flush();
//}
return $this->redirectToRoute('reunion_avis_index');
}
/**
* @Route("/loadODJ", name="loadODJ", methods={"POST"})
*/
public function loadODJ(Request $request)
{
$id = $request->request->get('id');
$em = $this->getDoctrine()->getManager();
$reunionAvi = $em->getRepository(ReunionAvis::class)->find($id);
$title = $reunionAvi->getReunionTitle()->getTitle(); // $reunionAvi->getReunionTitle()->getTitle();
$reunionTitle_id = $reunionAvi->getReunionTitle()->getId();
$processus = $reunionAvi->getProcessus();
$processusList = array();
foreach ($processus as $key => $proc) {
$processusList[$key]['id'] = $proc->getId();
$processusList[$key]['title'] = $proc->getTitle();
}
$datePlanification = $reunionAvi->getDatePlanification(true);
$lastDatePlanification = $reunionAvi->getLastDatePlanification(true);
$hourPlanification = $reunionAvi->getHourPlanification(true);
$theme = $reunionAvi->getTheme();
$animateur = $reunionAvi->getAnimateur()->getId();
$invited = $reunionAvi->getInvited();
$duration = $reunionAvi->getDuration();
$invitedList = array();
foreach ($invited as $key => $inv) {
$invitedList[$key]['id'] = $inv->getId();
$invitedList[$key]['name'] = $inv->getFullName();
}
$data = [
'title' => $title,
'processusList' => $processusList,
'datePlanification' => $datePlanification,
'lastDatePlanification' => $lastDatePlanification,
'hourPlanification' => $hourPlanification,
'theme' => $theme,
'animateur' => $animateur,
'invited' => $invitedList,
'duration' => $duration,
'reunionTitle_id' => $reunionTitle_id
];
return new JsonResponse($data, 200);
}
}