<?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 Dorcas\TabithaBundle\Entity\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Dorcas\TabithaBundle\Form\Type\ReferralsType;
use Dorcas\TabithaBundle\Entity\Referrals;


class ReferralsController extends Controller
{

    protected function getClient($client_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();

        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);
        return $client;
    }
    
     protected function getReferrals($client_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();
        // Gets a record with client_id = $client_id
        $referrals = $em->getRepository('DorcasTabithaBundle:Referrals')->findOneBy(array('client_id' => $client_id));

        return $referrals;
    }
    
    
        public function newAction($client_id)
    {
        $client = $this->getClient($client_id);
        if (!$client) {
            throw $this->createNotFoundException('Dorcas: Unable to find matching client_id = ' . $client_id);
        }
        
        // Use to check for an existing one-to-one client_id in the Referrals table
        $referrals = $this->getReferrals($client_id);
        if ($referrals) {
            //throw $this->createNotFoundException('Dorcas: Referral record already exists, client_id = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_show', array(
                'client_id' => $client_id))
            );
        }

        $referrals = new Referrals();
        $referrals->setClient($client);
		
		// Check emaillist checkbox
		$referrals->setSpecialprogramsEmaillist(true);
		
		
        $form   = $this->createForm(new ReferralsType(), $referrals);

        return $this->render('DorcasTabithaBundle:Referrals:referralsformnew.html.twig', array(
            'client'      => $client,
            'referrals' => $referrals,
            'form'   => $form->createView()
        ));
    }
  
    
    /**
     * Show a referral entry
     */
    public function showAction($client_id)
    {
        $client = $this->getClient($client_id);
        if (!$client) {
            //throw $this->createNotFoundException('Dorcas: Unable to find matching client = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new'));
        }
        
        $referrals = $this->getReferrals($client_id);
            if (!$referrals) {
            //throw $this->createNotFoundException('Dorcas: No finance record exists, client_id = ' . $client_id);
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_new', array(
                'client_id' => $client_id))
            );
        }
        
		// Check emaillist checkbox
		$referrals->setSpecialprogramsEmaillist(true);
		
        $form = $this->createForm(new ReferralsType(), $referrals);

    return $this->render('DorcasTabithaBundle:Referrals:referralsformupdate.html.twig', array(
        'client'      => $client,
        'referrals'  => $referrals,
        'form' => $form->createView(),
        ));
    }
    
        public function createAction($client_id)
    {
   
        $client = $this->getClient($client_id);
        if (!$client) {
        throw $this->createNotFoundException('Dorcas: Unable to find matching client_id = ' . $client_id);
        }

        $referrals  = new Referrals();
        $referrals->setClient($client);
        $request = $this->getRequest();
        $form    = $this->createForm(new ReferralsType(), $referrals);
        $form->bindRequest($request);
        
        $navnumber = $request->request->get("passnav");
      
        if ($form->isValid()) {          
            // Check for existing referral - duplicate defense
             $referralsCheck = $this->getReferrals($client_id);
            if ($referralsCheck == null) {        
                $em = $this->getDoctrine()
                        ->getEntityManager();
                $em->persist($referrals);
                $em->flush();           
            }
       
            switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                            break;                  
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_new', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 3:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 4:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "5":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_new', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                    }    
            
                    /*
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new') 
            ); */
        }

        return $this->render('DorcasTabithaBundle:Referrals:referralsformnew.html.twig', array(
            'client'      => $client,
            'referrals' => $referrals,
            'form'   => $form->createView()
        ));
    }
    
    public function updateAction($client_id)
        {
        $request = $this->get('request');

        // don't know what this is doing???
        if (is_null($client_id)) {
            $postData = $request->get('testimonial');
            $client_id = $postData['client_id'];
        }
        // Using this to check for existing client_id
        $client = $this->getClient($client_id);
        if (!$client) {
        throw $this->createNotFoundException('Dorcas: Unable to find matching client_id = ' . $client_id);
        }
        
        $referrals = $this->getReferrals($client_id); 
        $form = $this->createForm(new ReferralsType(), $referrals);
        
        $navnumber = $request->request->get("passnav");

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            if ($form->isValid()) {
                // save the object to the database
                $em = $this->getDoctrine()
                        ->getEntityManager();
                $em->flush();

                switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                            break;                  
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 3:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 4:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "5":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                    } 
                
                    /*
                return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new')
                ); */
            }
        }

        return $this->render('DorcasTabithaBundle:Referrals:referralsformupdate.html.twig', array(
        'client'      => $client,
        'referrals'  => $referrals,
        'form' => $form->createView(),
        ));
    }
    
}
