<?php

class AdminMenus   extends CI_Controller {
    
    var $tablename = ADMINMNEUS;
    var $primarykey = "AMID";
    var $parentkey = "PAMID";    
    
    public function index(){               
        $data['current'] = "adminmenus";          
        $this->session->unset_userdata('pid');      
        $data['adminmenus'] = $this->general->getAllRecordsWhere($this->tablename,array($this->parentkey=>0),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/adminmenus",$data);
    }
    
    public function viewSubCategory($pid){               
        $data['current'] = "adminmenus";      
        $this->session->set_userdata('pid', $pid);
        $data['adminmenus'] = $this->general->getAllRecordsWhere($this->tablename,array($this->parentkey=>$pid),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/adminmenus",$data);
    }
    
    public function AddItem(){        
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Category Title', 'trim|required');
        $this->form_validation->set_rules('txt_url', 'Category URL', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{         
            
            
            $Data = array(            
                "MenuName"          => $this->input->post("txt_title"),
                "MenuURL"           => $this->input->post("txt_url"),
                                                                                
                "is_active"             => $this->input->post("txt_active"),                 
                "date_added"            => date("Y-m-d H:i:s"),
                "date_modified"         => date("Y-m-d H:i:s"),
                "added_by"         => $this->session->userdata("AdminID")
            );
              
            
            if($this->session->userdata("pid") != "")
            {
                $Data[$this->parentkey] = $this->session->userdata("pid");
            }
            
            $this->general->insertRecord($this->tablename, $Data);                                                                                                                               
            $data['message'] = "Category Added Successfully";
        }
            
       
        $this->load->view(ADMINFOLDER."/addmenuadmins",$data);
    }
    
    
    public function editItem($id){
        
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_title', 'Category Title', 'trim|required');
        $this->form_validation->set_rules('txt_url', 'Category URL', 'trim');
        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{                        
            
            
            $Data = array(       
                "$this->primarykey" 	=> $id,
                "MenuName"          => $this->input->post("txt_title"),
                "MenuURL"           => $this->input->post("txt_url"),
                
                
                "is_active"             => $this->input->post("txt_active"),                                 
                "date_modified"         => date("Y-m-d H:i:s")
            );
                        
            if($this->session->userdata("pid") != "")
            {
                $Data[$this->parentkey] = $this->session->userdata("pid");
            }
            
            $this->general->updateRecord($this->tablename, $Data , "$this->primarykey");                                                       
                        
            $data['message'] = "Category Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editadminmenus",$data);                       
    }
    
                
    public function delete($id){        
        $newsRecord = $this->general->getRecordWhere($this->tablename, array("$this->primarykey"=>$id));
			if(count($newsRecord)){
				                                                                                                                                                                                
				$this->db->query("DELETE FROM ".$this->tablename." WHERE $this->primarykey = $id");
				$data['message'] = $this->general->generateMessage("Record Deleted Successfully !!!", "green");
			}else{
				$data['message'] = $this->general->generateMessage("No record found with the specified id.", "red");
			}        
                        
            if($this->session->userdata("pid") != "")
            {                
                $this->viewSubCategory($this->session->userdata("pid"));
            }
            else {  $this->index();  }
            
    }
    
                   
}
