src/Controller/Admin/DefaultController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. class DefaultController extends AbstractController {
  8.     use \App\Traits\Database;
  9.     use \App\Traits\Rights;
  10.     private function getVisitsCa($conn$frequency="") {
  11.         $data=array();$data_ca=array();
  12.         $dateMonth=new \DateTime('this month');
  13.         $startMonth=$dateMonth->format('Y-m-01 00:00:00');
  14.         if($frequency=="month") {
  15.             $dateEndMonth=new \DateTime('+1month');
  16.             $startDate=$dateMonth->format('Y-m-01 00:00:00');
  17.             $endDate=$dateEndMonth->format('Y-m-01 00:00:00');
  18.         }
  19.         elseif($frequency=="week") {
  20.             $dateWeek=new \DateTime('this week');$dateNextWeek=new \DateTime('next week');
  21.             $startDate=$dateWeek->format('Y-m-d 00:00:00');
  22.             $endDate=$dateNextWeek->format('Y-m-d 00:00:00');
  23.         }
  24.         else {
  25.             $dateNow=new \DateTime();$dateTomorrow=new \DateTime('+1day');
  26.             $startDate=$dateNow->format('Y-m-d 00:00:00');
  27.             $endDate=$dateTomorrow->format('Y-m-d 00:00:00');
  28.         }
  29.         $data=$this->fetch($conn'SELECT SUM(v.nb) as sum_visites, COUNT(DISTINCT v.ip, v.creation_date) as count_visites, v.creation_date
  30.             FROM visite v
  31.             WHERE v.creation_date BETWEEN "'.$startDate.'" AND "'.$endDate.'"');
  32.         if(!$data['sum_visites']) $data['sum_visites']="0";
  33.         return $data;
  34.     }
  35.     public function index(Request $request) {
  36.         $translate=$this->container->get('site.translate')->get();
  37.         $session=new Session();
  38.         if($session->get('admin')) {
  39.             $conn=$this->getDoctrine()->getConnection();
  40.             $is_admin=$this->checkAdmin($session$conn);
  41.             $dateNow=new \DateTime();
  42.             $pastday=new \DateTime('-2days');
  43.             $sevenday=new \DateTime('+15days');
  44.             $count_contact_pro=$this->fetchOne($conn"SELECT count(id) count FROM contact_pro WHERE creation_date>'".$pastday->format('Y-m-d')."'");
  45.             $count_contact=$this->fetchOne($conn"SELECT count(id) count FROM contact WHERE creation_date>'".$pastday->format('Y-m-d')."'");
  46.             $ca=array();
  47.             $ca['month']=$this->getVisitsCa($conn'month');
  48.             $ca['week']=$this->getVisitsCa($conn'week');
  49.             $ca['day']=$this->getVisitsCa($conn);
  50.             return $this->redirect($this->generateUrl('admin_prestashop_updater'));
  51.             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));
  52.         }
  53.         return $this->render('Admin/Default/index.html.twig', array('translate'=>$translate));
  54.     }
  55.     public function login(Request $request) {
  56.         $session=$this->get('session');
  57.         $options=array('login'=>trim($_POST['login']), 'password'=>trim($_POST['password']));
  58.         $result=$this->container->get('site.users')->login($options);
  59.         if($result['status']==0) return new JsonResponse(array('error'=>$result['message']));
  60.         if($result['body']['success']) {
  61.             $user_id=$result['body']['result']['id'];
  62.             $conn=$this->getDoctrine()->getConnection();
  63.             $user=$this->fetch($conn"SELECT is_admin FROM user WHERE id=$user_id");
  64.             if(!$user || !$user['is_admin']) return new JsonResponse(array('error'=>'Echec authentification'));
  65.             $session->set('admin'$user_id);
  66.             $session->set('login'$result['body']['result']['login']);
  67.             $session->set('jwt'$result['body']['jwt']);
  68.             return new JsonResponse(array('error'=>''));
  69.         }
  70.         return new JsonResponse(array('error'=>$result['body']['msg']));
  71.     }
  72.     public function deconnexion(Request $request) {
  73.         $session = new Session();
  74.         $session->set('id'0);
  75.         $session->set('admin'0);
  76.         //if(!$session->isStarted()) $session->start();
  77.         //$session->invalidate();
  78.         return $this->redirect($this->generateUrl('admin_homepage'));
  79.     }
  80. }
  81. ?>