<?php
namespace App\Controller;
use App\Entity\PDossier;
use App\Entity\Pemploye;
use App\Entity\PNotification;
use App\Entity\UsModule;
use App\Entity\UsSousModule;
use App\Entity\UserOperationDossier;
use App\Entity\Users;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class HomeController extends AbstractController
{
private $em;
public function __construct(ManagerRegistry $doctrine)
{
$this->em = $doctrine->getManager();
}
#[Route('/phpinfo', name: 'phpinfo', options:['expose' => true])]
public function phpinfo(Request $request , ManagerRegistry $doctrine): Response
{
ob_start();
phpinfo();
$phpinfo = ob_get_clean();
return new Response($phpinfo);
}
#[Route('/', name: 'app_site', options:['expose' => true])]
public function site(Request $request , ManagerRegistry $doctrine): Response
{
// dd('amine');
// dd($this->getUser());
if($this->getUser() instanceof Pemploye) {
if($this->getUser()->isIsFirstLogin()) {
return $this->redirectToRoute('app_register_changepassword');
}
return $this->redirectToRoute('app_espace_personnel_demande_conge');
}
if(in_array('ROLE_ADMIN', $this->getUser()->getRoles())){
$dossiers = $doctrine->getManager()->getRepository(PDossier::class)->findAll();
}
else if(in_array('ROLE_RESPONSABLE', $this->getUser()->getRoles())){
return $this->redirectToRoute('app_redirect');
} else {
$dossiers = $doctrine->getManager()->getRepository(UserOperationDossier::class)->finddossier($this->getUser()->getId());
}
// if($request->getSession()->get('dossier')) {
// // dd($request->getSession()->get('dossier'));
// $request->getSession()->remove('dossier');
// return $this->redirectToRoute('app_logout');
// }
// dd('amine');
return $this->render('security/site.html.twig', [
'dossiers' => $dossiers
]);
}
#[Route('/404', name: 'app_404', options:['expose' => true])]
public function app_404(Request $request , ManagerRegistry $doctrine): Response
{
dd('vous ne pouvez pas change le site!');
}
#[Route('/app_view_all', name: 'app_view_all', options:['expose' => true])]
public function app_view_all(Request $request , ManagerRegistry $doctrine): Response
{
$notifications = $doctrine->getManager()->getRepository(PNotification::class)->findBy(['user' => $this->getUser(), 'seen' => false]);
foreach($notifications as $notification) {
$notification->setSeen(true);
}
$doctrine->getManager()->flush();
return new JsonResponse('ok');
}
#[Route('/redirect', name: 'app_redirect', options:['expose' => true])]
public function redirectsite(Request $request): Response
{
if(in_array('ROLE_RESPONSABLE', $this->getUser()->getRoles())){
$user = $this->em->getRepository(Users::class)->find($this->getUser());
$dossier = $user->getDossierResponsable();
} else {
$dossier = $this->em->getRepository(PDossier::class)->find($request->get('site_id'));
}
$request->getSession()->set('dossier', $dossier);
if(in_array('ROLE_ADMIN', $this->getUser()->getRoles())){
$sousModules = $this->em->getRepository(UsSousModule::class)->findAll();
} else {
$sousModules = $this->em->getRepository(UsSousModule::class)->findByUserOperations($this->getUser(), $dossier);
}
$modules = $this->em->getRepository(UsModule::class)->getModuleBySousModule($sousModules);
$data = [];
// dd($sousModules);
foreach($modules as $module) {
$sousModuleArray = [];
foreach ($sousModules as $sousModule) {
if($sousModule->getModule()->getId() == $module->getId()) {
$sousModuleArray[] = $sousModule;
}
}
$data[] = [
'module' => $module,
'sousModule' => $sousModuleArray
];
}
// dd($data);
$request->getSession()->set('modules', $data);
if(count($sousModules) < 1) {
die("Vous n'avez aucun prévilege pour continue cette operation. veuillez contacter votre chef!");
}
$redirectUrl = $sousModules[0]->getlink();
if($this->isGranted('ROLE_RESPONSABLE')){
return $this->redirectToRoute($redirectUrl);
}
return new JsonResponse(['redirect_url' => $redirectUrl]);
}
}