<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Role Permissions
*/
class Menu_permission extends CI_Controller
{
        public function index()
        {
            $data['menus']=$this->menu_permission_model->get_menus(FALSE);
            $data['pages']=$this->menu_permission_model->get_pages(FALSE);
            $data['user_types']=$this->menu_permission_model->get_user_types(FALSE);
            $data['menu_permissions']=$this->menu_permission_model->get_menu_permissions(FALSE);
                    $this->load->view('templates/header');
                    $this->load->view('templates/nav',$data);
                    $this->load->view('menu_permission');
                    $this->load->view('templates/footer');
        }

        public function add_menu_permission()
	{

                $data['menus']=$this->menu_permission_model->get_menus(FALSE);
                $data['pages']=$this->menu_permission_model->get_pages(FALSE);
                $data['user_types']=$this->menu_permission_model->get_user_types(FALSE);
                $data['menu_permissions']=$this->menu_permission_model->get_menu_permissions(FALSE);
    
		$this->form_validation->set_rules('menu_name','Menu Name','required');
		$this->form_validation->set_rules('file_path','Page Link/Path','required');
		$this->form_validation->set_rules('under_menu_id','Under Menu','required');
		$this->form_validation->set_rules('menu_order','Menu Order','required');
		
                if($this->form_validation->run()===FALSE)
		{
			$this->load->view('templates/header');
			$this->load->view('templates/nav',$data);
			$this->load->view('menu_permission',$data);
			$this->load->view('templates/footer');
		}	
		else
		{
			$menu_name=$this->input->post('menu_name');
			$file_path=$this->input->post('file_path');
			$under_menu_id=$this->input->post('under_menu_id');
			$menu_order=$this->input->post('menu_order');

                        $master_menu_added=$this->menu_permission_model->add_menu_master($menu_name,$file_path,$under_menu_id,$menu_order);
                       if($master_menu_added):
                                $menu_id=$this->menu_permission_model->get_last_added_menu_id($menu_name);
                                $user_type_count=$this->menu_permission_model->get_user_types_count();
                                for($i=0;$i<=$user_type_count;$i++):
                                        $user_type_id=$this->input->post('user_type_id['.$i.']');
                                        $active_status=$this->input->post('active_status['.$i.']');
                                        if($user_type_id&&$active_status):
                                                $this->menu_permission_model->add_menu_permission($menu_id, $user_type_id, $active_status);
                                        endif;
                                endfor;
                        endif;

                        if($i==$user_type_count):
                                $menu_permission_added=true;
                        else: 
                                $menu_permission_added=false; 
                        endif;

                        $data['menus']=$this->menu_permission_model->get_menus(FALSE);
                        $data['pages']=$this->menu_permission_model->get_pages(FALSE);
                        $data['user_types']=$this->menu_permission_model->get_user_types(FALSE);
                        $data['menu_permissions']=$this->menu_permission_model->get_menu_permissions(FALSE);
                            
		/*	if($menu_permission_added):
			   $this->session->set_flashdata('menu_permission_added_success','Your Menu Permission has been created Successfully');
			else:
			   $this->session->set_flashdata('menu_permission_added_failed','Technical Problem Occured.Your Menu Permission had not been Added.Contact your Administrator.');                
			endif;
                */
                        if($master_menu_added):
                                $this->session->set_flashdata('mtr_menu_added_success','Your Menu has been created Successfully');
                        else: 
                                $this->session->set_flashdata('mtr_menu_added_failed','Technical Problem Occured.Your Menu had not been Added.Contact your Administrator.');                
                        endif;
			$this->load->view('templates/header');
			$this->load->view('templates/nav',$data);
			$this->load->view('menu_permission',$data);
			$this->load->view('templates/footer');
		}
	}


        public function add_page_function()
	{

                $data['menus']=$this->menu_permission_model->get_menus(FALSE);
                $data['pages']=$this->menu_permission_model->get_pages(FALSE);
                $data['user_types']=$this->menu_permission_model->get_user_types(FALSE);
                $data['menu_permissions']=$this->menu_permission_model->get_menu_permissions(FALSE);
    
		$this->form_validation->set_rules('page_name','Page Name','required');
		$this->form_validation->set_rules('page_link','Page Link/Path','required');
		$this->form_validation->set_rules('page_desc','Page Description','required');
		
                if($this->form_validation->run()===FALSE)
		{
			$this->load->view('templates/header');
			$this->load->view('templates/nav',$data);
			$this->load->view('menu_permission',$data);
			$this->load->view('templates/footer');
		}	
		else
		{
			$page_name=$this->input->post('page_name');
			$page_link=$this->input->post('page_link');
			$page_desc=$this->input->post('page_desc');
			
                        $page_added=$this->menu_permission_model->add_page_function($page_name,$page_link,$page_desc);
                
                        $data['menus']=$this->menu_permission_model->get_menus(FALSE);
                        $data['pages']=$this->menu_permission_model->get_pages(FALSE);
                        $data['user_types']=$this->menu_permission_model->get_user_types(FALSE);
                        $data['menu_permissions']=$this->menu_permission_model->get_menu_permissions(FALSE);
                            
			if($page_added):
			   $this->session->set_flashdata('page_added_success','Your Page has been created Successfully');
			else:
			   $this->session->set_flashdata('page_added_failed','Technical Problem Occured.Your Page had not been Added.Contact your Administrator.');                
			endif;

			$this->load->view('templates/header');
			$this->load->view('templates/nav',$data);
			$this->load->view('menu_permission',$data);
			$this->load->view('templates/footer');
		}
	}




    
}