<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Entity\User;
use App\Entity\Suivi;
use App\Entity\Contact;
use App\Entity\Recherche;
use App\Entity\Agenda;
use App\Form\ContactType;
class CoreController extends AbstractController {
private $resultsPerPage = 30;
/**
* @Route("/", name="HomePage")
*/
public function index(): Response {
$user = $this->getUser();
if ( $user )
{
if ( in_array('ROLE_CLIENT', $user->getRoles()) )
return $this->redirectToRoute('rdv');
elseif ( in_array('ROLE_USER', $user->getRoles() ))
return $this->redirectToRoute('contactsHomePage');
else
return $this->redirectToRoute('app_login');
}
return $this->redirectToRoute('app_login');
}
/**
* @Route("/contacts", name="contactsHomePage")
*/
public function contacts(): Response {
$user = $this->getUser();
if ( $user->getAgenda() == null ) {
$em = $this->getDoctrine()->getManager();
$agenda = new Agenda();
$agenda->setPraticien($user);
$agenda->setNom($user->displayName());
$em->persist($agenda);
$em->flush();
}
$licence = $user->getLicence();
$praticiens = $this->getDoctrine()->getRepository(User::class)->getPraticiens($licence);
return $this->render('core/index.html.twig', [ 'praticiens' => $praticiens ]);
}
/**
* @Route("/contacts/getMenu", methods={"GET"}, options={"expose"=true}, name="getMenu")
*/
public function getMenu() {
$licence = $this->getUser()->getLicence();
$praticiens = $this->getDoctrine()->getRepository(User::class)->getPraticiens($licence);
return $this->render('core/praticiensForMenu.html.twig', ['praticiens' => $praticiens]);
}
/**
* @Route("/contacts/search", methods={"POST"}, options={"expose"=true}, name="search")
*/
public function search(RepositoryManagerInterface $finder, Request $request): Response {
// Initialisations
$em = $this->getDoctrine()->getManager();
$request->request->get('page') ? $page = $request->request->get('page') : $page = 1;
$request->request->get('userId') ? $userId = $request->request->get('userId') : $userId = 'all';
$request->request->get('query') ? $query = $request->request->get('query') : $query = '';
$licence = $this->getUser()->getLicence();
// Filtre la requete
$query = trim($query);
$originalQuery = $query;
$unwanted_array = [
'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y',
];
$query = strtr( $query, $unwanted_array );
$query = preg_replace( '/[\W]/', ' ', $query);
// Enregistrement de la requête dans une variable de session
$this->get('session')->set('lastQuery', $query);
// Initialisation de la requete Elasticsearch
$elasticaQuery = new \Elastica\Query\BoolQuery();
// Recherche sur les mots saisis
if ( $query != "" ) {
$queryString = new \Elastica\Query\QueryString();
$queryString->setDefaultOperator('AND');
$queryString->setQuery($query);
$elasticaQuery->addMust($queryString);
}
// Filtre sur un praticien
if ( $userId != "all") {
$queryUser = new \Elastica\Query\Term();
$queryUser->setTerm('getUsersIds', $userId );
$elasticaQuery->addMust($queryUser);
$user = $em->getRepository(User::class)->find($userId);
$filtreLabel = $user->getPrenom();
}
else {
$filtreLabel = 'aucun';
$userId = 'all';
}
// Filtre sur la licence
$queryLicence = new \Elastica\Query\Term();
$queryLicence->setTerm('licenceId', $licence->getId() );
$elasticaQuery->addMust($queryLicence);
// Recherche des résultats d'Elasticsearch et pagination avec un fantaPaginator
$contactsPaginator = $finder->getRepository(Contact::class)->findPaginated($elasticaQuery);
$contactsPaginator->setMaxPerPage($this->resultsPerPage);
$contactsPaginator->setCurrentPage($page);
// Définitions des variables pour twig
$nbResults = $contactsPaginator->getNbResults();
$contacts = $contactsPaginator->getCurrentPageResults();
$nbPages = $contactsPaginator->getNbPages();
$em->flush();
// Selon le nombre de résultats on fait un pager
$pager = array();
if ( $nbPages > 1 ) {
// Calcul de la première et de la dernière page du Pager sur une base de 5 pages
if ( $nbPages <= 5) {
$firstPage = 1;
$lastPage = $nbPages;
}
elseif ( $page <= 3 ) {
$firstPage = 1;
$lastPage = 5;
}
elseif ( $page >= $nbPages - 2 ) {
$firstPage = $nbPages - 4;
$lastPage = $nbPages;
}
else {
$firstPage = $page - 2;
$lastPage = $page + 2;
}
// Bouton previous
$pager[] = array(
'type' => 'previousButton',
'state' => ($page == 1) ? 'disabled' : '',
'page' => $page - 1,
);
// Bouton des pages
for ( $i = $firstPage; $i <= $lastPage ; $i++ )
$pager[] = array(
'type' => 'pageButton',
'state' => ($page == $i) ? 'active' : '',
'page' => $i,
);
// Bouton next
$pager[] = array(
'type' => 'nextButton',
'state' => ($page == $nbPages) ? 'disabled' : '',
'page' => $page + 1,
);
} // Fin de la création du pager
// Rendu
return $this->render('contact/contactsList.html.twig', [
'contacts' => $contacts,
'nbResults' => $nbResults,
'nbPages' => $nbPages,
'pager' => $pager,
'page' => $page,
'query' => $originalQuery,
'userId' => $userId,
'filtreLabel' => $filtreLabel,
]);
}
}