<?php
namespace App\Controller\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use Symfony\Component\Security\Core\User\UserInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Arrondissement;
use App\Entity\Commune;
use App\Entity\Departement;
use App\Entity\PriseCharge;
use App\Entity\Profession;
use App\Entity\TypePriseCharge;
use App\Entity\User;
use App\Entity\ZoneSanitaire;
use App\Entity\Infos;
use App\Entity\SuiviFormation;
use App\Form\PcFiltreType;
use App\Repository\PriseChargeRepository;
use App\Repository\SuiviFormationRepository;
use App\Repository\ConnexionRepository;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DashboardController extends AbstractDashboardController
{
private $helper;
private $chartBuilder;
private $PriseChargeRepository;
private $ConnexionRepository;
private $SuiviFormationRepository;
public function __construct(UploaderHelper $helper, ConnexionRepository $ConnexionRepository, PriseChargeRepository $PriseChargeRepository, ChartBuilderInterface $chartBuilder, SuiviFormationRepository $SuiviFormationRepository)
{
$this->helper = $helper;
$this->PriseChargeRepository = $PriseChargeRepository;
$this->ConnexionRepository = $ConnexionRepository;
$this->chartBuilder = $chartBuilder;
$this->SuiviFormationRepository = $SuiviFormationRepository;
}
/**
* @Route("/admin", name="admin")
*/
public function index(): Response
{
$connexions = $this->ConnexionRepository->findBy([], ['id'=>'DESC'], 4,0);
$totalConnexions = $this->ConnexionRepository->getTotalWeekConnexion();
if ($this->isGranted('ROLE_ADMIN')) {
$priseCharges = $this->PriseChargeRepository->findBy([], ['id'=>'DESC'], 20,0);
$suiviFormations = $this->SuiviFormationRepository->findBy([], ['id'=>'DESC'], 20,0);
}
else
{
$priseCharges = $this->PriseChargeRepository->findBy(['createBy' => $this->getUser()], ['id'=>'DESC'], 20,0);
$suiviFormations = $this->SuiviFormationRepository->findBy(['createBy' => $this->getUser()], ['id'=>'DESC'], 20,0);
}
return $this->render('admin/dashboard.html.twig', [
'chart' => $this->chart(),
'chart1' => $this->chart1(),
'chart2' => $this->chart2(),
'chart3' => $this->chart3(),
'chart4' => $this->chart4(),
'chart5' => $this->chart5(),
'priseCharges' => $priseCharges,
'suiviFormations' => $suiviFormations,
'connexions' => $connexions,
'totalConnexions' => $totalConnexions,
]);
}
/**
* @Route("/download/pc", name="downloadCSV")
*/
public function download(): Response
{
$dt = new \DateTime;
$priseCharges = $this->PriseChargeRepository->toCSV();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$entete = ["Numero", "Age", "Sexe", "profession", "Date", "Type", "Departement", "Zone sanitaire", "Commune", "Arrondissement", "Centre de sante", "motif", "nom", "prenom"];
$sheet->fromArray($entete,NULL);
$sheet->fromArray($priseCharges,NULL,'A2');
$writer = new Xlsx($spreadsheet);
$fileName = 'prise_en_charge_'.$this->getUser()->getId().$dt->format('Ymdhis').'.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
$publicDirectory = $this->getParameter('kernel.project_dir') . '/downloads/';
$writer->save($temp_file);
$writer->save($publicDirectory.$fileName);
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
//return $this->redirectToRoute('admin');
}
/**
* @Route("/download/sf", name="downloadSfCSV")
*/
public function downloadSf(): Response
{
$dt = new \DateTime;
$suiviFormations = $this->SuiviFormationRepository->toCSV();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$entete = ["formationUtiles", "partageInformations", "description", "integrationModule", "precisionModule", "conseilsInformations", "precisionThematique", "nom", "prenom"];
$sheet->fromArray($entete,NULL);
$sheet->fromArray($suiviFormations,NULL,'A2');
$writer = new Xlsx($spreadsheet);
$fileName = 'suivi_formation_'.$this->getUser()->getId().$dt->format('Ymdhis').'.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
$publicDirectory = $this->getParameter('kernel.project_dir') . '/downloads/';
$writer->save($temp_file);
$writer->save($publicDirectory.$fileName);
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
//return $this->redirectToRoute('admin');
}
public function configureDashboard(): Dashboard
{
return Dashboard::new()
->setTitle('<img class="img-fluid" src="assets/images/ASAF-PASS.png">');
}
public function configureMenuItems(): iterable
{
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Prise en charge', 'fas fa-list', PriseCharge::class);
if ($this->isGranted('ROLE_ADMIN')) {
yield MenuItem::linkToCrud('Arrondissement', 'fas fa-list', Arrondissement::class);
yield MenuItem::linkToCrud('Commune', 'fas fa-list', Commune::class);
yield MenuItem::linkToCrud('Département', 'fas fa-list', Departement::class);
yield MenuItem::linkToCrud('Profession', 'fas fa-list', Profession::class);
yield MenuItem::linkToCrud('Type prise en charge', 'fas fa-list', TypePriseCharge::class);
yield MenuItem::linkToCrud('Utilisateur', 'fas fa-user', User::class);
yield MenuItem::linkToCrud('Zone sanitaire', 'fas fa-list', ZoneSanitaire::class);
yield MenuItem::linkToCrud('Informations générales', 'fas fa-info', Infos::class);
yield MenuItem::linkToCrud('Suivi Formation', 'fas fa-list', SuiviFormation::class);
}
}
public function configureUserMenu(UserInterface $user): UserMenu
{
return parent::configureUserMenu($user)
->setAvatarUrl($this->helper->asset($user, 'imageFile'))
->addMenuItems([
MenuItem::linkToRoute('Changer Mot de passe', 'fa fa-lock', 'user_pass_change'),
])
->addMenuItems([
MenuItem::linkToRoute('Profile', 'fa fa-id-card', 'user_profile')
]);
}
private function chart()
{
$typePriseCharge = $this->PriseChargeRepository->getTotalByTypePriseCharge();
$chart = $this->chartBuilder->createChart(Chart::TYPE_DOUGHNUT);
$chart->setData([
'labels' => array_column($typePriseCharge, 'libelle'),
'datasets' => [
[
'label' => 'My First dataset',
'backgroundColor' => [
'rgb(255, 205, 86)',
'rgb(188,143,143)',
'rgb(50,205,50)',
],
'data' => array_column($typePriseCharge, 'total'),
],
],
]);
return $chart;
}
private function chart1()
{
$sexe = $this->PriseChargeRepository->getTotalBySexe();
$sexeLibelle = [];
foreach (array_column($sexe, 'sexe') as $key => $value) {
if ($value) {
$sexeLibelle[$key] = "Masculin";
}
else
{
$sexeLibelle[$key] = "Féminin";
}
}
$chart = $this->chartBuilder->createChart(Chart::TYPE_PIE);
$chart->setData([
'labels' => $sexeLibelle,
'datasets' => [
[
'label' => 'My First dataset',
'backgroundColor' => [
'rgb(54, 162, 235)',
'rgb(255, 99, 132)',
],
'data' => array_column($sexe, 'total'),
],
],
]);
return $chart;
}
private function chart2()
{
$totalByDepartement = $this->PriseChargeRepository->getTotalByDepartement();
$chart = $this->chartBuilder->createChart(Chart::TYPE_BAR);
$chart->setData([
'labels' => array_column($totalByDepartement, 'libelle'),
'datasets' => [
[
'label' => 'Prise en charge par département',
'backgroundColor' => [
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
'rgb(54, 162, 235)',
],
'data' => array_column($totalByDepartement, 'total'),
],
],
]);
return $chart;
}
private function chart3()
{
$PriseChargePerMonth = $this->PriseChargeRepository->getPriseChargePerMonth();
//dd($PriseChargePerMonth);
$chart = $this->chartBuilder->createChart(Chart::TYPE_LINE);
$chart->setData([
'labels' => array_column($PriseChargePerMonth, 'label'),
'datasets' => [
[
'label' => 'Nombre de prise en charge par mois',
'data' => array_column($PriseChargePerMonth, 'total'),
'fill' => false,
'borderColor' => '#dc3545',
//'tension' => 0.1
],
],
]);
return $chart;
}
private function chart4()
{
$totalByAgeRange = $this->PriseChargeRepository->getTotalByAgeRange();
$chart = $this->chartBuilder->createChart(Chart::TYPE_BAR);
$chart->setData([
'labels' => array_column($totalByAgeRange, 'label'),
'datasets' => [
[
'label' => "Nombre de prise en charge par tranche d'âge",
'data' => array_column($totalByAgeRange, 'total'),
'backgroundColor' => [
'#d63384',
'#d63384',
'#d63384',
'#d63384',
'#d63384',
'#d63384',
'#d63384',
'#d63384',
],
],
],
]);
return $chart;
}
private function chart5()
{
$ConnexionPerMonth = $this->ConnexionRepository->getConnexionPerMonth();
$chart = $this->chartBuilder->createChart(Chart::TYPE_LINE);
$chart->setData([
'labels' => array_column($ConnexionPerMonth, 'label'),
'datasets' => [
[
'label' => 'Nombre de connexion par mois',
'data' => array_column($ConnexionPerMonth, 'total'),
'fill' => false,
'borderColor' => '#fd7e14',
//'tension' => 0.1
],
],
]);
return $chart;
}
}