<?php
namespace App\Controller;
use App\Entity\Stats;
use App\Form\StatsType;
use App\Repository\AdminStatsRepository;
use App\Entity\User;
use App\Form\UserType;
use App\Repository\UserRepository;
use App\Entity\Upartistik;
use App\Repository\UpartistikRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @Route("/admin/stats")
*/
class AdminStatsController extends AbstractController
{
/**
* @var UserRepository
*/
private $repository;
private $passwordEncoder;
public function __construct(AdminStatsRepository $statsRepo, UserRepository $repository, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, UpartistikRepository $upartRepo)
{
$this->repository = $repository;
$this->em = $em;
$this->passwordEncoder = $passwordEncoder;
$this->upartRepo = $upartRepo;
$this->statsRepo = $statsRepo;
}
/**
* @Route("/", name="stats_index", methods={"GET"})
*/
public function index(): Response
{
$dateDebut = new \DateTime();
$dateFin = new \DateTime();
$dateDebut->setTime(0, 0, 0);
// dump($dateDebut);
$dateFin->setTime(24, 0, 0);
// dump($dateFin);
$stats = $this->statsRepo->findByDate($dateDebut, $dateFin);
$listeIp = [];
for($i=0; $i<count($stats); $i++){
// recup ip
$listeIp[] = $stats[$i]->getIp();
}
$listeIp = array_unique($listeIp);
return $this->render('admin_stats/index.html.twig', [
'controller_name' => 'stats_index',
'upartRepo' => $this->upartRepo->find(1),
'stats' => $stats,
'dateDebut' => $dateDebut,
'dateFin' => $dateFin,
'listeIp' => $listeIp,
]);
}
/**
* @Route("/{jour}/{mois}/{annee}", name="stats_dateJour", methods={"GET"})
*/
public function stats_dateJour($jour, $mois, $annee): Response
{
$dateDebut = new \DateTime();
$dateDebut->setDate($annee, $mois, $jour);
$dateDebut->setTime(0, 0, 0);
$dateFin = new \DateTime();
$dateFin->setDate($annee, $mois, ($jour+1));
$dateFin->setTime(0, 0, 0);
$stats = $this->statsRepo->findByDate($dateDebut, $dateFin);
$listeIp = [];
for($i=0; $i<count($stats); $i++){
// recup ip
$listeIp[] = $stats[$i]->getIp();
}
$listeIp = array_unique($listeIp);
// dump($dateDebut);
// dump($dateFin);
// dump($stats);
// dump($listeIp);
return $this->render('admin_stats/index.html.twig', [
'controller_name' => 'stats_dateJour',
'upartRepo' => $this->upartRepo->find(1),
'dateDebut' => $dateDebut,
'dateFin' => $dateFin,
'stats' => $stats,
'listeIp' => $listeIp,
]);
}
/**
* @Route("/new", name="stats_new", methods={"GET", "POST"})
*/
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$stat = new Stats();
$form = $this->createForm(StatsType::class, $stat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($stat);
$entityManager->flush();
return $this->redirectToRoute('stats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin_stats/new.html.twig', [
'upartRepo' => $this->upartRepo->find(1),
'stat' => $stat,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="stats_show", methods={"GET"})
*/
public function show(Stats $stat): Response
{
return $this->render('admin_stats/show.html.twig', [
'upartRepo' => $this->upartRepo->find(1),
'stat' => $stat,
]);
}
/**
* @Route("/{id}/edit", name="stats_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Stats $stat, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(StatsType::class, $stat);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('stats_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('admin_stats/edit.html.twig', [
'upartRepo' => $this->upartRepo->find(1),
'stat' => $stat,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="stats_delete", methods={"POST"})
*/
public function delete(Request $request, Stats $stat, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$stat->getId(), $request->request->get('_token'))) {
$entityManager->remove($stat);
$entityManager->flush();
}
return $this->redirectToRoute('stats_index', [], Response::HTTP_SEE_OTHER);
}
}