<?php

class database {

    private $servername;
    private $user_db;
    private $password_db;
    private $database;
    private $koneksi;

    function database() {
        $this->load_conf_db();
        return $this->connect_db();
    }

    function load_conf_db() {
        $path = dirname(__FILE__) . '/koneksi.php';
        if (file_exists($path)) {
            $conf = include $path;
            $this->servername = @$conf['host'];
            $this->database = @$conf['dbname'];
            $this->user_db = @$conf['username'];
            $this->password_db = @$conf['password'];
        }
    }

    public function connect_db() {
        $this->koneksi = mysqli_connect($this->servername, $this->user_db, $this->password_db, $this->database);
        return $this->koneksi;
    }

    function db_query($sql) {
        $conn = $this->koneksi;
        return mysqli_query($conn, $sql);
    }

    function db_error($result) {
        $conn = $this->koneksi;
        return mysqli_error($conn);
    }

    function db_fetch_array($result) {
        return mysqli_fetch_array($result);
    }

    function db_num_rows($result) {
        return mysqli_num_rows($result);
    }

    function db_insert_id() {
        $conn = $this->koneksi;
        return mysqli_insert_id($conn);
    }

    function insert_record($table, array $val_cols) {
        $field = implode("`, `", array_keys($val_cols));
        $i = 0;
        foreach ($val_cols as $key => $value) {
            $StValue[$i] = "'" . $value . "'";
            $i++;
        }
        $StValues = implode(", ", $StValue);

        $sql = "INSERT INTO $table (`$field`) VALUES ($StValues)";
        
        $result = $this->db_query($sql);
        return $result;
    }

    function delete_record($table, array $val_cols) {
        //Append each element of val_cols associative array 
        $i = 0;
        foreach ($val_cols as $key => $value) {
            $exp[$i] = $key . " = '" . $value . "'";
            $i++;
        }
        $Stexp = implode(" AND ", $exp);

        $sql = "DELETE FROM $table WHERE $Stexp ";

        return $this->db_query($sql);
    }

    function update_record($table, array $set_val_cols, array $cod_val_cols) {
        //append set_val_cols associative array elements 
        $i = 0;
        foreach ($set_val_cols as $key => $value) {
            $set[$i] = $key . " = '" . $value . "'";
            $i++;
        }
        $Stset = implode(", ", $set);

        //append cod_val_cols associative array elements
        $i = 0;
        foreach ($cod_val_cols as $key => $value) {
            $cod[$i] = $key . " = '" . $value . "'";
            $i++;
        }
        $Stcod = implode(" AND ", $cod);

        $sql = "UPDATE $table SET $Stset WHERE $Stcod";

        return $this->db_query($sql);
    }

    function count_data($table, $field, $where = null) {
        $sql = "SELECT COUNT($field) FROM $table ";

        if ($where != null || $where != '') {
            $sql .= " WHERE $where ";
        }

        $result = $this->db_query($sql);
        return $row = $this->db_fetch_array($result);
    }

    function display_table_all_column($table, $where = null, $fetch = false, $limit = false, $limit_posisi = 0, $limit_batas = 0, $sort = '') {
        $sql = "SELECT * FROM $table ";

        if ($where != null || $where != '') {
            $sql .= " WHERE $where";
        }

        if (!empty($sort)) {
            $sql .= " ORDER BY " . $sort;
        }

        if ($limit) {
            $sql .= " LIMIT $limit_posisi , $limit_batas ";
        }

        if ($fetch) {
            $result = $this->db_query($sql);
            return $row = $this->db_fetch_array($result);
        } else {
            return $result = $this->db_query($sql);
        }
    }

    function find_in_table($table, $find_column = array(), $where = '') {
        $sql = "SELECT ";
        $column = '';
        if (!is_array($find_column)) {
            $column = $find_column;
        } else {
            $column = implode(",", $find_column);
        }
        $sql .= $column . " FROM " . $table . " " . $where;

        $result = $this->db_query($sql);
        $rows = $this->db_fetch_array($result);

        return $rows;
    }

    function cek_data_is_in_table($table, $field, $value) {
        $sql = "SELECT COUNT(" . $field . ") FROM " . $table . " WHERE " . $field . " = '" . $value . "'";
        $result = $this->db_query($sql);
        $num = $this->db_fetch_array($result);

        if ($num[0] > 0) {
            return true;
        } else {
            return false;
        }
    }

    function get_login_by_id($id_login){
        $sql = "SELECT * FROM login WHERE id_login = ".$id_login;
        $result = $this->db_query($sql);
        $row = $this->db_fetch_array($result);
        return $row;
    }

}

?>