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

class Action 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');
	}

	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'] = 'dashboard';
		
        // we are loading admin dashboard
		$this->load->view('master/dashboard',$data);	
	}
	
	
		public function create(){
	    
        $data = [];
        
        foreach ($_POST as $key => $value) {
            
            if($key == 'table'){$table = $value;}
            
            if($key == 'page'){$page = $value;}
            
            if (empty($value)) {
                $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
			    redirect(base_url('master/'.$page));
			    exit();
            }
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Create($data,$table);
        
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Submited Successfully.');
		    redirect(base_url('master/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/'.$page));
		    exit();
        }
           
	}
	

	
	
	public function edit_img($name)
	{
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
        }
        
        if (!empty($_FILES[$name]['name'])) {
                
            $column['id']=1;
            $table='tbl_settings';
            
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result[$name];
            $current_img ='./uploads/master/'.$name.'/'.$current_img;
            
            if(file_exists($current_img)){
                unlink($current_img);
            }
            
            
            $path = $_FILES[$name]['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $logo_name = ''.$name.'.'.$ext;
            
            $config['upload_path'] = 'uploads/master/'.$name.'';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $logo_name;
            
            $this->load->library('upload',$config);
            if($this->upload->do_upload($name)){
                $logo = $this->upload->data();
                $data[$name] = $logo_name;
                
                // we are updating data from tbl_settings
                $result  = $this->Common_model->Edit($column,$data,$table);
                if($result == true){
                    $this->session->set_flashdata('success','SUCCESS!! Website '.strtoupper("$name").' is Updated Successfully.');
    			    redirect(base_url('master/page/edit_redirect/'.$page));
    			    exit();
                } else{
                    $this->session->set_flashdata('error','Something went Wrong.');
    			    redirect(base_url('master/page/edit_redirect/'.$page));
    			    exit();
                }
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/page/edit_redirect/'.$page));
			    exit();
            }
           
        }
        else {
		    $this->session->set_flashdata('error','Website  '.$name.'  can not be Empty.');
			redirect(base_url('master/page/edit_redirect/'.$page));
		}
	}

	public function edit_content($id){
	    
        $data = [];
        
        foreach ($_POST as $key => $value) {
            
            if($key == 'table'){$table = $value;}
            
            if($key == 'page'){$page = $value;}
            
            if($key != 'status'){
                if (empty($value)) {
                    $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
    			    redirect(base_url('master/page/edit_redirect/'.$page));
    			    exit();
                }
            }
            
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        if($table=='tbl_faq'){
            $column['faq_id'] = $id;
        }
        else {
            $column['id'] = $id;
        }
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Updated Successfully.');
		    redirect(base_url('master/page/edit_redirect/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/page/edit_redirect/'.$page));
		    exit();
        }
           
	}
	
	
	public function delete(){
	    
        $data = [];
        
        foreach ($_POST as $key => $value) {
            
            if($key == 'table'){$table = $value;}
            
            if($key == 'page'){$page = $value;}
            
            if($key == 'id'){ $id = $column[$key] = $value;}
            
            if($key=='column'){
                if(!empty($id)){
                    $column[$value] = $id;
                    unset($column['id']);
                }
            }
            
            if (empty($value)) {
                $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
			    redirect(base_url('master/page/redirect/'.$page));
			    exit();
            }
        }
        
        
        $column1 = $column;
        $table1= $table;
            
        
            
        if($table=='tbl_property'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['photo'];
            $current_img1 ='./uploads/master/property/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
            
        if($table=='tbl_brands'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['brands'];
            $current_img1 ='./uploads/master/brands/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
            
        if($table=='tbl_p_photo'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['p_photo'];
            $current_img1 ='./uploads/master/property/more/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
            
        if($table=='tbl_customer'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['cust_image'];
            $current_img1 ='./uploads/website/customer/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
        
        if($table=='tbl_top_category'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['tcat_pic'];
            $current_img1 ='./uploads/shop/top_category/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
        
        
        if($table=='tbl_mid_category'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['mcat_pic'];
            $current_img1 ='./uploads/shop/mid_category/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
        
        
        if($table=='tbl_end_category'){
            $result1 = $this->Common_model->Result($column1,$table1);
            $current_img1 = $result1['ecat_pic'];
            $current_img1 ='./uploads/shop/end_category/'.$current_img1;
            if(file_exists($current_img1)){
                unlink($current_img1);
            }
        }
        
        // we are deleting data from tbl_
        $result  = $this->Common_model->Del($column,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Deleted Successfully.');
		    redirect(base_url('master/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/'.$page));
		    exit();
        }
           
	}


}

?>