/**
  * @param Request $request
  * @param null $id
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Exception
  */
 public function applyStep1Action(Request $request, $id = null)
 {
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $securityContext = $this->container->get('security.context');
     $user = $securityContext->getToken()->getUser();
     $newApplication = true;
     if (isset($id)) {
         $application = $em->getRepository('SudouxMortgageBundle:LoanApplication')->findOneBySite($site, $id);
         if (!isset($application)) {
             throw $this->createNotFoundException($this::LOAN_NOT_FOUND_MESSAGE);
         }
         $newApplication = false;
     } else {
         $application = new LoanApplication();
         $siteLoanOfficer = $site->getSettings()->getLoanOfficer();
         if (isset($siteLoanOfficer)) {
             $application->setLoanOfficer($siteLoanOfficer);
         }
     }
     if ($application->getLockStatus() > 0) {
         $session = $request->getSession();
         $session->getFlashBag()->add('error', $this::LOAN_LOCKED_MESSAGE);
         return $this->redirect($this->generateUrl('sudoux_mortgage_admin_loan_member', array('id' => $id)));
     }
     $showReferralSources = false;
     $referralSourceCount = $em->getRepository('SudouxCmsFormBundle:ReferralSource')->findAllActiveBySiteCount($site);
     if ($referralSourceCount > 0) {
         $showReferralSources = true;
     }
     $form = $this->createForm(new LoanApplicationType($site, $application), $application, array('validation_groups' => array('step1')));
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         if ($form->isValid()) {
             try {
                 if ($newApplication) {
                     $application->setLastStepCompleted(1);
                     $application->setSite($site);
                     $application->setAdminUser($user);
                     // set the defaults because the constructor is not called on prototype forms
                     $coBorrowers = $application->getCoBorrower();
                     if (count($coBorrowers) > 0) {
                         foreach ($coBorrowers as $coBorrower) {
                             $coBorrower->setDefaults();
                         }
                     }
                 }
                 //echo 'Co-Borrowers: ' . count($application->getCoBorrower()); exit;
                 $em->persist($application);
                 $em->flush();
                 return $this->redirect($this->generateUrl('sudoux_mortgage_admin_loan_step2', array('id' => $application->getId())));
             } catch (\Exception $e) {
                 $logger = $this->get('logger');
                 $logger->crit($e->getMessage());
                 throw $e;
             }
         }
     }
     return $this->render('SudouxMortgageBundle:LoanApplicationAdmin:applyStep1.html.twig', array('form' => $form->createView(), 'application' => $application, 'showReferralSources' => $showReferralSources));
 }