src/Controller/Front/DashboardController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Controller\AbstractScpController;
  4. use App\Entity\Link;
  5. use App\Entity\User;
  6. use App\Manager\ConsumptionManager;
  7. use App\Manager\ContractManager;
  8. use App\Manager\ConversationManager;
  9. use App\Manager\InvoiceManager;
  10. use App\Manager\MeterReadingManager;
  11. use App\Manager\NetworkInterventionManager;
  12. use App\Manager\SupervisionManager;
  13. use App\Manager\TimetableManager;
  14. use App\Manager\WateringTips\WateringTipsManager;
  15. use App\Manager\WaterPurificationApparatusManager;
  16. use App\Repository\App\DashboardRepository;
  17. use App\Repository\App\LinkRepository;
  18. use App\Repository\App\UserRepository;
  19. use App\Traits\DonneeCompteClientAvailableTrait;
  20. use Doctrine\DBAL\Exception;
  21. use Psr\Cache\InvalidArgumentException;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. class DashboardController extends AbstractScpController
  25. {
  26.     /**
  27.      * Injecte DonneeCompteClientManager dans le controlleur
  28.      * Ajoute automatiquement la variable donneeCompteClient aux paramètres de la méthode render
  29.      */
  30.     use DonneeCompteClientAvailableTrait;
  31.     public function __construct(
  32.         protected DashboardRepository $dashboardRepository,
  33.         protected UserRepository $userRepository,
  34.         protected InvoiceManager $invoiceManager,
  35.         protected ContractManager $contractManager,
  36.         protected ConsumptionManager $consumptionManager,
  37.         protected LinkRepository $linkRepository,
  38.         protected MeterReadingManager $meterReadingManager,
  39.         protected ConversationManager $conversationManager,
  40.         protected TimetableManager $timetableManager,
  41.         protected WaterPurificationApparatusManager $wpApparatusManager,
  42.         protected NetworkInterventionManager $networkInterventionManager,
  43.         protected WateringTipsManager $wateringTipsManager,
  44.         protected SupervisionManager $supervisionManager,
  45.     ) {
  46.     }
  47.     /**
  48.      * @Route("/", name="dashboard")
  49.      * @throws InvalidArgumentException
  50.      * @throws Exception
  51.      */
  52.     public function display(): Response
  53.     {
  54.         /* @var User $user */
  55.         $user $this->getUser();
  56.         $hasRoleWeather $user->hasRole('ROLE_ELIGIBLE_WATERING_TIPS');
  57.         $guestNumber 0;
  58.         if ($this->getUser()->isMainAccount()) {
  59.             $guestNumber $this->userRepository->count(['type' => User::TYPE_GUEST'mainAccount' => $this->getUser()]);
  60.         }
  61.         $interventions $this->getInterventions();
  62.         $hasWateringTips false;
  63.         $address = [];
  64.         if ($hasRoleWeather) {
  65.             $hasWateringTips = !empty($this->wateringTipsManager->getActiveWateringTipsByUser($user));
  66.             $address $this->wateringTipsManager->getAddressWithXYForWateringTips($usertrue);
  67.         }
  68.         ['stationWithoutIndex' => $stationWithoutIndex] = $this->meterReadingManager->getStationsWithIndexStatus($user);
  69.         return $this->render('front/dashboard/dashboard.html.twig', [
  70.             'dashboard'                 => $this->dashboardRepository->find(1),
  71.             'interventions'             => $interventions,
  72.             'isMeterReadingAuthorised'  => $this->meterReadingManager->isMeterReadingAuthorised($user),
  73.             'invoices'                  => $this->invoiceManager->getInvoicesToPay($user),
  74.             'guestNumber'               => $guestNumber,
  75.             'hasEBDContracts'           => $this->contractManager->hasEBDContracts($user),
  76.             'links'                     => $this->linkRepository->findAllByAreaAndRoles(Link::AREA_DASHBOARD$this->getUser()->getRoles()),
  77.             'consumptions'              => $this->consumptionManager->getDashboardConsumption($user),
  78.             'count_unread_conversation' => $this->conversationManager->countUnread($user),
  79.             'conversations'             => $this->conversationManager->getConversations($user3),
  80.             'deadlines'                 => $this->timetableManager->getNextDeadlines($user),
  81.             'apparatus'                 => $this->wpApparatusManager->getAllForDashboard($user),
  82.             'weatherWeek'               => $this->getParameter('weather_hidden') || $hasRoleWeather ?
  83.                 'HIDDEN' $this->wateringTipsManager->getWeatherWeekData($user),
  84.             'hasWateringTips'           => $hasWateringTips,
  85.             'hasDroughtAlert'           => $this->wateringTipsManager->isConcernedByDroughtAlert($user),
  86.             'activeTown'                => $address['NomCommune'] ?? null,
  87.             'weatherHidden'             => $this->getParameter('weather_hidden') ?? null,
  88.             'stationWithoutIndex'       => $stationWithoutIndex,
  89.         ]);
  90.     }
  91.     protected function getInterventions(): array
  92.     {
  93.         $data $this->networkInterventionManager->getInterventions($this->getUser());
  94.         $interventions = [];
  95.         foreach ($data as $intervention) {
  96.             $interventionDate md5($intervention['DateDebutIntervention'].$intervention['DateFinIntervention']);
  97.             if (!isset($interventions[$interventionDate])) {
  98.                 $interventions[$interventionDate] = [
  99.                     'DateDebutIntervention' => $intervention['DateDebutIntervention'],
  100.                     'DateFinIntervention'   => $intervention['DateFinIntervention'],
  101.                     'deliveryStations'      => [],
  102.                 ];
  103.             }
  104.             $interventions[$interventionDate]['deliveryStations'][] = $intervention;
  105.         }
  106.         return $interventions;
  107.     }
  108.     /**
  109.      * @throws InvalidArgumentException
  110.      */
  111.     #[Route(
  112.         '/weather',
  113.         name'display_weather',
  114.     )]
  115.     public function displayWeather(): Response
  116.     {
  117.         $weatherWeek $this->wateringTipsManager->getWeatherWeekData($this->getUser()) ?? [];
  118.         $address $this->wateringTipsManager->getAddressWithXYForWateringTips($this->getUser(), true);
  119.         $status array_key_exists('error'$weatherWeek) ? Response::HTTP_BAD_REQUEST Response::HTTP_OK;
  120.         $content $this->renderView('front/watering_tips/_weather_client_insert.html.twig', [
  121.             'dashboard'   => $this->dashboardRepository->find(1),
  122.             'weatherWeek' => $weatherWeek,
  123.             'activeTown'  => $address['LibelleCommune'] ?? null,
  124.         ]);
  125.         return new Response(
  126.             content$content,
  127.             status$status
  128.         );
  129.     }
  130. }