Пример #1
0
 /**
  * Validate a form page's values via AJAX
  *
  * @return void
  */
 public function validatePageAction()
 {
     $return = array();
     $pageSession = new Zend_Session_Namespace('landlords_insurance_quote');
     $postData = $this->getRequest()->getParams();
     $page = $postData['page'];
     if ($page == '') {
         return;
     }
     switch ($page) {
         case '1':
             $ajaxForm = new LandlordsInsuranceQuote_Form_Step1();
             if (isset($postData['through_letting_agent']) && $postData['through_letting_agent'] == 'yes') {
                 $subForm->getElement('letting_agent_name')->setRequired(true);
                 $subForm->getElement('letting_agent_town')->setRequired(true);
                 $subForm->getElement('letting_agent_asn')->setRequired(true);
             }
             break;
         case '2':
             $ajaxForm = new LandlordsInsuranceQuote_Form_Step2();
             $pageSession = new Zend_Session_Namespace('landlords_insurance_quote');
             if (isset($pageSession->quoteID)) {
                 $quoteID = $pageSession->quoteID;
                 $quoteManager = new Manager_Insurance_LandlordsPlus_Quote($quoteID);
                 $premiums = $quoteManager->calculatePremiums();
                 // Filter to mirror the subform element definitions.
                 $currencyFilterElements = array('building_value', 'contents_amount');
                 foreach ($currencyFilterElements as $filterElement) {
                     if (isset($postData[$filterElement])) {
                         $postData[$filterElement] = preg_replace(array('/\\..+$/', '/\\D/'), array('', ''), $postData[$filterElement]);
                     }
                 }
                 // Update the buildings insurance selection for the quote
                 if (isset($postData['need_building_insurance']) && $postData['need_building_insurance'] == 'yes' && $postData['building_built'] != '' && $postData['building_bedrooms'] != '' && $postData['building_type'] != '') {
                     // Add buildings insurance
                     $quoteManager->addBuildingsCover($postData['building_built'], $postData['building_bedrooms'], $postData['building_type'], $postData['building_insurance_excess'], isset($postData['building_accidental_damage']) ? $postData['building_accidental_damage'] : 0, $postData['building_value'] != '' ? $postData['building_value'] : 0);
                     // If we've calculated premiums but not got a dsi value - send a request back to front end for a
                     // manual value
                     if ($postData['override_dsi'] == '1' || $quoteManager->calculateDSI() == 0) {
                         // We've not got a DSI Value - so we need to make the building value a required field
                         $ajaxForm->getSubForm('subform_buildinginsurance')->getElement('building_value')->setRequired(true);
                         $return['rebuildValueRequired'] = true;
                     } else {
                         $return['rebuildValueRequired'] = false;
                         // Remove, re-add and recalculate buildings cover from scratch with given building value forcibly set to zero
                         if ($quoteManager->hasProduct(Manager_Insurance_LandlordsPlus_Quote::BUILDING_COVER)) {
                             $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::BUILDING_COVER);
                             $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::EMERGENCY_ASSISTANCE);
                         }
                         $quoteManager->addBuildingsCover($postData['building_built'], $postData['building_bedrooms'], $postData['building_type'], $postData['building_insurance_excess'], isset($postData['building_accidental_damage']) ? $postData['building_accidental_damage'] : 0, 0);
                     }
                 } else {
                     if ($quoteManager->hasProduct(Manager_Insurance_LandlordsPlus_Quote::BUILDING_COVER)) {
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::BUILDING_COVER);
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::EMERGENCY_ASSISTANCE);
                     }
                 }
                 // Update the contents insurance selection for the quote
                 if (isset($postData['need_contents_insurance']) && $postData['need_contents_insurance'] == 'yes') {
                     // Add contents insurance if they've chosen whether it's furnished or not
                     if (isset($postData['property_furnished'])) {
                         if ($postData['property_furnished'] == 'yes') {
                             if (isset($postData['contents_accidental_damage']) && isset($postData['contents_amount']) && isset($postData['contents_excess'])) {
                                 // Furnished property
                                 $quoteManager->addContentsCover(true, $postData['contents_accidental_damage'], $postData['contents_amount'], $postData['contents_excess']);
                             }
                         } elseif ($postData['property_furnished'] == 'no') {
                             // Un-furnished property
                             $quoteManager->addContentsCover(false);
                         }
                     }
                 } else {
                     $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::CONTENTS_COVER);
                     $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::UNFURNISHED_CONTENTS_COVER);
                 }
                 if ($premiums != '') {
                     // If this is a flood exclusion area - send that back so the page can display the messages
                     if ($premiums['riskAreas']['floodArea'] == '1000') {
                         $return['floodExcluded'] = true;
                     } else {
                         $return['floodExcluded'] = false;
                     }
                     // If this is a subsidence exclusion area - send that back so the page can display the messages
                     if ($premiums['riskAreas']['subsidenceArea'] == '1000') {
                         $return['subsidenceExcluded'] = true;
                     } else {
                         $return['subsidenceExcluded'] = false;
                     }
                 }
             }
             break;
         case '3':
             $ajaxForm = new LandlordsInsuranceQuote_Form_Step3();
             $pageSession = new Zend_Session_Namespace('landlords_insurance_quote');
             if (isset($pageSession->quoteID)) {
                 $quoteID = $pageSession->quoteID;
                 $quoteManager = new Manager_Insurance_LandlordsPlus_Quote($quoteID);
                 // Filter to mirror the subform element definitions.
                 $currencyFilterElements = array('rent_amount');
                 foreach ($currencyFilterElements as $filterElement) {
                     if (isset($postData[$filterElement])) {
                         $postData[$filterElement] = preg_replace(array('/\\..+$/', '/\\D/'), array('', ''), $postData[$filterElement]);
                     }
                 }
                 // Update the Emergency Assistance selection for the quote
                 if (isset($postData['need_emergency_assistance'])) {
                     if ($postData['need_emergency_assistance'] == 'yes') {
                         $quoteManager->addEmergencyAssistance();
                     } else {
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::EMERGENCY_ASSISTANCE);
                     }
                 }
                 // Update the Boiler & Heating selection for the quote
                 if (isset($postData['need_boiler_heating'])) {
                     if ($postData['need_boiler_heating'] == 'yes') {
                         $quoteManager->addBoilerAndHeatingCover();
                     } else {
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::BOILER_HEATING);
                     }
                 }
                 // Update the Prestiage Rent Guarantee for the quote
                 if (isset($postData['need_prestige_rent_guarantee'])) {
                     if ($postData['need_prestige_rent_guarantee'] == 'yes') {
                         $quoteManager->addRentGuarantee($postData['rent_amount']);
                     } else {
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::RENT_GUARANTEE);
                         // Also remove the free Legal Expenses or it causes a few "hiccups"
                         $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::LEGAL_EXPENSES);
                     }
                 }
                 // Update the Legal Expenses selection for the quote
                 if (isset($postData['need_legal_expenses'])) {
                     if ($postData['need_legal_expenses'] == 'yes') {
                         $quoteManager->addLegalExpensesCover();
                     } else {
                         // Don't remove the legal expenses if they've just been added for free by the rent guarantee product ;)
                         if (!$quoteManager->hasProduct(Manager_Insurance_LandlordsPlus_Quote::RENT_GUARANTEE)) {
                             $quoteManager->removeProduct(Manager_Insurance_LandlordsPlus_Quote::LEGAL_EXPENSES);
                         }
                     }
                 }
             }
             break;
         case '4':
             $ajaxForm = new LandlordsInsuranceQuote_Form_Step4();
             break;
         case '5':
             $ajaxForm = new LandlordsInsuranceQuote_Form_Step5();
             break;
         default:
             return;
     }
     $valid = $ajaxForm->isValid($postData);
     // TODO: Needs to run the LI+ quote manager and calculate premiums so that they can be returned later on
     if (!$valid) {
         $errorMessages = $ajaxForm->getMessagesFlattened();
         // We need to strip out some complex messages that the end user won't care about
         if (isset($errorMessages['email_address'])) {
             if (isset($errorMessages['email_address']['hostnameUnknownTld'])) {
                 unset($errorMessages['email_address']['hostnameUnknownTld']);
             }
             if (isset($errorMessages['email_address']['hostnameLocalNameNotAllowed'])) {
                 unset($errorMessages['email_address']['hostnameLocalNameNotAllowed']);
             }
         }
         $return['errorJs'] = $errorMessages;
         $return['errorCount'] = count($errorMessages);
         $return['errorHtml'] = $this->view->partial('partials/error-list.phtml', array('errors' => $errorMessages));
     } else {
         $return['errorJs'] = '';
         $return['errorCount'] = '';
         $return['errorHtml'] = '';
     }
     // Calculate premiums
     $pageSession = new Zend_Session_Namespace('landlords_insurance_quote');
     if (isset($pageSession->quoteID)) {
         $quoteID = $pageSession->quoteID;
         $quoteManager = new Manager_Insurance_LandlordsPlus_Quote($quoteID);
         $return['premiums'] = $quoteManager->calculateQuickPremiums();
         // Do a little bit of nicer formatting for the front end (add commas to thousands etc..)
         $return['fees'] = $quoteManager->getFees();
     }
     echo Zend_Json::encode($return);
 }