<?php
namespace App\Controller;
use App\Entity\Media;
use App\Entity\Detections;
use App\Form\NewMediaType;
use App\Form\MediaType;
use App\Enum\BasicEnum;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use App\Service\ChartsService;
use App\Service\DataType;
class MediaController extends AbstractController
{
private $security;
private $doctrine;
private $charts;
public function __construct(
ManagerRegistry $doctrine,
Security $security,
ChartsService $charts
)
{
$this->doctrine = $doctrine;
$this->security = $security;
$this->charts = $charts;
}
#[Route('/media/list', name: 'all_media')]
public function index(ManagerRegistry $doctrine): Response
{
$session = new Session();
//$session->start();
$user = $this->security->getUser();
$medias = $doctrine->getRepository(Media::class)->findBy(['createdBy'=>$user]);
if (!$medias) {
$session->getFlashBag()->add(
'warning',
'<h4 class="alert-heading">No Media yet !</h4>To proceed, first create a new media profile.'
);
return $this->redirectToRoute('new_media');
}else {
return $this->render('media/index.html.twig', [
'medias' => $medias,
]);
}
}
#[Route('/media/new', methods:"GET|POST", name: 'new_media')]
public function createMedia(Request $request): Response
{
$media = new Media();
$form = $this->createForm(NewMediaType::class, $media,[
'entityManager' => $this->doctrine->getManager(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$media->setCreatedBy($this->security->getUser());
// $media->setCreatedAt((new DateTime('now'))->format('Y-m-d H:i:s'));
$em = $this->doctrine->getManager();
$em->persist($media);
$em->flush();
/**
* Logo file upload
*/
$base64 = $request->request->get('logoFinal');
if ($base64) {
list($type, $data) = explode(';', $base64);
list(, $data) = explode(',', $base64);
$data = base64_decode($data);
$newFilename = uniqid() . '-logo.' . explode('/', $type)[1];
// Move the file to the directory where brochures are stored
try {
$path =$this->getParameter('upload_media_directory') . '/' . $media->getId() . '/logo';
if (!is_dir($path)) {
// dir doesn't exist, make it
mkdir($path, 0777, true);
}
file_put_contents(
$path . '/'.$newFilename,
$data
);
$media->setLogo($this->getParameter('upload_media_directory_display') . $media->getId() . '/logo/' . $newFilename);
$em->flush();
} catch (FileException $e) {
echo 'error';
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
//$media->setBrochureFilename($newFilename);
}
return $this->redirectToRoute('all_media');
}
// // tell Doctrine you want to (eventually) save the Product (no queries yet)
// $entityManager->persist($media);
// // actually executes the queries (i.e. the INSERT query)
// $entityManager->flush();
return $this->render('media/new.html.twig', [
'media' => $media,
'form' => $form->createView(),
]);
}
#[Route('/media/show/{id}', methods:"GET", name: 'show_media')]
public function showMedia(Media $media): Response
{
if (!($media->getCreatedBy() == $this->security->getUser())) {
$this->addFlash("error", "<h4>Forbidden</h4>Media can only be edited by its owner.");
return $this->redirectToRoute('all_media');
}else {
//countries
//$countries = BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country', 'Enum.Country.');
$countries = BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country', '');
//today is
$today=new \DateTime();
//yesterday
$yesterday = $today->modify("-1 days");
$dateRange = ['from'=>$yesterday, 'to' => new \DateTime()] ;
//yestermonth
$yestermonth = (new \DateTime())->sub( ( new \DateInterval('P1M')))->format('m');
//Last 7 days
$dateFrom = $today->modify("-6 days");
$to7daysDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P7D', false, true);
$to7daysConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P7D', true, true);
//$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");
$yester7daysDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P14D', false, true);
$yester7daysConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P14D', true, true);
// ! chart Head Cards
$chart7daysConsumers = $this->charts->generateLineChart(
$period = ['from' => (new \DateTime())->modify("-7 days"), 'to' => new \DateTime()],[
['label' => 'Last 7 days Consumers','data'=>$this->charts->prepareForCharts($to7daysConsumers, "nbrConsumers", $period)],
['label' => 'The 7 days before Consumers',"borderDash" => [5, 5], "color" => "#8ca1ca", 'data'=>$this->charts->prepareForCharts($yester7daysConsumers, "nbrConsumers", ['from' => (new \DateTime())->modify("-14 days"), 'to' => (new \DateTime())->modify("-7 days")])]
],"Consumers", null
);
$chart7daysDetections = $this->charts->generateLineChart(
$period = ['from' => (new \DateTime())->modify("-7 days"), 'to' => new \DateTime()],[
['label' => 'Last 7 days Consumers','data'=>$this->charts->prepareForCharts($to7daysDetections, "nbrDetections", $period)],
['label' => 'The 7 days before Consumers',"borderDash" => [5, 5], "color" => "#8ca1ca", 'data'=>$this->charts->prepareForCharts($yester7daysDetections, "nbrDetections", ['from' => (new \DateTime())->modify("-14 days"), 'to' => (new \DateTime())->modify("-7 days")])]
],"Detections", null
);
//Today
$todayDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P1D');
$todayConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P1D', true);
//! ****
$todayDetectionsbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D');
$todayDetectionsTopCountry = array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D', null, null, false, false, false, 3), "nbrDetections", "country");
$todayConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, null, 'P1D');
$todayConsumersTopCountry = array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D', null, null, false, true, false, 3), "nbrConsumers", "country");
// !chart Today Maps
$toDayWorldMapDetections = $this->charts->getMapAreas(array_column($todayDetectionsbyCountry, "nbrCountryDetections", "country"), "Detections", $countries);
$toDayWorldMapConsumers = $this->charts->getMapAreas(array_column($todayConsumersbyCountry, "nbrConsumers", "country"), "Consumers", $countries);
//! ****
$todayConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, true, 'P1D');
$todayDetectionsbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D');
$todayDetectionsTopRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D', null, null, false, false, false, 10 );
$todayConsumersTopRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D', null, null, false, true, false, 10 );
$todayConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, 'P1D', null, null, true);
//!chart Today OS
$toDayDoughnutDetectionsChart = $this->charts->generateDoughnutChart($todayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
$toDayDoughnutConsumersChart = $this->charts->generateDoughnutChart($todayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
// ! ****
$todayDetectionsbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, 'P1D', null, null, false, false, true);
$todayConsumersbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, 'P1D', null, null, false, true, true);
//! chart Today device type
$toDayBarDetectionsChart = $this->charts->generateBarChart($todayDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
$toDayBarConsumersChart = $this->charts->generateBarChart($todayConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
//Yesterday
$yesterdayDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P2D')[0]['nbrDetections'] - $todayDetections[0]['nbrDetections'];
$yesterdayConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P2D', true)[0]['nbrConsumers'] - $todayConsumers[0]['nbrConsumers'];
$yesterdayConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, null, null, null, $dateRange);
$yesterdayConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, true, null, null, $dateRange);
$yesterdayConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, null, $dateRange, true);
//this Month
$tomonthDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, (new \DateTime())->format('m'));
$tomonthConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, (new \DateTime())->format('m'), null, true);
$tomonthConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, false, null, (new \DateTime())->format('m'), null, false);
$tomonthConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, true, null, (new \DateTime())->format('m'), null, false);
$tomonthConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, (new \DateTime())->format('m'));
//YesterMonth
$yestermonthConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, $yestermonth, null, true);
$yestermonthConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, false, null, $yestermonth, null, false);
$yestermonthConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, true, null, $yestermonth, null, false);
$yestermonthConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, $yestermonth);
$yestermonthDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, $yestermonth);
//AVG
$avgPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', false, false, true);
$avgByCountryPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', true, false, true);
$avgByRegionPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', false, true, true);
return $this->render('media/show.html.twig', [
'media' => $media,
'todayTotalDetections' => $todayDetections,
'yesterdayTotalDetections' => $yesterdayDetections,
'tomonthTotalDetections' => $tomonthDetections,
'yestermonthTotalDetections' => $yestermonthDetections,
'tomonthTotalConsumers' => $tomonthConsumers,
'yestermonthTotalConsumers' => $yestermonthConsumers,
'todayTotalConsumers' => $todayConsumers,
'yesterdayTotalConsumers' => $yesterdayConsumers,
'todayConsumersbyCountry' => $todayConsumersbyCountry,
'todayDetectionsbyCountry' => $todayDetectionsbyCountry,
'yesterdayTotalConsumersByCountry' => $yesterdayConsumersbyCountry,
'todayConsumersbyRegion' => $todayConsumersbyRegion,
'yesterdayConsumersbyRegion' => $yesterdayConsumersbyRegion,
'todayConsumersbyOs' => $todayConsumersbyOs,
'yesterdayConsumersbyOs' => $yesterdayConsumersbyOs,
'tomonthConsumersbyOs' => $tomonthConsumersbyOs,
'yestermonthConsumersbyOs' => $yestermonthConsumersbyOs,
'tomonthConsumersbyCountry' => $tomonthConsumersbyCountry,
'yestermonthConsumersbyCountry' => $yestermonthConsumersbyCountry,
'tomonthConsumersbyRegion' => $tomonthConsumersbyRegion,
'yestermonthConsumersbyRegion' => $yestermonthConsumersbyRegion,
'avgPerDayLast30' => $avgPerDayLast30,
'avgByCountryPerDayLast30' => $avgByCountryPerDayLast30,
'avgByRegionPerDayLast30' => $avgByRegionPerDayLast30,
'to7daysDetections' => $to7daysDetections,
'to7daysConsumers' => $to7daysConsumers,
'yester7daysDetections' => $yester7daysDetections,
'yester7daysConsumers' => $yester7daysConsumers,
'chart7daysConsumers' => $chart7daysConsumers,
'chart7daysDetections'=> $chart7daysDetections,
'toDayWorldMapConsumers'=>$toDayWorldMapConsumers,
'toDayWorldMapDetections' => $toDayWorldMapDetections,
'toDayDoughnutDetectionsChart'=>$toDayDoughnutDetectionsChart,
'toDayDoughnutConsumersChart'=>$toDayDoughnutConsumersChart,
'todayDetectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
'toDayBarDetectionsChart'=>$toDayBarDetectionsChart,
'todayConsumersbyDeviceType'=>$todayConsumersbyDeviceType,
'toDayBarConsumersChart'=>$toDayBarConsumersChart,
'todayDetectionsTopCountry'=>$todayDetectionsTopCountry,
'todayConsumersTopCountry'=>$todayConsumersTopCountry,
'todayDetectionsTopRegion'=>$todayDetectionsTopRegion,
'todayConsumersTopRegion'=>$todayConsumersTopRegion,
'todayDetectionsbyRegion'=>$todayDetectionsbyRegion,
'countries'=>array_flip($countries)
]);
}
}
#[Route('/media/show/{id}/{period}', methods:"GET", name: 'update_show_media')]
public function updateShowMedia(Media $media, $period)
{
//initialisation
$countries = BasicEnum::getEnumAsArray('App\Enum\Additionnal\Country', '');
//today is
$today=new \DateTime();
//yesterday
$yesterday = $today->modify("-1 days");
$dateRange = ['from'=>$yesterday, 'to' => new \DateTime()] ;
//yestermonth
$yestermonth = (new \DateTime())->sub( ( new \DateInterval('P1M')))->format('m');
//general
$todayDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P1D');
$todayConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P1D', true);
switch ($period) {
case "today":
//Today
$todayDetectionsbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D');
$todayDetectionsTopCountry = array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D', null, null, false, false, false, 3), "nbrDetections", "country");
$todayConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, null, 'P1D');
$todayConsumersTopCountry = array_column($this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, 'P1D', null, null, false, true, false, 3), "nbrConsumers", "country");
$todayConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, true, 'P1D');
$todayDetectionsbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D');
$todayDetectionsTopRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D', null, null, false, false, false, 10 );
$todayConsumersTopRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, 'P1D', null, null, false, true, false, 10 );
$todayConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, 'P1D', null, null, true);
$todayDetectionsbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, 'P1D', null, null, false, false, true);
$todayConsumersbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, 'P1D', null, null, false, true, true);
// ! charts
$toDayDoughnutDetectionsChart = $this->charts->generateDoughnutChart($todayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
$toDayDoughnutConsumersChart = $this->charts->generateDoughnutChart($todayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
$toDayBarDetectionsChart = $this->charts->generateBarChart($todayDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
$toDayBarConsumersChart = $this->charts->generateBarChart($todayConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
$toDayWorldMapDetections = $this->charts->updateMapAreas($todayDetectionsbyCountry, array_flip($countries), DataType::Detections);
$toDayWorldMapConsumers = $this->charts->updateMapAreas($todayConsumersbyCountry, array_flip($countries), DataType::Consumers);
$detectionsByCountryDT = $this->charts->generateDTDetectionsByCountry($todayDetectionsbyCountry, DataType::Detections, $countries);
$consumersByCountryDT = $this->charts->generateDTDetectionsByCountry($todayConsumersbyCountry, DataType::Consumers, $countries);
$detectionsbyRegionDT = $this->charts->generateDTDetectionsByRegion($todayDetectionsbyRegion, DataType::Detections, $countries);
$consumersbyRegionDT = $this->charts->generateDTDetectionsByRegion($todayConsumersbyRegion, DataType::Consumers, $countries);
return new JsonResponse([
"detectionsbyCountry"=>$todayDetectionsbyCountry,
'detectionsTopCountry'=>$todayDetectionsTopCountry,
'consumersbyCountry'=>$todayConsumersbyCountry,
'consumersTopCountry'=>$todayConsumersTopCountry,
'worldMapDetections'=>$toDayWorldMapDetections,
'worldMapConsumers'=>$toDayWorldMapConsumers,
'consumersbyRegion'=>$todayConsumersbyRegion,
'detectionsTopRegion'=>$todayDetectionsTopRegion,
'consumersTopRegion'=>$todayConsumersTopRegion,
'consumersbyOs'=>$todayConsumersbyOs,
'doughnutDetectionsChart'=>$toDayDoughnutDetectionsChart,
'doughnutConsumersChart'=>$toDayDoughnutConsumersChart,
'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
'barDetectionsChart'=>$toDayBarDetectionsChart,
'barConsumersChart'=>$toDayBarConsumersChart,
'detectionsbyCountryTable'=>$detectionsByCountryDT,
'consumersbyCountryTable'=>$consumersByCountryDT,
'detectionsbyRegionTable'=>$detectionsbyRegionDT,
'consumersbyRegionTable'=>$consumersbyRegionDT,
'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT, 0, 10, true) ,
'consumersTopRegionTable'=>array_slice($consumersbyRegionDT, 0, 10, true),
]);
case "yesterday":
//yesterday
$yesterdayDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P2D')[0]['nbrDetections'] - $todayDetections[0]['nbrDetections'];
$yesterdayConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsPeriod($media, 'P2D', true)[0]['nbrConsumers'] - $todayConsumers[0]['nbrConsumers'];
$yestardayDetectionsbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, null, null, null, $dateRange);
$yesterdayConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, null, null, null, $dateRange);
$yesterdayConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, true, null, null, $dateRange);
$yesterdayDetectionsbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, null, null, $dateRange);
$yesterdayConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, null, $dateRange, true);
$yesterdayDetectionsbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, null, $dateRange, false, false, true);
$yesterdayConsumersbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, null, $dateRange, false, true, true);
$yesterdayWorldMapDetections = $this->charts->updateMapAreas($yesterdayConsumersbyCountry, array_flip($countries), DataType::Detections);
$yesterdayWorldMapConsumers = $this->charts->updateMapAreas($yesterdayConsumersbyCountry, array_flip($countries), DataType::Consumers);
$yesterdayDoughnutDetectionsChart = $this->charts->generateDoughnutChart($yesterdayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
$yesterdayDoughnutConsumersChart = $this->charts->generateDoughnutChart($yesterdayConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
$yesterdayBarDetectionsChart = $this->charts->generateBarChart($yesterdayDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
$yesterdayBarConsumersChart = $this->charts->generateBarChart($yesterdayConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
$yesterdayDetectionsByCountryDT = $this->charts->generateDTDetectionsByCountry($yestardayDetectionsbyCountry, DataType::Detections, $countries);
$yesterdayConsumersByCountryDT = $this->charts->generateDTDetectionsByCountry($yesterdayConsumersbyCountry, DataType::Consumers, $countries);
$detectionsbyRegionDT = $this->charts->generateDTDetectionsByRegion($yesterdayDetectionsbyRegion, DataType::Detections, $countries);
$consumersbyRegionDT = $this->charts->generateDTDetectionsByRegion($yesterdayConsumersbyRegion, DataType::Consumers, $countries);
return new JsonResponse([
"detectionsbyCountry"=>$yestardayDetectionsbyCountry,
//'detectionsTopCountry'=>$todayDetectionsTopCountry,
'consumersbyCountry'=>$yesterdayConsumersbyCountry,
//'consumersTopCountry'=>$todayConsumersTopCountry,
'worldMapDetections'=>$yesterdayWorldMapDetections,
'worldMapConsumers'=>$yesterdayWorldMapConsumers,
'consumersbyRegion'=>$yesterdayConsumersbyRegion,
'detectionsbyRegion'=>$yesterdayDetectionsbyRegion,
//'detectionsTopRegion'=>$todayDetectionsTopRegion,
//'consumersTopRegion'=>$todayConsumersTopRegion,
'consumersbyOs'=>$yesterdayConsumersbyOs,
'doughnutDetectionsChart'=>$yesterdayDoughnutDetectionsChart,
'doughnutConsumersChart'=>$yesterdayDoughnutConsumersChart,
//'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
//'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
'barDetectionsChart'=>$yesterdayBarDetectionsChart,
'barConsumersChart'=>$yesterdayBarConsumersChart,
'detectionsbyCountryTable'=>$yesterdayDetectionsByCountryDT,
'consumersbyCountryTable'=>$yesterdayConsumersByCountryDT,
'detectionsbyRegionTable'=>$detectionsbyRegionDT,
'consumersbyRegionTable'=>$consumersbyRegionDT,
'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT, 0, 10, true) ,
'consumersTopRegionTable'=>array_slice($consumersbyRegionDT, 0, 10, true),
]);
case "tomonth":
//toMonth
$tomonthDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, (new \DateTime())->format('m'));
$tomonthConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, (new \DateTime())->format('m'), null, true);
$tomonthDetectionsbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, false, null, (new \DateTime())->format('m'), null, false);
$tomonthConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, false, null, (new \DateTime())->format('m'), null, false);
$tomonthConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, true, null, (new \DateTime())->format('m'), null, false);
$tomonthDetectionsbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, null, (new \DateTime())->format('m'), null, false);
$tomonthDetectionsbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, (new \DateTime())->format('m'), null, false, false, true);
$tomonthConsumersbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, (new \DateTime())->format('m'), null, false, true, true);
$tomonthConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, (new \DateTime())->format('m'));
$tomonthWorldMapDetections = $this->charts->updateMapAreas($tomonthConsumersbyCountry, array_flip($countries), DataType::Detections);
$tomonthWorldMapConsumers = $this->charts->updateMapAreas($tomonthConsumersbyCountry, array_flip($countries), DataType::Consumers);
$tomonthDoughnutDetectionsChart = $this->charts->generateDoughnutChart($tomonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
$tomonthDoughnutConsumersChart = $this->charts->generateDoughnutChart($tomonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
$tomonthBarDetectionsChart = $this->charts->generateBarChart($tomonthDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
$tomonthBarConsumersChart = $this->charts->generateBarChart($tomonthConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
$tomonthDetectionsByCountryDT = $this->charts->generateDTDetectionsByCountry($tomonthDetectionsbyCountry, DataType::Detections, $countries);
$tomonthConsumersByCountryDT = $this->charts->generateDTDetectionsByCountry($tomonthConsumersbyCountry, DataType::Consumers, $countries);
$detectionsbyRegionDT = $this->charts->generateDTDetectionsByRegion($tomonthDetectionsbyRegion, DataType::Detections, $countries);
$consumersbyRegionDT = $this->charts->generateDTDetectionsByRegion($tomonthConsumersbyRegion, DataType::Consumers, $countries);
return new JsonResponse([
"detectionsbyCountry"=>$tomonthDetectionsbyCountry,
//'detectionsTopCountry'=>$todayDetectionsTopCountry,
'consumersbyCountry'=>$tomonthConsumersbyCountry,
//'consumersTopCountry'=>$todayConsumersTopCountry,
'worldMapDetections'=>$tomonthWorldMapDetections,
'worldMapConsumers'=>$tomonthWorldMapConsumers,
'consumersbyRegion'=>$tomonthConsumersbyRegion,
//'detectionsTopRegion'=>$todayDetectionsTopRegion,
//'consumersTopRegion'=>$todayConsumersTopRegion,
//'consumersbyOs'=>$yesterdayConsumersbyOs,
'doughnutDetectionsChart'=>$tomonthDoughnutDetectionsChart,
'doughnutConsumersChart'=>$tomonthDoughnutConsumersChart,
//'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
//'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
'barDetectionsChart'=>$tomonthBarDetectionsChart,
'barConsumersChart'=>$tomonthBarConsumersChart,
'detectionsbyCountryTable'=>$tomonthDetectionsByCountryDT,
'consumersbyCountryTable'=>$tomonthConsumersByCountryDT,
'detectionsbyRegionTable'=>$detectionsbyRegionDT,
'consumersbyRegionTable'=>$consumersbyRegionDT,
'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT, 0, 10, true) ,
'consumersTopRegionTable'=>array_slice($consumersbyRegionDT, 0, 10, true),
]);
case "yestermonth":
//yesterMonth
$yestermonthConsumersbyOs = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, false, false, null, $yestermonth, null, true);
$yestermonthConsumersbyCountry = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, false, null, $yestermonth, null, false);
$yestermonthConsumersbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findUniqueDevices(null, $media, true, true, null, $yestermonth, null, false);
$yestermonthDetectionsbyRegion = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, true, true, null, $yestermonth, null, false);
$yestermonthDetectionsbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, $yestermonth, null, false, false, true);
$yestermonthConsumersbyDeviceType = $this->doctrine->getManager()->getRepository(Detections::class)->findDetections(null, $media, false, null, null, $yestermonth, null, false, true, true);
$yestermonthConsumers = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, $yestermonth);
$yestermonthDetections = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, $yestermonth);
$yestermonthWorldMapDetections = $this->charts->updateMapAreas($yestermonthConsumersbyCountry, array_flip($countries), DataType::Detections);
$yestermonthWorldMapConsumers = $this->charts->updateMapAreas($yestermonthConsumersbyCountry, array_flip($countries), DataType::Consumers);
$yestermonthDoughnutDetectionsChart = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
$yestermonthDoughnutConsumersChart = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
$yestermonthBarDetectionsChart = $this->charts->generateBarChart($yestermonthDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
$yestermonthBarConsumersChart = $this->charts->generateBarChart($yestermonthConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
$yestermonthDetectionsByCountryDT = $this->charts->generateDTDetectionsByCountry($yestermonthConsumersbyCountry, DataType::Detections, $countries);
$yestermonthConsumersByCountryDT = $this->charts->generateDTDetectionsByCountry($yestermonthConsumersbyCountry, DataType::Consumers, $countries);
$detectionsbyRegionDT = $this->charts->generateDTDetectionsByRegion($yestermonthDetectionsbyRegion, DataType::Detections, $countries);
$consumersbyRegionDT = $this->charts->generateDTDetectionsByRegion($yestermonthConsumersbyRegion, DataType::Consumers, $countries);
return new JsonResponse([
"detectionsbyCountry"=>$yestermonthConsumersbyCountry,
//'detectionsTopCountry'=>$todayDetectionsTopCountry,
'consumersbyCountry'=>$yestermonthConsumersbyCountry,
//'consumersTopCountry'=>$todayConsumersTopCountry,
'worldMapDetections'=>$yestermonthWorldMapDetections,
'worldMapConsumers'=>$yestermonthWorldMapConsumers,
'consumersbyRegion'=>$yestermonthConsumersbyRegion,
//'detectionsTopRegion'=>$todayDetectionsTopRegion,
//'consumersTopRegion'=>$todayConsumersTopRegion,
//'consumersbyOs'=>$yesterdayConsumersbyOs,
'doughnutDetectionsChart'=>$yestermonthDoughnutDetectionsChart,
'doughnutConsumersChart'=>$yestermonthDoughnutConsumersChart,
//'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
//'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
'barDetectionsChart'=>$yestermonthBarDetectionsChart,
'barConsumersChart'=>$yestermonthBarConsumersChart,
'detectionsbyCountryTable'=>$yestermonthDetectionsByCountryDT,
'consumersbyCountryTable'=>$yestermonthConsumersByCountryDT,
'detectionsbyRegionTable'=>$detectionsbyRegionDT,
'consumersbyRegionTable'=>$consumersbyRegionDT,
'detectionsTopRegionTable'=>array_slice($detectionsbyRegionDT, 0, 10, true) ,
'consumersTopRegionTable'=>array_slice($consumersbyRegionDT, 0, 10, true),
]);
case "avg30days":
//avg30days
$avgPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', false, false, true);
$avgByCountryPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', true, false, true);
$avgByRegionPerDayLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->avgDetectionByPeriod(null, $media, 'P30D', false, true, true);
$avgConsumersLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalConsumersMonth($media, $yestermonth);
$avgDetectionsLast30 = $this->doctrine->getManager()->getRepository(Detections::class)->getMediaTotalDetectionsMonth($media, $yestermonth);
$avgWorldMapDetectionsLast30 = $this->charts->updateMapAreas($avgByCountryPerDayLast30, array_flip($countries), DataType::Detections);
$avgWorldMapConsumersLast30 = $this->charts->updateMapAreas($avgByCountryPerDayLast30, array_flip($countries), DataType::Consumers);
//$avgDoughnutDetectionsChartLast30 = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Detections, "os");
// $avgDoughnutConsumersChartLast30 = $this->charts->generateDoughnutChart($yestermonthConsumersbyOs, "nbrConsumers", "nbrOsDetections", DataType::Consumers, "os");
// $avgBarDetectionsChartLast30 = $this->charts->generateBarChart($yesterdayDetectionsbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Detections, "deviceType");
// $avgBarConsumersChartLast30 = $this->charts->generateBarChart($yesterdayConsumersbyDeviceType, "nbrConsumers", "nbrDeviceDetections", DataType::Consumers, "deviceType");
$avgDetectionsByCountryDTLast30 = $this->charts->generateDTDetectionsByCountry($avgByCountryPerDayLast30, DataType::Detections, $countries);
$avgConsumersByCountryDTLast30 = $this->charts->generateDTDetectionsByCountry($avgByCountryPerDayLast30, DataType::Consumers, $countries);
return new JsonResponse([
// "detectionsbyCountry"=>$todayDetectionsbyCountry,
//'detectionsTopCountry'=>$todayDetectionsTopCountry,
'consumersbyCountry'=>$avgByCountryPerDayLast30,
//'consumersTopCountry'=>$todayConsumersTopCountry,
'worldMapDetections'=>$avgWorldMapDetectionsLast30,
'worldMapConsumers'=>$avgWorldMapConsumersLast30,
// 'consumersbyRegion'=>$yestermonthConsumersbyRegion,
//'detectionsTopRegion'=>$todayDetectionsTopRegion,
//'consumersTopRegion'=>$todayConsumersTopRegion,
//'consumersbyOs'=>$yesterdayConsumersbyOs,
// 'doughnutDetectionsChart'=>$yestermonthDoughnutDetectionsChart,
// 'doughnutConsumersChart'=>$yestermonthDoughnutConsumersChart,
//'detectionsbyDeviceType'=>$todayDetectionsbyDeviceType,
//'consumersbyDeviceType'=>$todayConsumersbyDeviceType,
// 'barDetectionsChart'=>$yesterdayBarDetectionsChart,
//'barConsumersChart'=>$yesterdayBarConsumersChart,
'detectionsbyCountryTable'=>$avgByCountryPerDayLast30,
'consumersbyCountryTable'=>$avgByCountryPerDayLast30
]);
}
}
#[Route('/media/edit/{id}', methods:"GET|POST", name: 'edit_media')]
public function editMedia(Request $request, Media $media): Response
{
if (!($media->getCreatedBy() == $this->security->getUser())) {
$this->addFlash("error", "<h4>Forbidden</h4>Media can only be edited by its owner.");
return $this->redirectToRoute('all_media');
}else {
$form = $this->createForm(MediaType::class, $media,[
'entityManager' => $this->doctrine->getManager(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->doctrine->getManager();
/**
* Logo file upload
*/
$base64 = $request->request->get('logoFinal');
if ($base64) {
list($type, $data) = explode(';', $base64);
list(, $data) = explode(',', $base64);
$data = base64_decode($data);
$newFilename = uniqid() . '-logo.' . explode('/', $type)[1];
// Move the file to the directory where brochures are stored
try {
$path =$this->getParameter('upload_media_directory') . '/' . $media->getId() . '/logo';
if (!is_dir($path)) {
// dir doesn't exist, make it
mkdir($path, 0777, true);
}
file_put_contents(
$path . '/'.$newFilename,
$data
);
$media->setLogo($this->getParameter('upload_media_directory_display') . $media->getId() . '/logo/' . $newFilename);
// $em->flush();
} catch (FileException $e) {
echo 'error';
$this->addFlash('error', 'Something went wrong when uploading the file !');
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
//$media->setBrochureFilename($newFilename);
}
$em->flush();
$this->addFlash('success', 'Media '.$media->getName().' updated successfully');
return $this->redirectToRoute('all_media');
}
}
return $this->render('media/edit.html.twig', [
'media' => $media,
'form' => $form->createView(),
]);
}
}