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

class Ad_slider extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
		
        // loading common model
        $this->load->model('Common_model');
        $this->load->helper('common_helper');
	}

	public function index()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'AD Slider';
		
		$table = 'tbl_ad_slider';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['ad_slider'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/ad_slider/list',$data);	
	}


	public function edit($id)
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'AD Slider';
		
		$table2 = 'tbl_ad_slider';
		$column2['id'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		$data['ad_slider'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/ad_slider/edit',$data);	
	}

	public function create()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'AD Slider';
		
		
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['ad_slider'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/ad_slider/create',$data);	
	}
	
	public function edit_content($id){
	    
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $column['id']= $id;
        $table='tbl_ad_slider';
            
        if (!empty($_FILES['img']['name'])) {
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result['img'];
            $current_img ='./uploads/shop/ad_slider/'.$current_img;
            if(file_exists($current_img) && !empty($result['img'])){
                unlink($current_img);
            }
            
            $path = $_FILES['img']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $img_name = 'ad_slider-'.$id.'-'.date('ymdHis').'.'.$ext;
            $img_name2 = 'ad_slider-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/ad_slider/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $img_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('img')){
                $img = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$img_name2,$config['upload_path'].$img_name,590,300,FALSE);
				
				if(file_exists($config['upload_path'].$img_name2)){
                    unlink($config['upload_path'].$img_name2);
                }
                
                $data['img'] = $img_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/ad_slider/edit/'.$id));
			    exit();
            }
           
        }
        
        // we are updating data from tbl_settings
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! AD Slider is Updated Successfully.');
		    redirect(base_url('master/ad_slider/edit/'.$id));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/ad_slider/edit/'.$id));
		    exit();
        }
        
        
	}
	
	public function add(){
	    
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $column_name = 'id';
        $table='tbl_ad_slider';
        
        $id = $this->Common_model->Results_last_id($column_name, $table);
        $id = $id+1;
            
        if (!empty($_FILES['img']['name'])) {
            
            $path = $_FILES['img']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $img_name = 'ad_slider-'.$id.'-'.date('ymdHis').'.'.$ext;
            $img_name2 = 'ad_slider-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/ad_slider/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $img_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('img')){
                $img = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$img_name2,$config['upload_path'].$img_name,590,300,FALSE);
				
				if(file_exists($config['upload_path'].$img_name2)){
                    unlink($config['upload_path'].$img_name2);
                }
                
                $data['img'] = $img_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/ad_slider/create/'));
			    exit();
            }
           
        }
        
        // we are updating data from tbl_settings
        $result  = $this->Common_model->Create($data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! AD Slider is Added Successfully.');
		    redirect(base_url('master/ad_slider/create/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/ad_slider/create/'));
		    exit();
        }
        
        
	}

	
}

?>