DashboardController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\CommentaireRepository;
  4. use App\Repository\FilmRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Attribute\Route;
  8. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  9. use Symfony\UX\Chartjs\Model\Chart;
  10. class DashboardController extends AbstractController
  11. {
  12. #[Route('/dashboard', name: 'app_dashboard')]
  13. public function index(
  14. ChartBuilderInterface $chartBuilderInterface,
  15. FilmRepository $filmRepository,
  16. CommentaireRepository $commentaireRepository
  17. ): Response
  18. {
  19. $filmsVus = $filmRepository->countFilmsVusBy($this->getUser());
  20. $filmsFavoris = $filmRepository->countFilmsFavorisBy($this->getUser());
  21. $commentairesParMois = $commentaireRepository->countCommentairesParMois();
  22. $chart = $chartBuilderInterface->createChart(Chart::TYPE_DOUGHNUT);
  23. $chart->setData([
  24. 'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  25. 'datasets' => [
  26. [
  27. 'label' => 'My First dataset',
  28. 'backgroundColor' => 'rgb(255, 99, 132)',
  29. 'borderColor' => 'rgb(255, 99, 132)',
  30. 'data' => [0, 10, 5, 2, 20, 30, 45],
  31. ],
  32. ],
  33. ]);
  34. $chart->setOptions([
  35. 'scales' => [
  36. 'y' => [
  37. 'suggestedMin' => 0,
  38. 'suggestedMax' => 100,
  39. ],
  40. ],
  41. ]);
  42. $chartDatesAjout = $chartBuilderInterface->createChart(Chart::TYPE_LINE);
  43. $ajoutsParMois = $filmRepository->countAjoutsParMois();
  44. $months = [];
  45. foreach ($ajoutsParMois as $data) {
  46. $months[$data['month']]['nbFilms'] = $data['entryCount'];
  47. }
  48. foreach ($commentairesParMois as $data) {
  49. $months[$data['month']]['nbComs'] = $data['entryCount'];
  50. }
  51. ksort($months);
  52. foreach($months as &$mois) {
  53. if(!isset($mois['nbFilms'])) {
  54. $mois['nbFilms'] = 0;
  55. }
  56. if (!isset($mois['nbComs'])) {
  57. $mois['nbComs'] = 0;
  58. }
  59. }
  60. $chartDatesAjout->setData([
  61. 'labels' => array_keys($months),
  62. 'datasets' => [
  63. [
  64. 'label' => 'Nombre de films ajoutés par mois',
  65. 'data' => array_map(fn($value): int => $value['nbFilms'], $months)
  66. ],
  67. [
  68. 'label' => 'Nombre de commentaires ajoutés par mois',
  69. 'data' => array_map(fn($value): int => $value['nbComs'], $months)
  70. ],
  71. ]
  72. ]);
  73. $chart->setOptions([
  74. 'scales' => [
  75. 'y' => [
  76. 'suggestedMin' => 0,
  77. 'suggestedMax' => 100,
  78. ],
  79. ],
  80. ]);
  81. return $this->render('dashboard/index.html.twig', [
  82. 'chart' => $chart,
  83. 'chartDatesAjout' => $chartDatesAjout,
  84. ]);
  85. }
  86. }