<?php
namespace App\Controller\Admin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends AbstractController {
use \App\Traits\Database;
use \App\Traits\Rights;
private function getVisitsCa($conn, $frequency="") {
$data=array();$data_ca=array();
$dateMonth=new \DateTime('this month');
$startMonth=$dateMonth->format('Y-m-01 00:00:00');
if($frequency=="month") {
$dateEndMonth=new \DateTime('+1month');
$startDate=$dateMonth->format('Y-m-01 00:00:00');
$endDate=$dateEndMonth->format('Y-m-01 00:00:00');
}
elseif($frequency=="week") {
$dateWeek=new \DateTime('this week');$dateNextWeek=new \DateTime('next week');
$startDate=$dateWeek->format('Y-m-d 00:00:00');
$endDate=$dateNextWeek->format('Y-m-d 00:00:00');
}
else {
$dateNow=new \DateTime();$dateTomorrow=new \DateTime('+1day');
$startDate=$dateNow->format('Y-m-d 00:00:00');
$endDate=$dateTomorrow->format('Y-m-d 00:00:00');
}
$data=$this->fetch($conn, 'SELECT SUM(v.nb) as sum_visites, COUNT(DISTINCT v.ip, v.creation_date) as count_visites, v.creation_date
FROM visite v
WHERE v.creation_date BETWEEN "'.$startDate.'" AND "'.$endDate.'"');
if(!$data['sum_visites']) $data['sum_visites']="0";
return $data;
}
public function index(Request $request) {
$translate=$this->container->get('site.translate')->get();
$session=new Session();
if($session->get('admin')) {
$conn=$this->getDoctrine()->getConnection();
$is_admin=$this->checkAdmin($session, $conn);
$dateNow=new \DateTime();
$pastday=new \DateTime('-2days');
$sevenday=new \DateTime('+15days');
$count_contact_pro=$this->fetchOne($conn, "SELECT count(id) count FROM contact_pro WHERE creation_date>'".$pastday->format('Y-m-d')."'");
$count_contact=$this->fetchOne($conn, "SELECT count(id) count FROM contact WHERE creation_date>'".$pastday->format('Y-m-d')."'");
$ca=array();
$ca['month']=$this->getVisitsCa($conn, 'month');
$ca['week']=$this->getVisitsCa($conn, 'week');
$ca['day']=$this->getVisitsCa($conn);
return $this->redirect($this->generateUrl('admin_prestashop_updater'));
return $this->render('Admin/Default/homepage.html.twig', array('ca'=>$ca, 'count_contact'=>$count_contact, 'count_contact_pro'=>$count_contact_pro, 'is_admin'=>$is_admin, 'translate'=>$translate));
}
return $this->render('Admin/Default/index.html.twig', array('translate'=>$translate));
}
public function login(Request $request) {
$session=$this->get('session');
$options=array('login'=>trim($_POST['login']), 'password'=>trim($_POST['password']));
$result=$this->container->get('site.users')->login($options);
if($result['status']==0) return new JsonResponse(array('error'=>$result['message']));
if($result['body']['success']) {
$user_id=$result['body']['result']['id'];
$conn=$this->getDoctrine()->getConnection();
$user=$this->fetch($conn, "SELECT is_admin FROM user WHERE id=$user_id");
if(!$user || !$user['is_admin']) return new JsonResponse(array('error'=>'Echec authentification'));
$session->set('admin', $user_id);
$session->set('login', $result['body']['result']['login']);
$session->set('jwt', $result['body']['jwt']);
return new JsonResponse(array('error'=>''));
}
return new JsonResponse(array('error'=>$result['body']['msg']));
}
public function deconnexion(Request $request) {
$session = new Session();
$session->set('id', 0);
$session->set('admin', 0);
//if(!$session->isStarted()) $session->start();
//$session->invalidate();
return $this->redirect($this->generateUrl('admin_homepage'));
}
}
?>