<commit_before><?php

namespace MIW\IntranetBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use MIW\DataAccessBundle\Document\Sport;
use MIW\IntranetBundle\Form\Type\SportType;

class SportController extends Controller
{
    
    /**
        * @Route("/ajax/find/sport/",name="intranet_ajax_find_sport")
     */
    public function ajaxSearchSportAction(Request $request)
    {
        $value = $request->get('sport');
    
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $sport = $dm->getRepository('MIWDataAccessBundle:Sport')->find($value);

        $serializer = $this->get('jms_serializer');
        $data=$serializer->serialize($sport, 'json'); 
        
        $response = new Response();
        $response->setContent($data);

        return $response;
    }
    
    public function createSportAction() {
        $form = $this->createForm(new SportType());
        return $this->container->get('templating')->renderResponse('MIWIntranetBundle:Sport:addSport.html.twig', 
                array('form' => $form->createView()));
    }
    
    /**
        * @Route("/ajax/subscribe/sport/",name="intranet_ajax_subscribe_sport")
     */
    public function subscribeSportAction(Request $request) {
        $mySport = $request->get('sport');
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        //hacer un concat
        //$sports = $user->getSports();
        //$result = array_merge($sports, array($mySport=>array('level'=>$myLevel)));
        $user->setSports(array($mySport=>array('level'=>$myLevel)));
        $dm->persist($user);
        $dm->flush();   
    }
    
    public function modifySportAction() {
        $form = $this->createForm(new SportType());
        return $this->container->get('templating')->renderResponse('MIWIntranetBundle:Sport:editSport.html.twig', 
                array('form' => $form->createView()));
    }
    
    /**
        * @Route("/ajax/edit/sport/",name="intranet_ajax_edit_sport")
     */
    public function editSportAction(Request $request) {
        $mySport = $request->get('sport');
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();
        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');                
        $user->setSports(array($mySport->getId()=>array('level'=>$myLevel)));
        $dm->persist($user);
        $dm->flush();                
    }
    
    /**
        * @Route("/ajax/delete/sport/",name="intranet_ajax_delete_sport")
     */
    public function deleteSportAction(Request $request) {
        $mySport = $request->get('sport');
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();
        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');                
        $user->setSports(array($mySport->getId()=>array('level'=>$myLevel)));
        $dm->persist($user);
        $dm->flush();                
    }
    
}<commit_msg>Modificado los ajax de añadir y borrar deportes<commit_after><?php

namespace MIW\IntranetBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use MIW\DataAccessBundle\Document\Sport;
use MIW\IntranetBundle\Form\Type\SportType;

class SportController extends Controller
{
    
    /**
        * @Route("/ajax/find/sport/",name="intranet_ajax_find_sport")
     */
    public function ajaxSearchSportAction(Request $request)
    {
        $value = $request->get('sport');
    
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $sport = $dm->getRepository('MIWDataAccessBundle:Sport')->find($value);

        $serializer = $this->get('jms_serializer');
        $data=$serializer->serialize($sport, 'json'); 
        
        $response = new Response();
        $response->setContent($data);

        return $response;
    }
    
    public function createSportAction() {
        $form = $this->createForm(new SportType());
        return $this->container->get('templating')->renderResponse('MIWIntranetBundle:Sport:addSport.html.twig', 
                array('form' => $form->createView()));
    }
    
    /**
        * @Route("/ajax/subscribe/sport/",name="intranet_ajax_subscribe_sport")
     */
    public function ajaxsubscribeSportAction(Request $request) {
        $idSport = $request->get('sport');
        var_dump($idSport);
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        //no funciona
        $sports = $user->getSports();
        echo " deportes: ".$sports;
        $newSport = array($idSport=>array('level'=>$myLevel));
        echo " nuevo: ".$newSport;
        $result = array_push($sports, $newSport);
        echo " final: ".$sports;
        $user->setSports($sports);
        $dm->persist($user);
        $dm->flush();   
    }
    
    public function modifySportAction(Request $request) {
        $idSport = $request->get('idSport');
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $sport = $dm->getRepository('MIWDataAccessBundle:Sport')->find($idSport);
        $name = $sport->getName();//no funciona
                
        $user = $this->get('security.context')->getToken()->getUser();
        $mySports = $user->getSports();        
        foreach ($mySports as $obj_key => $array) {
            if($obj_key == $idSport) {
                foreach ($array as $key => $value) {
                    switch ($key) {
                        case "level":
                            $level = $value;
                            break;
                        case "position":
                            $position = $value;
                            break;
                    }
                }
            }
        }
                
        return $this->container->get('templating')->renderResponse('MIWIntranetBundle:Sport:editSport.html.twig', 
                array('form' => $form->createView(), 'idSport' => $idSport, 'name' => $name, 'level' => $level, 'position' => $position));
    }
    
    /**
        * @Route("/ajax/edit/sport/",name="intranet_ajax_edit_sport")
     */
    public function ajaxeditSportAction(Request $request) {
        $mySport = $request->get('sport');
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $sports = $user->getSports();
        $newSport = array($mySport=>array('level'=>$myLevel));
        $result = array_push($sports, $newSport);
        $user->setSports($sports);
        $dm->persist($user);
        $dm->flush();          
    }
    
    /**
        * @Route("/ajax/delete/sport/",name="intranet_ajax_delete_sport")
     */
    public function ajaxdeleteSportAction(Request $request) {
        $mySport = $request->get('sport');
        $myLevel = $request->get('level');
        $user = $this->get('security.context')->getToken()->getUser();        
        $dm = $this->get('doctrine.odm.mongodb.document_manager');
        $sports = $user->getSports();
        $newSport = array($mySport=>array('level'=>$myLevel));
        $result = array_push($sports, $newSport);
        $user->setSports($sports);
        $dm->persist($user);
        $dm->flush();      
    }
    
}
{'fraction_non_alphanumeric': 0.1708411214953271, 'fraction_numerical': 0.0, 'mean_word_length': 2.5341259357111405, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 129, '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': '6075796', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2364, 'n_words': 311}