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

class Profile extends CI_Controller
{
   function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
 $this->load->model('studentprofilemodel','',TRUE);
  $this->load->model('systemusermodel','',TRUE);
    $this->load->model('usermodel','',TRUE);



        if(empty($this->session->userdata('logged_in'))) {
            $this->session->set_flashdata('flash_data', 'You don\'t have access!');
            redirect('login');
        }
    }
	
 
	
			     public function studentprofile() {
$session_data = $this->session->userdata('logged_in');

	    $data['surname'] = $session_data['surname'];
	 	$data['id'] = $session_data['id'];
		$surname= $data['surname'] ;
		$userid= $data['id'] ;

 $data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];


		//$biodata= $this->studentprofilemodel->populatebiodata($userid);

		

		
	        		$usr_result = $this->usermodel->populateallarray('smsconfig','id',$companyid);
			
//$usr_result= $this->studentprofilemodel->populateimages($userid);


$img=$usr_result[0]['logo'];
//check if surname and password is correct



							if (($img!="") ||($img!=null))//active user record is present
							{
//$data['images']= $this->studentprofilemodel->populateimages($userid);
 $data['img'] = $img;

		$this->load->view("template/headerpagemin");

		$this->load->view('userdata/viewprofile',$data);
		$this->load->view("template/footerpage");
 

		}else{
			$this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong>Your profile has not been uploaded.. Please select correct image type  .e.g jpg,jpeg,png,gif.
						  then click upload button..</div>');


				$this->load->view("template/headerpage");

		$this->load->view('userdata/profile', $data);
		$this->load->view("template/footerpage");
 


		}

		
			
    }
 
		
public function uploadprofile() {
    
    $file= $_FILES["photo"]['name'];
	$session_data = $this->session->userdata('logged_in');

$data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];

if($file!=""){
		
	  


		$config['upload_path'] = is_dir($file).'uploads/';
  		$new_date=date('Y-m-d');
		$ext = explode(".", strtolower($file));
		 $ext = strtolower(end($ext));
					
     	 $new_name = $new_date.time().".".$ext;
	
		$config['file_name'] = $new_name;
        $config['overwrite'] = FALSE;
        $config["allowed_types"] = 'jpg|jpeg|png|gif';
        $config["max_size"] = 3024;
        $config['encrypt_name'] = FALSE;
      $this->load->library('upload', $config);
	  
	 

        if(!$this->upload->do_upload('photo')) {               
             $this->data['error'] = $this->upload->display_errors();
             $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong>Your profile was not  uploaded.. Please select correct image type with less than 3MB  .e.g jpg,jpeg,png,gif.
						  </div>');
             redirect('user/profileform');
         
       
        } else {
			
        $this->db->set('logo',$new_name );
         $this->db->where('id', $companyid);
         $this->db->update('smsconfig');
        

		$this->session->set_flashdata('msg', '<div class="alert alert-success fade in">
						  <a href="#" class="close" data-dismiss="alert">&times;</a>
						 
						<strong>Success!</strong> You have successfully Updated you profile!</div>');
 redirect('user/profileform');



        }  


}



		
		}



		
		
		
		
		
 
    public function logout() {
	
        $data = ['id', 'email'];
        $this->session->unset_userdata('logged_in');
		session_destroy();

  		 redirect('login', 'refresh');
       
    }
}
  ?>