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

class Liberary_definations extends CI_Controller {
     private $userInfo = array();
    public function __construct()
    {
        parent::__construct();
        $this->user_model->check_login("admin");
        $this->load->model('Subject_model','su');
        $this->load->model("user_model");
        $this->load->model('student_model','studentmo');
        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
        $this->load->model("Lib_def_model",'ldm');
        $this->load->model("Lib_item_model",'lim');
    } 
//===================catagory=============================================================== 
   public function index()
    {
      //  $this->user_model->check_permissions('Liberary_definations/index');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['cat']=$this->ldm->all_data("catagory");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/catagory_add",$data);
        //$this->load->view("footer");
    }
    
   public function dead_stock()
    {
      //  $this->user_model->check_permissions('Liberary_definations/dead_stock');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['cat']=$this->ldm->all_data("ded_type");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/ded_type",$data);
        //$this->load->view("footer");
    }
    
     public function mis_place()
    {
        
      //   $this->user_model->check_permissions('Liberary_definations/mis_place');
      $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['item']=$this->lim->all_data("item");
        $data['staff']=$this->lim->all_datawithoutisdelete("staff");
        $data['teacher']=$this->lim->all_datawithoutisdelete("teacher");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/mis_place",$data);
        $this->load->view("footer");
    }
    
    public function save_dedst()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
           if($data['cat_id']>0){
                 $this->ldm->save_ds($data);
                 redirect("Liberary_definations/dead_stock");
            }else
            {
                $this->ldm->save_ds($data);
                    redirect("Liberary_definations/dead_stock");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }
    
     public function add_ds()
    {
      //  $this->user_model->check_permissions('Liberary_definations/add_ds');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['item']=$this->lim->all_data("item");
        $data['ds_type']=$this->db->where('is_delete',0)->get("ded_type")->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/add_ds",$data);
      //  $this->load->view("footer");
    }
    
    	public function view_ds()
    {
     //  $this->user_model->check_permissions('Liberary_definations/view_ds');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$data['w']=$this->db->select('deadstock_lib.*,ded_type.name as ded_nam')
		                    ->from('deadstock_lib')
		                    ->join('ded_type','ded_type.id=deadstock_lib.status')
		                   ->get()->result_array();
	//	var_dump($data['w']);die;
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_libstock",$data);
        $this->load->view("footer");
    }
    
//=====================================Save Catagory=============================================
    public function save_cat()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
            if($data['cat_id']>0){
                 $this->ldm->save_cat($data);
                 redirect("Liberary_definations/index");
            }else
            {
                $this->ldm->save_cat($data);
                    redirect("Liberary_definations/index");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }
//===========================del catagory====================================
     public function del_cat($id=0)
	{
        $this->ldm->del_cat($id);
         redirect("Liberary_definations/index");
    }
//======================================type of items======================================
   public function Type()
    {
       // $this->user_model->check_permissions('Liberary_definations/Type');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['all']=$this->ldm->all_data("item_type");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/type_add",$data);
        //$this->load->view("footer");
    }
//=====================================Save Type=============================================
    public function save_type()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
            if($data['type_id']>0){
                 $this->ldm->save_type($data);
                 redirect("Liberary_definations/Type");
            }else
            {
                $this->ldm->save_type($data);
                    redirect("Liberary_definations/Type");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }
//===========================del type====================================
     public function del_type($id=0)
	{
        $this->ldm->del_type($id);
         redirect("Liberary_definations/Type");
    }
//====================================Publisher========================================
   public function Publisher()
    {
       // $this->user_model->check_permissions('Liberary_definations/Publisher');
	$data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['all']=$this->ldm->all_data("publisher");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/publisher_add",$data);
       // $this->load->view("footer");
    }
	public function get_Publishers()
    {
		$s = array(
		"id","name","address"
			);
			
		$this->db->select($s);
		$this->db->where("is_delete",0);
		$name = $this->db->get("publisher")->result_array();
		$i=1;
		foreach($name as $val){
			
					
		$row  =  "<a href='" .  base_url() . "Liberary_definations/del_publisher/" . $val['id'] . "' ><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
		$row_edit  =  "<a href='" .  base_url() . "Liberary_definations/view_edit_Publisher/" . $val['id'] . "' ><i class='fa fa-edit' aria-hidden='true'></i></a>";

 
		$data[] = array('name'=>$val['name'], 'id'=>$i, 'address'=>$val['address'], 'actions'=>$row_edit ."  " . $row );
		$i++;
		}
             
		
	$results = array(
			"sEcho" => 1,
        "iTotalRecords" => count($data),
        "iTotalDisplayRecords" => count($data),
          "aaData"=>$data);
/*while($row = $result->fetch_array(MYSQLI_ASSOC)){
  $results["data"][] = $row ;
}*/

		echo json_encode($results);
		
		
		
     }
	 
	
//=====================================Save Publisher=============================================
    public function save_publisher()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
            if($data['publisher_id']>0){
                 $this->ldm->save_publisher($data);
                 redirect("Liberary_definations/Publisher");
            }else
            {
                $this->ldm->save_publisher($data);
                    redirect("Liberary_definations/Publisher");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }
    
//===========================edit Publisher====================================
	public function view_edit_Publisher($id=0)
    {
        
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('publisher')->result_array();
		//var_dump($data['w']);die;
        //$data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_edit_Publisher",$data);
        $this->load->view("footer");
    }
	
	public function edit_Publisher($id=0)
	{
		$da = array(
			'name'=>$this->input->post('p_name'),
			'address' =>$this->input->post('publisher_address'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('publisher',$da);
         redirect("Liberary_definations/Publisher");
    }
	    
//===========================del Publisher====================================
     public function del_publisher($id=0)
	{
        $this->ldm->del_publisher($id);
         redirect("Liberary_definations/Publisher");
    }
//========================================Writer Portion===============================
   public function writer()
    {
       // $this->user_model->check_permissions('Liberary_definations/writer');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/Writer_add",$data);
        //$this->load->view("footer");
    }
	
	
	public function get_writer()
    {
		$s = array(
		"id","name"
			);
		$this->db->select($s);
		$this->db->where("is_delete",0);
		$name = $this->db->get("writer")->result_array();
		$i=1;
		foreach($name as $val){
			$row  =  "<a href='" .  base_url() . "Liberary_definations/del_writer/" . $val['id'] . "' ><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
	$row_edit  =  "<a href='" .  base_url() . "Liberary_definations/view_edit_writer/" . $val['id'] . "' ><i class='fa fa-edit' aria-hidden='true'></i></a>";

		$data[] = array('name'=>$val['name'], 'id'=>$i , "actions" =>$row_edit . " " . $row);
		$i++;
		}
             
		
	$results = array(
			"sEcho" => 1,
        "iTotalRecords" => count($data),
        "iTotalDisplayRecords" => count($data),
          "aaData"=>$data);
/*while($row = $result->fetch_array(MYSQLI_ASSOC)){
  $results["data"][] = $row ;
}*/
	echo json_encode($results);
	 }
	
	public function writer_part2(){
	 
        $aColumns = array('name','id');
        $sTable = 'writer';
        $iDisplayStart = $this->input->get_post('iDisplayStart', true);
        $iDisplayLength = $this->input->get_post('iDisplayLength', true);
        $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
        $iSortingCols = $this->input->get_post('iSortingCols', true);
        $sSearch = $this->input->get_post('sSearch', true);
        $sEcho = $this->input->get_post('sEcho', true);
        if(isset($iDisplayStart) && $iDisplayLength != '-1')
        {
            $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
        }
        if(isset($iSortCol_0))
        {
            for($i=0; $i<intval($iSortingCols); $i++)
            {
                $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
                $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
                $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
    
                if($bSortable == 'true')
                {
                    $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
                }
            }
        }
        if(isset($sSearch) && !empty($sSearch))
        {
            for($i=0; $i<count($aColumns); $i++)
            {
                $bSearchable = $this->input->get_post('bSearchable_'.$i, true);
                
                // Individual column filtering
                if(isset($bSearchable) && $bSearchable == 'true')
                {
                    $this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
                }
            }
        }
        //var_dump($sTable);die();
        $this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
        $rResult = $this->db->where('is_delete',0)->get($sTable);
        $this->db->select('FOUND_ROWS() AS found_rows');
        $iFilteredTotal = $this->db->get()->row()->found_rows;
        $iTotal = $this->db->where('is_delete',0)->count_all($sTable);
        $output = array(
            'sEcho' => intval($sEcho),
            'iTotalRecords' => $iTotal,
            'iTotalDisplayRecords' => $iFilteredTotal,
            'aaData' => array()
        );
        
        foreach($rResult->result_array() as $aRow)
        {
            $row = array();
            
            foreach($aColumns as $col)
            {
                if($col=="id")
                {
                    $get_vl=$aRow[$col];
                  $row[]= "<a href='" .  base_url() . "Liberary_definations/view_edit_writer/" . $get_vl . "' ><i class='fa fa-edit' aria-hidden='true'></i></a>"."  " .
                   "<a href='" .  base_url() . "Liberary_definations/del_writer/" . $get_vl . "' ><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                }else
                {
                
                $row[] = $aRow[$col];
                }
            }
            $output['aaData'][] = $row;
           
        }
      
        echo json_encode($output);
      
	}
//=====================================Save writer=============================================
public function save_writer()
	{
		if ($this->input->post())
        {
            $formdata=$this->input->post();
           
             if($this->input->post('writer_id',true)>0){
                 
                    $this->ldm->save_writer($formdata);
             }else{
            $name=$this->input->post('writer_name',TRUE);
            $data=array(
                'writer_id'=>0,
                'name'=>$name
                );
            $qq=$this->ldm->save_writer($data);

             }
            redirect("Liberary_definations/writer");
        }
	}
	
	//===========================edit writer====================================
	public function view_edit_writer($id=0)
    {
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('writer')->result_array();
        //$data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_edit_writer",$data);
        $this->load->view("footer");
    }
	
	public function edit_writer($id=0)
	{
		$da = array(
			'name'=>$this->input->post('writer_name'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('writer',$da);
         redirect("Liberary_definations/writer");
    }
    
    	public function view_edit_type($id=0)
    {
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('item_type')->result_array();
        //$data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_edit_type",$data);
        $this->load->view("footer");
    }
	
	public function edit_type($id=0)
	{
		$da = array(
			'name'=>$this->input->post('type'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('item_type',$da);
         redirect("Liberary_definations/type");
    }
    
    	public function view_edit_cat($id=0)
    {
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('catagory')->result_array();
        //$data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_edit_cat",$data);
        $this->load->view("footer");
    }
	
	public function edit_cat($id=0)
	{
		$da = array(
			'name'=>$this->input->post('cat'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('catagory',$da);
         redirect("Liberary_definations/index");
    }
    	public function view_edit_almirah($id=0)
    {
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('almirah')->result_array();
        //$data['all']=$this->ldm->all_data("writer");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/view_edit_almirah",$data);
        $this->load->view("footer");
    }
	
	public function edit_almirah($id=0)
	{
		$da = array(
			'name'=>$this->input->post('name'),
			'total_shelf'=>$this->input->post('shelf'),
			'shelf_total_books'=>$this->input->post('books'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('almirah',$da);
         redirect("Liberary_definations/almirah");
    }
	
	
//===========================del writer====================================
     public function del_writer($id=0)
	{
        $this->ldm->del_writer($id);
         redirect("Liberary_definations/writer");
    }
//========================================Almirah Portion===============================
   
        
    public function almirah()
    {
       // $this->user_model->check_permissions('Liberary_definations/almirah');
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['all']=$this->ldm->all_data("catagory");
        $data['almirah']=$this->ldm->almir();
       
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("Liberary/almirah_add",$data);
       //$this->load->view("footer");
    } 
        
        
    
//====================================Almirah save===================================
/*
public function save_almirah()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
            if($data['almirah_id']>0){
                 $this->ldm->save_almirah($data);
                 redirect("Liberary_definations/almirah");
            }else
            {
                $this->ldm->save_almirah($data);
                    redirect("Liberary_definations/almirah");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }
    
*/  
public function save_almirah()
	{
        if($this->input->post())
        {
            $data=$this->input->post();
            if($data['almirah_id']>0){
                 $this->ldm->save_almirah($data);
                 redirect("Liberary_definations/almirah");
            }else
            {
                $this->ldm->save_almirah($data);
                    redirect("Liberary_definations/almirah");
                }
        }else{
             echo "Please Fill your fileds and then submit here";
        }
    }


//===========================del writer====================================
     public function del_almirah($id=0)
	{
        $this->ldm->del_almirah($id);
         redirect("Liberary_definations/almirah");
    }
//+++++++++++++++++++++++Ajax Function+++++++++++++++++++++++++++++++++
//=======================catagory view for updation========================
public function SearchById($id=0,$tbl="")
{
    $this->db->select('*');
    $ret=$this->db->where('is_delete',0)->where('id',$id)->get($tbl)->row_array();
    echo  JSON_encode($ret);
}
//===========================function for uploading image=============================

 private function do_upload()
    {
        $type = explode('.', $_FILES["pic"]["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["pic"]["tmp_name"], $url))
                return $url;
        return ""; 
    }



}