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

class News 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'] = 'News';
		
		$table = 'tbl_photo2';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['News'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/News/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'] = 'News';
		
		$table2 = 'tbl_photo2';
		$column2['id'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		$data['News'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/News/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'] = 'News';
		
		
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['News'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/News/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_photo2';
            
        if (!empty($_FILES['photo']['name'])) {
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result['photo'];
            $current_img ='./uploads/website/News/'.$current_img;
            if(file_exists($current_img)){
                unlink($current_img);
            }
            
            $path = $_FILES['photo']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $photo_name = 'News-'.$id.'.'.$ext;
            $photo_name2 = 'News-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/website/News/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $photo_name2;
            
            $this->load->library('upload',$config);
            
            if($this->upload->do_upload('photo')){
                $photo = $this->upload->data();
                
                // Resizeing Image Part
				//resizeImage($config['upload_path'].$photo_name2,$config['upload_path'].$photo_name,270,195,FALSE);
				
				// if(file_exists($config['upload_path'].$photo_name2)){
    //                 unlink($config['upload_path'].$photo_name2);
    //             }
                
                $data['photo'] = base_url().'uploads/website/News/'.$photo_name2;
    
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/News/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!! News is Updated Successfully.');
		    redirect(base_url('master/News/edit/'.$id));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/News/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_photo2';
        
        $id = $this->Common_model->Results_last_id($column_name, $table);
        $id = $id+1;
            
        if (!empty($_FILES['photo']['name'])) {
            
            $path = $_FILES['photo']['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $photo_name = 'News-'.$id.'.'.$ext;
            $photo_name2 = 'News-'.$id.'-'.rand().'.'.$ext;
            
            $config['upload_path'] = 'uploads/website/News/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $photo_name2;
            
            $this->load->library('upload',$config);
            if ($this->upload->do_upload('photo')) {
            $photo = $this->upload->data();
            $photo_name = $photo['file_name']; // Make sure you're using this correctly
            $photo_name2 = 'temp_' . $photo_name; // Example for resized version if needed
            $data['photo'] = base_url().'uploads/website/News/'.$photo_name;
            // Resizing Image
            // if (resizeImage($config['upload_path'] . $photo_name2, $config['upload_path'] . $photo_name, 270, 195, FALSE)) {
        
            //     // Remove temp resized image if exists
            //     if (file_exists($config['upload_path'] . $photo_name2)) {
            //         unlink($config['upload_path'] . $photo_name2);
            //     }
        
            //     $data['photo'] = $photo_name;
            // } else {
            //     $this->session->set_flashdata('error', 'Image uploaded but failed to resize.');
            //     redirect(base_url('master/News/create/'));
            //     exit();
            // }

} else {
    // Upload failed
    $error = $this->upload->display_errors('', ''); // Clean output
    $this->session->set_flashdata('error', 'Upload Failed: ' . $error);
    redirect(base_url('master/News/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!! News is Added Successfully.');
		    redirect(base_url('master/News/create/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/News/create/'));
		    exit();
        }
        
        
	}
	
	
	public function delete($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'] = 'News';
		
		$table2 = 'tbl_photo2';
		
		$this->db->where('id',$id);
		$this->db->delete($table2);
		
		$data['News'] = $result2;
		
        // we are loading admin dashboard
		redirect(base_url('master/News'));
	}

	
}

?>