<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Doctrine\Persistence\ManagerRegistry;
use Twig\Environment as TwigEnvironment;
class DefaultController extends AbstractController {
use \App\Traits\Database;
use \App\Traits\Rights;
private $conn;
private $twig;
public function __construct(ManagerRegistry $managerRegistry, TwigEnvironment $twig) {
$this->conn = $managerRegistry->getManager()->getConnection();
$this->twig = $twig;
}
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 profile(Request $request) {
//$toolsUsers=new ToolsUsers();
//echo $toolsUsers->myMd5(1, "123456", '2023-01-27 00:13:09');
$trans=$this->container->get('site.translate')->get();
$trans['params']['v']=$this->version($this->conn, (isset($_GET['m']))?$_GET['m']:0);
if(isset($_GET['m']) && $trans['params']['v']['mobile_redirect'] && (!isset($_GET['v']) || $trans['params']['v']['name']!=$_GET['v']))
return $this->redirect($_SERVER['SCRIPT_URI'].$trans['params']['v']['mobile_redirect']);
$session=$request->getSession();
if(!$session->get('id')) return $this->redirect($this->generateUrl('admin_homepage').$trans['params']['v']['mobile_redirect']);
$is_admin=$this->checkAdmin($session, $this->conn);
$dateNow=new \DateTime();
$user_id=$session->get('id');
$item=$this->fetch($this->conn, "SELECT u.*, u.gender gender_name
FROM user u
WHERE u.id=".$user_id);
if(isset($_POST['ok'])) {
$errors=array();$message='';
$receive_mail="0";$gender_id=0;
$firstname=addslashes(ucwords(strtolower(trim($_POST['firstname']))));
$lastname=addslashes(strtoupper(trim($_POST['lastname'])));
$password=addslashes(trim($_POST['password']));
$confirm_password=addslashes(trim($_POST['confirm_password']));
$phone=addslashes(trim($_POST['phone']));
if(strlen($firstname)<2) $errors['firstname']='Veuillez entrer un prénom correct (min. 2cars)';
if(strlen($lastname)<2) $errors['lastname']='Veuillez entrer un nom correct (min. 2cars)';
if(isset($_POST['receive_mail']) && is_numeric($_POST['receive_mail']) && $_POST['receive_mail']) $receive_mail=1;
if(strlen($password)>1) {
if(strlen($password)<6) $errors['password']='Veuillez entrer un password correct (min. 6cars)';
if(strlen($confirm_password)<6) $errors['confirm_password']='Veuillez entrer un password correct (min. 6cars)';
if($password!=$confirm_password) {
$errors['password']='Veuillez entrer un password correct (min. 6cars)';
$errors['confirm_password']='Veuillez entrer un password correct (min. 6cars)';
}
}
if(count($errors)>0 || $message) return new JsonResponse(array('s'=>0, 'e'=>$errors, 'm'=>$message));
try {
$this->query($this->conn, 'START TRANSACTION');
$this->query($this->conn, 'UPDATE user SET firstname="'.$firstname.'", lastname="'.$lastname.'" WHERE id="'.$user_id.'"');
if(strlen($password)>1) {
$toolsUsers=new ToolsUsers();
$mdp=$toolsUsers->myMd5($user_id, trim($password), $item['creation_date']);
$this->query($this->conn, 'UPDATE user SET password="'.$mdp.'" WHERE id="'.$user_id.'"');
}
$this->query($this->conn, 'COMMIT');
} catch(\Exception $e) {
$this->query($this->conn, 'ROLLBACK');
return new JsonResponse(array('s'=>0, 'e'=>$errors, 'm'=>'Erreur de transaction'.$e->getMessage()));
}
return new JsonResponse(array('s'=>1));
}
$genders=array();
return new Response($this->twig->render('Default/profile.html.twig', array('item'=>$item, 'genders'=>$genders, 'is_admin'=>$is_admin, 'trans'=>$trans)));
}
public function index(Request $request) {
$trans=$this->container->get('site.translate')->get();
$trans['params']['urlcdn']='https://cdnls.touti.ma/';
$trans['params']['v']=$this->version($this->conn, (isset($_GET['m']))?$_GET['m']:0);
if(isset($_GET['m']) && $trans['params']['v']['mobile_redirect'] && (!isset($_GET['v']) || $trans['params']['v']['name']!=$_GET['v']))
return $this->redirect($_SERVER['SCRIPT_URI'].$trans['params']['v']['mobile_redirect']);
$session=$request->getSession();
if($session->get('id')) {
$is_admin=$this->checkAdmin($session, $this->conn);
$user=$this->fetch($this->conn, "SELECT u.*, u.gender gender_name
FROM user u
WHERE u.id=".$session->get('id'));
$dateNow=new \DateTime();
$pastday=new \DateTime('-2days');
$sevenday=new \DateTime('+15days');
$count_contact_pro=$this->fetchOne($this->conn, "SELECT count(id) count FROM contact_pro WHERE creation_date>'".$pastday->format('Y-m-d')."'");
$count_contact=$this->fetchOne($this->conn, "SELECT count(id) count FROM contact WHERE creation_date>'".$pastday->format('Y-m-d')."'");
$ca=array();
$ca['month']=$this->getVisitsCa($this->conn, 'month');
$ca['week']=$this->getVisitsCa($this->conn, 'week');
$ca['day']=$this->getVisitsCa($this->conn);
return new Response($this->twig->render('Default/homepage.html.twig', ['user'=>$user, 'ca'=>$ca, 'count_contact'=>$count_contact, 'count_contact_pro'=>$count_contact_pro, 'is_admin'=>$is_admin, 'trans'=>$trans]));
}
return new Response($this->twig->render('Default/index.html.twig', ['trans'=>$trans]));
}
public function login(Request $request) {
$session=$request->getSession();
$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'];
$user=$this->fetch($this->conn, "SELECT is_admin FROM user WHERE id=$user_id");
if(!$user) return new JsonResponse(array('error'=>'Echec authentification'));
$session->set('id', $user_id);
$session->set('login', $result['body']['result']['login']);
$session->set('jwt', $result['body']['jwt']);
return new JsonResponse(array('s'=>1));
}
return new JsonResponse(array('s'=>0, 'm'=>$result['body']['msg']));
}
public function deconnexion(Request $request) {
$session=$request->getSession();
$session->set('id', 0);
//if(!$session->isStarted()) $session->start();
//$session->invalidate();
return $this->redirect($this->generateUrl('admin_homepage'));
}
}
?>