<?php
namespace App\Controller;
use App\Entity\Action;
use App\Entity\Conversation;
use App\Entity\Recherche;
use App\Entity\Secteur;
use App\Entity\SocietyList;
use App\Entity\User;
use App\Form\ActionType;
use App\Repository\ActionRepository;
use App\Repository\SecteurRepository;
use App\Service\Siren;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("", name="default")
*/
public function index(Siren $siren, Request $request, ActionRepository $actionRepository): Response
{
$entityManager = $this->getDoctrine()->getManager();
$conversation = $entityManager->getRepository(Conversation::class)->find(1);
foreach ($conversation->getMessages() as $val) {
if (empty($val->getUser())) {
$entityManager->remove($val);
$entityManager->flush();
}
}
if (empty($conversation)) {
$conversation = new Conversation();
$entityManager->persist($conversation);
$entityManager->flush();
}
$society = count($entityManager->getRepository(SocietyList::class)->findByCreator($this->getUser()));
$recherches = count($entityManager->getRepository(Recherche::class)->findByUser($this->getUser()));
$allRecherches = $entityManager->getRepository(Recherche::class)->findAll();
$users = $entityManager->getRepository(User::class)->findAll();
$data = [];
$labels = [];
foreach ($users as $user) {
$labels[$user->getId()] = $user->getFirstname();
foreach ($allRecherches as $val) {
if ($val->getUser() == $user) {
key_exists($user->getId(), $data) ? $data[$user->getId()] += 1 : $data[$user->getId()] = 1;
}
}
}
$actions = $entityManager->getRepository(Action::class)->findByCreator($this->getUser());
foreach ($actions as $k => $val) {
$date = date('Y') . date('m') . date('d');
$date1 = explode('-', $val->getEnd());
$date1 = $date1[2] . $date1[1] . $date1[0];
if ($date1 < $date) {
unset($actions[$k]);
}
}
$actions = count($actions);
return $this->render('default/index.html.twig', [
'conversation' => $conversation,
'society' => $society,
'recherches' => $recherches,
'actions' => $actions,
'controller_name' => 'DefaultController',
'labels' => $labels,
'data' => $data,
]);
}
/**
* @Route("/prospecter", name="prospecter")
*/
public function prospecter(Request $request, Siren $siren, SecteurRepository $secteurRepository, EntityManagerInterface $entityManager): Response
{
$secteurs = $secteurRepository->findAll();
$index = [
"Agriculture / Peche" => [1, 2, 3],
"Energie / Environnement" => [5, 6, 7, 8, 9, 35, 36, 37, 38, 39],
"Industrie Agro-alimentaire" => [10, 11],
"Industrie" => [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33],
"Construction / BTP" => [41, 42, 43],
"Commerce Automobile" => [45],
"Commerce de gros" => [46],
"Commerce de détail" => [47, 95, 96],
"Transport" => [49, 48, 49, 50, 51],
"Hôtel / Restauration" => [55, 56],
"Edition et Logiciels" => [58],
"Audiovisuel" => [59, 60],
"Telecom" => [61],
"Conseil informatique" => [62],
"Info / web / Pub" => [63, 73],
"Finance / Assurance" => [64, 65, 66],
"Immobilier / SCI" => [68],
"Juridique / Compta" => [69],
"Conseil de gestion" => [70],
"Ingénierie / R&D" => [71, 72],
"Autre activité technique" => [74],
"Santé / Social" => [75, 86, 87, 88],
"Location / Bail" => [77],
"RH / Formation" => [78, 85],
"Voyage / Sécurité" => [79, 80],
"Bâtiment / Nettoyage" => [81],
"Services entreprises" => [82],
"Autres" => [84, 94, 97, 98, 99],
"Arts / Spectacles" => [90, 91, 92, 93],
];
// dd($siren,$request);
return $this->render('prospecter/prospecter.html.twig', [
'secteurs' => $secteurs,
'categories' => $index,
]);
}
/**
* @Route("/prospecter/filter", name="prospecter_filter")
*/
public function prospecterFilter(Request $request, Siren $siren, SecteurRepository $secteurRepository): Response
{
$data = json_decode($request->getContent(), true);
$q = "periode(etatAdministratifEtablissement:A) AND etatAdministratifUniteLegale:A ";
// $q ="";
$category = "";
$effectif = "";
$localisation = "";
$i = 0;
if (!empty($data['naf'])) {
foreach ($data['naf'] as $val) {
$i > 0 ? $q .= " OR " : $q .= " AND ";
$q .= "activitePrincipaleUniteLegale:" . $val;
$i++;
}
}
if (!empty($data['category'])) {
$category = " AND categorieEntreprise:" . $data['category'];
}
if (!empty($data['effectif'])) {
foreach ($data['effectif'] as $key => $val) {
$key == "0" ?
$effectif .= " AND trancheEffectifsEtablissement:" . $val
:
$effectif .= " OR trancheEffectifsEtablissement:" . $val;
}
}
if (!empty($data['localisation'])) {
$localisation = " AND codePostalEtablissement:" . $data['localisation'] . "*";
}
$q .= $category . $effectif;
$q .= $category . $localisation;
// dd($q,$data);
$data = $siren->getSocietiesBy($q);
$recherche = new Recherche();
$date = new \DateTime('now');
$recherche->setCreatedAt($date);
$recherche->setUser($this->getUser());
$this->getDoctrine()->getManager()->persist($recherche);
$this->getDoctrine()->getManager()->flush();
// dd($data);
$data === null ? $data = ["code" => "400"] : null;
$response = new Response(json_encode($data));
// $response->headers->set('Access-Control-Allow-Origin: *');
// $response->headers->set("Access-Control-Allow-Headers: *");
return $response;
}
}