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

class Register extends CI_Controller {
     
	public function __construct()
	{
	    parent::__construct();
		  
	} 
	
	
	public function register_user(){
		
		$this->form_validation->set_rules('u_name','Name', 'trim|required');
        $this->form_validation->set_rules('u_email','Email', 'trim|required');	
        $this->form_validation->set_rules('u_password','Password', 'trim|required');
        
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
		}else{
			$useremail = $this->input->post('u_email');
			$query = $this->Data_access_model->select_data('user_email','hs_user',array('user_email'=>$useremail));
			if($query->num_rows() > 0){ 
				$check_useremail = $query->row();
				if($check_useremail->user_email == $useremail){
					echo 2;
				}
			}else{
				$varification_code = md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8));
				$user_name = ucwords($this->input->post('u_name'));
				$to_email = $this->input->post('u_email');
				$data = array(
				'user_name' =>ucwords($this->input->post('u_name')),
				'user_email' =>$this->input->post('u_email'),
				'user_password' =>md5($this->input->post('u_password')),
				'registration_date' =>date('d M Y h:i:s'),
				'user_type' =>2,
				'user_status' =>0,
				'user_email_verify_code' => $varification_code,
				); 

				$this->Data_access_model->insert_data('hs_user',$data);
					
				if($this->db->affected_rows() > 0)
				{	
						$message_link = '<a href=" '. base_url("register/verify_email/").''.$varification_code.' "> '.$this->hs->getwebsettings('everifi_link').' </a>';
						
						$email_content = $this->hs->getwebsettings('everifi_template');  
						$email_content = str_replace("[username]",$user_name, $email_content);
						$email_content = str_replace("[link]", $message_link,$email_content);
						$email_content = str_replace("[break]", "<br>", $email_content);
						$data['emcontent'] = $email_content;

						$email_template = $this->load->view('theme/email_template',$data,true);
						$subject = $this->hs->getwebsettings('everifi_subject');
						//sending mail
						$this->hs->send_mail($to_email,$subject,$email_template);
						echo 1;
							
				}
			}
		}
	}


	public function verify_email($verify_code){
		
		if(!empty($verify_code)){
		 
			$user_verify = $this->Data_access_model->select_data('*','hs_user',array('user_email_verify_code' =>$verify_code));
			if($user_verify->num_rows() > 0){
				$userinfo = $user_verify->row();
				if($userinfo->user_email_verify_code === $verify_code){
					
					$data =array(
					'user_status'=> 1,
					'user_email_verify_code'=>null,
					);
					
					$this->Data_access_model->update_data('hs_user',array('user_id'=>$userinfo->user_id),$data);
					if($this->db->affected_rows() > 0){
					   
						redirect(base_url()); 
					}
				  
				}else{
					
					redirect(base_url());
				} 
 			 
			}else{
				redirect(base_url());
			}
		}else{
			
			redirect(base_url());
		}
	} 
}