/**
  * @param CController $controller
  * @param array       $template_data
  * @param int         $eye_id
  * @param User        $notify_user
  *
  * @throws Exception
  *
  * @return bool
  */
 private function processEventForEye(CController $controller, array $template_data, $eye_id, User $notify_user = null)
 {
     $eye_name = $this->getEyeNameById($eye_id);
     $template_data += $this->getSideSpecificTemplateData($eye_name);
     $attachments = array();
     $attach_size = 0;
     if ($app_file = $this->createAndSavePdfForSide($controller, $template_data, $eye_name)) {
         $attachments[] = $app_file;
         $attach_size += $app_file->size;
     }
     if ($ec = $this->getElement('Element_OphCoTherapyapplication_ExceptionalCircumstances')) {
         foreach ($ec->{"{$eye_name}_filecollections"} as $fc) {
             $attachments[] = $fc->getZipFile();
             $attach_size += $fc->getZipFile()->size;
         }
     }
     $service_info = $this->getServiceInfo();
     $link_to_attachments = $attach_size > Helper::convertToBytes(Yii::app()->params['OphCoTherapyapplication_email_size_limit']);
     $template_data['link_to_attachments'] = $link_to_attachments;
     $email_text = $this->generateEmailForSide($controller, $template_data, $eye_name);
     $message = Yii::app()->mailer->newMessage();
     if ($template_data['compliant']) {
         $recipient_type = 'Compliant';
         $message->setSubject(Yii::app()->params['OphCoTherapyapplication_compliant_email_subject']);
     } else {
         $recipient_type = 'Non-compliant';
         $message->setSubject(Yii::app()->params['OphCoTherapyapplication_noncompliant_email_subject']);
     }
     $recipient_type = $template_data['compliant'] ? 'Compliant' : 'Non-compliant';
     try {
         $recipients = $this->getEmailRecipients($service_info, $recipient_type);
     } catch (Exception $e) {
         Yii::app()->user->setFlash('error', $e->getMessage());
         $controller->redirect('/OphCoTherapyapplication/default/view/' . $this->event->id);
     }
     $email_recipients = array();
     foreach ($recipients as $recipient) {
         if (!$recipient->isAllowed()) {
             throw new Exception("Recipient email address {$recipient->recipient_email} is not in the list of allowed domains");
         }
         $email_recipients[$recipient->recipient_email] = $recipient->recipient_name;
     }
     $message->setFrom(Yii::app()->params['OphCoTherapyapplication_sender_email']);
     $message->setTo($email_recipients);
     if ($notify_user && $notify_user->email) {
         $cc = true;
         if (Yii::app()->params['restrict_email_domains']) {
             $domain = preg_replace('/^.*?@/', '', $notify_user->email);
             if (!in_array($domain, Yii::app()->params['restrict_email_domains'])) {
                 Yii::app()->user->setFlash('warning.warning', 'You will not receive a copy of the submission because your email address ' . $notify_user->email . ' is not on a secure domain');
                 $cc = false;
             }
         }
         if ($cc) {
             $message->setCc($notify_user->email);
         }
     }
     $message->setBody($email_text);
     if (!$link_to_attachments) {
         foreach ($attachments as $att) {
             $message->attach(Swift_Attachment::fromPath($att->getPath())->setFilename($att->name));
         }
     }
     if (Yii::app()->mailer->sendMessage($message)) {
         $email = new OphCoTherapyapplication_Email();
         $email->event_id = $this->event->id;
         $email->eye_id = $eye_id;
         $email->email_text = $email_text;
         if (!$email->save()) {
             throw new Exception('Unable to save email: ' . print_r($email->getErrors(), true));
         }
         $email->addAttachments($attachments);
         $this->event->audit('therapy-application', 'submit');
         $this->event->info = self::STATUS_SENT;
         if (!$this->event->save()) {
             throw new Exception('Unable to save event: ' . print_r($this->event->getErrors(), true));
         }
         return true;
     } else {
         OELog::log("Failed to send email for therapy application event_id '{$this->event->id}', eye_id '{$eye_id}'");
         // clean up
         if ($app_file) {
             $app_file->delete();
         }
         return false;
     }
 }
 private function createEmail(Event $event)
 {
     $email = new OphCoTherapyapplication_Email();
     $email->event_id = $event->id;
     $email->save();
     return $email;
 }