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

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
public function __construct()
{
parent::__construct();
$this->load->model('akrsp_model1');
$this->load->helper('date');
$this->load->library('Datatables');
$this->load->library('table');
$this->load->database();
$this->load->library('form_validation');
 $this->load->library("pagination");
}
	 public function index()
	{
		if($this->session->userdata('logged')==true && $this->session->userdata('adminloginid')!='')
	{
	
		$this->load->view('admin_dashboard');
		
	}
	else
	{
	$this->load->view('admin_login');	
	}
	
	}
	
	public function login()
	{
		
		
		
		$login=array('email'=>$this->input->post('email'),
					'password'=>md5($this->input->post('password'))
					);
					
					$this->load->model('akrsp_model1');
					$result=$this->akrsp_model1->login($login);
					if($result)
					{
						foreach($result as $var)
							{
								$data = array(
       									 'email' => $var->email,
										 'admin_id'=> $var->user_id,
										 'adminloginid'=>$var->user_id,
										 'name'=> $var->name,
										 'region_id'=> $var->region_id,
      									 'loggedinadmin' => true      
        );
								
								
								$this->session->set_userdata($data);
								$this->session->set_userdata('loggedinadmin',$data['loggedinadmin']);
								$this->session->set_userdata('adminloginid',$data['adminloginid']);
							    $this->load->view('admin_dashboard');
							}
					}
					else
					{
					
					echo "<br>wrong entry";
						
					}
		
	}
	public function welcome_page()
	{
		if($this->session->userdata('loggedinadmin')==true)
		{
		$this->load->view('welcome_message');
		}
		else
		{
			$this->index();
		}
		
	}
	
	public function logout()
	{
		if($this->session->userdata('loggedinadmin')==true)
		{
		$this->session->unset_userdata('loggedinadmin');
		$this->session->unset_userdata('adminloginid');
		$this->session->unset_userdata('admin_id');
		
	   //unset($_SESSION['user_id'],$_SESSION['adminloginid']);
	//$this->session->sess_destroy();
	$this->index();
		}
		else
		{
			$this->index();
		}
	}
	//forgot password
public function forgot_password()
{
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome/forgot');
}
else
{
$email=$this->input->post('email');
//get data from users tables
$sel_user_data=mysql_query("select * from njobn_users where email='".$email."'");
$fetach_user_data=mysql_fetch_array($sel_user_data);
$name=$fetach_user_data['name'];
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 8; $i++) {
	$randomString .= $characters[rand(0, $charactersLength - 1)];
}

//get email address for admin
$sel_admin_data=mysql_query("select * from setting where setting_id=1");
$fetach_admin_data=mysql_fetch_array($sel_admin_data);
$fromemail=$fetach_admin_data['email'];
$this->db->where('email',$email);
$query = $this->db->get('njobn_users');
if($query->num_rows() !=1)
{
$this->session->set_flashdata('erroe_msg', 'Email does not exist');	 			  							
redirect('welcome/forgot');
exit;
}


//email sendig code
  $password = $randomString;
  $update_admin = mysql_query("UPDATE njobn_users SET password='".md5($password)."' WHERE email='".$email."'");
  $to = $this->input->post('email');
  $subject="Job N Job change password request";     
  $body = "Dear $name<br>".
  "Your password at Job N Job has been reseted.<br>".
  "Your new password: $password <br>".
  "Best Regards<br>".
  "Job N Job<br>"; 
  $headers = "From: " . strip_tags($fromemail) . "\r\n";
  $headers .= "Reply-To: ". strip_tags($fromemail) . "\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  $mail=mail($to, $subject, $body,$headers); 
  if($mail)
  {
$this->session->set_flashdata('success_msg', 'Password sent successfully please check your email box');	 			  							
redirect('welcome/forgot');
					}	
}
}

	//code for to load fogot
public function forgot()
{		
$this->load->view('forgot');
}
public function admin_login()
{		
$this->load->view('admin_login');
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */