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



class Aqadmin extends CI_Controller 

{

	

	public function __construct()

	{

		parent::__construct();
	
		$this->load->library('form_validation');
		$this->load->model('aqadmin_model');		
		$this->load->library("pagination");
		$this->clear_cache();	
		define( 'SERVER_KEY', 'AAAA1CNpN5U:APA91bEqX8wnND4s74HM05GBogdOvhy99_te7ZZvgCzETTchl9j0MoYoDl5CxNehv5NRwjWI7v3gp-AqcxMA6ykPiU9zkEvYeRZWEyQMeAwY7TvI4YgSa8eqdWhed7BQZ0pmmjv3_kWt' );
	}
	function index()
	{
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Password','trim|required|xss_clean|callback_check_database');		
		if($this->form_validation->run() == FALSE)
		{	
			$datas['value']=0;
			$admin=$this->session->userdata('alogged_in');					
			$datas['login']=$admin;	
		    $this->load->view('login/login_view',$datas);
		}
		else{
		$email =$this->input->post("email");
		$password =md5($this->input->post("password"));
		$result = $this->aqadmin_model->login($email, $password);
	 	if($result==FALSE)
		{
			$datas['value']=1;
			$datas['msg']="Incorrect username or password";
			$admin=$this->session->userdata('alogged_in');					
			$datas['login']=$admin;	
            $this->load->view('login/login_view',$datas);
		}else
		{
			$sess_array = array();
			foreach($result as $row){			
				$sess_array = array('log_id' => $row->a_id,'email' => $row->email,'password' => $row->password,'role' => $row->role,'link' => $row->link);            
				$this->session->set_userdata('alogged_in', $sess_array);
				$admin=$this->session->userdata('alogged_in');		
				
				if($admin['role']=='super')
				{
					$this->session->set_userdata('user','admin');
					/*$this->session->set_userdata('adid','1');*/
				}
				else
				{
					$this->session->set_userdata('user','staff');
					/*$this->session->set_userdata('adid',$admin['log_id']);*/
				}
	}
	if($this->session->userdata('alogged_in'))
			{
			
       redirect('aqadmin/dashboard');

			}	
		}
		}
}

function dashboard()
	{
	    
		$admin=$this->session->userdata('alogged_in');					
		$datas['login']=$admin;	
		$this->session->set_userdata('active_menu', "dashboard");
		$datas['active_menu']="dashboard";
		$this->load->view('layout/header_view',$datas);
		$this->load->view('layout/sidebar_view',$datas);
		$this->load->view('dashboard/dash_view',$datas);
		$this->load->view('layout/footer_view');		
     }
    
   
	function edit_profile()
	{
		$admin=$this->session->userdata('alogged_in');	

		$adid=$admin['log_id']; 
		$this->session->set_userdata('adid',$adid);
		$this->form_validation->set_rules('adname', 'Name','trim|xss_clean');
		$this->form_validation->set_rules('email', 'Email','trim|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{ 
			$con=array('a_id'=>$adid);
			$datas['adm']=$this->aqadmin_model->select_condn('exp_admin',$con);    
			$admin=$this->session->userdata('alogged_in');					
			$datas['login']=$admin;	
			$datas['active_menu']="dashboard";	
			$this->load->view('layout/header_view',$datas);
			$this->load->view('layout/sidebar_view',$datas);
			$this->load->view('admin/edit_profile',$datas);
			$this->load->view('layout/footer_view');
		}
		else
		{
			$adid=$this->session->userdata('adid'); 
			
			$nam=$this->input->post("adname");
			$email=$this->input->post("email");
			
	        $con=array('a_id'=>$adid);
			$data=array('username'=>$nam,'email'=>$email);
			$this->aqadmin_model->updatedata('exp_admin',$data,$con);
			$this->session->set_flashdata('success_msg', 'Admin details updated');
	    	redirect('aqadmin/edit_profile');
    	
		}
	}
	function change_password()
	{
		$this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean');
		$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean');
		$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|matches[new_password]');
		
		if($this->form_validation->run() == FALSE)
		{ 
			$con=array('a_id'=>1);
			$datas['adm']=$this->aqadmin_model->select_condn('exp_admin',$con);    
			$admin=$this->session->userdata('alogged_in');					
			$datas['login']=$admin;	
			$datas['active_menu']="dashboard";	
			$this->load->view('layout/header_view',$datas);
			$this->load->view('layout/sidebar_view',$datas);
			$this->load->view('admin/edit_profile',$datas);
			$this->load->view('layout/footer_view');

					
		}
		else
		{
			$admin=$this->session->userdata('alogged_in');
			$adminmail=$admin['email']; 
			$old_password=md5($this->input->post("old_password"));
			$con=array('email'=>$adminmail,'password'=>$old_password);
			$result=$this->aqadmin_model->select_condn('exp_admin',$con);  
			if(count($result)==1)
			{
				$new_password=md5($this->input->post("new_password"));
				$con=array('email'=>$adminmail);
				$data=array('password'=>$new_password);
				$this->aqadmin_model->updatedata('exp_admin',$data,$con);
				$this->session->set_flashdata('pwd_success_msg', 'Password changed successfully');
				redirect('aqadmin/edit_profile');
			}
					
			else
			{
				$this->session->set_flashdata('pwd_success_msg', 'Wrong password entered');
			   	redirect('aqadmin/edit_profile');
			}	

					
		}
	}
	function forgot_password()
	{
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
			
		if($this->form_validation->run() == FALSE)
		{	
			$datas['value']=0;
		    $this->load->view('login/forgot_password',$datas);
		}
		else
		{
			$email =$this->input->post("email");
			$result = $this->aqadmin_model->select_admin_email($email);
		 	if($result==FALSE)
			{
				$datas['value']=1;
				$datas['msg']="Mail Id does not exist.";
				
	            $this->load->view('login/forgot_password',$datas);
			}
			else
			{
				$token=md5(uniqid(rand(), true));
				
				foreach($result as $row)
				{
					$login_id=$row->a_id;
					$login_email=$row->email;
					$data=array('reset_token'=>$token);
					$condn=array('a_id'=>$login_id);
					$update=$this->aqadmin_model->updatedata('exp_admin',$data,$condn);
										
				}
				$today=strtotime(date('d-m-Y H:i'));
				$resetlink=base_url()."index.php/aqadmin/reset_admin_password/".$login_id."/".$today."/".$token;

				

				$to = $login_email;
				$subject = "Password Reset".date('d/m/Y H:i:s');

				$message = "
					<html>
					<head>
					<title>Password Reset Link</title>
					</head>
					<body>
					<p>Click the link below to Reset password.</p>
				<p><a href='".$resetlink."' target='blank'>Click to reset</a></p>
				</body>
					<p>If clicking doesn't seem to work, you can copy and paste the link into your browser's address window.</p></br>".$resetlink."

					<p>Thanks<br>
					<b>Expert Solutions</b></p>
					</body>
					</html>
					";

				// Always set content-type when sending HTML email
				$headers = "MIME-Version: 1.0" . "\r\n";
				$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

				// More headers
				$headers .= 'From: <password.reset@expertsolutions.co>' . "\r\n";
				

				$check=mail($to,$subject,$message,$headers);
				


				$datas['value']=1;
				$datas['msg']="Resetlink is send to the given mail id.";
				$datas['resetlink']=$resetlink;
	            $this->load->view('login/forgot_password',$datas);
			}
		}
	}
	function reset_admin_password()
	{
		if($this->input->post("token"))
		{
			$token=$this->input->post("token");
			$login_id=$this->input->post("login_id");
			$link_date=$this->input->post("link_date");
		}
		else
		{
			$token=$this->uri->segment(5);
			$link_date=$this->uri->segment(4);
			$login_id=$this->uri->segment(3);
		}
		
		$todate=strtotime(date('d-m-Y H:i'));
		$diff=$todate-$link_date;
		// echo $diff;
		if (($todate - $link_date) < 86400) 
		{
			$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean');
			$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|matches[new_password]');
			if($this->form_validation->run() == FALSE)
			{	
				$datas['value']=0;
				$datas['token']=$token;
				$datas['login_id']=$login_id;
				$datas['link_date']=$link_date;
				$this->load->view('login/reset_admin_password',$datas);
			}
			else
			{
				$condn=array('a_id'=>$login_id,'reset_token'=>$token);
				$result=$this->aqadmin_model->select_condn('exp_admin',$condn);
				// echo $result->num_rows();
				
				if($result->num_rows()==1)
				{
					$password =md5($this->input->post("new_password"));
					$data=array('password'=>$password,'reset_token'=>NULL);
					$condn=array('a_id'=>$login_id);
					$update=$this->aqadmin_model->updatedata('exp_admin',$data,$condn);
					$datas['value']=1;
					$datas['msg']="Password reset successfully.";
					$admin=$this->session->userdata('alogged_in');					
					$datas['login']=$admin;	
		            $this->load->view('login/login_view',$datas);
				}
				else
				{
					$datas['value']=1;
					$datas['msg']="Link expired. Please enter mail id.";
				
		    		$this->load->view('login/forgot_password',$datas);
				}
				
			}
		}
		else
		{
			if($token==NULL)
			{
				/*$datas['value']=0;
				
				$admin=$this->session->userdata('alogged_in');					
				$datas['login']=$admin;	
	            $this->load->view('login/login_view',$datas);*/
	            redirect('aqadmin/index');
			}
			else
			{
				$datas['value']=1;
				$datas['msg']="Link expired. Please enter mail id.";
				
		    	$this->load->view('login/forgot_password',$datas);
			}
			
		}
		
		
	}
	function view_testimonials()
	{
		
	    $datas['testimonials']=$this->aqadmin_model->select('exp_testimonials');
	
		$admin=$this->session->userdata('alogged_in');					
		$datas['login']=$admin;	
		$datas['active_menu']="testimonials";
		$this->load->view('layout/header_view',$datas);
		$this->load->view('layout/sidebar_view',$datas);
		$this->load->view('testimonials/view_testimonials',$datas);
		$this->load->view('layout/footer_view');		
    }
	function add_testimonials()
	{
		$id=$this->input->post('id');
		$this->form_validation->set_rules('testimonial', 'Testimonial','trim|required|xss_clean');
		// $this->form_validation->set_rules('b2b_ar_catnam', 'b2b_ar_catnam','trim|required|xss_clean');
		$this->form_validation->set_rules('name', 'Name','trim|required|xss_clean');
		$this->form_validation->set_rules('loc', 'Location','trim|required|xss_clean');
				
		if($this->form_validation->run() == FALSE)
		{
        	
        	if($id=="")
        	{
        		$admin=$this->session->userdata('alogged_in');					
				$datas['login']=$admin;	
				$datas['active_menu']="testimonials";
				$this->load->view('layout/header_view',$datas);
				$this->load->view('layout/sidebar_view',$datas);
				$this->load->view('testimonials/add_testimonials',$datas);
				$this->load->view('layout/footer_view');
        	}
        	else
        	{
        		redirect('aqadmin/edit_testimonials?id='.$id);
        	}
			
		}
		else
		{
		
		$testimonial=$this->input->post("testimonial");
		// $b2b_ar_catnam=$this->input->post("b2b_ar_catnam");
		$name=$this->input->post("name");
		$location=$this->input->post("loc");
		$date=date("Y/m/d");
		$data=array('testimonial'=>$testimonial,'name'=>$name,'location'=>$location,'date'=>$date);
		if($id=="")
		{
			$this->aqadmin_model->insert('exp_testimonials',$data);
		}
		
		else
		{
			$con=array('id'=>$id);
			$this->aqadmin_model->updatedata('exp_testimonials',$data,$con);
		}
		$this->session->set_flashdata('success_msg', 'Testimonial added successfully');
    	redirect('aqadmin/view_testimonials');
		}
	}
	function edit_testimonials()
	{
		$id=$this->uri->segment(3);

		$con=array('id'=>$id);
	    $datas['testimonial_row']=$this->aqadmin_model->select_single('exp_testimonials',$con);
	        
		$admin=$this->session->userdata('alogged_in');					
		$datas['login']=$admin;	
		$datas['active_menu']="b2b";
		$this->load->view('layout/header_view',$datas);
		$this->load->view('layout/sidebar_view',$datas);
		$this->load->view('testimonials/add_testimonials',$datas);
		$this->load->view('layout/footer_view');
		
		
	}	
    function logout()
	{    
     $this->session->unset_userdata('alogged_in');
     $this->session->sess_destroy();
	 redirect('aqadmin','refresh');
	}	
	function clear_cache()
    {
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
    }
}
