<?php

namespace BodyRep\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
use JMS\SecurityExtraBundle\Annotation\Secure;

# Annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ProfileController extends Controller
{
    /**
     * @Route("/", name="_profile")
     * @Template()
     */
    public function indexAction($username)
    {

      if ($this->getUser()->getUsername() == $username)
        return new RedirectResponse($this->generateUrl('_member_profile'));

        $db = $this->getDoctrine()->getManager();

        $query = $db->createQuery('
                        SELECT p
                        FROM BodyRep:Profile p
                        WHERE p.username = :username')                
                    ->setParameter('username', $username)                     
                    ->setMaxResults(1);
         if (sizeof($query->getResult()) != 1)
           throw $this->createNotFoundException("User '".$username."' not found");

         $profile = $query->getSingleResult();
       $username = $this->getUser()->getUsername();
        $db = $this->getDoctrine()->getManager();
        $query = $db->createQuery('
                          SELECT m
                          FROM BodyRep:Member m
                          WHERE m.username = :username')                
                      ->setParameter('username', $username)                     
                      ->setMaxResults(1);

         if (sizeof($query->getResult()) != 1)
           throw $this->createNotFoundException("User '".$username."' not found");

         $member = $query->getSingleResult();

         return (array('sFullName' => $profile->getFullName(), 'name' => $member->getFullName()));

    }
    /**
     * @Route("/rep", name="_profile_reputation")
     * @Template()
     */
    public function reputationAction($username)
    {
      return $this->indexAction($username);
    }

  
}
