<?php
namespace App\Controller\Website;
use App\Repository\SuluUsersRepository;
use Sulu\Bundle\SearchBundle\Controller\WebsiteSearchController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use App\Service\ApiSearchService;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class UserController extends WebsiteSearchController
{
private Environment $twig;
private SuluUsersRepository $sur;
private $apiSearchService;
private Security $security;
private UrlGeneratorInterface $urlGenerator;
private Manager $esManagerLive;
public function __construct(
Environment $twig,
SuluUsersRepository $sur,
ApiSearchService $apiSearchService,
Security $security,
UrlGeneratorInterface $urlGenerator,
Manager $esManagerLive
) {
$this->twig = $twig;
$this->sur = $sur;
$this->apiSearchService = $apiSearchService;
$this->security = $security;
$this->urlGenerator = $urlGenerator;
$this->esManagerLive = $esManagerLive;
}
/**
* Obtiens les informations d'un utilisateur par son id
*
* @param integer $id
* @return Response
*/
public function getUserByIdAction($id): Response
{
$user = $this->sur->findOneByContactId($id);
$actual_user = $this->security->getUser();
$events = $this->loadArticles($id, "agenda");
$articles = $this->loadArticles($id, "articles");
$aap = $this->loadArticles($id, "aapami");
$offres = $this->loadArticles($id, "offres");
$gt = $this->loadArticles($id, "groupe-travail");
if (is_null($actual_user)) {
$loginUrl = $this->urlGenerator->generate('sulu_community.login');
return new RedirectResponse($loginUrl);
}
if (empty($user) || !$user->getContact()->getVisibleAnnuaire()) {
//$loginUrl = $this->urlGenerator->generate('/acteurs-territoires/annuaire');
return new RedirectResponse('/acteurs-territoires/annuaire');
}
$response = new Response(
$this->twig->render(
'users/singleUser.html.twig',
array('user' => $user, 'events' => $events, 'articles' => $articles, 'aap' => $aap, 'offres' => $offres, "gt" => $gt)
)
);
return $response;
}
private function loadArticles($id, $structureType)
{
$repository = $this->getRepository();
$search = $repository->createSearch()
->addSort(new FieldSort('published', FieldSort::DESC))
->addQuery(new TermQuery('author_id', $id))
->addQuery(new TermQuery('structure_type', $structureType));
return $repository->findDocuments($search);
}
/**
* @return Repository
*/
private function getRepository()
{
return $this->esManagerLive->getRepository(ArticleViewDocument::class);
}
/**
* Va chercher tous les utilisateurs qui sont des utilisateurs visible sur l'annuaire
*
* @return Response
*/
public function getUserListAction(Request $request): Response
{
$query = $this->getRequestParameter($request, 'q', false, '');
$elems = $this->getRequestParameter($request, 'elems', false, '');
$territoires = $this->getRequestParameter($request, 'territoires', false, '');
$departement = $this->getRequestParameter($request, 'departement', false, '');
$thematiques = $this->getRequestParameter($request, 'theme', false, '');
$typologieActeur = $this->getRequestParameter($request, 'typologieActeur', false, '');
$order = $this->getRequestParameter($request, 'order', false, '');
$step = $this->getRequestParameter($request, 'step', false, '');
/* $elems = $this->getRequestParameter($request, 'elems', false, ''); */
$page = $this->getRequestParameter($request, 'page', false, '');
$page = ($page ? $page : 1);
$offset = 0;
$request = $this->sur->getUserList($query, $territoires, $departement, $thematiques, $typologieActeur, $order, $step);
$nbPages = $this->apiSearchService->nbPages($elems, count($request));
if ($page == 1) {
array_splice($request, $elems);
} elseif ($page != null && intval($page) > 1) {
$offset = $elems * ($page - 1);
array_splice($request, 0, $offset);
}
$response = new Response($this->twig->render(
'users/userList.html.twig',
array(
'request' => $request,
'pageCount' => $nbPages,
'currentPage' => $page,
)
));
return $response;
}
}