/**
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Symfony\Component\Form\Exception\AlreadyBoundException
  */
 public function addAction(Request $request)
 {
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $officer = new LoanOfficer();
     $officer->setSite($site);
     $siteType = $site->getSiteType();
     if (isset($siteType)) {
         $siteBranch = $site->getSettings()->getBranch();
         if ($siteType->getKeyName() == 'branch' && isset($siteBranch)) {
             $officer->setBranch($siteBranch);
         }
     }
     $form = $this->createForm(new LoanOfficerType($site), $officer);
     if ($request->getMethod() == 'POST') {
         $form->bind($request);
         if ($form->isValid()) {
             $user = $this->get('security.context')->getToken()->getUser();
             $photoData = $form['officer_photo_file']->getData();
             if (isset($photoData)) {
                 $photo = new File();
                 $photo->setName(sprintf('%s %s', $form['first_name']->getData(), $form['last_name']->getData()));
                 $photo->setUser($user);
                 $photo->setSite($site);
                 $photo->setFile($photoData);
                 $officer->setOfficerPhoto($photo);
             }
             $em->persist($officer);
             $em->flush($officer);
             $request->getSession()->getFlashBag()->add('success', 'Your loan officer has been created.');
             return $this->redirect($this->generateUrl('sudoux_mortgage_admin_loan_officer'));
         }
     }
     return $this->render('SudouxMortgageBundle:LoanOfficerAdmin:add.html.twig', array('form' => $form->createView(), 'officer' => $officer));
 }
 /**
  * @param $csvPath
  * @throws \Exception
  */
 protected function importLoanOfficersFromCsv($csvPath)
 {
     ini_set('auto_detect_line_endings', true);
     $siteId = $this->site->getId();
     $header = array('first_name', 'last_name', 'email', 'los_id', 'nmls_id', 'title', 'phone_office', 'phone_mobile', 'phone_tollfree', 'fax', 'signature', 'bio', 'branch_nmls_id');
     $headerValid = true;
     $batchCount = 10;
     if (file_exists($csvPath)) {
         if (($handle = fopen($csvPath, "r")) !== FALSE) {
             $row = 0;
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 if ($row == 0) {
                     // validate the header
                     for ($i = 0; $i < count($data); $i++) {
                         if ($data[$i] != $header[$i]) {
                             $headerValid = false;
                             break;
                         }
                     }
                     if (!$headerValid) {
                         throw new \Exception('Csv headers are not valid. Correct format is ' . implode(',', $header));
                     }
                 } else {
                     $loanOfficer = new LoanOfficer();
                     $loanOfficer->setFirstName($this->getCsvValue($data[0]));
                     $loanOfficer->setLastName($this->getCsvValue($data[1]));
                     $loanOfficer->setEmail($this->getCsvValue($data[2]));
                     $loanOfficer->setLosId($this->getCsvValue($data[3]));
                     $loanOfficer->setNmlsId($this->getCsvValue($data[4]));
                     $loanOfficer->setTitle($this->getCsvValue($data[5]));
                     $loanOfficer->setPhoneOffice($this->getCsvValue($data[6]));
                     $loanOfficer->setPhoneMobile($this->getCsvValue($data[7]));
                     $loanOfficer->setPhoneTollfree($this->getCsvValue($data[8]));
                     $loanOfficer->setFax($this->getCsvValue($data[9]));
                     $loanOfficer->setSignature($this->getCsvValue($data[10]));
                     $loanOfficer->setBio($this->getCsvValue($data[11]));
                     $loanOfficer->setSite($this->site);
                     $loanOfficer->setAutoCreateUser(false);
                     // this needs to be set after setSite to override default functionality
                     // lookup branch
                     $branchNmlsId = $this->getCsvValue($data[12]);
                     if (!empty($branchNmlsId)) {
                         $branch = $this->em->getRepository('SudouxMortgageBundle:Branch')->findOneBySiteAndNmlsId($this->site, $branchNmlsId);
                         if (isset($branch)) {
                             $loanOfficer->setBranch($branch);
                         }
                     }
                     $this->em->persist($loanOfficer);
                     if ($row % $batchCount == 0) {
                         $this->em->flush();
                         $this->em->clear();
                         $this->site = $this->em->getRepository('SudouxCmsSiteBundle:Site')->find($siteId);
                         $this->output->writeln(sprintf('%s rows processed', $row));
                     }
                 }
                 $row++;
             }
             exit;
             $this->em->flush();
             $this->em->clear();
             $this->output->writeln(sprintf('Processing complete! %s rows processed', $row));
         }
     }
 }