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


class news extends CI_Controller {

    private $userInfo = array();
    private $is_super;
    private $branch;
  
    public function __construct()
    {
        parent::__construct();
        $this->user_model->check_login("report");
        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
        $this->is_super = $this->user_model->is_super();
        $this->branch = $this->user_model->getBranch();
        $this->load->model("report_model");
        $this->load->model("voucher_model");
        $this->load->model("account_model");
        $this->load->model("generic_model");
        $this->load->model('Showexam_model');
        $this->load->model("Teacher_model");
        $this->load->model("Salary_model","sm");
     
    }

    public function add_news()
    {
           $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] =$this->user_model->userInfo("first_name,last_name");
        $data['branchId']=$this->report_model->getBranch()[0]['id'];
        $data['branch']=$this->report_model->getBranch();
        $data['branches']=$this->report_model->getAllBranches();   
        $data['cat']=$this->db->select('*')->from('new_categorie')->get()->result_array();
         //var_dump($data['branch']); die();

        $data['classes']=$this->report_model->getAllRecords('class',array('is_delete'=>0, 'branch'=>$data['branchId']));
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('News/add_news',$data);
    }

    public function save_news() 
    {

        if($this->input->post())
        {
        if(!empty($this->input->post('branch'))) 
        {

        $branch=$this->input->post('branch');
        }
        else 
        {
        $branch=0;
        } 
          $url = $this->do_upload();
            $this->load->library('upload');
            $da=$this->input->post();

        $data = array(
        'branch_id' =>$branch,
        'cat_id' =>$this->input->post('cat'),
        'images' =>$url,
        'news' =>$this->input->post('notice')
        );
        $this->db->insert('news',$data);
        redirect("News/view_new", "refresh");
        }

    }


        private function do_upload()
    {
        $type = explode('.', $_FILES["img"]["name"]);
        $type = $type[count($type)-1];
        $url = "./images/".uniqid(rand()).'.'.$type;
        if (in_array($type, array("png","jpg","jpeg","gif")))
            if(move_uploaded_file($_FILES["img"]["tmp_name"], $url))
                return $url;
        return ""; 
    }
 
 
    public function view_new() 
    {
            $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] =$this->user_model->userInfo("first_name,last_name");
        $data['branchId']=$this->report_model->getBranch()[0]['id'];
        

        $data['branch']=$this->report_model->getBranch();
        $data['branches']=$this->report_model->getAllBranches();   
        $data['cat']=$this->db->select('*')->from('new_categorie')->get()->result_array();
        $sup=$this->user_model->is_super();
        if($sup=="true")
        {    
        $data['news']=$this->db->select('*')->from('news')->where('is_delete',0)->where('branch_id',0)->get()->result_array();
        }
        else
        {
        $id=$this->report_model->getBranch()[0]['id'];
        $data['news']=$this->db->select('*')->from('news')->where('branch_id',$id)->where('is_delete',0)->get()->result_array();
        }
         // var_dump($data['news']); die();
       
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('News/view_news',$data);

    }
    
        public function delete() 
        {
        $id=$this->uri->segment(3);
        $done=$this->db->where('id',$id)->update('news',array('is_delete'=>1));
        if($done)
        {
            
       //$this->session->set_flashdata('msg', 'Product is Deleted!');
        redirect("News/view_new", "refresh");
        }
        }

    



}
    ?>