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

class Mid_category 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'] = 'Mid Category';
		
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		
		$table = 'tbl_mid_category';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['mid_category'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/mid_category/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'] = 'Mid Category';
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		
		$table2 = 'tbl_mid_category';
		$column2['mcat_id'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		$data['mid_category'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/mid_category/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'] = 'Mid Category';
		
		
		$table1 = 'tbl_top_category';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['top_category'] = $result1;
		
		
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['mid_category'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/mid_category/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['mcat_id']= $id;
        $table='tbl_mid_category';
            
        if (!empty($_FILES['mcat_pic']['name'])) {
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result['mcat_pic'];
            $current_img ='./uploads/shop/mid_category/'.$current_img;
            if(file_exists($current_img)){
                unlink($current_img);
            }
            
            $path = $_FILES['mcat_pic']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $mcat_pic_name = 'mid_category-'.$id.'.'.$ext;
            $mcat_pic_name2 = 'mid_category-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/mid_category/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $mcat_pic_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('mcat_pic')){
                $mcat_pic = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$mcat_pic_name2,$config['upload_path'].$mcat_pic_name,225,225,FALSE);
				
				if(file_exists($config['upload_path'].$mcat_pic_name2)){
                    unlink($config['upload_path'].$mcat_pic_name2);
                }
                
                $data['mcat_pic'] = $mcat_pic_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/mid_category/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!! Mid Category is Updated Successfully.');
		    redirect(base_url('master/mid_category/edit/'.$id));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/mid_category/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 = 'mcat_id';
        $table='tbl_mid_category';
        
        $mcat_id = $this->Common_model->Results_last_id($column_name, $table);
        $id = $mcat_id+1;
            
        if (!empty($_FILES['mcat_pic']['name'])) {
            
            $path = $_FILES['mcat_pic']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $mcat_pic_name = 'mid_category-'.$id.'.'.$ext;
            $mcat_pic_name2 = 'mid_category-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/shop/mid_category/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $mcat_pic_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('mcat_pic')){
                $mcat_pic = $this->upload->data();
                
                // Resizeing Image Part
				resizeImage($config['upload_path'].$mcat_pic_name2,$config['upload_path'].$mcat_pic_name,225,225,FALSE);
				
				if(file_exists($config['upload_path'].$mcat_pic_name2)){
                    unlink($config['upload_path'].$mcat_pic_name2);
                }
                
                $data['mcat_pic'] = $mcat_pic_name;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/mid_category/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!! Mid Category is Added Successfully.');
		    redirect(base_url('master/mid_category/create/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/mid_category/create/'));
		    exit();
        }
        
        
	}

	
}

?>