src/Controller/DefaultController.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Twig\Environment as TwigEnvironment;
  9. class DefaultController extends AbstractController {
  10.     use \App\Traits\Database;
  11.     use \App\Traits\Rights;
  12.     private $conn;
  13.     private $twig;
  14.     public function __construct(ManagerRegistry $managerRegistryTwigEnvironment $twig) {
  15.         $this->conn $managerRegistry->getManager()->getConnection();
  16.         $this->twig $twig;
  17.     }
  18.     private function getVisitsCa($conn$frequency="") {
  19.         $data=array();$data_ca=array();
  20.         $dateMonth=new \DateTime('this month');
  21.         $startMonth=$dateMonth->format('Y-m-01 00:00:00');
  22.         if($frequency=="month") {
  23.             $dateEndMonth=new \DateTime('+1month');
  24.             $startDate=$dateMonth->format('Y-m-01 00:00:00');
  25.             $endDate=$dateEndMonth->format('Y-m-01 00:00:00');
  26.         }
  27.         elseif($frequency=="week") {
  28.             $dateWeek=new \DateTime('this week');$dateNextWeek=new \DateTime('next week');
  29.             $startDate=$dateWeek->format('Y-m-d 00:00:00');
  30.             $endDate=$dateNextWeek->format('Y-m-d 00:00:00');
  31.         }
  32.         else {
  33.             $dateNow=new \DateTime();$dateTomorrow=new \DateTime('+1day');
  34.             $startDate=$dateNow->format('Y-m-d 00:00:00');
  35.             $endDate=$dateTomorrow->format('Y-m-d 00:00:00');
  36.         }
  37.         $data=$this->fetch($conn'SELECT SUM(v.nb) as sum_visites, COUNT(DISTINCT v.ip, v.creation_date) as count_visites, v.creation_date
  38.             FROM visite v
  39.             WHERE v.creation_date BETWEEN "'.$startDate.'" AND "'.$endDate.'"');
  40.         if(!$data['sum_visites']) $data['sum_visites']="0";
  41.         return $data;
  42.     }
  43.     public function profile(Request $request) {
  44.         //$toolsUsers=new ToolsUsers();
  45.         //echo $toolsUsers->myMd5(1, "123456", '2023-01-27 00:13:09');
  46.         $trans=$this->container->get('site.translate')->get();
  47.         $trans['params']['v']=$this->version($this->conn, (isset($_GET['m']))?$_GET['m']:0);
  48.         if(isset($_GET['m']) && $trans['params']['v']['mobile_redirect'] && (!isset($_GET['v']) || $trans['params']['v']['name']!=$_GET['v']))
  49.             return $this->redirect($_SERVER['SCRIPT_URI'].$trans['params']['v']['mobile_redirect']);
  50.         $session=$request->getSession();
  51.         if(!$session->get('id')) return $this->redirect($this->generateUrl('admin_homepage').$trans['params']['v']['mobile_redirect']);
  52.         $is_admin=$this->checkAdmin($session$this->conn);
  53.         $dateNow=new \DateTime();
  54.         $user_id=$session->get('id');
  55.         $item=$this->fetch($this->conn"SELECT u.*, u.gender gender_name
  56.             FROM user u
  57.             WHERE u.id=".$user_id);
  58.         
  59.         if(isset($_POST['ok'])) {
  60.             $errors=array();$message='';
  61.             $receive_mail="0";$gender_id=0;
  62.             $firstname=addslashes(ucwords(strtolower(trim($_POST['firstname']))));
  63.             $lastname=addslashes(strtoupper(trim($_POST['lastname'])));
  64.             $password=addslashes(trim($_POST['password']));
  65.             $confirm_password=addslashes(trim($_POST['confirm_password']));
  66.             $phone=addslashes(trim($_POST['phone']));
  67.             if(strlen($firstname)<2$errors['firstname']='Veuillez entrer un prĂ©nom correct (min. 2cars)';
  68.             if(strlen($lastname)<2$errors['lastname']='Veuillez entrer un nom correct (min. 2cars)';
  69.             if(isset($_POST['receive_mail']) && is_numeric($_POST['receive_mail']) && $_POST['receive_mail']) $receive_mail=1;
  70.             if(strlen($password)>1) {
  71.                 if(strlen($password)<6$errors['password']='Veuillez entrer un password correct (min. 6cars)';
  72.                 if(strlen($confirm_password)<6$errors['confirm_password']='Veuillez entrer un password correct (min. 6cars)';
  73.                 if($password!=$confirm_password) {
  74.                     $errors['password']='Veuillez entrer un password correct (min. 6cars)';
  75.                     $errors['confirm_password']='Veuillez entrer un password correct (min. 6cars)';
  76.                 }
  77.             }
  78.             if(count($errors)>|| $message) return new JsonResponse(array('s'=>0'e'=>$errors'm'=>$message));
  79.             try {
  80.                 $this->query($this->conn'START TRANSACTION');
  81.                 $this->query($this->conn'UPDATE user SET firstname="'.$firstname.'", lastname="'.$lastname.'" WHERE id="'.$user_id.'"');
  82.                 if(strlen($password)>1) {
  83.                     $toolsUsers=new ToolsUsers();
  84.                     $mdp=$toolsUsers->myMd5($user_idtrim($password), $item['creation_date']);
  85.                     $this->query($this->conn'UPDATE user SET password="'.$mdp.'" WHERE id="'.$user_id.'"');
  86.                 }
  87.                 $this->query($this->conn'COMMIT');
  88.             } catch(\Exception $e) {
  89.                 $this->query($this->conn'ROLLBACK');
  90.                 return new JsonResponse(array('s'=>0'e'=>$errors'm'=>'Erreur de transaction'.$e->getMessage()));
  91.             }
  92.             return new JsonResponse(array('s'=>1));
  93.         }
  94.         $genders=array();
  95.         return new Response($this->twig->render('Default/profile.html.twig', array('item'=>$item'genders'=>$genders'is_admin'=>$is_admin'trans'=>$trans)));
  96.     }
  97.     public function index(Request $request) {
  98.         $trans=$this->container->get('site.translate')->get();
  99.         $trans['params']['urlcdn']='https://cdnls.touti.ma/';
  100.         $trans['params']['v']=$this->version($this->conn, (isset($_GET['m']))?$_GET['m']:0);
  101.         if(isset($_GET['m']) && $trans['params']['v']['mobile_redirect'] && (!isset($_GET['v']) || $trans['params']['v']['name']!=$_GET['v']))
  102.             return $this->redirect($_SERVER['SCRIPT_URI'].$trans['params']['v']['mobile_redirect']);
  103.         $session=$request->getSession();
  104.         if($session->get('id')) {
  105.             $is_admin=$this->checkAdmin($session$this->conn);
  106.             $user=$this->fetch($this->conn"SELECT u.*, u.gender gender_name
  107.                 FROM user u
  108.                 WHERE u.id=".$session->get('id'));
  109.             $dateNow=new \DateTime();
  110.             $pastday=new \DateTime('-2days');
  111.             $sevenday=new \DateTime('+15days');
  112.             $count_contact_pro=$this->fetchOne($this->conn"SELECT count(id) count FROM contact_pro WHERE creation_date>'".$pastday->format('Y-m-d')."'");
  113.             $count_contact=$this->fetchOne($this->conn"SELECT count(id) count FROM contact WHERE creation_date>'".$pastday->format('Y-m-d')."'");
  114.             $ca=array();
  115.             $ca['month']=$this->getVisitsCa($this->conn'month');
  116.             $ca['week']=$this->getVisitsCa($this->conn'week');
  117.             $ca['day']=$this->getVisitsCa($this->conn);
  118.             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]));
  119.         }
  120.         return new Response($this->twig->render('Default/index.html.twig', ['trans'=>$trans]));
  121.     }
  122.     public function login(Request $request) {
  123.         $session=$request->getSession();
  124.         $options=array('login'=>trim($_POST['login']), 'password'=>trim($_POST['password']));
  125.         $result=$this->container->get('site.users')->login($options);
  126.         if($result['status']==0) return new JsonResponse(array('error'=>$result['message']));
  127.         if($result['body']['success']) {
  128.             $user_id=$result['body']['result']['id'];
  129.             $user=$this->fetch($this->conn"SELECT is_admin FROM user WHERE id=$user_id");
  130.             if(!$user) return new JsonResponse(array('error'=>'Echec authentification'));
  131.             $session->set('id'$user_id);
  132.             $session->set('login'$result['body']['result']['login']);
  133.             $session->set('jwt'$result['body']['jwt']);
  134.             return new JsonResponse(array('s'=>1));
  135.         }
  136.         return new JsonResponse(array('s'=>0'm'=>$result['body']['msg']));
  137.     }
  138.     public function deconnexion(Request $request) {
  139.         $session=$request->getSession();
  140.         $session->set('id'0);
  141.         //if(!$session->isStarted()) $session->start();
  142.         //$session->invalidate();
  143.         return $this->redirect($this->generateUrl('admin_homepage'));
  144.     }
  145. }
  146. ?>