<?php
class BBDD {
	private $host;
	private $username;
	private $password;
	private $db_name;
	private $con;
	
	//Constructor
	public function __Construct() {		
		$this->host = "mysql380int.srv-hostalia.com";
		$this->username = "u5837549_1l2c";
		$this->password = "QpVQ8Rzr";
		$this->db_name = "db5837549_1list2cookAPP";
		$this->connect();
	}
	
	//Estableix la connexió amb la BBDD
	private function connect() {
		$this->con = mysqli_connect($this->host, $this->username, $this->password, $this->db_name) or die("cannot connect");
		mysqli_set_charset($this->con,"utf8");
	}
	
	//Funció que valida i retorna el camp adaptat, si torna "false" significa que no s'ha pogut validar
	public function checkParameter($request,$numeric = false,$email = false,$lower = false) {
		$return = 0;
		
		$request = $_REQUEST[$request];
		
		//Comprova si existeix i no esta buit
		if(isset($request) && (!empty($request) || $request == "0")) {
			//Si s'ha de validar que sigui númeric es valida
			if($numeric) {
				if(is_numeric ($request)) $return = 1;
				else $return = 3; //Error no és un valor numèric
			} else {
				$return = 1;
			}
			//Si s'ha de validar que tingui format e-mail es valida
			if($email) {
				if (filter_var($request, FILTER_VALIDATE_EMAIL)) $return = 1;
				else $return = 2; //Error no és un email vàlid
			} else {
				$return = 1;
			}
				
		}
		
		//Si totes les validacions són correctes es prepara el paràmetre
		if($return === 1) {	
			$return = addslashes(strip_tags($request));
			//Si s'ha especificat que sigui en minuscules, es converteix en minuscules
			if($lower) $return = strtolower($return);
		}
		
		return $return;
	}
	
	//Consulta
	public function select($fields,$table,$where = false,$order = false,$limit = false) {
		$sql = "SELECT ".$fields." FROM ".$table;
		if($where !== false) $sql .= " WHERE ".$where;
		if($order !== false) $sql .= " ORDER BY ".$order;
		if($limit !== false) $sql .= " LIMIT 0,".$limit;
		$sql .= ";";
		
		$result = mysqli_query($this->con,$sql);
		
		return $result;
	}
	
	//Consulta unica
	public function selectUnique($field,$table,$where = false) {
		$return = 0;
		
		$sql = "SELECT ".$field." as result FROM ".$table;
		if($where !== false) $sql .= " WHERE ".$where;
		$sql .= " LIMIT 0,1;";
		
		$result = mysqli_query($this->con,$sql);
		
		if($row = mysqli_fetch_object($result)) {
			$return = $row->result;
		}
		
		return $return;
	}
	
	//Insert
	public function insert($table,$fields,$values) {
		$sql = "INSERT INTO $table ($fields) VALUES($values);";
		
		$result = mysqli_query($this->con,$sql);
		
		return $result;
	}
	
	//Insert y retorna el ID
	public function insertID($table,$fields,$values) {		
		$result = $this->insert($table,$fields,$values);
		
		return mysqli_insert_id($this->con);
	}
	
	//Update
	public function update($table,$fields,$where) {
		$sql = "UPDATE $table SET $fields WHERE $where;"; 
		
		$result = mysqli_query($this->con,$sql);
		
		return $result;
	}
	
	//Delete
	public function delete($table,$where) {
		$sql = "DELETE FROM $table WHERE $where;"; 
		
		$result = mysqli_query($this->con,$sql);
		
		return $result;
	}
	
	//Transforma un array de resultats a JSON
	public function resultsToJson($result) {		
		$json = array();

		if(mysqli_num_rows($result)) {
			while($row = mysqli_fetch_object($result)) {
				$json[] = $row;
			}
		}
		
		return $json;
	}
	
	//Desconnexió de la BBDD
	public function disconnect() {
		mysqli_close($this->con);
	}
}
?>