/**
  * Helper function for generating email attachment HTML fragment
  *
  * @param Model_Referencing_Reference
  * Encapsulates details of the reference.
  *
  * @return string
  */
 public function uploadEmailAttachments($reference)
 {
     $pageSession = new Zend_Session_Namespace('tenants_referencing_tracker');
     $tatMailManager = new Manager_Referencing_TatMail($reference);
     $attachments = $tatMailManager->detailAttachments();
     // Filter out names from complete paths and make HTML safe, and add up total file size
     $fileNames = array();
     $totalSize = 0;
     foreach ($attachments as $file => $size) {
         $fileNames[] = htmlentities(substr($file, strrpos($file, '/') + 1));
         $totalSize += $size;
     }
     // Return partial view HTML
     return $this->view->partial('tenants-referencing-tracker/partials/upload-email-attachments.phtml', array('totalSize' => $totalSize, 'maxSize' => 4194304, 'attachments' => $fileNames));
 }
 /**
  * Send e-mail to HomeLet screen
  *
  * @return void
  */
 public function emailAction()
 {
     if ($this->_enquiryId == null) {
         return;
     }
     $pageForm = new Form_TenantsReferencingTracker_Email();
     $request = $this->getRequest();
     $formData = $request->getPost();
     $pageForm->populate($formData);
     $tatManager = new Manager_Referencing_Tat($this->_enquiryId);
     $tat = $tatManager->getTat();
     $tatMailManager = new Manager_Referencing_TatMail($tatManager->_reference);
     if ($request->isPost()) {
         // Check if user's going back to the TAT index, or is submitting the form
         if (isset($formData['back'])) {
             // Redirect to index page
             $this->_helper->redirector->gotoUrl('/tenants/reference-tracker');
             return;
         } else {
             // Check if this is to add/remove attachments, or is a full submit
             // TODO: It'd be nice to handle this by separating it out into a smooth non-page-refreshing AJAX method
             if (isset($formData['attachButton']) || isset($formData['deleteButton'])) {
                 // Handle attachments
                 if (isset($formData['attachButton'])) {
                     $tatMailManager->addAttachments();
                 } else {
                     $tatMailManager->deleteAttachments();
                 }
             } else {
                 if ($pageForm->isValid($formData)) {
                     // Successful set of data, send e-mail and show message to user
                     $data = $pageForm->getValues();
                     $content = '';
                     $content .= "Enquiry ID: {$this->_enquiryId}\r\n\r\n";
                     $content .= "Name: {$data['name']}\r\n\r\n";
                     $content .= "Contact number or e-mail address: {$data['contact_info']}\r\n\r\n";
                     $content .= "Message to assessor:\r\n{$data['message']}\r\n\r\n";
                     $attachmentInfo = $tatMailManager->detailAttachments();
                     if (count($attachmentInfo) > 0) {
                         $tatMailManager->notifyAssessorWithAttachments($content);
                         $tatMailManager->deleteAttachments();
                     } else {
                         $tatMailManager->notifyAssessor($content);
                     }
                     // Log MI event
                     Application_Core_ActivityLogger::log('TAT Email HomeLet', 'complete', 'TAT', null, "IRN: {$this->_enquiryId}");
                     // Redirect to confirmation page, the redirect prevents
                     //   multiple submissions if user refreshes browser
                     $this->_helper->redirector->gotoUrl('/tenants/reference-tracker/emailsent');
                     return;
                 }
             }
         }
     }
     $this->view->reference = $tatManager->_reference;
     $this->view->form = $pageForm;
 }