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

class kriteria
{
	private $db_handle;
	
    public function __construct()
    {
       $this->db_handle = new kontrolDB();
    }
    
    public function tampil()
    {        
		$sql = "SELECT * FROM knn_kriteria ORDER BY id_kriteria ASC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }

    public function tampil_inp()
    {        
        $sql = "SELECT * FROM knn_kriteria WHERE masukan<>3 ORDER BY id_kriteria ASC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }

    public function tampil_kriteria()
    {        
		$sql = "SELECT * FROM knn_kriteria WHERE jenis=1 ORDER BY id_kriteria ASC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampil_keputusan()
    {        
		$sql = "SELECT * FROM knn_kriteria WHERE jenis=2 ORDER BY id_kriteria ASC LIMIT 0,1";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
	
    public function tampilById($id)
    {        
		$query = "SELECT * FROM knn_kriteria WHERE id_kriteria=?";
        $paramType = "i";
        $paramValue = array(
            $id
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
    public function cekkriteria($data, $pilih)
    {     
		$query=""; $paramType=""; $paramValue=array();  
		if($pilih==1) {
			$query = "SELECT * FROM knn_kriteria WHERE kriteria=?";
			$paramType = "s";
			$paramValue = array(
				$data[0]
			);
		}elseif($pilih==2) {
			$query = "SELECT * FROM knn_kriteria WHERE kriteria=? AND id_kriteria<>?";
			$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_kriteria (kriteria,jenis,masukan,keterangan) 
									  VALUES (?, ?, ?, ?)";
        $paramType  = "siis";
        $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) 
    {
		$query = "UPDATE knn_kriteria SET kriteria=?, jenis=?, masukan=?, keterangan=? WHERE id_kriteria=?";
		$paramType  = "siisi";
		$paramValue = array($data[0],$data[1],$data[2],$data[3],$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_kriteria WHERE id_kriteria= ?";
        $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);
    }
    
}
?>