<?php
namespace App\Controller\Admin;
use App\Controller\Admin\DemandeurCrudController;
use App\Entity\Demande;
use App\Entity\Entreprise;
use App\Entity\Form;
use App\Entity\FormUser;
use App\Entity\Meeting;
use App\Entity\NoteToDo;
use App\Entity\Partenaire;
use App\Entity\Particulier;
use App\Entity\Reclamation;
use App\Entity\Setting;
use App\Entity\User;
use App\Form\MeetingType;
use App\Form\NoteToDoType;
use App\Form\ReclamationType;
use App\Entity\ReclamationReponse;
use App\Form\ReclamationReponseType;
use App\Form\SettingType;
use App\Repository\FormUserRepository;
use App\Repository\MeetingRepository;
use App\Repository\SettingRepository;
use App\Service\ExportExlService;
use App\Service\NotificationManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\Admin\RDCrudController;
use App\Controller\Admin\UserCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Asset;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use App\Controller\Admin\FormCrudController;
use Symfony\Component\Routing\Generator\UrlGenerator;
use App\Security\EmailVerifier;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
#[Route('/')]
class DashboardController extends AbstractDashboardController
{
private $adminUrlGenerator;
private $entityManager;
private $emailVerifier;
public function __construct( AdminUrlGenerator $adminUrlGenerator, EntityManagerInterface $entityManager, EmailVerifier $emailVerifier, SettingRepository $settingRepository)
{
$this->adminUrlGenerator = $adminUrlGenerator;
$this->entityManager = $entityManager;
$this->emailVerifier = $emailVerifier;
$this->settingRepository = $settingRepository;
}
#[Route('/admin', name: 'admin_dashboard')]
public function index(): Response
{
$forms = $this->entityManager->getRepository(Form::class)->findAll();
// statistiques pour admin
$formUserEtat = $this->entityManager->getRepository(FormUser::class)->countWithStatus();
$demandesEtat = $this->entityManager->getRepository(Demande::class)->countWithStatus();
$demandesEtatMerged = $this->mergeArraysByColumnValue( $formUserEtat, $demandesEtat );
$formUserVille = $this->entityManager->getRepository(FormUser::class)->countWithVille();
$demandesVille = $this->entityManager->getRepository(Demande::class)->countWithVille();
$demandesVilleMerged = $this->mergeArraysByColumnValue( $formUserVille, $demandesVille );
$reclamationsEtatForDemande = $this->entityManager->getRepository(Reclamation::class)->countWithStatusForDemande();
$reclamationsEtatForFormuser = $this->entityManager->getRepository(Reclamation::class)->countWithStatusForFormuser();
$reclamationsEtatMerged = $this->mergeArraysByColumnValue( $reclamationsEtatForDemande, $reclamationsEtatForFormuser );
$demandeursVilleForDemande = $this->entityManager->getRepository(User::class)->countWithVilleForDemande();
$demandeursVilleForFormuser = $this->entityManager->getRepository(User::class)->countWithVilleForFormuser();
$reclamationsVilleMerged = $this->mergeArraysByColumnValue( $demandeursVilleForDemande, $demandeursVilleForFormuser );
$countDemandeurs = $this->entityManager->getRepository(User::class)->countByRole('ROLE_USER');
$countResponsables = $this->entityManager->getRepository(User::class)->countByRole('ROLE_RD');
// statistiques pour responsable dossier (Rd)
$formUserEtatRd = $this->entityManager->getRepository(FormUser::class)->countWithStatusRd( $this->getUser()->getId() );
$demandesEtatRd = $this->entityManager->getRepository(Demande::class)->countWithStatusRd( $this->getUser()->getId() );
$demandesEtatRdMerged = $this->mergeArraysByColumnValue( $formUserEtatRd, $demandesEtatRd );
$reclamationsEtatRdForDemande = $this->entityManager->getRepository(Reclamation::class)->countWithStatusRdForDemande( $this->getUser()->getId() );
$reclamationsEtatRdForFormuser = $this->entityManager->getRepository(Reclamation::class)->countWithStatusRdForFormuser( $this->getUser()->getId() );
$reclamationsEtatRdMerged = $this->mergeArraysByColumnValue( $reclamationsEtatRdForDemande, $reclamationsEtatRdForFormuser );
$user = $this->getUser();
if ($this->isGranted("ROLE_ADMIN")) {
$meeting = $this->entityManager->getRepository(Meeting::class)->findAll();
} else {
$meeting_demande_rd = $this->entityManager->getRepository(Meeting::class)->getMyMeetingsForDemandeRD($user);
$meeting_formUser_rd = $this->entityManager->getRepository(Meeting::class)->getMyMeetingsForFormUserRD($user);
$meeting = array_merge($meeting_demande_rd, $meeting_formUser_rd);
}
$date_meeting = [];
foreach ( $meeting as $value ) {
$date_meeting []= $value->getMeetingDate()->format("j-n-Y");
}
$new_meeting = new Meeting();
$form_meeting = $this->createForm(MeetingType::class, $new_meeting);
$new_note = new NoteToDo();
$form_note = $this->createForm(NoteToDoType::class, $new_note);
$noteToDo = $this->entityManager->getRepository(NoteToDo::class)->getMyNotesForUser($user);
/* if ($this->isGranted("ROLE_ADMIN")) {
$noteToDo = $this->entityManager->getRepository(NoteToDo::class)->findAll();
} else {
$noteToDo = $this->entityManager->getRepository(NoteToDo::class)->findAll();
}*/
return $this->render('admin/admin_dashboard.html.twig', [
'forms' => $forms,
'demandesEtat' => $demandesEtat,
'formUserEtat' => $this->mergeArraysOfArrayByColumn($formUserEtat, 'fid'),
'demandesVille' => $demandesVille,
'formUserVille' => $this->mergeArraysOfArrayByColumn($formUserVille, 'fid'),
'demandeursVilleForDemande' => $demandeursVilleForDemande,
'demandeursVilleForFormuser' => $this->mergeArraysOfArrayByColumn($demandeursVilleForFormuser, 'fid'),
'reclamationsEtatDemande' => $reclamationsEtatForDemande,
'reclamationsEtatFormUser' => $this->mergeArraysOfArrayByColumn($reclamationsEtatForFormuser, 'fid'),
'demandesEtatRd' => $demandesEtatRd,
'formUserEtatRd' => $this->mergeArraysOfArrayByColumn($formUserEtatRd, 'fid'),
// 'demandesEtatRd' => $demandesEtatRdMerged,
'reclamationsEtatRdDemande' => $reclamationsEtatRdForDemande,
'reclamationsEtatRdFormUser' => $this->mergeArraysOfArrayByColumn($reclamationsEtatRdForFormuser, 'fid'),
'countDemandeurs' => $countDemandeurs,
'countResponsables' => $countResponsables,
// 'countDemande' => array_sum( array_column($demandesEtatMerged, 'count') ),
'countDemande' => array_sum( array_column($demandesEtat, 'count') ),
'countFormUser' => array_sum( array_column($formUserEtat, 'count') ),
'countReclamation' => array_sum( array_column($reclamationsEtatMerged, 'count') ),
'countDemandeRd' => array_sum( array_column($demandesEtatRd, 'count') ),
'countFormUserRd' => array_sum( array_column($formUserEtatRd, 'count') ),
'countReclamationRd' => array_sum( array_column($reclamationsEtatRdMerged, 'count') ),
'meeting' => $meeting,
'date_meeting'=> json_encode($date_meeting),
'form_meeting' => $form_meeting->createView(),
'noteToDo' => $noteToDo,
'form_noteToDo' => $form_note->createView(),
]);
}
#[Route('/chart/export/excel', name: 'chart_export_excel')]
public function excelExportDemandeEtat(Request $request, ExportExlService $exportExlService)
{
$file_full_name = $exportExlService->xlsExportData( json_decode( $request->query->get('chart_data') ) );
return $this->file($file_full_name);
}
#[Route('/redirect/email/{type}/{id}', name: 'app_redirect_email')]
public function redirectEmail( $type = 'demande', $id ): Response
{
if ( $type == 'demande' ){
$edit_demande_url = $this->adminUrlGenerator
->setController(DemandeCrudController::class)
->setAction(Action::EDIT)
->setEntityId($id)
->generateUrl();
}
if ( $type == 'formuser' ){
$edit_demande_url = $this->adminUrlGenerator
->setController(FormUserCrudController::class)
->setAction(Action::EDIT)
->setEntityId($id)
->generateUrl();
}
if ( $type == 'demandeur' ){
$edit_demande_url = $this->adminUrlGenerator
->setController(DemandeurCrudController::class)
->setAction(Action::EDIT)
->setEntityId($id)
->generateUrl();
}
if ( $type == 'reclamation' ){
$edit_demande_url = $this->adminUrlGenerator
->setController(ReclamationCrudController::class)
->setAction(Action::EDIT)
->setEntityId($id)
->generateUrl();
}
return $this->redirect($edit_demande_url);
}
#[Route('/Demandeurs', name: 'Demandeurs')]
public function Demandeurs(): Response
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
$url = $this->adminUrlGenerator->setController(DemandeurCrudController::class)->generateUrl();
return $this->redirect($url);
}
#[Route('/Responsable_dossier', name: 'Responsable_dossier')]
public function Responsable_dossier(): Response
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
$url = $this->adminUrlGenerator->setController(RDCrudController::class)->generateUrl();
return $this->redirect($url);
}
#[Route('/Reclamation', name: 'Reclamation')]
public function Reclamation(Request $request, EntityManagerInterface $entityManager): Response
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
$url = $this->adminUrlGenerator->setController(ReclamationCrudController::class)->generateUrl();
return $this->redirect($url);
}
#[Route('/Reclamation/{id}/Response', name: 'ReclamationResponse')]
public function ResponseNew($id, Request $request, EntityManagerInterface $entityManager): Response
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
$reclamation = $this->getDoctrine()->getRepository(Reclamation::class)->find($id);
if (($this->isGranted('ROLE_ADMIN') || $this->getUser() == $reclamation->getUser()) == false)
return $this->redirectToRoute('Reclamation');
$reponse = new ReclamationReponse();
$form = $this->createForm(ReclamationReponseType::class, $reponse);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$reponse->setUser($this->getUser());
$reponse->setReclamation($reclamation);
$entityManager->persist($reponse);
$entityManager->flush();
return $this->redirectToRoute('ReclamationResponse', ['id' => $id]);
}
return $this->renderForm('reclamation/ReclamationResponse.html.twig', [
'form' => $form,
'reclamation' => $reclamation
]);
}
#[Route('/form/reponses', name: 'FormReponses')]
public function FormReponses(): Response
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
$url = $this->adminUrlGenerator->setController(FormUserCrudController::class)->generateUrl();
return $this->redirect($url);
}
#[Route('/audit/list', name: 'admin_audit_list')]
public function adminAuditList()
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
return $this->renderForm('audit/audit_list.html.twig', [
]);
}
#[Route('/audit/history/{entity}', name: 'admin_audit_history')]
public function adminAuditHistory( $entity )
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
return $this->renderForm('audit/audit_history.html.twig', [
'entity' => $entity
]);
}
#[Route('/audit/history/entry/{entity}/{id}', name: 'admin_audit_history_entry')]
public function adminAuditHistoryEntry( $entity, $id )
{
$redirection = $this->VerificationAccount();
if ($redirection) {
return $redirection;
}
return $this->renderForm('audit/audit_history_entity.html.twig', [
'entity' => $entity,
'id' => $id
]);
}
public function configureDashboard( ): Dashboard
{
$logo_path = '/images/logo/igppp-logo.png';
$app_logo = $this->settingRepository->findOneBy(['paramName' => 'logo_app']);
if ($app_logo){
$logo = $app_logo->getParamValue();
$logo_path = "/uploads/setting/".$logo;
//$logo_path = $logo;
}
return Dashboard::new()
// you can include HTML contents too (e.g. to link to an image)
->setTitle('<img src="' . $logo_path . '" alt="IGPPP" width="150" height="90">')
// the path defined in this method is passed to the Twig asset() function
->setFaviconPath('favicon.svg')
// the domain used by default is 'messages'
//->setTranslationDomain('my-custom-domain')
// there's no need to define the "text direction" explicitly because
// its default value is inferred dynamically from the user locale
->setTextDirection('ltr')
// set this option if you prefer the page content to span the entire
// browser width, instead of the default design which sets a max width
->renderContentMaximized()
// set this option if you prefer the sidebar (which contains the main menu)
// to be displayed as a narrow column instead of the default expanded design
->renderSidebarMinimized()
// by default, users can select between a "light" and "dark" mode for the
// backend interface. Call this method if you prefer to disable the "dark"
// mode for any reason (e.g. if your interface customizations are not ready for it)
->disableDarkMode()
// by default, all backend URLs are generated as absolute URLs. If you
// need to generate relative URLs instead, call this method
->generateRelativeUrls();
}
public function configureAssets(): Assets
{
return Assets::new()
->addHtmlContentToHead('<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">')
->addJsFile('js/plugin/jquery-3.6.0.min.js')
->addJsFile('js/plugin/jquery-ui-1.11.4.min.js')
->addJsFile('js/alerte.js')
->addJsFile('//cdn.jsdelivr.net/npm/sweetalert2@11')
->addJsFile('js/custom/widget.js')
->addCssFile('css/custom/style-dasboard.css')
->addCssFile('css/custom/widget.css')
;
}
public function configureCrud(): Crud
{
return Crud::new()->showEntityActionsInlined();
}
public function configureActions(): Actions
{
return Actions::new()
->addBatchAction(Action::BATCH_DELETE)
->add(Crud::PAGE_INDEX, Action::NEW)
->add(Crud::PAGE_INDEX, Action::EDIT)
->add(Crud::PAGE_INDEX, Action::DELETE)
->add(Crud::PAGE_DETAIL, Action::EDIT)
->add(Crud::PAGE_DETAIL, Action::INDEX)
->add(Crud::PAGE_DETAIL, Action::DELETE)
->add(Crud::PAGE_EDIT, Action::SAVE_AND_RETURN)
->add(Crud::PAGE_EDIT, Action::SAVE_AND_CONTINUE)
->add(Crud::PAGE_NEW, Action::SAVE_AND_RETURN)
->add(Crud::PAGE_NEW, Action::SAVE_AND_ADD_ANOTHER)
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setIcon('fa fa-add')->setLabel(false);
})
->update(Crud::PAGE_INDEX, Action::EDIT, function (Action $action) {
return $action->setIcon('fa fa-pen')->setLabel(false)->setHtmlAttributes(['title'=>'Modifier']);
})
->update(Crud::PAGE_INDEX, Action::DELETE, function (Action $action) {
return $action->setIcon('fa fa-trash')->setLabel(false);
})
;
}
public function configureMenuItems(): iterable
{
yield MenuItem::linkToDashboard('acceuil', '/images/home.png');
yield MenuItem::linkToRoute('gestion_demandeur', '/images/profile.png', 'Demandeurs')->setPermission('ROLE_ADMIN');
yield MenuItem::linkToRoute('gestion_responsable_dossier', '/images/user.png', 'Responsable_dossier')->setPermission('ROLE_ADMIN');
yield MenuItem::linkToRoute('gestion_reclamation', '/images/warning.png', 'Reclamation');
// yield MenuItem::subMenu('Gestion des demandes', '/images/question.png')->setSubItems([
// MenuItem::linkToCrud('Demandes du formulaire principale', '/images/home.png', Demande::class),
// MenuItem::linkToRoute('Demandes des autres formulaires', '/images/home.png', 'FormReponses'),
// ]);
yield MenuItem::linkToCrud('suivi_demande_form_principl', '/images/pass.png', Demande::class);
yield MenuItem::linkToRoute('suivi_demande_autre_formulaire', '/images/document.png', 'FormReponses');
yield MenuItem::linkToCrud('generateur_formulaire', '/images/new-document.png', Form::class)->setPermission('ROLE_ADMIN');
yield MenuItem::linkToRoute('historique_action', '/images/history.png', 'admin_audit_list')->setPermission('ROLE_ADMIN');
}
public function VerificationAccount()
{
if ($this->getUser()) {
if (!$this->getUser()->isVerified()) {
$this->addFlash('VerifAccount', 'Vous devez vérifier votre compte !');
return $this->redirectToRoute('app_logout');
}
if (!$this->getUser()->getCompleted()) {
$this->addFlash('VerifAccount', 'Vous devez compléter votre profil !');
return $this->redirectToRoute('app_profil');
}
if (!$this->getUser()->isActive()) {
$this->addFlash('VerifAccount', 'Votre compte est désactivé !');
return $this->redirectToRoute('app_logout');
}
return null;
} else {
return $this->redirectToRoute('login');
}
}
private function mergeArraysByColumnValue( $array1, $array2 ){
$result = [];
foreach(array_merge($array1,$array2) as $array){
if(isset($result[$array['label']])){
$result[$array['label']]['count'] += $array['count'];
}else{
$result[$array['label']] = $array;
}
}
return array_values($result);
}
private function mergeArraysOfArrayByColumn( $array, $column ){
$result = [];
foreach ( $array as $array_to_merge ){
$key = $array_to_merge[$column];
unset($array_to_merge[$column]);
$result[ $key ][] = $array_to_merge;
}
return $result;
}
}