src/Controller/MediaController.php line 125

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Media;
  4. use App\Entity\Detections;
  5. use App\Form\NewMediaType;
  6. use App\Form\MediaType;
  7. use App\Enum\BasicEnum;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  18. use App\Service\ChartsService;
  19. use App\Service\DataType;
  20. class MediaController extends AbstractController
  21. {
  22.     private $security;
  23.     private $doctrine;
  24.     private $charts;
  25.     public function __construct(
  26.         ManagerRegistry $doctrine,
  27.         Security $security,
  28.         ChartsService $charts
  29.     )
  30.     {
  31.         $this->doctrine $doctrine;
  32.         $this->security $security;
  33.         $this->charts $charts;
  34.        
  35.     }
  36.     #[Route('/media/list'name'all_media')]
  37.     public function index(ManagerRegistry $doctrine): Response
  38.     {
  39.         $session = new Session();
  40.         //$session->start();
  41.         $user $this->security->getUser();
  42.         $medias $doctrine->getRepository(Media::class)->findBy(['createdBy'=>$user]);
  43.         if (!$medias) {
  44.            
  45.             $session->getFlashBag()->add(
  46.                 'warning',
  47.                 '<h4 class="alert-heading">No Media yet !</h4>To proceed, first create a new media profile.'
  48.             );
  49.             return $this->redirectToRoute('new_media');
  50.         }else {
  51.         return $this->render('media/index.html.twig', [
  52.             'medias' => $medias,
  53.         ]);
  54.     }
  55.     }
  56.     #[Route('/media/new'methods:"GET|POST"name'new_media')]
  57.     public function createMedia(Request $request): Response
  58.     {
  59.         $media = new Media();
  60.         $form $this->createForm(NewMediaType::class, $media,[
  61.             'entityManager' => $this->doctrine->getManager(),
  62.         ]);
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted() && $form->isValid()) {
  65.             $media->setCreatedBy($this->security->getUser());
  66.            // $media->setCreatedAt((new DateTime('now'))->format('Y-m-d H:i:s'));
  67.             $em $this->doctrine->getManager();
  68.             $em->persist($media);
  69.             $em->flush();
  70.             /**
  71.              * Logo file upload
  72.              */
  73.             $base64 $request->request->get('logoFinal');
  74.             if ($base64) {
  75.                 list($type$data) = explode(';'$base64);
  76.                 list(, $data)      = explode(','$base64);
  77.                 $data base64_decode($data);
  78.                 $newFilename uniqid() . '-logo.' explode('/'$type)[1];
  79.                 // Move the file to the directory where brochures are stored
  80.                 try {
  81.                     $path =$this->getParameter('upload_media_directory') . '/' $media->getId() . '/logo';
  82.                     if (!is_dir($path)) {
  83.                         // dir doesn't exist, make it
  84.                         mkdir($path0777true);
  85.                     }
  86.                     file_put_contents(
  87.                         $path '/'.$newFilename,
  88.                         $data
  89.                     );
  90.                     $media->setLogo($this->getParameter('upload_media_directory_display') . $media->getId() . '/logo/' $newFilename);
  91.                     $em->flush();
  92.                 } catch (FileException $e) {
  93.                     echo 'error';
  94.                     // ... handle exception if something happens during file upload
  95.                 }
  96.                 // updates the 'brochureFilename' property to store the PDF file name
  97.                 // instead of its contents
  98.                 //$media->setBrochureFilename($newFilename);
  99.             }
  100.             return $this->redirectToRoute('all_media');
  101.         }
  102.         // // tell Doctrine you want to (eventually) save the Product (no queries yet)
  103.         // $entityManager->persist($media);
  104.         // // actually executes the queries (i.e. the INSERT query)
  105.         // $entityManager->flush();
  106.         return $this->render('media/new.html.twig', [
  107.             'media' => $media,
  108.             'form' => $form->createView(),
  109.         ]);
  110.     }
  111.     #[Route('/media/show/{id}'methods:"GET"name'show_media')]
  112.     public function showMedia(Media $media): Response
  113.     {
  114.         if (!($media->getCreatedBy() == $this->security->getUser())) {
  115.         
  116.             $this->addFlash("error""<h4>Forbidden</h4>Media can only be edited by its owner.");
  117.             return $this->redirectToRoute('all_media');
  118.         }else {
  119.             //countries
  120.             //$countries = BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country', 'Enum.Country.');
  121.             $countries BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country''');
  122.             //today is
  123.             $today=new \DateTime();
  124.               //yesterday
  125.               $yesterday $today->modify("-1 days");
  126.               $dateRange = ['from'=>$yesterday'to' => new \DateTime()] ;
  127.               //yestermonth
  128.               $yestermonth = (new \DateTime())->sub( ( new \DateInterval('P1M')))->format('m');
  129.             //Last 7 days
  130.             $dateFrom $today->modify("-6 days");
  131.            
  132.             $to7daysDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P7D'falsetrue);
  133.             $to7daysConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P7D'truetrue);
  134.             //$chart7daysConsumers = $this->charts->generateLineChart(['from' => $dateFrom->format('Y-m-d'), 'to' => $today->format("Y-m-d")], [['label' => 'Last 7 days Consumers', 'data' => ["0.00", "0.00", "0.00", "0.00", "0.00", "5.26", "0.00"]], ['label' => 'Detections last 7 days', 'background' => "transparent", 'data' => ["0.00", "3.00", "0.00", "5.00", "0.00", "0.00", "0.00"], "borderDash" => [5, 5], "color" => "#8ca1ca"]], "Consumers", null, "nbrConsumers");
  135.            
  136.             $yester7daysDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P14D'falsetrue);
  137.             $yester7daysConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P14D'truetrue);
  138.                 // ! chart Head Cards
  139.             $chart7daysConsumers $this->charts->generateLineChart(
  140.                 $period = ['from' => (new \DateTime())->modify("-7 days"), 'to' => new \DateTime()],[
  141.                     ['label' => 'Last 7 days Consumers','data'=>$this->charts->prepareForCharts($to7daysConsumers"nbrConsumers"$period)], 
  142.             ['label' => 'The 7 days before Consumers',"borderDash" => [55], "color" => "#8ca1ca"'data'=>$this->charts->prepareForCharts($yester7daysConsumers"nbrConsumers",  ['from' => (new \DateTime())->modify("-14 days"), 'to' => (new \DateTime())->modify("-7 days")])]
  143.         ],"Consumers"null
  144.             );
  145.             $chart7daysDetections $this->charts->generateLineChart(
  146.                 $period = ['from' => (new \DateTime())->modify("-7 days"), 'to' => new \DateTime()],[
  147.                     ['label' => 'Last 7 days Consumers','data'=>$this->charts->prepareForCharts($to7daysDetections"nbrDetections"$period)], 
  148.             ['label' => 'The 7 days before Consumers',"borderDash" => [55], "color" => "#8ca1ca"'data'=>$this->charts->prepareForCharts($yester7daysDetections"nbrDetections",  ['from' => (new \DateTime())->modify("-14 days"), 'to' => (new \DateTime())->modify("-7 days")])]
  149.         ],"Detections"null
  150.             );
  151.                 
  152.             //Today
  153.             $todayDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P1D');
  154.             $todayConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P1D'true);
  155.                  //! ****
  156.             $todayDetectionsbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D');
  157.             $todayDetectionsTopCountry  array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D'nullnullfalsefalsefalse3), "nbrDetections""country");
  158.             $todayConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruenull'P1D');
  159.             $todayConsumersTopCountry  array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D'nullnullfalsetruefalse3), "nbrConsumers""country");
  160.                 // !chart Today Maps
  161.             $toDayWorldMapDetections $this->charts->getMapAreas(array_column($todayDetectionsbyCountry"nbrCountryDetections""country"), "Detections"$countries);
  162.             $toDayWorldMapConsumers $this->charts->getMapAreas(array_column($todayConsumersbyCountry"nbrConsumers""country"), "Consumers"$countries);
  163.                 //! ****
  164.             $todayConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruetrue'P1D');
  165.             $todayDetectionsbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'); 
  166.             $todayDetectionsTopRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'nullnullfalsefalsefalse10 ); 
  167.             $todayConsumersTopRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'nullnullfalsetruefalse10 );
  168.             $todayConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalse'P1D'nullnulltrue);
  169.              //!chart Today OS
  170.             $toDayDoughnutDetectionsChart $this->charts->generateDoughnutChart($todayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Detections"os");
  171.             $toDayDoughnutConsumersChart $this->charts->generateDoughnutChart($todayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Consumers"os");
  172.                 // ! ****
  173.             $todayDetectionsbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenull'P1D'nullnullfalsefalsetrue);
  174.             $todayConsumersbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenull'P1D'nullnullfalsetruetrue);
  175.                 //! chart Today device type
  176.             $toDayBarDetectionsChart $this->charts->generateBarChart($todayDetectionsbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Detections"deviceType");
  177.             $toDayBarConsumersChart $this->charts->generateBarChart($todayConsumersbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Consumers"deviceType");
  178.             //Yesterday
  179.             $yesterdayDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P2D')[0]['nbrDetections'] - $todayDetections[0]['nbrDetections'];
  180.             $yesterdayConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P2D'true)[0]['nbrConsumers'] - $todayConsumers[0]['nbrConsumers'];
  181.             $yesterdayConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruenullnullnull$dateRange);
  182.             $yesterdayConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsetruenullnull$dateRange);
  183.             $yesterdayConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenullnull$dateRangetrue);
  184.             //this Month
  185.             $tomonthDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, (new \DateTime())->format('m'));
  186.             $tomonthConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenull, (new \DateTime())->format('m'), nulltrue);
  187.             $tomonthConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruefalsenull, (new \DateTime())->format('m'), nullfalse);
  188.             $tomonthConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsetruenull, (new \DateTime())->format('m'), nullfalse);
  189.             $tomonthConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, (new \DateTime())->format('m'));
  190.             //YesterMonth
  191.             $yestermonthConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenull$yestermonthnulltrue);
  192.             $yestermonthConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruefalsenull$yestermonthnullfalse);
  193.             $yestermonthConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsetruenull$yestermonthnullfalse);
  194.             $yestermonthConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media$yestermonth);
  195.             $yestermonthDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media$yestermonth);
  196.             //AVG
  197.             $avgPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'falsefalsetrue);
  198.             $avgByCountryPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'truefalsetrue);
  199.             $avgByRegionPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'falsetruetrue);
  200.             return $this->render('media/show.html.twig', [
  201.                 'media' => $media,
  202.                 'todayTotalDetections' => $todayDetections,
  203.                 'yesterdayTotalDetections' => $yesterdayDetections,
  204.                 'tomonthTotalDetections' => $tomonthDetections,
  205.                 'yestermonthTotalDetections' => $yestermonthDetections,
  206.                 'tomonthTotalConsumers' => $tomonthConsumers,
  207.                 'yestermonthTotalConsumers' => $yestermonthConsumers,
  208.                 'todayTotalConsumers' => $todayConsumers,
  209.                 'yesterdayTotalConsumers' => $yesterdayConsumers,
  210.                 'todayConsumersbyCountry' => $todayConsumersbyCountry
  211.                 'todayDetectionsbyCountry' => $todayDetectionsbyCountry,
  212.                 'yesterdayTotalConsumersByCountry' => $yesterdayConsumersbyCountry,
  213.                 'todayConsumersbyRegion' => $todayConsumersbyRegion,
  214.                 'yesterdayConsumersbyRegion' => $yesterdayConsumersbyRegion,
  215.                 'todayConsumersbyOs' => $todayConsumersbyOs,
  216.                 'yesterdayConsumersbyOs' => $yesterdayConsumersbyOs,
  217.                 'tomonthConsumersbyOs' => $tomonthConsumersbyOs,
  218.                 'yestermonthConsumersbyOs' => $yestermonthConsumersbyOs,
  219.                 'tomonthConsumersbyCountry' => $tomonthConsumersbyCountry,
  220.                 'yestermonthConsumersbyCountry' => $yestermonthConsumersbyCountry,
  221.                 'tomonthConsumersbyRegion' => $tomonthConsumersbyRegion,
  222.                 'yestermonthConsumersbyRegion' => $yestermonthConsumersbyRegion,
  223.                 'avgPerDayLast30' => $avgPerDayLast30,
  224.                 'avgByCountryPerDayLast30' => $avgByCountryPerDayLast30,
  225.                 'avgByRegionPerDayLast30' => $avgByRegionPerDayLast30,
  226.                 'to7daysDetections' => $to7daysDetections,
  227.                 'to7daysConsumers' => $to7daysConsumers,
  228.                 'yester7daysDetections' => $yester7daysDetections,
  229.                 'yester7daysConsumers' => $yester7daysConsumers,
  230.                 'chart7daysConsumers' => $chart7daysConsumers,
  231.                 'chart7daysDetections'=> $chart7daysDetections,
  232.                 'toDayWorldMapConsumers'=>$toDayWorldMapConsumers,
  233.                 'toDayWorldMapDetections' => $toDayWorldMapDetections,
  234.                 'toDayDoughnutDetectionsChart'=>$toDayDoughnutDetectionsChart,
  235.                 'toDayDoughnutConsumersChart'=>$toDayDoughnutConsumersChart,
  236.                 'todayDetectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  237.                 'toDayBarDetectionsChart'=>$toDayBarDetectionsChart,
  238.                 'todayConsumersbyDeviceType'=>$todayConsumersbyDeviceType,
  239.                 'toDayBarConsumersChart'=>$toDayBarConsumersChart,
  240.                 'todayDetectionsTopCountry'=>$todayDetectionsTopCountry,
  241.                 'todayConsumersTopCountry'=>$todayConsumersTopCountry,
  242.                 'todayDetectionsTopRegion'=>$todayDetectionsTopRegion,
  243.                 'todayConsumersTopRegion'=>$todayConsumersTopRegion,
  244.                 'todayDetectionsbyRegion'=>$todayDetectionsbyRegion,
  245.                 'countries'=>array_flip($countries)
  246.             ]);
  247.         }
  248.     }
  249.     #[Route('/media/show/{id}/{period}'methods:"GET"name'update_show_media')]
  250.     public function updateShowMedia(Media $media$period
  251.     {
  252.         //initialisation
  253.         $countries BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country''');
  254.          //today is
  255.          $today=new \DateTime();
  256.          //yesterday
  257.          $yesterday $today->modify("-1 days");
  258.          $dateRange = ['from'=>$yesterday'to' => new \DateTime()] ;
  259.          //yestermonth
  260.          $yestermonth = (new \DateTime())->sub( ( new \DateInterval('P1M')))->format('m');
  261.          //general
  262.          $todayDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P1D');
  263.          $todayConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P1D'true);
  264.         switch ($period) {
  265.             case "today":
  266.                 //Today
  267.                 $todayDetectionsbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D');
  268.                 $todayDetectionsTopCountry  array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D'nullnullfalsefalsefalse3), "nbrDetections""country");
  269.                 $todayConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruenull'P1D');
  270.                 $todayConsumersTopCountry  array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenull'P1D'nullnullfalsetruefalse3), "nbrConsumers""country");
  271.                 $todayConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruetrue'P1D');
  272.                 $todayDetectionsbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'); 
  273.                 $todayDetectionsTopRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'nullnullfalsefalsefalse10 ); 
  274.                 $todayConsumersTopRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetrue'P1D'nullnullfalsetruefalse10 );
  275.                 $todayConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalse'P1D'nullnulltrue);
  276.                 $todayDetectionsbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenull'P1D'nullnullfalsefalsetrue);
  277.                 $todayConsumersbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenull'P1D'nullnullfalsetruetrue);
  278.                 // ! charts
  279.                 $toDayDoughnutDetectionsChart $this->charts->generateDoughnutChart($todayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Detections"os");
  280.                 $toDayDoughnutConsumersChart $this->charts->generateDoughnutChart($todayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Consumers"os");
  281.                 $toDayBarDetectionsChart $this->charts->generateBarChart($todayDetectionsbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Detections"deviceType");
  282.                 $toDayBarConsumersChart $this->charts->generateBarChart($todayConsumersbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Consumers"deviceType");
  283.                 $toDayWorldMapDetections $this->charts->updateMapAreas($todayDetectionsbyCountryarray_flip($countries), DataType::Detections);
  284.                 $toDayWorldMapConsumers $this->charts->updateMapAreas($todayConsumersbyCountryarray_flip($countries), DataType::Consumers);
  285.                 $detectionsByCountryDT $this->charts->generateDTDetectionsByCountry($todayDetectionsbyCountryDataType::Detections$countries);
  286.                 $consumersByCountryDT $this->charts->generateDTDetectionsByCountry($todayConsumersbyCountryDataType::Consumers$countries);
  287.                 $detectionsbyRegionDT $this->charts->generateDTDetectionsByRegion($todayDetectionsbyRegionDataType::Detections$countries);
  288.                 $consumersbyRegionDT $this->charts->generateDTDetectionsByRegion($todayConsumersbyRegionDataType::Consumers$countries);
  289.                 return new JsonResponse([
  290.                     "detectionsbyCountry"=>$todayDetectionsbyCountry,
  291.                     'detectionsTopCountry'=>$todayDetectionsTopCountry,
  292.                     'consumersbyCountry'=>$todayConsumersbyCountry,
  293.                     'consumersTopCountry'=>$todayConsumersTopCountry,
  294.                     'worldMapDetections'=>$toDayWorldMapDetections,
  295.                     'worldMapConsumers'=>$toDayWorldMapConsumers,
  296.                     'consumersbyRegion'=>$todayConsumersbyRegion,
  297.                     'detectionsTopRegion'=>$todayDetectionsTopRegion,
  298.                     'consumersTopRegion'=>$todayConsumersTopRegion,
  299.                     'consumersbyOs'=>$todayConsumersbyOs,
  300.                     'doughnutDetectionsChart'=>$toDayDoughnutDetectionsChart,
  301.                     'doughnutConsumersChart'=>$toDayDoughnutConsumersChart,
  302.                     'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  303.                     'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
  304.                     'barDetectionsChart'=>$toDayBarDetectionsChart,
  305.                     'barConsumersChart'=>$toDayBarConsumersChart,
  306.                     'detectionsbyCountryTable'=>$detectionsByCountryDT,
  307.                     'consumersbyCountryTable'=>$consumersByCountryDT,
  308.                     'detectionsbyRegionTable'=>$detectionsbyRegionDT,
  309.                     'consumersbyRegionTable'=>$consumersbyRegionDT,
  310.                     'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT010true) ,
  311.                     'consumersTopRegionTable'=>array_slice($consumersbyRegionDT010true),
  312.                 ]);
  313.             case "yesterday":
  314.                 //yesterday
  315.                 $yesterdayDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P2D')[0]['nbrDetections'] - $todayDetections[0]['nbrDetections'];
  316.                 $yesterdayConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media'P2D'true)[0]['nbrConsumers'] - $todayConsumers[0]['nbrConsumers'];
  317.                 $yestardayDetectionsbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruenullnullnull$dateRange);
  318.                 $yesterdayConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruenullnullnull$dateRange);
  319.                 $yesterdayConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruetruenullnull$dateRange);
  320.                 $yesterdayDetectionsbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetruenullnull$dateRange);
  321.                 $yesterdayConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenullnull$dateRangetrue);
  322.                 $yesterdayDetectionsbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnullnull$dateRangefalsefalsetrue);
  323.                 $yesterdayConsumersbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnullnull$dateRangefalsetruetrue);
  324.                 $yesterdayWorldMapDetections $this->charts->updateMapAreas($yesterdayConsumersbyCountryarray_flip($countries), DataType::Detections);
  325.                 $yesterdayWorldMapConsumers $this->charts->updateMapAreas($yesterdayConsumersbyCountryarray_flip($countries), DataType::Consumers);
  326.                 $yesterdayDoughnutDetectionsChart $this->charts->generateDoughnutChart($yesterdayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Detections"os");
  327.                 $yesterdayDoughnutConsumersChart $this->charts->generateDoughnutChart($yesterdayConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Consumers"os");
  328.                 $yesterdayBarDetectionsChart $this->charts->generateBarChart($yesterdayDetectionsbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Detections"deviceType");
  329.                 $yesterdayBarConsumersChart $this->charts->generateBarChart($yesterdayConsumersbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Consumers"deviceType");
  330.                 $yesterdayDetectionsByCountryDT $this->charts->generateDTDetectionsByCountry($yestardayDetectionsbyCountryDataType::Detections$countries);
  331.                 $yesterdayConsumersByCountryDT $this->charts->generateDTDetectionsByCountry($yesterdayConsumersbyCountryDataType::Consumers$countries);
  332.                 $detectionsbyRegionDT $this->charts->generateDTDetectionsByRegion($yesterdayDetectionsbyRegionDataType::Detections$countries);
  333.                 $consumersbyRegionDT $this->charts->generateDTDetectionsByRegion($yesterdayConsumersbyRegionDataType::Consumers$countries);
  334.                 return new JsonResponse([
  335.                     "detectionsbyCountry"=>$yestardayDetectionsbyCountry,
  336.                     //'detectionsTopCountry'=>$todayDetectionsTopCountry,
  337.                     'consumersbyCountry'=>$yesterdayConsumersbyCountry,
  338.                     //'consumersTopCountry'=>$todayConsumersTopCountry,
  339.                     'worldMapDetections'=>$yesterdayWorldMapDetections,
  340.                     'worldMapConsumers'=>$yesterdayWorldMapConsumers,
  341.                     'consumersbyRegion'=>$yesterdayConsumersbyRegion,
  342.                     'detectionsbyRegion'=>$yesterdayDetectionsbyRegion,
  343.                     //'detectionsTopRegion'=>$todayDetectionsTopRegion,
  344.                     //'consumersTopRegion'=>$todayConsumersTopRegion,
  345.                     'consumersbyOs'=>$yesterdayConsumersbyOs,
  346.                     'doughnutDetectionsChart'=>$yesterdayDoughnutDetectionsChart,
  347.                     'doughnutConsumersChart'=>$yesterdayDoughnutConsumersChart,
  348.                     //'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  349.                     //'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
  350.                     'barDetectionsChart'=>$yesterdayBarDetectionsChart,
  351.                     'barConsumersChart'=>$yesterdayBarConsumersChart,
  352.                     'detectionsbyCountryTable'=>$yesterdayDetectionsByCountryDT,
  353.                     'consumersbyCountryTable'=>$yesterdayConsumersByCountryDT,
  354.                     'detectionsbyRegionTable'=>$detectionsbyRegionDT,
  355.                     'consumersbyRegionTable'=>$consumersbyRegionDT
  356.                     'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT010true) ,
  357.                     'consumersTopRegionTable'=>array_slice($consumersbyRegionDT010true),
  358.                 ]);
  359.                 case "tomonth":
  360.                     //toMonth
  361.                     $tomonthDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, (new \DateTime())->format('m'));
  362.                     $tomonthConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenull, (new \DateTime())->format('m'), nulltrue);
  363.                     $tomonthDetectionsbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruefalsenull, (new \DateTime())->format('m'), nullfalse);
  364.                     $tomonthConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruefalsenull, (new \DateTime())->format('m'), nullfalse);
  365.                     $tomonthConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruetruenull, (new \DateTime())->format('m'), nullfalse);
  366.                     $tomonthDetectionsbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetruenull, (new \DateTime())->format('m'), nullfalse);
  367.                     $tomonthDetectionsbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnull, (new \DateTime())->format('m'), nullfalsefalsetrue);
  368.                     $tomonthConsumersbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnull, (new \DateTime())->format('m'), nullfalsetruetrue);
  369.                     $tomonthConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, (new \DateTime())->format('m'));
  370.                     $tomonthWorldMapDetections $this->charts->updateMapAreas($tomonthConsumersbyCountryarray_flip($countries), DataType::Detections);
  371.                     $tomonthWorldMapConsumers $this->charts->updateMapAreas($tomonthConsumersbyCountryarray_flip($countries), DataType::Consumers);
  372.                     $tomonthDoughnutDetectionsChart $this->charts->generateDoughnutChart($tomonthConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Detections"os");
  373.                     $tomonthDoughnutConsumersChart $this->charts->generateDoughnutChart($tomonthConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Consumers"os");
  374.                     $tomonthBarDetectionsChart $this->charts->generateBarChart($tomonthDetectionsbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Detections"deviceType");
  375.                     $tomonthBarConsumersChart $this->charts->generateBarChart($tomonthConsumersbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Consumers"deviceType");
  376.                     $tomonthDetectionsByCountryDT $this->charts->generateDTDetectionsByCountry($tomonthDetectionsbyCountryDataType::Detections$countries);
  377.                     $tomonthConsumersByCountryDT $this->charts->generateDTDetectionsByCountry($tomonthConsumersbyCountryDataType::Consumers$countries);
  378.                     $detectionsbyRegionDT $this->charts->generateDTDetectionsByRegion($tomonthDetectionsbyRegionDataType::Detections$countries);
  379.                     $consumersbyRegionDT $this->charts->generateDTDetectionsByRegion($tomonthConsumersbyRegionDataType::Consumers$countries);
  380.                     return new JsonResponse([
  381.                         "detectionsbyCountry"=>$tomonthDetectionsbyCountry,
  382.                         //'detectionsTopCountry'=>$todayDetectionsTopCountry,
  383.                         'consumersbyCountry'=>$tomonthConsumersbyCountry,
  384.                         //'consumersTopCountry'=>$todayConsumersTopCountry,
  385.                         'worldMapDetections'=>$tomonthWorldMapDetections,
  386.                         'worldMapConsumers'=>$tomonthWorldMapConsumers,
  387.                         'consumersbyRegion'=>$tomonthConsumersbyRegion,
  388.                         //'detectionsTopRegion'=>$todayDetectionsTopRegion,
  389.                         //'consumersTopRegion'=>$todayConsumersTopRegion,
  390.                         //'consumersbyOs'=>$yesterdayConsumersbyOs,
  391.                         'doughnutDetectionsChart'=>$tomonthDoughnutDetectionsChart,
  392.                         'doughnutConsumersChart'=>$tomonthDoughnutConsumersChart,
  393.                         //'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  394.                         //'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
  395.                         'barDetectionsChart'=>$tomonthBarDetectionsChart,
  396.                         'barConsumersChart'=>$tomonthBarConsumersChart,
  397.                         'detectionsbyCountryTable'=>$tomonthDetectionsByCountryDT,
  398.                         'consumersbyCountryTable'=>$tomonthConsumersByCountryDT,
  399.                         'detectionsbyRegionTable'=>$detectionsbyRegionDT,
  400.                         'consumersbyRegionTable'=>$consumersbyRegionDT,
  401.                         'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT010true) ,
  402.                         'consumersTopRegionTable'=>array_slice($consumersbyRegionDT010true),
  403.     
  404.                         
  405.                     ]);
  406.                     case "yestermonth":
  407.                         //yesterMonth
  408.                         $yestermonthConsumersbyOs  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediafalsefalsenull$yestermonthnulltrue);
  409.                         $yestermonthConsumersbyCountry  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruefalsenull$yestermonthnullfalse);
  410.                         $yestermonthConsumersbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null$mediatruetruenull$yestermonthnullfalse);
  411.                         $yestermonthDetectionsbyRegion  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediatruetruenull$yestermonthnullfalse);
  412.                         $yestermonthDetectionsbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnull$yestermonthnullfalsefalsetrue);
  413.                         $yestermonthConsumersbyDeviceType  $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null$mediafalsenullnull$yestermonthnullfalsetruetrue);
  414.                         $yestermonthConsumers $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media$yestermonth);
  415.                         $yestermonthDetections $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media$yestermonth);
  416.                         $yestermonthWorldMapDetections $this->charts->updateMapAreas($yestermonthConsumersbyCountryarray_flip($countries), DataType::Detections);
  417.                         $yestermonthWorldMapConsumers $this->charts->updateMapAreas($yestermonthConsumersbyCountryarray_flip($countries), DataType::Consumers);
  418.                         $yestermonthDoughnutDetectionsChart $this->charts->generateDoughnutChart($yestermonthConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Detections"os");
  419.                         $yestermonthDoughnutConsumersChart $this->charts->generateDoughnutChart($yestermonthConsumersbyOs"nbrConsumers""nbrOsDetections"DataType::Consumers"os");
  420.                         $yestermonthBarDetectionsChart $this->charts->generateBarChart($yestermonthDetectionsbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Detections"deviceType");
  421.                         $yestermonthBarConsumersChart $this->charts->generateBarChart($yestermonthConsumersbyDeviceType"nbrConsumers""nbrDeviceDetections"DataType::Consumers"deviceType");
  422.                         $yestermonthDetectionsByCountryDT $this->charts->generateDTDetectionsByCountry($yestermonthConsumersbyCountryDataType::Detections$countries);
  423.                         $yestermonthConsumersByCountryDT $this->charts->generateDTDetectionsByCountry($yestermonthConsumersbyCountryDataType::Consumers$countries);
  424.                         $detectionsbyRegionDT $this->charts->generateDTDetectionsByRegion($yestermonthDetectionsbyRegionDataType::Detections$countries);
  425.                         $consumersbyRegionDT $this->charts->generateDTDetectionsByRegion($yestermonthConsumersbyRegionDataType::Consumers$countries);
  426.                         return new JsonResponse([
  427.                             "detectionsbyCountry"=>$yestermonthConsumersbyCountry,
  428.                             //'detectionsTopCountry'=>$todayDetectionsTopCountry,
  429.                             'consumersbyCountry'=>$yestermonthConsumersbyCountry,
  430.                             //'consumersTopCountry'=>$todayConsumersTopCountry,
  431.                             'worldMapDetections'=>$yestermonthWorldMapDetections,
  432.                             'worldMapConsumers'=>$yestermonthWorldMapConsumers,
  433.                             'consumersbyRegion'=>$yestermonthConsumersbyRegion,
  434.                             //'detectionsTopRegion'=>$todayDetectionsTopRegion,
  435.                             //'consumersTopRegion'=>$todayConsumersTopRegion,
  436.                             //'consumersbyOs'=>$yesterdayConsumersbyOs,
  437.                             'doughnutDetectionsChart'=>$yestermonthDoughnutDetectionsChart,
  438.                             'doughnutConsumersChart'=>$yestermonthDoughnutConsumersChart,
  439.                             //'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  440.                             //'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
  441.                             'barDetectionsChart'=>$yestermonthBarDetectionsChart,
  442.                             'barConsumersChart'=>$yestermonthBarConsumersChart,
  443.                             'detectionsbyCountryTable'=>$yestermonthDetectionsByCountryDT,
  444.                             'consumersbyCountryTable'=>$yestermonthConsumersByCountryDT,
  445.                             'detectionsbyRegionTable'=>$detectionsbyRegionDT,
  446.                             'consumersbyRegionTable'=>$consumersbyRegionDT,
  447.                             'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT010true) ,
  448.                             'consumersTopRegionTable'=>array_slice($consumersbyRegionDT010true),
  449.                         
  450.                         ]);
  451.                         case "avg30days":
  452.                             //avg30days
  453.                             $avgPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'falsefalsetrue);
  454.                             $avgByCountryPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'truefalsetrue);
  455.                             $avgByRegionPerDayLast30 $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null$media'P30D'falsetruetrue);
  456.                             $avgConsumersLast30 $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media$yestermonth);
  457.                             $avgDetectionsLast30 $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media$yestermonth);
  458.                             $avgWorldMapDetectionsLast30 $this->charts->updateMapAreas($avgByCountryPerDayLast30array_flip($countries), DataType::Detections);
  459.                             $avgWorldMapConsumersLast30 $this->charts->updateMapAreas($avgByCountryPerDayLast30array_flip($countries), DataType::Consumers);
  460.                             //$avgDoughnutDetectionsChartLast30 = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
  461.                           //  $avgDoughnutConsumersChartLast30 = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
  462.                            // $avgBarDetectionsChartLast30 = $this->charts->generateBarChart($yesterdayDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
  463.                           //  $avgBarConsumersChartLast30 = $this->charts->generateBarChart($yesterdayConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
  464.                             $avgDetectionsByCountryDTLast30 $this->charts->generateDTDetectionsByCountry($avgByCountryPerDayLast30DataType::Detections$countries);
  465.                             $avgConsumersByCountryDTLast30 $this->charts->generateDTDetectionsByCountry($avgByCountryPerDayLast30DataType::Consumers$countries);
  466.                             return new JsonResponse([
  467.                                // "detectionsbyCountry"=>$todayDetectionsbyCountry,
  468.                                 //'detectionsTopCountry'=>$todayDetectionsTopCountry,
  469.                                 'consumersbyCountry'=>$avgByCountryPerDayLast30,
  470.                                 //'consumersTopCountry'=>$todayConsumersTopCountry,
  471.                                 'worldMapDetections'=>$avgWorldMapDetectionsLast30,
  472.                                 'worldMapConsumers'=>$avgWorldMapConsumersLast30,
  473.                             //    'consumersbyRegion'=>$yestermonthConsumersbyRegion,
  474.                                 //'detectionsTopRegion'=>$todayDetectionsTopRegion,
  475.                                 //'consumersTopRegion'=>$todayConsumersTopRegion,
  476.                                 //'consumersbyOs'=>$yesterdayConsumersbyOs,
  477.                               //  'doughnutDetectionsChart'=>$yestermonthDoughnutDetectionsChart,
  478.                                // 'doughnutConsumersChart'=>$yestermonthDoughnutConsumersChart,
  479.                                 //'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
  480.                                 //'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
  481.                                // 'barDetectionsChart'=>$yesterdayBarDetectionsChart,
  482.                                 //'barConsumersChart'=>$yesterdayBarConsumersChart,
  483.                                 'detectionsbyCountryTable'=>$avgByCountryPerDayLast30,
  484.                                 'consumersbyCountryTable'=>$avgByCountryPerDayLast30
  485.                             ]);
  486.         }
  487.     }
  488.     #[Route('/media/edit/{id}'methods:"GET|POST"name'edit_media')]
  489.     public function editMedia(Request $requestMedia $media): Response
  490.     {  
  491.        if (!($media->getCreatedBy() == $this->security->getUser())) {
  492.         
  493.         $this->addFlash("error""<h4>Forbidden</h4>Media can only be edited by its owner.");
  494.         return $this->redirectToRoute('all_media');
  495.     }else {
  496.         $form $this->createForm(MediaType::class, $media,[
  497.             'entityManager' => $this->doctrine->getManager(),
  498.         ]);
  499.         $form->handleRequest($request);
  500.         if ($form->isSubmitted() && $form->isValid()) {
  501.             $em $this->doctrine->getManager();
  502.              /**
  503.              * Logo file upload
  504.              */
  505.             $base64 $request->request->get('logoFinal');
  506.             if ($base64) {
  507.                 list($type$data) = explode(';'$base64);
  508.                 list(, $data)      = explode(','$base64);
  509.                 $data base64_decode($data);
  510.                 $newFilename uniqid() . '-logo.' explode('/'$type)[1];
  511.                 // Move the file to the directory where brochures are stored
  512.                 try {
  513.                     $path =$this->getParameter('upload_media_directory') . '/' $media->getId() . '/logo';
  514.                     if (!is_dir($path)) {
  515.                         // dir doesn't exist, make it
  516.                         mkdir($path0777true);
  517.                     }
  518.                     file_put_contents(
  519.                         $path '/'.$newFilename,
  520.                         $data
  521.                     );
  522.                     $media->setLogo($this->getParameter('upload_media_directory_display') . $media->getId() . '/logo/' $newFilename);
  523.                    // $em->flush();
  524.                     
  525.                 } catch (FileException $e) {
  526.                     echo 'error';
  527.                     $this->addFlash('error''Something went wrong when uploading the file !');
  528.                     // ... handle exception if something happens during file upload
  529.                 }
  530.                
  531.                 // updates the 'brochureFilename' property to store the PDF file name
  532.                 // instead of its contents
  533.                 //$media->setBrochureFilename($newFilename);
  534.             }
  535.             $em->flush();
  536.             $this->addFlash('success''Media '.$media->getName().' updated successfully');
  537.             return $this->redirectToRoute('all_media');
  538.         }
  539.     }
  540.         return $this->render('media/edit.html.twig', [
  541.             'media' => $media,
  542.             'form' => $form->createView(),
  543.         ]);
  544.     }
  545. }