<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Notification\Notification;
use Doctrine\Persistence\ManagerRegistry;
use App\Repository\NotificationRepository;
use App\Service\StatService;
use App\Form\ExportType;
use App\Service\ExportDataStat;
/**
* @Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_AGENT') or is_granted('ROLE_ADMINISTRATOR') or is_granted('ROLE_CONSULTANT')")
*/
class DashboardController extends AbstractController
{
private $manager;
private $notificationRepository;
private $stats;
private $exportDataStat;
public function __construct(StatService $stats,ManagerRegistry $doctrine, NotificationRepository $notificationRepository,ExportDataStat $exportDataStat)
{
$this->manager = $doctrine->getManager();
$this->notificationRepository=$notificationRepository;
$this->stats = $stats;
$this->exportDataStat = $exportDataStat;
}
#[Route('/', name: 'app_dashboard')]
public function index(Notification $notification,Request $request): Response
{
/*$notification->sendNotification();*/
$exportForm = $this->createForm(ExportType::class);
$exportForm->handleRequest($request);
if($exportForm->isSubmitted())
{
$this->exportDataStat->export();
}
$stats = $this->stats->getStats();
return $this->render('dashboard/index.html.twig', [
'stats' => $stats,
'exportForm'=>$exportForm->createView()
]);
}
#[Route('/save/token', name: 'app_token')]
public function saveToken(Request $request)
{
$user=$this->getUser();
try{
$user->setToken($request->request->get('token'));
$this->manager->persist($user);
$this->manager->flush();
}catch(\Exeption $e){
}
return $this->json(['message'=>'succes']);
}
#[Route('/notif/number', name: 'app_notif')]
public function getNotif()
{
$notifCount=count($this->notificationRepository->findBy(['direction'=>'agent','status'=>0]));
return $this->json(['number'=> $notifCount]);
}
}