<?php

App::import('Core');
App::import('Component', 'Email');
class FirstreminderShell extends Shell {

	public $uses = array('Evaluation','DocLabel','Customer'); 
	
	
	public function startup() 
	{
		$this->Email = new EmailComponent();
		$this->Email->delivery = 'smtp';
		$this->Email->smtpOptions = array(
			'host' => 'mail.e-testmetrix.ca',
			'username' => 'admin.service+e-testmetrix.ca',
			'password' => '20metrix11-'
		);
	}
	
	public function main() 
	{
		$labelStr = array();
		$evalEmps = $this->Evaluation->listTestsToSendEmailFirstRem();
		if (empty($evalEmps)) 
		{
			$this->out('All evaluations have been sent');
			$this->_stop();
		}
		$this->out('All evaluations have been sent');
		$n = count($evalEmps);
		$this->out('Sending Emails by Test - number of tests "'.$n.'"');
		
		//DEFINITION OF LABELS - IMPORT LABELSEMAIL FUNCTION FROM DOCLABEL MODEL
		//last digit of the three variables called should start by zero(french), then one(english) i.e = (1, 1, 0) - (1, 1, 1) OR ELSE!!
		//Variable names are self explanatory...
		//All businesses
		$labelsEmailReminder = $this->DocLabel->labelsEmail(8, 1, 0);
		$labelsEmailReminder = $this->DocLabel->labelsEmail(8, 1, 1);
		//Jewish General Hospital
		$labelsEmailReminderJGH = $this->DocLabel->labelsEmail(9, 1, 0);
		$labelsEmailReminderJGH = $this->DocLabel->labelsEmail(9, 1, 1);
		
				
		//$passw = Security::hash('ano77mous-ny', null, true);
		$passw = '00c383efdccfff3c654bf5f91b4a12bd5ee2fe56';
		
		foreach($evalEmps as $evalEmp) {
			$currentEmail = $evalEmp['Employee']['email'];
			$finalMsg ='';

			$this->out('Sending to '.$currentEmail.'... ', false);			
			
			$this->Email->sendAs = 'html';
			
			if ($evalEmp['Employee']['langue'] == 1) { $field_langue = 'name_eng';	}
			if ($evalEmp['Employee']['langue'] == 0) { $field_langue = 'name_fre';	}
			
			$tokeval = $evalEmp['Evaluation']['tokenhash'];	

			//WPS REMINDER MESSAGE
			if  ($evalEmp['Employee']['customer_id'] == 11)
			{ 
				$this->Email->subject = $labelsEmailReminderJGH[0]['DocLabel'][$field_langue];
				$finalMsg .= $labelsEmailReminderJGH[1]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminderJGH[2]['DocLabel'][$field_langue].'<br/><br/>'; 
				$finalMsg .= $labelsEmailReminderJGH[3]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminderJGH[4]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminderJGH[5]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/validate_emailaccess/wtf:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailReminderJGH[6]['DocLabel'][$field_langue].'</a><br/><br/>';
				$finalMsg .= $labelsEmailReminderJGH[7]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= 'Testmetrix Admin Service<br/><br/><br/>';		
				//$finalMsg .= '<font size="-2"><i><b>'.$labelsEmailReminderJGH[8]['DocLabel'][$field_langue].'<br/>'; 
				// $finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/refuse_emailaccess/ouqstapw:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailReminderJGH[9]['DocLabel'][$field_langue].'</b></i><br/><br/></font>';			
			}				
			
			//GENERAL REMINDER MESSAGE
			else 
			{ 
				$this->Email->subject = $labelsEmailReminder[0]['DocLabel'][$field_langue];
				$finalMsg .= $labelsEmailReminder[1]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminder[2]['DocLabel'][$field_langue].' '.$labelsEmailReminder[3]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminder[4]['DocLabel'][$field_langue].'<br/><br/>'.$labelsEmailReminder[5]['DocLabel'][$field_langue].' '.$labelsEmailReminder[6]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailReminder[7]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/validate_emailaccess/wtf:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailReminder[8]['DocLabel'][$field_langue].'</a><br/><br/>';
				
				$finalMsg .= '<font size="-1"><i>If the link does not open on your browser, please right click the link and choose from the menu the following options according to your web browser: Internet Explorer: "Copy shortcut". Edge "Copy link". Mozilla Firefox: "Copy Link Location". Chrome: "Copy link address". Open a new tab or window on your browser, right click on the address bar and press "paste and go".</i><br/><br/><br/>';	

				$finalMsg .= '<font size="3">'.$labelsEmailReminder[9]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= 'Testmetrix Admin Service<br/><br/><br/>';		
				//$finalMsg .= '<font size="-2"><i><b>'.$labelsEmailReminder[10]['DocLabel'][$field_langue].'<br/>';
				//$finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/refuse_emailaccess/ouqstapw:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailReminder[11]['DocLabel'][$field_langue].'</b></i><br/><br/></font>';	
				
				
			}	
					//REPLACE STRINGS FOR CUSTOMIZED MESSAGES WITH INSERTED FIELDS
					$finalMsg = str_replace('#', '<b>'.$evalEmp['Customer']['name'].'</b>', $finalMsg);
					$finalMsg = str_replace('?', '<b>'.$evalEmp['Employee']['first_name'].'</b>', $finalMsg);
					
			$this->Email->from = 'Testmetrix Admin Service <admin.service@e-testmetrix.ca>';
			$this->Email->to = $evalEmp['Employee']['first_name'].' '.$evalEmp['Employee']['last_name'].'<'.$currentEmail.'>';
			###ADDED A BLIND CARBON COPY OF EACH ORDER EMAIL###
			###CREATED FEBRUARY 12, 2017###
			$this->Email->bcc = array('admin.service@e-testmetrix.ca','admin@e-testmetrix.ca');
			$sent = $this->Email->send($finalMsg);
			
			if ($sent) {
				$this->out('DONE');
			} else {
				$error = !empty($this->Email->smtpError) ? $this->Email->smtpError : '';
				$this->out('ERROR' . (!empty($error) ? ': '.$error : ''));
			}
			#Update the Eval 'sent' field to 1 to generate a second reminder status for the sent field
			$this->Evaluation->id = $evalEmp['Evaluation']['id'];
			$this->Evaluation->saveField('sent', true);			
		
			$this->Email->reset();
		}
		

	}
}
?>