<?php

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

	//public $uses = array('Newsletter', 'Subscriber');
	public $uses = array('Evaluation','DocLabel');
	
	
	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->listTestsToSendEmail();
		
		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.'"');
		
		//$this->loadModel('DocLabel');
		$labelsEmailClaim = $this->DocLabel->labelsEmail(1, 1);
		$labelsEmailFollowUp = $this->DocLabel->labelsEmail(2, 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_fra';	}
			
			//$tokeval = $this->Security->hash($employeename, null, true);
			//$tokeval = sha1($employeename);
			$tokeval = $evalEmp['Evaluation']['tokenhash'];							
			
			if ($evalEmp['Evaluation']['test_serie'] == 1) 
			{ 
				$this->Email->subject = $labelsEmailClaim[0]['DocLabel'][$field_langue];
				$finalMsg .= $labelsEmailClaim[1]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailClaim[2]['DocLabel'][$field_langue].' '.$labelsEmailClaim[3]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailClaim[4]['DocLabel'][$field_langue].'<br/><br/>'.$labelsEmailClaim[5]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/validate_emailaccess/wtf:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailClaim[6]['DocLabel'][$field_langue].'</a><br/><br/>';
				$finalMsg .= $labelsEmailClaim[7]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= 'Testmetrix Admin Service<br/>';
			}
			
			if ($evalEmp['Evaluation']['test_serie'] > 1) 
			{ 
				$this->Email->subject = $labelsEmailFollowUp[0]['DocLabel'][$field_langue];
				$finalMsg .= $labelsEmailFollowUp[1]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailFollowUp[2]['DocLabel'][$field_langue].' '.$labelsEmailFollowUp[3]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= $labelsEmailFollowUp[4]['DocLabel'][$field_langue].'<br/><br/>'.$labelsEmailFollowUp[5]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= '<a href ='.'https://e-testmetrix.ca/pages/validate_emailaccess/wtf:'.$tokeval.'/udans:'.$passw.'/f08n:'.$evalEmp['Employee']['id'].'>'.$labelsEmailFollowUp[6]['DocLabel'][$field_langue].'</a><br/><br/>';
				$finalMsg .= $labelsEmailFollowUp[7]['DocLabel'][$field_langue].'<br/><br/>';
				$finalMsg .= 'Testmetrix Admin Service<br/>';
			}		
						
			$this->Email->from = 'Testmetrix Admin Service <admin.service@e-testmetrix.ca>';
			$this->Email->to = $evalEmp['Employee']['first_name'].' '.$evalEmp['Employee']['last_name'].'<'.$currentEmail.'>';
			$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
			$this->Evaluation->id = $evalEmp['Evaluation']['id'];
			$this->Evaluation->saveField('sent', true);			
		
			$this->Email->reset();
		}
		

	}
}
?>