<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

class User_model extends CI_Model {


    function is_logged_in() {
        if ($this->session) {
            $username = $this->session->userdata('username');
            $password = $this->session->userdata('password');
            $user = $this->session->userdata('accesslevel');
            if (($username != NULL) && ($password != NULL) && ($user != NULL)) {
                if ($this->login($username, $password) == TRUE)
                    return TRUE;
                else
                    return FALSE;
            }
            else
                return FALSE;
        }
        else
            return FALSE;
    }

    function is_superadmin () {
        if ($this->session) {
            $username = $this->session->userdata('username');
            $password = $this->session->userdata('password');
            $user = $this->session->userdata('accesslevel');
            if (($username != NULL) && ($password != NULL) && ($user != NULL)) {
                if ($this->check_superadmin($username, $password) == TRUE)
                    return TRUE;
                else
                    return FALSE;
            }
            else
                return FALSE;
        }
        else
            return FALSE;
    }

    function check_superadmin($username, $password) {
        $this->db->select('username, password, realname');
        $this->db->from('user');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->where('access_level', '1');
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    function get_data($username = "", $password = "") {
        $username = ($username == "") ? $this->session->userdata('username') : $username;
        $password = ($password == "") ? $this->session->userdata('password') : $password;
        $this->db->select('*');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        return $this->db->get('user');
    }

    function login ($username, $password) {
        $this->db->select('username, password, realname');
        $this->db->from('user');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    function update_password($password, $oldpass) {
        $username = $this->session->userdata('username');

        $this->db->set('password', $password);
        $this->db->where('username', $username);
        $this->db->where('password', $oldpass);
        $this->db->update('user');
    }

    function is_admin() {
        return ($this->session->userdata('accesslevel') == '1') ? true : false;
    }

    function is_user($level) {
        return ($this->session->userdata('accesslevel') == $level) ? true : false;
    }

/* ===== user ===== */
    function list_user() {
        $this->db->select('*');
        $this->db->from('user');
        return $this->db->get();
    }

    function insert_user() {
        $this->db->set('username', $this->input->post('username'));
        $this->db->set('password', md5($this->input->post('password')));
        $this->db->set('realname', $this->input->post('realname'));
        $this->db->set('email', $this->input->post('email'));
        $this->db->set('address', $this->input->post('address'));
        $this->db->set('phone', $this->input->post('phone'));
        $this->db->set('access_level', $this->input->post('access_level'));
        $this->db->insert('user');
    }

    function update_user($id) {
        $this->db->set('username', $this->input->post('username'));
        if ($this->input->post('password'))
            $this->db->set('password', md5($this->input->post('password')));
        $this->db->set('realname', $this->input->post('realname'));
        $this->db->set('email', $this->input->post('email'));
        $this->db->set('address', $this->input->post('address'));
        $this->db->set('phone', $this->input->post('phone'));
        $this->db->set('access_level', $this->input->post('access_level'));
        $this->db->where('userid', $id);
        $this->db->update('user');
    }

    function view_user($id) {
        $this->db->where('userid', $id);
        return $this->db->get('user');
    }

    function delete_user($id) {
        $this->db->where('userid', $id);
        $this->db->delete('user');
    }

/* ===== MENU ===== */
    function list_menu() {
        $this->db->select('*');
        $this->db->from('usermenu');
        return $this->db->get();
    }

    function superadminmenu() {
        $this->db->select('*');
        $this->db->from('usermenu');
        $this->db->where('superadmin=1');
        return $this->db->get();
    }

    function adminmenu() {
        $this->db->select('*');
        $this->db->from('usermenu');
        $this->db->where('admin=1');
        return $this->db->get();
    }

    function insert_menu() {
        $this->db->set('nama', $this->input->post('nama'));
        $this->db->set('url', $this->input->post('url'));
        $this->db->set('superadmin', $this->input->post('superadmin'));
        $this->db->set('admin', $this->input->post('admin'));
        $this->db->set('limit', $this->input->post('limit'));
        $this->db->insert('usermenu');
    }

    function update_menu($id) {
        $this->db->set('nomorip', $this->input->post('nomorip'));
        $this->db->set('port', $this->input->post('port'));
        $this->db->set('keterangan', $this->input->post('keterangan'));
        $this->db->where('id', $id);
        $this->db->update('config');
    }

    function view_menu($id) {
        $this->db->where('id', $id);
        return $this->db->get('config');
    }

    function delete_menu($id) {
        $this->db->where('id', $id);
        $this->db->delete('config');
    }
    
/* ===== IP ===== */
    function list_ip() {
        $this->db->select('*');
        $this->db->from('config');
        return $this->db->get();
    }

    function insert_ip() {
        $this->db->set('nomorip', $this->input->post('nomorip'));
        $this->db->set('port', $this->input->post('port'));
        $this->db->set('keterangan', $this->input->post('keterangan'));
        $this->db->insert('config');
    }

    function update_ip($id) {
        $this->db->set('nomorip', $this->input->post('nomorip'));
        $this->db->set('port', $this->input->post('port'));
        $this->db->set('keterangan', $this->input->post('keterangan'));
        $this->db->where('id', $id);
        $this->db->update('config');
    }

    function view_ip($id) {
        $this->db->where('id', $id);
        return $this->db->get('config');
    }

    function delete_ip($id) {
        $this->db->where('id', $id);
        $this->db->delete('config');
    }

    function aktifkan($id) {
        $this->db->set('aktif', 1);
        $this->db->where('id', $id);
        $this->db->update('config');
    }

    function nonaktifkan($id) {
        $this->db->set('aktif', 0);
        $this->db->where('id', $id);
        $this->db->update('config');
    }


}
?>
