<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		session_start();
		
		date_default_timezone_set('Asia/Singapore');
		$this->load->model('edm_m');
	}
	
	public function content()
	{	
		$html = '<strong>Dear member</strong><br>123';
		
		return $html;
	}
	 
	public function index()
	{			
		
		$edms = $this->edm_m->pending_edm_list();
	
		
		foreach($edms as $s)
		{
		
			$listing = $this->edm_m->pending_list($s['id']);
			

			$dd['status'] = 2;
			$this->edm_m->update($s['id'],$dd);
			foreach($listing as $a)
			{
					$blast_info = $this->edm_m->info($a['blast_id']);
					$data['content'] = $blast_info['content'];
					
					$html = '';
					$html = $this->load->view('emails/mass-email.php',$data,TRUE);
				
					if($blast_info['edm_type'] == 1)
						$url = site_url('form/viewlink1/'.$a['code'].'/'.$a['company_code']);

					
					$html = str_replace('[NAME]','Dear '.$a['name'],$html);
					$html = str_replace('[EMAIL]',$a['email'],$html);
					$html = str_replace('[COMPANY]',$a['company_name'],$html);
					$html = str_replace('[DATE]',date('j M Y'),$html);
					$html = str_replace('[URL]',$url,$html);				
				
			
					$from = $this->config->item('from');
					$username = $this->config->item('username');
					$password = $this->config->item('password');
					$host = $this->config->item('host');
					$port = $this->config->item('port');
					$from_name = $this->config->item('from_name');
					$subject = $blast_info['subject'];
					$to = '';
					$to = $a['email'];	
					$cc = '';
					$cc = $a['cc_emails'];	
					$cc = $cc.',replies@imas.org.sg';		
			
					if($to != '')
					{
						send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename,$file,$filename,$cc);
						$id = $a['id'];
						$d['datetime_sent'] = date('Y-m-d H:i:s');
						$d['email_content'] = $html;
						$this->edm_m->update_email_blast($id,$d);
					}
			}
			
			
			$dd['status'] = 3;
			$this->edm_m->update($s['id'],$dd);
		}
	}
	
	public function reminder()
	{			
		$s['edm_id'] = 6;
		
		$blast_info = $this->edm_m->info($s['edm_id']);
		
		$org_listing = $this->edm_m->reminder_pending_list1($s['edm_id']);
		
		$submission_listing = $this->edm_m->reminder_pending_list2(6,1);
		
		
		foreach($submission_listing as $o)
		{
			$submission_t[] = $o['company_code'];
		}
		
		//generating the pending list
		$count = 0;
		foreach($org_listing as $a)
		{	
				$r_id = '';
				$r_id = $a['id'];
				if(in_array($a['company_code'],$submission_t))
					continue;
				
				$count++;
			
				$d = $a;
				unset($d['datetime_open']);
				unset($d['datetime_sent']);
			
				$old_email = $d['email_content'];
				unset($d['id']);
				
				$d['reminder_id'] = $s['id'];
			
				$data['content'] = $content = $this->content();
					$html = $this->load->view('emails/mass-email.php',$data,TRUE);
						
				if($blast_info['edm_type'] == 1)
					$url = site_url('form/viewlink1/'.$a['code'].'/'.$a['company_code']);
		
				$html = str_replace('[CONTENT]',$content,$html);
				
				$html = str_replace('[NAME]','Dear '.$a['name'],$html);
				$html = str_replace('[EMAIL]',$a['email'],$html);
				$html = str_replace('[COMPANY]',$a['company_name'],$html);
				$html = str_replace('[DATE]',date('j M Y'),$html);
				$html = str_replace('[URL]',$url,$html);
			
				$html .= $old_email;
				
				$from = $this->config->item('from');
				$username = $this->config->item('username');
				$password = $this->config->item('password');
				$host = $this->config->item('host');
				$port = $this->config->item('port');
				$from_name = $this->config->item('from_name');
				$subject = 'test';
				//$to = $a['email'];	
				$to = 'tech@arkstudio.sg';
				
				if($to != '' && $r_id != '')
				{					
					send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename,$bcc);
					$dr['reminder_email_content'] = $html;
					$dr['reminder_datetime_sent'] = date('Y-m-d H:i:s');
					$this->edm_m->update_reminder_email_blast($r_id,$dr);
				}
				
				exit;
		}
		
		print $count;
		
	}
	
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */