<?php
require_once ("kontrolDB.php");

class administrator
{
	private $db_handle;
	
    public function __construct()
    {
       $this->db_handle = new kontrolDB();
    }
    
    public function tampil()
    {        
		$sql = "SELECT * FROM knn_administrator ORDER BY id_administrator";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampilById($id)
    {        
		$query = "SELECT * FROM knn_administrator WHERE id_administrator=?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
    public function cekAdministrator($data, $pilih)
    {     
		$query=""; $paramType=""; $paramValue=array();  
		if($pilih==1) {
			$query = "SELECT * FROM knn_administrator WHERE username=?";
			$paramType = "s";
			$paramValue = array(
				$data[0]
			);
		}elseif($pilih==2) {
			$query = "SELECT * FROM knn_administrator WHERE username=? AND id_administrator<>?";
			$paramType = "si";
			$paramValue = array(
				$data[0], $data[1]
			);
		}
        
        $result = $this->db_handle->cekData($query, $paramType, $paramValue);
        return $result;
    }
		
    public function tambah($data=array()) 
    {
        $query = "INSERT INTO knn_administrator (nama,username,password,terdaftar) 
									  VALUES (?, ?, ?, ?)";
        $paramType  = "ssss";
        $paramValue = array();
		$paramValue = $data;
        
        $insertId   = $this->db_handle->insert($query, $paramType, $paramValue);
        
        if ($insertId>0) {
			return true;
        } else {
            echo 'Error: cannot execute the command';
            return false;
        }        
    }
	
    public function ubah($data=array(), $id, $pass) 
    {
		$query=""; $paramType=""; $paramValue=array();  
		if($pass==1) {
			$query = "UPDATE knn_administrator SET nama=?,username=?,password=? WHERE id_administrator=?";
			$paramType  = "sssi";
			$paramValue = array($data[0],$data[1],$data[2],$id);        
		} elseif($pass==0) {
			$query = "UPDATE knn_administrator SET nama=?,username=? WHERE id_administrator=?";
			$paramType  = "ssi";
			$paramValue = array($data[0],$data[1],$id);        
		}
		
		$result = $this->db_handle->update($query, $paramType, $paramValue);
        if ($result==false) {
            echo 'Error: cannot execute the command';
            return false;
        } else {
            return true;
        } 
    }
	
    public function ubahPassword($data, $id) 
    {
		$query = "UPDATE knn_administrator SET password=? WHERE id_administrator=?";
		$paramType  = "si";
		$paramValue = array($data,$id);        
		
		$result = $this->db_handle->update($query, $paramType, $paramValue);
        if ($result==false) {
            echo 'Error: cannot execute the command';
            return false;
        } else {
            return true;
        } 
    }
	
    public function hapus($id) {
        $query = "DELETE FROM knn_administrator WHERE id_administrator= ?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);
		return $result;
    }
	
	public function escapeString($value)
    {
        return $this->db_handle->escape_string($value);
    }
    
}
?>