src/Controller/Website/UserController.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website;
  3. use App\Repository\SuluUsersRepository;
  4. use Sulu\Bundle\SearchBundle\Controller\WebsiteSearchController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Twig\Environment;
  8. use App\Service\ApiSearchService;
  9. use ONGR\ElasticsearchBundle\Service\Manager;
  10. use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
  11. use ONGR\ElasticsearchDSL\Sort\FieldSort;
  12. use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. class UserController extends WebsiteSearchController
  17. {
  18.     private Environment $twig;
  19.     private SuluUsersRepository $sur;
  20.     private $apiSearchService;
  21.     private Security $security;
  22.     private UrlGeneratorInterface $urlGenerator;
  23.     private Manager $esManagerLive;
  24.     public function __construct(
  25.         Environment $twig,
  26.         SuluUsersRepository $sur,
  27.         ApiSearchService $apiSearchService,
  28.         Security $security,
  29.         UrlGeneratorInterface $urlGenerator,
  30.         Manager $esManagerLive
  31.     ) {
  32.         $this->twig $twig;
  33.         $this->sur $sur;
  34.         $this->apiSearchService $apiSearchService;
  35.         $this->security $security;
  36.         $this->urlGenerator $urlGenerator;
  37.         $this->esManagerLive $esManagerLive;
  38.     }
  39.     /**
  40.      * Obtiens les informations d'un utilisateur par son id
  41.      *
  42.      * @param integer $id
  43.      * @return Response 
  44.      */
  45.     public function getUserByIdAction($id): Response
  46.     {
  47.         $user $this->sur->findOneByContactId($id);
  48.         $actual_user =  $this->security->getUser();
  49.         $events $this->loadArticles($id"agenda");
  50.         $articles $this->loadArticles($id"articles");
  51.         $aap $this->loadArticles($id"aapami");
  52.         $offres $this->loadArticles($id"offres");
  53.         $gt $this->loadArticles($id"groupe-travail");
  54.         if (is_null($actual_user)) {
  55.             $loginUrl $this->urlGenerator->generate('sulu_community.login');
  56.             return new RedirectResponse($loginUrl);
  57.         }
  58.         if (empty($user) || !$user->getContact()->getVisibleAnnuaire()) {
  59.             //$loginUrl = $this->urlGenerator->generate('/acteurs-territoires/annuaire');
  60.             return new RedirectResponse('/acteurs-territoires/annuaire');
  61.         }
  62.         $response = new Response(
  63.             $this->twig->render(
  64.                 'users/singleUser.html.twig',
  65.                 array('user' => $user'events' => $events'articles' => $articles'aap' => $aap'offres' => $offres"gt" => $gt)
  66.             )
  67.         );
  68.         return $response;
  69.     }
  70.     private function loadArticles($id$structureType)
  71.     {
  72.         $repository $this->getRepository();
  73.         $search $repository->createSearch()
  74.             ->addSort(new FieldSort('published'FieldSort::DESC))
  75.             ->addQuery(new TermQuery('author_id'$id))
  76.             ->addQuery(new TermQuery('structure_type'$structureType));
  77.         return $repository->findDocuments($search);
  78.     }
  79.     /**
  80.      * @return Repository
  81.      */
  82.     private function getRepository()
  83.     {
  84.         return $this->esManagerLive->getRepository(ArticleViewDocument::class);
  85.     }
  86.     /**
  87.      * Va chercher tous les utilisateurs qui sont des utilisateurs visible sur l'annuaire
  88.      *
  89.      * @return Response 
  90.      */
  91.     public function getUserListAction(Request $request): Response
  92.     {
  93.         $query $this->getRequestParameter($request'q'false'');
  94.         $elems $this->getRequestParameter($request'elems'false'');
  95.         $territoires $this->getRequestParameter($request'territoires'false'');
  96.         $departement $this->getRequestParameter($request'departement'false'');
  97.         $thematiques $this->getRequestParameter($request'theme'false'');
  98.         $typologieActeur $this->getRequestParameter($request'typologieActeur'false'');
  99.         $order $this->getRequestParameter($request'order'false'');
  100.         $step $this->getRequestParameter($request'step'false'');
  101.         /*  $elems = $this->getRequestParameter($request, 'elems', false, ''); */
  102.         $page $this->getRequestParameter($request'page'false'');
  103.         $page = ($page $page 1);
  104.         $offset 0;
  105.         $request $this->sur->getUserList($query$territoires$departement$thematiques$typologieActeur$order$step);
  106.         $nbPages $this->apiSearchService->nbPages($elemscount($request));
  107.         if ($page == 1) {
  108.             array_splice($request$elems);
  109.         } elseif ($page != null && intval($page) > 1) {
  110.             $offset $elems * ($page 1);
  111.             array_splice($request0$offset);
  112.         }
  113.         $response = new Response($this->twig->render(
  114.             'users/userList.html.twig',
  115.             array(
  116.                 'request' => $request,
  117.                 'pageCount' => $nbPages,
  118.                 'currentPage' => $page,
  119.             )
  120.         ));
  121.         return $response;
  122.     }
  123. }