<?php
class mail_model extends CI_Model {
	

	function __construct() {
		parent::__construct();
  	}
	
	 function sendActivationMailtoUser($userActiveKey,$first_name,$emailId,$password){
 		$mailTemplates = $this->getMailTemplates(4);
		$verifyUrl = mail_url().'home/verify/'.$userActiveKey; 
		$subject  = $mailTemplates['template_subject'];
		$phrase  = $mailTemplates['template_content'];
	 
		$search = array("{SITE_NAME}", "{SITE_PATH}","{USER_NAME}","{EMAIL}","{VERIFY_URL}","{PWD}");
		$replace   = array(site_name(),mail_url(),$first_name,$emailId,$verifyUrl,$password );
		$newphrase = str_replace($search, $replace, $phrase);
		
 		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$this->email->initialize($config);
		$this->email->clear(TRUE);
		$this->email->from(site_from_email(),site_name());
		$this->email->to($emailId);
		$this->email->subject($subject);
		$this->email->message($newphrase);
		$this->email->send();
		return true;
	} 
 
	 function sendVerificaionEmailToAdmin($first_name,$emailId,$userActiveKey){
 		$mailTemplates = $this->getMailTemplates(64);
		$verifyUrl = mail_url().'admin/verify/'.$userActiveKey; 
		$subject  = $mailTemplates['template_subject'];
		$phrase  = $mailTemplates['template_content'];
		$phrase = str_replace( base_url(), mail_url(), $phrase); 
		$search = array("{SITE_NAME}", "{SITE_PATH}","{USER_NAME}","{EMAIL}","{PWD}","{ADMIN_URL}","{USER_TYPE}","{VERIFY_URL}");
		$replace   = array(site_name(),mail_url(),$first_name,$emailId,$password,$verifyUrl,$userType,$verifyUrl );
		$newphrase = str_replace($search, $replace, $phrase);
 		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$this->email->initialize($config);
		$this->email->clear(TRUE);
		$this->email->from(site_from_email(),site_name());
		$this->email->to($emailId);
		$this->email->subject($subject);
		$this->email->message($newphrase);
		$this->email->send();
		return true;
		
	} 
	function adminForgotMail($userName,$userEmail,$userActiveKey){
		//echo $userEmail;
  		$mailTemplates = $this->getMailTemplates(3);
		$verifyUrl = mail_url().'admin/verify/'.$userActiveKey; 
		$subject  = $mailTemplates['template_subject'];
		$phrase  = $mailTemplates['template_content'];
		$phrase = str_replace( base_url(), mail_url(), $phrase); 
		$search = array("{SITE_NAME}", "{SITE_PATH}","{USER_NAME}","{EMAIL}","{PWD}","{ADMIN_URL}","{USER_TYPE}","{VERIFY_URL}");
		$replace   = array(site_name(),mail_url(),$userName,$emailId,$password,$verifyUrl,$userType,$verifyUrl );
		$newphrase = str_replace($search, $replace, $phrase);
		//
  		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$this->email->initialize($config);
		$this->email->clear(TRUE);
		$this->email->from(site_from_email(),site_name());
		$this->email->to($userEmail);
		$this->email->subject($subject);
		$this->email->message($newphrase);
		$this->email->send();
		return true;	
	} 
	
	
 	function getMailTemplates($tempId = false){
		$this->db->where('template_id',$tempId);
		$mailQuery = $this->db->get('email_templates');
 		if($mailQuery->num_rows()){return $mailQuery->row_array();}
		else return false;
		
	} 
	

}



