<?php
class M_st_menu extends CI_Model {

    function index() {
        $this->db->order_by('mm_sort', 'ASC');
        $query = $this->db->get('master_menu');
        return $query->result();
    }
    
    function menuactive() {
        $this->db->where('mm_status', 1);
        $this->db->order_by('mm_sort', 'ASC');
        $query = $this->db->get('master_menu');
        return $query->result();
    }

    function fieldactive($id) {
        $this->db->where('mf_status', 1);
        $this->db->where('mf_fk_menu_id', $id);
        $query = $this->db->get('master_field');
        return $query->result();
    }
    
    function insert() {
        $setstatus = 0;
        if($this->input->post('status')==1){
            $setstatus = 1;
        }

        $link = null; $icon = null; $parent = null; $level = null; $action = null; 
        if($this->input->post('link')!=""){$link = $this->input->post('link');}
        if($this->input->post('icon')!=""){$icon = $this->input->post('icon');}
        if($this->input->post('parent')!=""){$parent = $this->input->post('parent');}
        if($this->input->post('level')!=""){$level = $this->input->post('level');}
        if($this->input->post('action')!=""){$action = $this->input->post('action');}

        $insert = array(
            'mm_name'       => $this->input->post('nama'),
            'mm_link'       => $link,
            'mm_icon'       => $icon,
            'mm_parent'     => $parent,
            'mm_level'      => $level,
            'mm_action'     => $action,
            'mm_sort'       => $this->input->post('sort'),
            'mm_status'     => $setstatus,
        );
        $this->db->insert('master_menu', $insert);
        $lastid = $this->db->insert_id();

        $field = $_POST['fieldlabel'];$num=0;
        foreach($field as $key =>$datanya)
        {
            $insert_field    = array(
                'mf_label'      => $_POST['fieldlabel'][$num],
                'mf_field_id'   => $_POST['fieldid'][$num],
                'mf_name'       => $_POST['fieldname'][$num],
                'mf_status'     => "1",
                'mf_fk_menu_id' => $lastid,
            );
            $insert = $this->db->insert('master_field', $insert_field);
            $num++;
        }
    }
    
    function edit($id) {
        $this->db->where('mm_id', $id);
        $query = $this->db->get('master_menu');
        return $query;
    }
    
    function prosesedit() {
        $setstatus = 0;
        $id = $this->input->post('id');

        if($this->input->post('status')==1){
            $setstatus = 1;
        }
        
        $link = null; $icon = null; $parent = null; $level = null; $action = null; 
        if($this->input->post('link')!=""){$link = $this->input->post('link');}
        if($this->input->post('icon')!=""){$icon = $this->input->post('icon');}
        if($this->input->post('parent')!=""){$parent = $this->input->post('parent');}
        if($this->input->post('level')!=""){$level = $this->input->post('level');}
        if($this->input->post('action')!=""){$action = $this->input->post('action');}

        $update = array(
            'mm_name'       => $this->input->post('nama'),
            'mm_link'       => $link,
            'mm_icon'       => $icon,
            'mm_parent'     => $parent,
            'mm_level'      => $level,
            'mm_action'     => $action,
            'mm_sort'       => $this->input->post('sort'),
            'mm_status'     => $setstatus,
        );
        $this->db->where('mm_id', $id);
        $this->db->update('master_menu', $update);

        $field = $_POST['fieldlabel'];$num=0;
        foreach($field as $key =>$datanya){
            if($_POST['delfield'][$num]==1){
                $this->db->where('mf_id', $_POST['idfield'][$num]);
                $this->db->delete('master_field');
            }elseif($_POST['delfield'][$num]==2){
                $insert_field    = array(
                    'mf_label'      => $_POST['fieldlabel'][$num],
                    'mf_field_id'   => $_POST['fieldid'][$num],
                    'mf_name'       => $_POST['fieldname'][$num],
                    'mf_status'     => "1",
                    'mf_fk_menu_id' => $id,
                );
                $this->db->insert('master_field', $insert_field);
            }else{
                $update_field    = array(
                    'mf_label'      => $_POST['fieldlabel'][$num],
                    'mf_field_id'   => $_POST['fieldid'][$num],
                    'mf_name'       => $_POST['fieldname'][$num],
                    'mf_fk_menu_id' => $id,
                );
                $this->db->where('mf_id', $_POST['idfield'][$num]);
                $this->db->update('master_field', $update_field);
            }
            $num++;
        }

    }

    function delete($id) {
        $this->db->where('mm_id', $id);
        $this->db->delete('master_menu');

        $this->db->where('mf_fk_menu_id', $id);
        $this->db->delete('master_field');
    }
    
    function disabled($id) {
        $update = array(
            'mm_status' => 0,
        );
        $this->db->where('mm_id', $id);
        $this->db->update('master_menu', $update);
    }
    
    function active($id) {
        $update = array(
            'mm_status' => 1,
        );
        $this->db->where('mm_id', $id);
        $this->db->update('master_menu', $update);
    }
}
?>