<commit_before><?php

namespace Rooty\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Doctrine\ORM\Query\ResultSetMapping;
use Rooty\UserBundle\Form\Type\QuickAdminFormType;

/**
* Users controller
*
* @Route("/users")
*/
class UserController extends Controller
{
    /**
    * Lists all User entities
    * @Route("/", name="users") 
    * @Template()
    * @Secure(roles="ROLE_USER")
    */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('RootyUserBundle:User')->findAll();
        
        return array(
            'entity' => $entity,
        );
    }
    
    /**
     *
     * @Route("/{id}/adminUpdate", name="user_admin_update")
     */
    public function adminUpdateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('RootyUserBundle:User')->find($id);
        $user = $this->get('security.context')->getToken()->getUser();
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity');
        }
        
        if (!($user->hasRole('ROLE_ADMIN'))) {
            throw new AccessDeniedException();
        }
        
        $form = $this->createForm(new QuickAdminFormType(), $entity);
        $request = $this->getRequest();
        $form->bindRequest($request);
        
        if ($form->isValid()) {
            $em->flush();
            
            $this->get('session')->setFlash('notice', 'Ваши изменения успешно сохранены!');
            
            return $this->redirect($this->generateUrl('user_show', array('id' => $id)));
        }
    }
    
    /**
    * Finds and displays a User entity
    * @Route("/{id}/show", name="user_show") 
    * @Template()
    * @Secure(roles="ROLE_USER")
    */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $user = $em->getRepository('RootyUserBundle:User')->findOneById($id);
        
        if (!$user) {
            throw $this->createNotFoundException('Пользователь не найден.');
        }
        
        $torrentsUploaded = $em->getRepository('RootyTorrentBundle:Torrent')->findBy(array('added_by' => $id));
        //$torrentsDownloaded = $em->getRepository('RootyTorrentBundle:TorrentUserStats')->findBy(array('user' => $id, 'finished' => 'yes'));
        
        /* Downloaded = finished, but not uploaded */
        $rsm = new ResultSetMapping;
        $rsm->addScalarResult('type', 'type');
        $rsm->addScalarResult('title', 'title');
        
        $sql = 'SELECT Type.title AS type, t.title AS title FROM user_torrents AS ut JOIN torrents AS t ON t.id = ut.torrent_id JOIN Type ON Type.id = t.type_id WHERE ut.user_id = :user_id AND ut.finished = 1 AND t.added_by_id != ut.user_id';
        $query = $em->createNativeQuery($sql, $rsm);
        $query->setParameter('user_id', $user->getId());
        $torrentsDownloaded = $query->getScalarResult();
      
        
        $query = $em->createQuery('SELECT COUNT(c.id) FROM Rooty\CommentBundle\Entity\Comment c');
        $commentCount = $query->getSingleScalarResult();
        
        $quickAdminForm = $this->createForm(new QuickAdminFormType(), $user);
        
        return array(
            'user' => $user,
            'comment_count' => $commentCount,
            'torrents_uploaded' => $torrentsUploaded,
            'torrents_downloaded' => $torrentsDownloaded,
            'admin_form' => $quickAdminForm->createView(),
        );
    }
}<commit_msg>Fix SQL error on user show page<commit_after><?php

namespace Rooty\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Doctrine\ORM\Query\ResultSetMapping;
use Rooty\UserBundle\Form\Type\QuickAdminFormType;

/**
* Users controller
*
* @Route("/users")
*/
class UserController extends Controller
{
    /**
    * Lists all User entities
    * @Route("/", name="users") 
    * @Template()
    * @Secure(roles="ROLE_USER")
    */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('RootyUserBundle:User')->findAll();
        
        return array(
            'entity' => $entity,
        );
    }
    
    /**
     *
     * @Route("/{id}/adminUpdate", name="user_admin_update")
     */
    public function adminUpdateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('RootyUserBundle:User')->find($id);
        $user = $this->get('security.context')->getToken()->getUser();
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity');
        }
        
        if (!($user->hasRole('ROLE_ADMIN'))) {
            throw new AccessDeniedException();
        }
        
        $form = $this->createForm(new QuickAdminFormType(), $entity);
        $request = $this->getRequest();
        $form->bindRequest($request);
        
        if ($form->isValid()) {
            $em->flush();
            
            $this->get('session')->setFlash('notice', 'Ваши изменения успешно сохранены!');
            
            return $this->redirect($this->generateUrl('user_show', array('id' => $id)));
        }
    }
    
    /**
    * Finds and displays a User entity
    * @Route("/{id}/show", name="user_show") 
    * @Template()
    * @Secure(roles="ROLE_USER")
    */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $user = $em->getRepository('RootyUserBundle:User')->findOneById($id);
        
        if (!$user) {
            throw $this->createNotFoundException('Пользователь не найден.');
        }
        
        $torrentsUploaded = $em->getRepository('RootyTorrentBundle:Torrent')->findBy(array('added_by' => $id));
        //$torrentsDownloaded = $em->getRepository('RootyTorrentBundle:TorrentUserStats')->findBy(array('user' => $id, 'finished' => 'yes'));
        
        /* Downloaded = finished, but not uploaded */
        $rsm = new ResultSetMapping;
        $rsm->addScalarResult('type', 'type');
        $rsm->addScalarResult('title', 'title');
        
        $sql = 'SELECT Type.title AS type, t.title AS title FROM user_torrents AS ut JOIN torrents AS t ON t.id = ut.torrent_id JOIN torrent_types AS Type ON Type.id = t.type_id WHERE ut.user_id = :user_id AND ut.finished = 1 AND t.added_by_id != ut.user_id';
        $query = $em->createNativeQuery($sql, $rsm);
        $query->setParameter('user_id', $user->getId());
        $torrentsDownloaded = $query->getScalarResult();
      
        
        $query = $em->createQuery('SELECT COUNT(c.id) FROM Rooty\CommentBundle\Entity\Comment c');
        $commentCount = $query->getSingleScalarResult();
        
        $quickAdminForm = $this->createForm(new QuickAdminFormType(), $user);
        
        return array(
            'user' => $user,
            'comment_count' => $commentCount,
            'torrents_uploaded' => $torrentsUploaded,
            'torrents_downloaded' => $torrentsDownloaded,
            'admin_form' => $quickAdminForm->createView(),
        );
    }
}
{'fraction_non_alphanumeric': 0.1563272207049031, 'fraction_numerical': 0.00026011184809468074, 'mean_word_length': 2.6359338061465722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6074670', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2297, 'n_words': 394}