/**
  * Retrieve a referencing report
  */
 public function viewReferencingReportAction()
 {
     $request = $this->getRequest();
     $response = $this->getResponse();
     $refNo = $request->getParam('refno');
     $download = $request->getParam('download');
     $reporttype = '';
     $reportkey = $request->getParam('report');
     // Validate the refNo parameter
     preg_match('/([0-9]*\\.[0-9]*)/', $refNo, $refNo);
     if (count($refNo) == 2) {
         $refNo = $refNo[1];
     } else {
         // Fails validation, return error
         $this->render('view-document-not-found');
         return;
     }
     // Validate direct landlord is the correct owner of the reference
     // Get the customer session
     $customerSession = $this->auth->getStorage()->read();
     // Get list of external reference numbers
     $referenceManager = new Manager_Referencing_Reference();
     $referenceIds = $referenceManager->getAllReferenceIds($customerSession->id);
     if (!in_array($refNo, $referenceIds)) {
         // This reference does not belong to the customer
         $this->render('view-document-not-found');
         return;
     }
     // Get Latest report
     $legacyRefManager = new Manager_ReferencingLegacy_Munt();
     $report = $legacyRefManager->getLatestReport($refNo);
     // Check the $reportkey parameter against the key provided by the report object returned.
     // If they dont match, display a notice page that the report is out of date.
     if ($reportkey != '' && $report->validationKey != $reportkey) {
         $this->view->download = $download == 'true' ? 'true' : 'false';
         $this->view->report = $report;
         $this->render('reference-report-outofdate');
         return;
     }
     // Set the report type of that of the report object
     $reporttype = $report->reportType;
     $params = Zend_Registry::get('params');
     $baseRefUrl = $params->baseUrl->referencing;
     $reportUri = $baseRefUrl . 'cgi-bin/refviewreport.pl?refno=' . $refNo . '&repType=' . $reporttype;
     //error_log('debug: ' . $reportUri);
     $filename = $this->_buildReportAttachementFilename('Report', $refNo);
     // Get the latest report
     $reportDatasource = new Datasource_ReferencingLegacy_ReportHistory();
     $timegenerated = $reportDatasource->getTimeReportGenerated($refNo, $reporttype);
     // Check report file cache
     if (Application_Cache_Referencing_ReportFileCache::getInstance()->has($filename, $timegenerated)) {
         // Return from cache
         $pdfContent = Application_Cache_Referencing_ReportFileCache::getInstance()->get($filename, $timegenerated);
         $this->getResponse()->appendBody($pdfContent);
     } else {
         // Request report from legacy
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $reportUri);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
         curl_setopt($curl, CURLOPT_TIMEOUT, 50);
         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $pdfContent = curl_exec($curl);
         curl_close($curl);
         if (!$pdfContent) {
             $this->render('view-document-not-found');
             return;
         }
         // Cache result
         Application_Cache_Referencing_ReportFileCache::getInstance()->set($filename, $pdfContent, $timegenerated);
         $this->getResponse()->appendBody($pdfContent);
     }
     // Create filename
     // AJD - Why is this being done again? Also - it doesn't follow the new filename schema. Address must not be used.
     /*$referenceManager = new Manager_Referencing_Reference();
       $reference = $referenceManager->getReference($refNo);
       $filename = ucfirst(strtolower($reporttype)) . ', ' . $reference->propertyLease->address->addressLine1 . ', ' . $reference->propertyLease->address->addressLine2 . '.pdf';
       $filename = preg_replace('/&|\\//', '', $filename);*/
     // Apply appropriate headers
     //        $response->setHeader('Pragma', '');
     //        $response->setHeader('Cache-Control', '');
     if ($download == 'true') {
         // Downloading
         header('Pragma: ');
         // Remove pragma
         header('Cache-Control: ');
         // Remove cache control
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename=' . $filename);
         //           $response->setHeader('Content-Description', 'File Transfer');
         //           $response->setHeader('Content-Type', 'application/octet-stream');
         //           $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
     } else {
         header('Pragma: ');
         // Remove pragma
         header('Cache-Control: ');
         header('Content-Type: application/pdf');
         // Viewing
         //            $response->setHeader('Content-Type', 'text/plain');
     }
     $this->_helper->layout()->disableLayout();
     $this->_helper->viewRenderer->setNoRender(true);
 }
 /**
  * Retrieve the AgentID for the reference and display the report.
  *
  * @todo: push parameters into the view
  *
  * @return void
  */
 public function retrieveAction()
 {
     $baseRefUrl = $this->_params->connect->baseUrl->referencing;
     $request = $this->getRequest();
     $refno = $request->getParam('refno');
     $reptype = $request->getParam('repType');
     // Requested time generation for the report
     $timegenerated1 = $request->getParam('generated');
     if (!$timegenerated1) {
         $timegenerated1 = 0;
     }
     // Latest time generation of the report
     $reportDatasource = new Datasource_ReferencingLegacy_ReportHistory();
     $timegenerated2 = $reportDatasource->getTimeReportGenerated($refno, $reptype);
     //$timegenerated2=strtotime($timegenerated);
     // If refno belongs to the agent then push params into view
     if ($this->_isReferenceOwnedBy($refno, $this->_agentSchemeNumber)) {
         // Create Filters for params
         $filters = array('*' => array('StringTrim', 'HtmlEntities', 'StripTags'));
         //Create Validators
         $validators = array('*' => array('allowEmpty' => false));
         // Check values
         $requestFilter = new Zend_Filter_Input($filters, $validators, array('refno' => $refno, 'reptype' => $reptype));
         if ($requestFilter->isValid()) {
             $this->view->refNo = $requestFilter->getEscaped('refno');
             $this->view->repType = $requestFilter->getEscaped('reptype');
             $this->view->timegenerated1 = $timegenerated1;
             $this->view->timegenerated2 = $timegenerated2;
             //              $this->_helper->layout()->disableLayout();
             //              $page=$this->_helper->redirector->gotoUrlAndExit($baseRefUrl . '/cgi-bin/refviewreport.pl?refno=' . $refno.'&repType=interim');
             //              $this->_redirect($page);
         }
     } else {
         $this->render('report_error');
     }
 }