<?php
/*(c) Noel Kenfack <noel.kenfack@yahoo.fr>
*/
namespace App\Controller\General\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\General\Template\Recherche;
use App\Form\Produit\Service\MessageType;
use App\Entity\Produit\Service\Message;
use App\Entity\Users\User\User;
use App\Entity\Users\User\Notification;
use App\Service\Servicetext\GeneralServicetext;
use App\Entity\Produit\Produit\Categorie;
use App\Entity\Produit\Service\Typearticle;
use App\Entity\Produit\Produit\Panier;
use App\Entity\Users\User\Imgprofil;
use App\Entity\Produit\Produit\Collectionproduit;
use App\Entity\Produit\Produit\Produit;
use App\Entity\Produit\Produit\Caracteristique;
use App\Entity\Produit\Produit\Produitpanier;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Service\Email\Singleemail;
use App\Security\TokenAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\HttpFoundation\Request;
class MenuController extends AbstractController
{
private $params;
private $_servicemail;
private $authenticator;
private $guardHandler;
public function __construct(ParameterBagInterface $params, Singleemail $servicemail, TokenAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler)
{
$this->params = $params;
$this->_servicemail = $servicemail;
$this->authenticator = $authenticator;
$this->guardHandler = $guardHandler;
}
public function menubare(GeneralServicetext $service, Request $request, $id = 0, $position = 'admin')
{
$em = $this->getDoctrine()->getManager();
$liste_categorie = $em->getRepository(Categorie::class)
->myFindAll();
foreach($liste_categorie as $cat)
{
$cat->setEm($em);
}
$liste_notif = $em->getRepository(Notification::class)
->findBy(array('user'=>$this->getUser(),'lut'=>'false'), array('date'=>'desc'));
$liste_interface = $em->getRepository(Typearticle::class)
->findBy(array('position'=>'interface'), array('rang'=>'asc'));
$user = null;
if($id > 0)
{
$user = $em->getRepository(User::class)
->find($id);
}
$categorieapp = $em->getRepository(Categorie::class)
->findOneBy(array('rang'=>2));
$solde = 0;
if($this->getUser() != null)
{
$solde = $service->getSoldeAccount($this->getUser()->getId());
}
$user = null;
if($this->getUser() == null and isset($_COOKIE["PIDSESSREM"]) and $_COOKIE["PIDSESSREM"] != 'delete')
{
$cookies = $_COOKIE["PIDSESSREM"];
$username = trim($service->decrypt($cookies, $this->params->get('saltcookies')));
if($service->email($username) || $service->telephone($username))
{
$repository = $em->getRepository(User::class);
$user = $repository->findOneBy(array('username'=>$username));
if($user != null)
{
$response = $this->guardHandler->authenticateUserAndHandleSuccess(
$user, // the User object you just created
$request,
$this->authenticator, // authenticator whose onAuthenticationSuccess you want to use
'main' // the name of your firewall in security.yaml
);
}
}
}
return $this->render('Theme/General/Template/Menu/menubare.html.twig',
array('liste_categorie'=>$liste_categorie,'position'=>$position,'liste_notif'=>$liste_notif,
'liste_interface'=>$liste_interface, 'user'=>$user, 'solde'=>$solde, 'categorieapp'=>$categorieapp));
}
public function menubareleft(User $user, GeneralServicetext $service, $position = 'admin')
{
$em = $this->getDoctrine()->getManager();
$liste_categorie = $em->getRepository(Categorie::class)
->myFindAll();
$nbprod = 0;
$produitpanier = null;
if($this->getUser() != null)
{
$panier = $em->getRepository(Panier::class)
->findOneBy(array('user'=>$this->getUser(),'payer'=>0));
if($panier != null)
{
$produitpanier = $panier->getProduitpaniers();
foreach($produitpanier as $prodpan)
{
$nbprod = $nbprod + $prodpan->getQuantite();
}
}
}
$solde = $service->getSoldeAccount($user->getId());
$profiluser = $em->getRepository(Imgprofil::class)
->findOneBy(array('user'=>$user));
return $this->render('Theme/General/Template/Menu/menubareleft.html.twig',
array('liste_categorie'=>$liste_categorie,'nbprod'=>$nbprod,'produitpanier'=>$produitpanier,
'user'=>$user,'profiluser'=>$profiluser,'solde'=>$solde,'position'=>$position));
}
public function menuright(User $user)
{
return $this->render('Theme/General/Template/Menu/menuright.html.twig',
array('user'=>$user));
}
public function menuleft(User $user)
{
return $this->render('Theme/General/Template/Menu/menuleft.html.twig',
array('user'=>$user));
}
public function menubareright()
{
$em = $this->getDoctrine()->getManager();
$mess = new Message();
if($this->getUser() != null)
{
$id = $this->getUser()->getId();
}else{
$id = 0;
}
$liste_message = $em->getRepository(Message::class)
->myfindmessvalide($id);
$form = $this->createForm(new MessageType, $mess);
return $this->render('Theme/General/Template/Menu/menubareright.html.twig',
array('form'=>$form->createView(),'liste_message'=>$liste_message));
}
public function footer($position = 'admin')
{
$em = $this->getDoctrine()->getManager();
$mess = new Message();
$form = $this->createForm(MessageType::class, $mess);
$liste_categorie = $em->getRepository(Categorie::class)
->myFindAll();
foreach($liste_categorie as $cat)
{
$cat->setEm($em);
}
return $this->render('Theme/General/Template/Menu/footer.html.twig',
array('form'=>$form->createView(),'position'=>$position, 'liste_categorie'=>$liste_categorie));
}
public function takeaction(GeneralServicetext $service)
{
$session = $this->get('session');
$em = $this->getDoctrine()->getManager();
$envoi = $session->get('test_takeaction');
if($envoi !== 100)
{
$date1 = new \Datetime();
$date2 = new \Datetime();
$datedebut = $date1->modify("-1 month")->format('Y-m-d');
$datefin = $date2->modify("+1 month")->format('Y-m-d');
$lastupdate = time() - 60*60*24;
$liste_commande = $em->getRepository(Produitpanier::class)
->findCommandeExpireOn($datedebut, $datefin, $lastupdate);
$siteweb = $this->params->get('siteweb');
$sitename = $this->params->get('sitename');
$emailadmin = $this->params->get('emailadmin');
foreach($liste_commande as $commande)
{
$panier = $commande->getPanier();
$date_courante = new \DateTime();
$date_expire = $commande->getExpireon();
$interval = $date_courante->diff($date_expire);
if($date_courante < $date_expire){
//Expire dans....
$produit = $commande->getProduit();
if($produit->getSouscategorie()->getCategorie()->getRang() == 1)
{
$titre = $panier->getUser()->name(25).', Votre domaine <strong>'.$commande->getDomaine().'</strong> expire dans '.$interval->format('%a').' Jour(s)';
}else{
$titre = $panier->getUser()->name(25).', Votre produit <strong>'.$produit->getNom().'</strong> expire dans '.$interval->format('%a').' Jour(s)';
}
}else{
//Expiré il y'a ...
$produit = $commande->getProduit();
if($produit->getSouscategorie()->getCategorie()->getRang() == 1)
{
$titre = $panier->getUser()->name(25).', Votre domaine <strong>'.$commande->getDomaine().'</strong> a expiré il y\'a '.$interval->format('%a').' Jour(s)';
}else{
$titre = $panier->getUser()->name(25).', Votre produit <strong>'.$produit->getNom().'</strong> a expiré il y\'a '.$interval->format('%a').' Jour(s)';
}
}
if($service->email($panier->getUser()->getUsername()))
{
$siteweb = $this->params->get('siteweb');
$sitename = $this->params->get('sitename');
$emailadmin = $this->params->get('emailadmin');
if($service->email($panier->getUser()->getUsername()))
{
$response = $this->_servicemail->sendNotifEmail(
$panier->getUser()->name(40), //Nom du destinataire
$panier->getUser()->getUsername(), //Email Destinataire
$titre, //Objet de l'email
$titre, //Grand Titre de l'email
'Nous vous informons de la date d\'expiration de vos services/produits afin d\'éviter toutes interruption de services. </br>Utilisez le lien ci-dessous pour renouveler votre commande.', //Contenu de l'email
$siteweb.'/user/orders/scat/'.$panier->getUser()->getId().'/'.$produit->getSouscategorie()->getId() //Lien à suivre
);
}
}
//Notification client.
$notification = new Notification($service);
$notification->setUser($panier->getUser());
$notification->setTitre($titre);
$notification->setDescription('Nous vous informons de la date d\'expiration de vos services/produits afin d\'éviter toutes interruption de services. </br>Utilisez le lien ci-dessous pour renouveler votre commande.');
$em->persist($notification);
$commande->setLastupdate(time());
$em->flush();
}
$liste_categorie = $em->getRepository(Categorie::class)
->myFindAll();
foreach($liste_categorie as $cat)
{
$cat->setEm($em);
$compt = 1;
foreach($cat->getSouscategories() as $scat)
{
$firstprod = $em->getRepository(Produit::class)
->findOneBy(array('souscategorie'=>$scat), array('newprise'=>'asc'));
if($firstprod != null)
{
$scat->setElement($firstprod);
}
}
}
return $this->render('Theme/General/Template/Menu/takeaction.html.twig', array('liste_categorie'=>$liste_categorie));
}
return new Response('');
}
public function stopAlerttakeaction()
{
$session = $this->get('session');
$session->set('test_takeaction',100);
echo 1;
exit;
}
public function cardmenu()
{
$em = $this->getDoctrine()->getManager();
$idcard = 0;
if(isset($_COOKIE["PIDCARD"]) and $_COOKIE["PIDCARD"] != 'empty')
{
$idcard = $_COOKIE["PIDCARD"];
}
$liste_prod = new \Doctrine\Common\Collections\ArrayCollection();
$oldpanier = $em->getRepository(Panier::class)
->findOneBy(array('id'=>$idcard,'sousmis'=>0));
if($oldpanier == null and $this->getUser() != null)
{
$oldpanier = $em->getRepository(Panier::class)
->findOneBy(array('user'=>$this->getUser(),'sousmis'=>0));
}
if($oldpanier != null)
{
$liste_prod = $oldpanier->getProduitpaniers();
}
//Si le panier lié à cette achine n'a pas d'utilisateur, on l'assicie à l'utilisateur connecté.
if($oldpanier != null and $oldpanier->getUser() == null and $this->getUser() != null)
{
$oldpanier->setUser($this->getUser());
$em->flush();
}
return $this->render('Theme/General/Template/Menu/cardmenu.html.twig',
array('oldpanier'=>$oldpanier,'liste_prod'=>$liste_prod));
}
public function carddashboard(User $user)
{
$em = $this->getDoctrine()->getManager();
$oldrenew = $em->getRepository(Collectionproduit::class)
->myfindOneBy($user->getId());
return $this->render('Theme/General/Template/Menu/carddashboard.html.twig',
array('user'=>$user, 'oldrenew'=>$oldrenew));
}
public function showmodalofferts()
{
$em = $this->getDoctrine()->getManager();
$liste_produit = $em->getRepository(Produit::class)
->findBestOffres(20);
$offres_plan = new \Doctrine\Common\Collections\ArrayCollection();
$souscategorie = null;
foreach($liste_produit as $prod)
{
$souscategorie = $prod->getSouscategorie();
$offres_plan = $em->getRepository(Caracteristique::class)
->findBy(array('souscategorie'=>$souscategorie),array('rang'=>'asc'));
break;
}
return $this->render('Theme/General/Template/Menu/showmodalofferts.html.twig',
array('offres_plan'=>$offres_plan, 'liste_produit'=>$liste_produit));
}
}