<?php
class viewlevel{
    var $id;
    
    function listlevel(){
        $level = mysql_query("SELECT * FROM level ORDER BY id_level DESC");
        
        while ($l = mysql_fetch_array($level)){
            $data[$i] = array(
                        'id_level' => $l[id_level],
                        'level' => $l[level],
                        'status' => $l[status],
                        'akses' => $l[akses],
                        'aktif' => $l[aktif]
                    );
            
            $i++;
        }
        
        return $data;
    }
    
    function level(){
        $level = mysql_query("SELECT * FROM level WHERE id_level = $this->id");
        $l = mysql_fetch_array($level);
        
        $data = array(
                'id_level' => $l[id_level],
                'level' => $l[level],
                'status' => $l[status],
                'akses' => $l[akses],
                'aktif' => $l[aktif],
                'session' => $l[session]
            );
        
        return $data;
    }
    
    function addlevel($level,$status,$akses,$aktif,$session){
        
        mysql_query("INSERT INTO level( level,
                                        status,
                                        akses,
                                        aktif,
                                        session) 
                                        VALUES(
                                        '$level',
                                        '$status',
                                        '$akses',
                                        '$aktif',
                                        '$session')");
    }
    
    function editlevel($level,$status,$akses,$aktif){
        
        mysql_query("UPDATE level SET   level       = '$level',
                                        status      = '$status',
                                        akses       = '$akses',
                                        aktif       = '$aktif'  
                                        WHERE
                                        id_level    = $this->id");
    }
    
    function deletelevel(){
        mysql_query("DELETE FROM level WHERE id_level = $this->id");
        $this->deleteotoritas();
    }
            
    function otoritas($menu,$submenu){
        $otoritas = mysql_query("SELECT * FROM level_otoritas WHERE id_level = $this->id AND id_menu = $menu AND id_submenu = $submenu");
        $o = mysql_fetch_array($otoritas);
        
        $data = array(
                'id_otoritas' => $o[id_otoritas],
                'id_level' => $o[id_level],
                'id_menu' => $o[id_menu],
                'id_submenu' => $o[id_submenu]
            );
        
        return $data;
    }
    
    function addotoritas($menu,$submenu){
        
        mysql_query("INSERT INTO level_otoritas (   id_level,
                                                    id_menu,
                                                    id_submenu) 
                                                    VALUES (
                                                    $this->id,
                                                    $menu,
                                                    $submenu)");
        
    }
    
    function deleteotoritas(){
        
        mysql_query("DELETE FROM level_otoritas WHERE id_level = $this->id");
        
    }
    
    function editotoritas($menu,$submenu){
        $this->deleteotoritas();
        $this->addotoritas($menu, $submenu);
    }
    
    function searchid($session){
        $level = mysql_query("SELECT * FROM level WHERE session = '$session'");
        $l = mysql_fetch_array($level);
        
        $data = $l[id_level];
        
        return $data;
    }
}
