/**
  * Save a record to the database and return the id on success
  * @param $data array
  * @return
  * @author John Burrin
  * @since
  */
 public function save($agencyData)
 {
     //Zend_Debug::dump($agencyData);die();
     $agent = new LettingAgents_Datasource_AgentApplication();
     $this->_index = $agent->save($agencyData);
     // Save the general email address
     $objEmail = new LettingAgents_Object_AgencyEmail();
     $dsEmail = new LettingAgents_Datasource_Email();
     $objEmail->setAddress_type(LettingAgents_Object_EmailTypes::General);
     $objEmail->setAgency_id($this->_index);
     $objEmail->setEmail_address($agencyData->get_contact_email());
     $dsEmail->deleteById($this->_index, LettingAgents_Object_EmailTypes::General);
     $dsEmail->save($objEmail);
     return $this->_index;
 }
 /**
  * Initialise the step 3 form
  *
  * @return void
  */
 public function step3Action()
 {
     // Append the address lookup javascript
     $this->view->headScript()->appendScript('var ajaxValidate = true; var ajaxValidatePage = 3;');
     $this->view->headScript()->appendFile('/assets/cms/js/letting-agents/addressLookup.js', 'text/javascript');
     $pageForm = new LettingAgents_Form_Step3();
     $pageSession = new Zend_Session_Namespace('letting_agents_application');
     // Setup an agency object
     $agent = new LettingAgents_Manager_AgentApplication();
     $agentData = new LettingAgents_Object_AgentApplication();
     $agentData = $agent->fetchByUid($pageSession->agentUniqueId);
     if ($this->getRequest()->isPost()) {
         // We need to validate and save the data
         // TODO: Need to re-factor this to make it quicker
         $valid = $this->_formStepCommonValidate($pageForm, 3);
         if ($valid) {
             $postData = $pageForm->getValues();
             //Insert Remaining data for the agency application
             //Zend_Debug::dump($postData);die();
             $agentData->set_current_referencing_supplier($postData['subform_companydetail']['current_referencing_supplier']);
             $agentData->set_number_of_branches($postData['subform_companydetail']['no_of_branches']);
             $agentData->set_number_of_employees($postData['subform_companydetail']['no_of_staff']);
             $agentData->set_number_of_lets($postData['subform_companydetail']['no_of_properties_managed']);
             $agentData->set_number_of_landlords($postData['subform_companydetail']['no_of_landlords']);
             $agentData->set_fax_number($postData['subform_companyfax']['fax_number']);
             $agentData->set_company_website_address($postData['subform_companyfax']['company_website_address']);
             $agentData->set_contact_email($postData['subform_multipleemails']['general_email_address']);
             $agent->save($agentData);
             // Multiple email stuff
             // Delete all the currect email for this agency
             $emails = new LettingAgents_Datasource_Email();
             $emails->deleteById($agentData->get_id());
             foreach ($postData['subform_multipleemails'] as $address_type => $email_address) {
                 $saveData = new LettingAgents_Object_AgencyEmail();
                 $saveData->setAddress_type($address_type);
                 $saveData->setAgency_id($agentData->get_id());
                 $saveData->setEmail_address($email_address);
                 $emails->save($saveData);
             }
             //die();
             // Insert the office data in to the agency_office table
             // First the Trading Address
             $officeManager = new LettingAgents_Manager_Office();
             $data = new LettingAgents_Object_AgencyOffice();
             $data->set_uid($postData['subform_tradingaddress']['trading_uid']);
             $data->set_address_1($postData['subform_tradingaddress']['trading_address_line1']);
             $data->set_address_2($postData['subform_tradingaddress']['trading_address_line2']);
             $data->set_address_3($postData['subform_tradingaddress']['trading_address_line3']);
             $data->set_postcode($postData['subform_tradingaddress']['trading_address_postcode']);
             $data->set_agency_id($agentData->get_id());
             $data->set_office_type(LettingAgents_Object_OfficeTypes::BranchOffice);
             $officeManager->save($data);
             // Reset the data Object
             $data = new LettingAgents_Object_AgencyOffice();
             // Insert the Accounts Office address
             $data->set_uid($postData['subform_accountsaddress']['accounts_uid']);
             $data->set_address_1($postData['subform_accountsaddress']['accounts_address_line1']);
             $data->set_address_2($postData['subform_accountsaddress']['accounts_address_line2']);
             $data->set_address_3($postData['subform_accountsaddress']['accounts_address_line3']);
             $data->set_postcode($postData['subform_accountsaddress']['accounts_address_postcode']);
             $data->set_agency_id($agentData->get_id());
             $data->set_office_type(LettingAgents_Object_OfficeTypes::HeadOffice);
             $officeManager->save($data);
             // Now the Head Office
             $this->_formStepCommonNavigate(3);
             return;
         } elseif (isset($_POST['back'])) {
             $this->_formStepCommonNavigate(3);
             return;
         }
     }
     // Load the element data from the database if we can
     if ($this->_formStepCommonPopulate($pageForm, 3)) {
         // Render the page unless we have been redirected
         $this->view->form = $pageForm;
         $this->render('step');
     }
 }