<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_mail extends CI_model{
	
	var $table = "trn_user";
	function __construct()
	{
		parent::__construct();
		$this->load->library('email');
	}	
	
	function getname($name)
	{
		$this->db->select('trn_id')
			 ->from($this->table)
			 ->where('email',$name);
		$result = $this->db->get();		
		date_default_timezone_set('Asia/Phnom_Penh');
		if($result->num_rows() > 0)
		{
			$msg="The email is already taken, please try new.";
			return $msg;
		}
		else
		{
			$data['email_verification'] = md5($name.time());
			$data['email'] = $name;
			$data['createdate'] = date("Y-m-d h:i:sa");
			$activation = md5($name.time());
			$this->db->insert($this->table,$data);
			$config['charset'] = 'iso-8859-1';
			$config['mailtype'] = 'html';
			$config['smtp_timeout'] = '5';
			$this->email->initialize($config);
			$this->email->from('info@channellimo.com');
			$this->email->to($name);
			$this->email->subject('Email verification');
			$this->email->message('Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> <a href="'.base_url("mail/activation/".$activation."").'">'.base_url("mail/activation/".$activation."").'</a>');
			$this->email->send();
			$this->email->print_debugger();
			$msg="Registration successful, please activate email."; 
			return $msg;
		 }
		 
		 
	}	
	
	function getcode($code)
	{
		$this->db->select('trn_id')
			 ->from($this->table)
			 ->where('email_verification',$code);
		$c = $this->db->get();
		
		if($c->num_rows > 0)
		{
			$this->db->select('trn_id')
			 ->from($this->table)
			 ->where('email_verification',$code)
			 ->where('status','0');
			$result = $this->db->get();
			if($result->num_rows() == 1)
			{
				$data['status'] = "1";
				$this->db->where("email_verification",$code);
				$this->db->update($this->table,$data);
				$msg = "Your account is activated"; 
			}
			else
			{
				$msg = "Your account is already active, no need to activate again";
			}	
		}
		else
		{
			$msg = "Wrong activation code.";	
		}
		return $msg;	
	}
	
	
	
	
}

