<commit_before><?php

namespace TBN\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TBN\MainBundle\Controller\TBNController as Controller;
use TBN\UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class UserController extends Controller
{

    public function urlRedirectAction($term)
    {
        $params = [
            "type" => "membres"
        ];

        if ($term) {
            $params["q"] = $term;
        }

        return new RedirectResponse($this->get("router")->generate("tbn_search_query", $params));
    }

    protected function checkUserUrl($slug, $username, $id, $routeName, array $extraParams = []) {
        $em = $this->getDoctrine()->getManager();
        $repoUser = $em->getRepository('TBNUserBundle:User');

        if(! $id) {
            $user = $repoUser->findOneBy(['username' => $username]);
        }else {
            $user = $repoUser->find($id);
        }

        if(! $user || ! $user->getSlug()) {
            throw new NotFoundHttpException('User not found');
        }

        if($user->getSlug() !== $slug) {
            $routeParams = array_merge(['id' => $user->getId(), 'slug' => $user->getSlug()], $extraParams);
            return new RedirectResponse($this->generateUrl($routeName, $routeParams));
        }

        return $user;
    }

    public function detailsAction($id = null, $slug = null, $username = null)
    {
        $result = $this->checkUserUrl($slug, $username, $id, 'tbn_user_details');
        if($result instanceof Response) {
            return $result;
        }
        $user = $result;

        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository("TBNAgendaBundle:Agenda");

        $siteManager = $this->get('site_manager');
        $currentSite = $siteManager->getCurrentSite();
        if ($currentSite && $user->getSite() !== $currentSite) {
            return new RedirectResponse($this->generateUrl('tbn_user_details', [
                'username' => $user->getUsername(),
                'subdomain' => $user->getSite()->getSubdomain()
            ]));
        }

        return $this->render('TBNUserBundle:Membres:details.html.twig', [
            "user" => $user,
            "next_events" => $repo->findAllNextEvents($user),
            "previous_events" => $repo->findAllNextEvents($user, false),
            "etablissements" => $repo->findAllPlaces($user),
            "count_participations" => $repo->getCountParticipations($user),
            "count_interets" => $repo->getCountInterets($user),
        ]);
    }

    public function statsAction(Request $request, $type, $id = null, $slug = null, $username = null)
    {
        $result = $this->checkUserUrl($slug, $username, $id, 'tbn_user_stats', ['type' => $type]);
        if($result instanceof Response) {
            return $result;
        }
        $user = $result;

        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository("TBNAgendaBundle:Agenda");
        $str_date = $repo->getLastDateStatsUser($user);

        $response = $this->cacheVerif($str_date);
        if ($response !== null) {<commit_msg>Fix id manquant dans routing user<commit_after><?php

namespace TBN\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TBN\MainBundle\Controller\TBNController as Controller;
use TBN\UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class UserController extends Controller
{

    public function urlRedirectAction($term)
    {
        $params = [
            "type" => "membres"
        ];

        if ($term) {
            $params["q"] = $term;
        }

        return new RedirectResponse($this->get("router")->generate("tbn_search_query", $params));
    }

    protected function checkUserUrl($slug, $username, $id, $routeName, array $extraParams = []) {
        $em = $this->getDoctrine()->getManager();
        $repoUser = $em->getRepository('TBNUserBundle:User');

        if(! $id) {
            $user = $repoUser->findOneBy(['username' => $username]);
        }else {
            $user = $repoUser->find($id);
        }

        if(! $user || ! $user->getSlug()) {
            throw new NotFoundHttpException('User not found');
        }

        if($user->getSlug() !== $slug) {
            $routeParams = array_merge(['id' => $user->getId(), 'slug' => $user->getSlug()], $extraParams);
            return new RedirectResponse($this->generateUrl($routeName, $routeParams));
        }

        return $user;
    }

    public function detailsAction($id = null, $slug = null, $username = null)
    {
        $result = $this->checkUserUrl($slug, $username, $id, 'tbn_user_details');
        if($result instanceof Response) {
            return $result;
        }
        $user = $result;

        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository("TBNAgendaBundle:Agenda");

        $siteManager = $this->get('site_manager');
        $currentSite = $siteManager->getCurrentSite();
        if ($currentSite && $user->getSite() !== $currentSite) {
            return new RedirectResponse($this->generateUrl('tbn_user_details', [
                'username' => $user->getUsername(),
                'id' => $user->getId(),
                'subdomain' => $user->getSite()->getSubdomain()
            ]));
        }

        return $this->render('TBNUserBundle:Membres:details.html.twig', [
            "user" => $user,
            "next_events" => $repo->findAllNextEvents($user),
            "previous_events" => $repo->findAllNextEvents($user, false),
            "etablissements" => $repo->findAllPlaces($user),
            "count_participations" => $repo->getCountParticipations($user),
            "count_interets" => $repo->getCountInterets($user),
        ]);
    }

    public function statsAction(Request $request, $type, $id = null, $slug = null, $username = null)
    {
        $result = $this->checkUserUrl($slug, $username, $id, 'tbn_user_stats', ['type' => $type]);
        if($result instanceof Response) {
            return $result;
        }
        $user = $result;

        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository("TBNAgendaBundle:Agenda");
        $str_date = $repo->getLastDateStatsUser($user);

        $response = $this->cacheVerif($str_date);
        if ($response !== null) {
{'fraction_non_alphanumeric': 0.16129994036970782, 'fraction_numerical': 0.0, 'mean_word_length': 2.727222222222222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883602', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1944, 'n_words': 345}