
<?php defined('BASEPATH')or exit('No Direct Access is Allowed');?>
<?php
class Import extends MY_Controller{


	public function __construct(){
	
		 parent::__construct();
		 
		$this->load->helper(['form','url','html','security']);
		$this->load->library(['pagination','form_validation','csvimport']);
	
        $this->load->model('admin/master/Importmodel','import_model');
		
		date_default_timezone_set('Asia/Kolkata');
		ini_set('max_execution_time', 0);
		set_time_limit(0); 
		error_reporting(0);

	}
	

    /*$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
    $array3 = array('green' => 6, 'blue' => 7, 'yellow' => 7, 'cyan'   => 8);
    
    var_dump(array_intersect_key($array3, $array1,$array1));*/

		function remove_array_valuewise($array,$value){
			$holding=array();
			foreach($array as $k => $v){
				if($value!=$v){
					$holding[$k]=$v;
				}
			}   
			return $holding;
		}
		
		function keep_array_valuewise($array,$value){
			$holding=array();
			foreach($array as $k => $v){
				if($value==$v){
					$holding[$k]=$v;
				}
			}   
			return $holding;
		}
		
		function remove_array_keywise($array,$key){
			$holding=array();
			foreach($array as $k => $v){
				if($key!=$k){
					$holding[$k]=$v;
				}
			}   
			return $holding;
		}
		
		function keep_array_keywise($array,$key){
			$holding=array();
			foreach($array as $k => $v){
				if($key==$k){
					$holding[$k]=$v;
				}
			}   
			return $holding;
		}
		
		//$lunch = array('sandwich' => 'cheese', 'cookie'=>'oatmeal','drink' => 'tea','fruit' => 'apple');
		//echo '<pre>';
		//print_r($lunch);
		//$lunch=arem($lunch,'apple');
		//print_r($lunch);
		
		//$lunch=keep_array_valuewise($lunch,'apple');
		//print_r($lunch);
		//$lunch=akrem($lunch,'sandwich');
		//print_r($lunch);
		//echo '</pre>';

	
	public function index(){
	
		$this->load->view('adminlte/panel/header',['userdata'=>$this->user_data,'title'=>'Csv Data Import Master List','menu_auth'=>$this->menu_auth]);
		$this->load->view('adminlte/panel/sidebar');
		$this->load->view('adminlte/master/import/import');
		$this->load->view('adminlte/panel/footer');
	}
	
	public function import_category() {
	
		  $this->load->view('adminlte/panel/header');
		  $this->load->view('adminlte/panel/sidebar');	
        $data['error'] = '';    //initialize image upload error array to empty

        $config['upload_path'] = site_url()."upload1/uploads_csv";
        $config['allowed_types'] = 'csv';
        $config['max_size'] = '200000';
	
		
        $this->load->library('upload', $config);
		//echo "<pre>";print_r($this->upload);die;
        // If upload failed, display error
        if (!$this->upload->do_upload()) {
            $data['error'] = $this->upload->display_errors();
            $this->load->view('adminlte/master/import/import', $data);
			//$this->session->set_flashdata('success', $data);
				//return redirect("admin/master/Import");
        }
		else{
		
            $file_data = $this->upload->data();
            $file_path =   $config['upload_path'].$file_data['file_name'];
            
            if ($this->csvimport->get_array($file_path)) {
			
                $csv_array = $this->csvimport->get_array($file_path);

                foreach ($csv_array as $row) {
					$count=$this->import_model->duplicate_category(strtolower(trim($row['category'])),strtolower(trim($row['category_code'])));
					if($count==0){
						
						$insert_data = array('category'=>strtolower(trim($row['category'])),
						'description'=>strtolower(trim($row['description'])),'category_code'=>strtolower(trim($row['category_code'])),
						);
						
						$this->import_model->insert_category($insert_data);
					}
					else{
						$category	=	strtolower(trim($row['category']));
						$description	=	strtolower(trim($row['description']));
						$category_code	=	strtolower(trim($row['category_code']));
						$this->import_model->update_category($category,$description,$category_code);
					}
					
                }
				
                $this->session->set_flashdata('success', 'Category Csv Data Imported Succesfully');
				return redirect("admin/master/Import");
               
            }
			else{ 
                $data['error'] = "Error occured in importing Process of Category";
                $this->load->view('adminlte/master/import/import', $data);
			}
        }
        $this->load->view('adminlte/panel/footer');
    } 
 
}

?>

