<?php
namespace App\Controller\Dysfonctionnement;
use App\Entity\ActionPlan;
use App\Entity\Dysfonctionnement\Cause5p;
use App\Entity\Dysfonctionnement\Cause6m;
use App\Entity\Dysfonctionnement\Constat;
use App\Entity\Reunion;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use App\Notification\SendNotification;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/dysfonctionnement/reunion")
*/
class ReunionController extends AbstractController
{
private $notifyReunion;
public function __construct(SendNotification $notifyReunion)
{
$this->notifyReunion = $notifyReunion;
}
/**
* @Route("/5p/{id}", name="analyse_5p", methods={"GET"})
*/
public function index5P(Reunion $reunion): Response
{
$users = $this->getDoctrine()->getRepository(User::class)->findAll();
$time = $reunion->getDurationEstimated()*60;
return $this->render('dysfonctionnementTemplate/reunion/index.html.twig',[
'users'=> $users,
'reunion'=>$reunion,
'time'=>$time,
'hide_global_timer'=>true,
]);
}
/**
* @Route("/6m/{id}", name="analyse_6m", methods={"GET"})
*/
public function index6M(Reunion $reunion): Response
{
$users = $this->getDoctrine()->getRepository(User::class)->findAll();
$time = $reunion->getDurationEstimated()*60;
$actionplan = $reunion->getConstat()->getActionPlanCorrectifDefinitive();
$all_causes = $reunion->getConstat()->getCause6ms();
$causes_methodes=[];
$causes_materiels=[];
$causes_matieres=[];
$causes_main_doeuvre=[];
$causes_milieu=[];
$causes_management=[];
foreach($all_causes as $cause) {
switch ($cause->getBranch()) {
case Cause6m::BRANCH_METHODES:
$causes_methodes[]=$cause;
break;
case Cause6m::BRANCH_MATERIELS:
$causes_materiels[]=$cause;
break;
case Cause6m::BRANCH_MATIERES:
$causes_matieres[]=$cause;
break;
case Cause6m::BRANCH_MAIN_DOEUVRE:
$causes_main_doeuvre[]=$cause;
break;
case Cause6m::BRANCH_MILIEU:
$causes_milieu[]=$cause;
break;
case Cause6m::BRANCH_MANAGEMENT:
$causes_management[]=$cause;
break;
}
}
return $this->render('dysfonctionnementTemplate/reunion/reunion_6m.html.twig',[
'users'=> $users,
'reunion'=>$reunion,
'actionplan'=>$actionplan,
'time'=>$time,
'causes_methodes'=>$causes_methodes,
'causes_materiels'=>$causes_materiels,
'causes_matieres'=>$causes_matieres,
'causes_main_doeuvre'=>$causes_main_doeuvre,
'causes_milieu'=>$causes_milieu,
'causes_management'=>$causes_management,
'hide_global_timer'=>true,
]);
}
/**
* @Route("/pdf/{id}", name="generate_constat_pdf", methods={"GET","POST"})
*/
public function generate_constat_pdf(Reunion $reunion, Request $request)
{
$path_logo = $this->getParameter('kernel.project_dir').'/public/img/persyst-logo.png';
$type_logo = pathinfo($path_logo, PATHINFO_EXTENSION);
$data_logo = file_get_contents($path_logo);
$base64_logo = 'data:image/' . $type_logo . ';base64,' . base64_encode($data_logo);
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
if($reunion->getConstat()->getMethodeAnalyse()==Constat::ANALYSE_5P) {
$html = $this->renderView('pdf/constat_reunion/index.html.twig', [
'base64_logo' => $base64_logo,
'reunion'=>$reunion
]);
}else {
$html = $this->renderView('pdf/constat_reunion/rapport6m.html.twig', [
'base64_logo' => $base64_logo,
'reunion'=>$reunion
]);
}
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$doc = 'reunion_constat_'.$reunion->getRef().'.pdf';
$dir = $this->getParameter('kernel.project_dir').'/public/uploads/pdf/reunion/'.$reunion->getId().'/';
if (!file_exists($dir)) {
$fs=new Filesystem();
$fs->mkdir($dir);
}
$output = $dompdf->output();
$pdfFilepath = $dir.$doc;
file_put_contents($pdfFilepath, $output);
$reunion->setPdfFile('uploads/pdf/reunion/'.$reunion->getId().'/'.$doc);
$this->getDoctrine()->getManager()->flush();
$diffuser = $request->query->get('diffuser','');
if($diffuser) {
foreach ($reunion->getParticipants() as $invited) {
$this->notifyReunion->notifyReunion(
$reunion->getCreatedBy()->getEmail(),
$invited->getUser()->getEmail(),
$reunion->getRef(),
$reunion->getPdfFile()
);
}
return $this->redirectToRoute('analyse_5p',array('id'=>$reunion->getId()));
}
$dompdf->stream($doc, [
"Attachment" => true
]);
}
/**
* @Route("/loadConstats", name="loadConstats", methods={"POST"})
*/
public function loadConstats(Request $request): JsonResponse
{
$constat_id=$request->query->get('constat','');
$type_constat = $request->request->get('type_constat', '');
if($type_constat =="Réclamation / plainte") {
$type_constat = "reclamation";
}
if($type_constat =="Dysfonctionnement") {
$type_constat = "dysfonctionnement";
}
$reunion_title = $request->request->get('reunion_title', '');
$methodeAnalyse="6m";
if($reunion_title =="Arbre des causes - 5P") {
$methodeAnalyse = "5p";
}
$constats_data=[];
if($type_constat) {
$constats = $this->getDoctrine()->getRepository(Constat::class)->findBy(['type'=>$type_constat,'methodeAnalyse'=>$methodeAnalyse]);
foreach($constats as $constat) {
if(!$constat->getReunion() || strval($constat->getId()) == $constat_id) {
$constats_data[] = ['id'=>$constat->getId(),'title'=>$constat->getReference()];
}
}
}
return new JsonResponse(['constats'=>$constats_data],200);
}
/**
* @Route("/5p/save_cause5p", name="save_cause5p", methods={"POST"})
*/
public function save_cause5p(Request $request): JsonResponse
{
$em = $this->getDoctrine()->getManager();
$id = $request->request->get('id');
$question = $request->request->get('question','');
$reponse = $request->request->get('reponse','');
$maitrisable = $request->request->get('maitrisable','non');
$effet = $request->request->get('effet');
$closed = $request->request->get('closed','');
$maitrisable_bool = $maitrisable=='oui';
$cause5p = $this->getDoctrine()->getRepository(Cause5p::class)->find($id);
$cause5p->setQuestion($question);
$cause5p->setReponse($reponse);
$cause5p->setIsMaitrisable($maitrisable_bool);
$cause5p->setEffet($effet);
if($closed) {
$constat = $cause5p->getConstat();
$constat->setIsAnalyseClosed(true);
if($cause5p->getQuestionNumber() <5) {
$rest_cause5p = $this->getDoctrine()->getRepository(Cause5p::class)->findCause5ps($cause5p->getQuestionNumber(),$cause5p->getConstat());
foreach($rest_cause5p as $cause) {
$cause->setIsClosed(true);
}
}
}
$em->flush();
return new JsonResponse(['updated'=>1]);
}
/**
* @Route("/5p/save_cause5p/all", name="save_all_causes5p", methods={"POST"})
*/
public function save_all_causes5p(Request $request): JsonResponse
{
$all_causes = $request->request->get('all_causes',[]);
if($all_causes) {
$last_question_number=1;
foreach($all_causes as $cause) {
$cause5p = $this->getDoctrine()->getRepository(Cause5p::class)->find($cause['id']);
$cause5p->setQuestion($cause['question']);
$cause5p->setReponse($cause['reponse']);
$maitrisable_bool = $cause['maitrisable']=='oui';
$cause5p->setIsMaitrisable($maitrisable_bool);
$cause5p->setEffet($cause['effet']);
$constat = $cause5p->getConstat();
$last_question_number = $cause5p->getQuestionNumber();
}
$closed = $request->request->get('closed','');
if($closed) {
$constat->setIsAnalyseClosed(true);
if($cause5p->getQuestionNumber() <5) {
$rest_cause5p = $this->getDoctrine()->getRepository(Cause5p::class)->findCause5ps($last_question_number,$constat);
foreach($rest_cause5p as $cause) {
$cause->setIsClosed(true);
}
}
}
$em = $this->getDoctrine()->getManager();
$em->flush();
}
return new JsonResponse(['updated'=>1]);
}
/**
* @Route("/save_reunion/{id}", name="save_reunion", methods={"POST"})
*/
public function save_reunion(Reunion $reunion, Request $request):JsonResponse
{
$em= $this->getDoctrine()->getManager();
$responsables = $request->request->get('responsables',[]);
$comment = $request->request->get('comment','');
$constat = $reunion->getConstat();
$constat->removeAllResponsables();
foreach($responsables as $responsable_id) {
$responsable= $em->getRepository(User::class)->find($responsable_id);
$constat->addResponsable($responsable);
}
$reunion->setCommentaire($comment);
$em->flush();
return new JsonResponse(['saved'=>1]);
}
}