<?php
namespace AdminBundle\Controller;
use AdminBundle\Entity\User;
use AdminBundle\Form\UserType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('AdminBundle:User')->findByRole(3);
$acheteurs = $em->getRepository('AdminBundle:User')->findByRole(2);
$offres = $em->getRepository('FournisseurBundle:Offre')->findByIs_active(0);
$varietes = $em->getRepository('AdminBundle:Variete')->findAll();
$souks = $em->getRepository('AdminBundle:Souk')->findAll();
$professionnels = $em->getRepository('AdminBundle:Professionnel')->findAll();
$actualites = $em->getRepository('AdminBundle:Actualite')->findAll();
$n = count($users);
$n2 = count($acheteurs);
$n3 = count($offres);
$n4 = count($varietes);
$n5 = count($souks);
$n6 = count($professionnels);
$n7 = count($actualites);
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` WHERE `variete_id`=1 GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$mejhoul = 0;
$signeMejhoul = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$mejhoul = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signeMejhoul = "+";
else
$signeMejhoul = "-";
}
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` WHERE `variete_id`=5 GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$bouzekri = 0;
$signeBouzekri = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$bouzekri = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signeBouzekri = "+";
else
$signeBouzekri = "-";
}
//boufegouss
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` WHERE `variete_id`=4 GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$bouf = 0;
$signebouf = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$bouf = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signebouf = "+";
else
$signebouf = "-";
}
//khalt
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` WHERE `variete_id`=6 GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$bouf2 = 0;
$signebouf2 = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$bouf2 = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signebouf2 = "+";
else
$signebouf2 = "-";
}
//aziza
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` WHERE `variete_id`=7 GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$bouf3 = 0;
$signebouf3 = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$bouf3 = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signebouf3 = "+";
else
$signebouf3 = "-";
}
$RAW_QUERY = 'SELECT semaine as s,avg(prix) as p FROM `bourse` GROUP BY `semaine` order by `semaine` DESC';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
if (count($elements) < 2) {
$bouzekrii = 0;
$signeBouzekrii = "+";
} else {
$i = 1;
foreach ($elements as $element) {
if ($i == 1) {
$montant1 = $element["p"];
}
if ($i == 2) {
$montant2 = $element["p"];
break;
}
$i = $i + 1;
}
$bouzekrii = $montant2 / $montant1 * 100;
if ($montant1 >= $montant2)
$signeBouzekrii = "+";
else
$signeBouzekrii = "-";
}
$tab = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$RAW_QUERY = 'SELECT month(created) as m,count(*) as v FROM `offre` WHERE year(created)=year(now()) GROUP BY month(created)';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
foreach ($elements as $element) {
$tab[$element["m"] - 1] = $element["v"];
}
$res = "[";
foreach ($tab as $key => $value) {
$res = $res . "," . $value;
}
$res = $res . "]";
$tab = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$RAW_QUERY = 'SELECT month(created) as m,count(*) as v FROM `offre` WHERE `is_vendu` = 1 and year(created)=year(now()) GROUP BY month(created)';
$statement = $em->getConnection()->prepare($RAW_QUERY);
$statement->execute();
$elements = $statement->fetchAll();
foreach ($elements as $element) {
$tab[$element["m"] - 1] = $element["v"];
}
$res2 = "[";
foreach ($tab as $key => $value) {
$res2 = $res2 . "," . $value;
}
$res2 = $res2 . "]";
return $this->render('AdminBundle:Default:index.html.twig', array("nombreGIE" => $n, "nombreAcheteurs" => $n2, "nombreOffres" => $n3,
"nombreVarietes" => $n4, "bourseMejhoul" => $mejhoul, "signeMejhoul" => $signeMejhoul
, "boursemoyenne" => $bouzekrii, "signemoyenne" => $signeBouzekrii,
"boursebouf" => $bouf, "signebouf" => $signebouf,
"boursekhalt" => $bouf2, "signekhalt" => $signebouf2,
"bourseaziza" => $bouf3, "signeaziza" => $signebouf3,
"bourseBouzekri" => $bouzekri, "signeBouzekri" => $signeBouzekri,
"nombresouks" => $n5, "nombreprofessionnels" => $n6, "nombreactualites" => $n7,
"dataoffre" => $res, "dataachat" => $res2));
}
/**
* @Route("/admin/ajouter_utilisateur", name="ajouter_user")
*/
public function ajouterUserAction(Request $request)
{
$form = $this->createForm(UserType::class);
$form->handleRequest($request);
//var_dump($this->get('kernel')->getRootDir()); exit();
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$user = new User();
$username = $form["username"]->getData();
$email = $form["email"]->getData();
$repository = $em->getRepository("AdminBundle:User");
$user2 = $em->getRepository('AdminBundle:User')->findByUsername($username);
$user3 = $em->getRepository('AdminBundle:User')->findByEmail($email);
if ($user2) {
$this->addFlash(
'message',
'Identifiant existe déja dans la BD, veuillez choisir un autre.'
);
return $this->render('AdminBundle:Default:register.html.twig', array(
'form' => $form->createView(),
));
}
if ($user3) {
$this->addFlash(
'message',
'Email existe déja dans la BD, veuillez choisir un autre.'
);
return $this->render('AdminBundle:Default:register.html.twig', array(
'form' => $form->createView(),
));
}
$nom = $form["nom"]->getData();
$prenom = $form["prenom"]->getData();
$password = $form["password"]->getData();
$role = $form['role']->getData();
$file = $form["image"]->getData();
$user->setUsername($username);
$user->setEmail($email);
$user->setNom($nom);
$user->setPrenom($prenom);
$user->setEnabled(1);
$user->setRole($role);
if ($role === 1) {
$roles = 'ROLE_ADMIN';
} elseif ($role === 2) {
$roles = 'ROLE_ACHETEUR';
} else {
$roles = 'ROLE_FOURNISSEUR';
}
$user->setRoles([$roles]);
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 10; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$user->setSalt($randomString);
$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$encodedPassword = $encoder->encodePassword($password, $user->getSalt());
$user->setPassword($encodedPassword);
if ($file != null) {
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
try {
$file->move(
$this->getParameter('images_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
//$temp = "http://" . $request->getHost() ."/pmp/pmp-api/web/assets/files/images/" . $fileName;
$user->setImage($fileName);
}
$em->persist($user);
$em->flush();
return $this->redirectToRoute('list_users');
}
return $this->render('AdminBundle:Default:register.html.twig', array(
'form' => $form->createView(),
));
}
/**
* @Route("/admin/liste_utilisateur", name="list_users")
*/
public function listUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository("AdminBundle:User");
$users = $repository->findBy([],['id'=>'desc']);
return $this->render('AdminBundle:Default:listusers.html.twig', array(
'users' => $users,
));
}
/**
*
* @Route("/admin/supprimer_utilisateur/{id}", name="remove_user")
*/
public function removeUserAction(User $user)
{
$em = $this->getDoctrine()->getManager();
try {
$em->remove($user);
$em->flush();
$fileSystem = new Filesystem();
//$end = array_slice(explode('/', $user->getImage()), -1)[0];
if ($user->getImage() != null) {
$path = $this->getParameter('images_directory') . "/" . $user->getImage();
$fileSystem->remove($path);
}
} catch (\Doctrine\DBAL\DBALException $e) {
$this->addFlash(
'message',
'une erreur est survenu !.'
);
return $this->redirect($this->generateUrl('list_users'));
}
return $this->redirectToRoute('list_users');
}
/**
*
* @Route("/admin/modifier_utilisateur/{id}", name="edit_user")
*/
public function editRegisterAction(Request $request, User $user)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
$session = $this->get('session');
if ($form->isSubmitted()) {
$oldusername = $session->get('oldusername');
$oldemail = $session->get('oldemail');
$oldimage = $session->get('oldimage');
if (strcmp($oldusername, $form["username"]->getData()) !== 0) {
$otherUser = $em->getRepository('AdminBundle:User')->findByUsername($form["username"]->getData());
if ($otherUser) {
$this->addFlash(
'message',
'Identifiant existe déja dans la BD, veuillez choisir un autre.'
);
return $this->render('AdminBundle:Default:modifieruser.html.twig', array(
'form' => $form->createView(),
));
}
}
if (strcmp($oldemail, $form["email"]->getData()) !== 0) {
$otherUser = $em->getRepository('AdminBundle:User')->findByEmail($form["email"]->getData());
if ($otherUser) {
$this->addFlash(
'message',
'Email existe déja dans la BD, veuillez choisir un autre.'
);
return $this->render('AdminBundle:Default:modifieruser.html.twig', array(
'form' => $form->createView(),
));
}
}
$password = $form["password"]->getData();
$role = $form['role']->getData();
$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$encodedPassword = $encoder->encodePassword($password, $user->getSalt());
$user->setPassword($encodedPassword);
$user->setRole($role);
if ($role === 1) {
$roles = 'ROLE_ADMIN';
} elseif ($role === 2) {
$roles = 'ROLE_ACHETEUR';
} else {
$roles = 'ROLE_FOURNISSEUR';
}
$user->setRoles([$roles]);
$file = $user->getImage();
if ($file != null) {
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
try {
$file->move(
$this->getParameter('images_directory'),
$fileName
);
} catch (FileException $e) {
}
//$temp = "http://" . $request->getHost() ."/pmp/pmp-api/web/assets/files/images/" . $fileName;
$user->setImage($fileName);
} else {
$user->setImage($oldimage);
}
$em->flush();
return $this->redirectToRoute('list_users');
} else {
$session->set('oldusername', $user->getUsername());
$session->set('oldemail', $user->getEmail());
$session->set('oldimage', $user->getImage());
}
return $this->render('AdminBundle:Default:modifieruser.html.twig', array(
'form' => $form->createView(),
));
}
/**
*
* @Route("/admin/blocker_utilisateur/{id}", name="blocker_user")
*/
public function blockerUserAction(User $user)
{
$em = $this->getDoctrine()->getManager();
$user->setEnabled(false);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('list_users');
}
/**
*
* @Route("/admin/activer_utilisateur/{id}", name="activer_user")
*/
public function activerUserAction(User $user)
{
$em = $this->getDoctrine()->getManager();
$user->setEnabled(true);
$em->persist($user);
$em->flush();
return $this->redirectToRoute('list_users');
}
/**
*
* @Route("/admin/trace", name="trace")
*/
public function traceAction()
{
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository("AdminBundle:Trace");
$traces = $repository->findBy([],['created'=>'desc']);
return $this->render('AdminBundle:Trace:index.html.twig', array(
'traces' => $traces,
));
}
}