<?php
/*
 * Developer : John D'Alto (jdalto@yahoo.com)
 * Date : 9/1/2012
 * All code (c)2012 JLS Consulting, all rights reserved
 */

namespace Dorcas\TabithaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Dorcas\TabithaBundle\Entity\Messages;

class MessageController extends Controller
{
    
    /**
     * Gets the newest record from the Messages table and passes it to Twig
     */
        public function showAction()
    {
        // Get newest record         
         $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery(
            'SELECT m FROM DorcasTabithaBundle:Messages m ORDER BY m.created_dt DESC');
        $query->setFirstResult('0');
        $query->setMaxResults('1');  
        

        if(!$query->getResult())
        {
            $message = array ('message' => 'Welcome the the Tabitha Manager Message.');
        }
        else
        {
            // Return a single record - important
            $message = $query->getSingleResult();
         }   
        return $this->render('DorcasTabithaBundle:Message:messageform.html.twig', array(
                'message' => $message
            ));
    }
    

     /**
     * Saves the Manager's Message to a new record in the Messages table
     */
    public function createAction()
    {
        $message = new Messages();
        
        // Get user from session
        $user = $this->get('security.context')->getToken()->getUser();
        
        $request = $this->getRequest();
        
        // There is no form, so get user data from the Request
        $messageMessage = $request->request->get('ManagersMessage');
        
        $message->setMessage($messageMessage);
        $message->setUser($user->getUsername());
        
        // Get navigation selection from the Request
        $navnumber = $request->request->get("passnav");

            $em = $this->getDoctrine()
                       ->getEntityManager();
            $em->persist($message);
            $em->flush();
            
             switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new'));
                            break; 
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_search_show'));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        case "8":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_message_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_message_show', array(
                        'message' => $message)));
                    }    

        // Should never get here - but leaving it in for now....
        return $this->render('DorcasTabithaBundle:Message:messageform.html.twig', array(
                'message' => $message
        ));
    }
    
}
