<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email2 extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		session_start();
		
		date_default_timezone_set('Asia/Singapore');
		$this->load->model('edm_m');
	}
	
	public function test()
	{
		
		
		$content = "
		<p>Dear Members,</p>
<p><strong>IMAS Online Members' Directory Update</strong></p>
<p>Thank you for contributing to the IMAS Online Members Directory. It is time once again for members to update their company information.</p>
<p>Designated administrators appointed by each member firm can sign in to the directory <a href='https://imas.org.sg/login/'>here</a>, and amend the information by clicking on <strong>'My Company Profile'</strong> at the top right icon, after logging in.</p>
<p>We would be grateful if you could complete the exercise and send us a confirmation to <a href='mailto:replies@imas.org.sg'>replies@imas.org.sg</a> <strong><u>by Thursday, 4 January 2024.</u></strong> </p>
If you have any queries, please contact the IMAS Secretariat at <a href='mailto:replies@imas.org.sg'>replies@imas.org.sg</a> or at +65 6223 9353. </p>
<p>Thank you for your attention in this matter.</p>";
		
		$data['content'] = $content;
		
		$html = $this->load->view('emails/mass-email.php',$data,TRUE);
		
		$from = 'replies@imas.org.sg';
		$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 = 'IMAS Online Members\' Directory Update';
		$to = $a['email'];
		$to = 'juliet_lee@imas.org.sg';
		$cc = 'jasmine_ng@imas.org.sg,tech@arkstudio.sg';
		
		
		
		if($to != '')
		{
			
			send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename,$file,$filename,$cc);
		}
	}
	 
	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 = $this->load->view('emails/mass-email.php',$data,TRUE);
					
					
					$url = site_url('form/viewlink2/'.$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);				
				
					$to = '';
					
					$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 = $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_old()
	{			
		
		$edms = $this->edm_m->pending_reminder_list();
		
		foreach($edms as $s)
		{
		
			$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($s['edm_id'],$blast_info['edm_type']);
			
			
			foreach($submission_listing as $o)
			{
				$submission_t[] = $o['company_code'];
			}
			
			//generating the pending list
			$dd['status'] = 2;
			$this->edm_m->update_reminder($s['id'],$dd);
			foreach($org_listing as $a)
			{
					
					if(in_array($a['company_code'],$submission_t))
						continue;
				
					$d = $a;
					unset($d['datetime_open']);
					unset($d['datetime_sent']);
				
				    $old_email = $d['email_content'];
					unset($d['id']);
					
					$d['reminder_id'] = $s['id'];
				
					$r_id = $this->edm_m->add_to_reminder_list($d);
				
					$data['content'] = $content = $s['content'];
					$html = $this->load->view('emails/mass-reminder-email.php',$data,TRUE);
				
					if($blast_info['edm_type'] == 1)
						$url = site_url('form/viewlink1/'.$a['code'].'/'.$a['company_code']);
				
					if($blast_info['edm_type'] == 2)
						$url = site_url('form/viewlink2/'.$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 = $s['subject'];
					$to = $a['email'];	
					if($to != '')
					{
						send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename,$bcc);
						$dr['datetime_sent'] = date('Y-m-d H:i:s');
						$this->edm_m->update_reminder_email_blast($r_id,$dr);
					}
			}
			$dd2['status'] = 3;
			$this->edm_m->update_reminder($s['id'],$dd2);
		}
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */