<?php
/*
*/
header('Cache-control: private'); // IE 6 FIX
class Crud {
	public $insertInto;
	public $insertColumns;
	public $insertValues;
	public $message;
	public $select;
	public $from;
	public $condition;
	public $rows;
	public $update;
	public $set;
	public $deleteFrom;
	public $consulta;
	public $conexion;

	public $parametros;
	public $procedure;

	public function Directo(){
		global $parametros;
		global $procedure;
		$model = new Conexion();
		$conexion = $model->conectar();
		$parametros = $this->parametros;
		$procedure = $this->procedure;
		$sql = "CALL $procedure($parametros)";
		error_log($sql);
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		while ($filas = $consulta->fetch()) {
			$this->rows[] = $filas;
		}
	}
	public function Create(){
		global $lang;
		$model = new conexion;
		$conexion = $model->conectar();
		$insertInto = $this->insertInto;
		$insertColumns = $this->insertColumns;
		$insertValues = $this->insertValues;
		$sql = "INSERT INTO $insertInto ($insertColumns) VALUES ($insertValues)";
		error_log($sql);
		
		$querySincro = "insert into sincronizacion(Consulta) values(\"$sql\");";
		error_log($querySincro);
		$nose = $conexion->prepare($querySincro);
		if (!$nose) {
		    $error_log("Fallo sincronizacion");}
		else {
			$nose->execute();
		}
		
		$consulta = $conexion->prepare($sql);
		if (!$consulta) {
			$this->message = "Error al registrar";
		}
		else {
			if ($consulta->execute()) {
				$this->message = "Registrado correctamente";
				
			}
			else {
				$mensajeError = $conexion->errorInfo();
				$this->message = $mensajeError[2];
			}
		}
		
	}

	public function CreateId(){
		global $lang;
		$model = new conexion;
		$conexion = $model->conectar();
		$insertInto = $this->insertInto;
		$insertColumns = $this->insertColumns;
		$insertValues = $this->insertValues;
		$sql = "INSERT INTO $insertInto ($insertColumns) VALUES ($insertValues)";
		error_log($sql);
		
		$querySincro = "insert into sincronizacion(Consulta) values(\"$sql\");";
		error_log($querySincro);
		$nose = $conexion->prepare($querySincro);
		if (!$nose) {
		    $error_log("Fallo sincronizacion");}
		else {
			$nose->execute();
		}
		
		$consulta = $conexion->prepare($sql);
		if (!$consulta) {
			$this->message = "Error al registrar";
		}
		else {
			$consulta->execute();
			$last_id = $conexion->lastInsertId();
			$this->message = $last_id;
		}
	}
	
	public function CreateIdWithSelect(){
		global $lang;
		$model = new conexion;
		$conexion = $model->conectar();
		$insertInto = $this->insertInto;
		$insertColumns = $this->insertColumns;
		$insertValues = $this->insertValues;
		$sql = "INSERT INTO $insertInto ($insertColumns) $insertValues";
		error_log($sql);

		$querySincro = "insert into sincronizacion(Consulta) values(\"$sql\");";
		error_log($querySincro);
		$nose = $conexion->prepare($querySincro);
		if (!$nose) {
		    $error_log("Fallo sincronizacion");}
		else {
			$nose->execute();
		}

		$consulta = $conexion->prepare($sql);
		if (!$consulta) {
			$this->message = "Error al registrar";
		}
		else {
			$consulta->execute();
			$last_id = $conexion->lastInsertId();
			$this->message = $last_id;
		}
	}

	public function Read(){
		$model = new Conexion();
		$conexion = $model->conectar();
		$select = $this->select;
		$from = $this->from;
		$condition = $this->condition;
		if ($condition != '') {
			$condition = "WHERE " . $condition;
		}
		$sql = "SELECT $select FROM $from $condition";
		//echo "<br>" . $sql . "<br>";
		error_log($sql);
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		while ($filas = $consulta->fetch(PDO::FETCH_ASSOC)) {
			$this->rows[] = $filas;
		}
	}
	
	public function ReadNoNumbers(){
		$model = new Conexion();
		$conexion = $model->conectar();
		$select = $this->select;
		$from = $this->from;
		$condition = $this->condition;
		if ($condition != '') {
			$condition = "WHERE " . $condition;
		}
		$sql = "SELECT $select FROM $from $condition";
		//echo "<br>" . $sql . "<br>";
		error_log($sql);
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		while ($filas = $consulta->fetch(PDO::FETCH_ASSOC)) {
			$this->rows[] = $filas;
		}
	}

	public function ReadId(){
		$model = new Conexion();
		$conexion = $model->conectar();
		$from = $this->from;
		$sql = "SHOW TABLE STATUS LIKE '$from'";
		//echo "<br>" . $sql . "<br>";
		error_log($sql);
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		while ($filas = $consulta->fetch()) {
			$this->rows[] = $filas;
		}
	}

	public function ReadAll(){
		$model = new Conexion();
		$conexion = $model->conectar();
		$select = $this->select;
		$from = $this->from;
		$condition = $this->condition;
		if ($condition != '') {
			$condition = "WHERE " . $condition;
		}
		$sql = "SELECT $select FROM $from $condition";
		//echo "<br>" . $sql . "<br>";
		error_log($sql);
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		$filas = $consulta->fetchAll();
		$this->rows = $filas;
	}

	public function Read2() {
		$model = new Conexion();
		$conexion = $model->conectar();
		$select = $this->select;
		$from = $this->from;
		$condition = $this->condition;
		if ($condition != ''){
			$condition = "WHERE " . $condition;
		}
		//$sql = "SELECT $select FROM $from $condition ORDER BY id ASC";
		$consulta = $conexion->prepare($sql);
		$consulta->execute();
		while ($filas = $consulta->fetch()) {
			$this->rows[] = $filas;
		}
	}
	public function Update() {
		global $lang;
		$model = new Conexion;
		$conexion = $model->conectar();
		$update = $this->update;
		$set = $this->set;
		$condition = $this->condition;
		if ($condition != '') {
			$condition = " WHERE " . $condition;
		}
		$sql = "UPDATE $update SET $set $condition";
		error_log($sql);
		
		$querySincro = "insert into sincronizacion(Consulta) values(\"$sql\");";
		error_log($querySincro);
		$nose = $conexion->prepare($querySincro);
		if (!$nose) {
		    $error_log("Fallo sincronizacion");}
		else {
			$nose->execute();
		}
		
 		//echo "<br>" . $sql . "<br>";
		$consulta = $conexion->prepare($sql);
		if (!$consulta) {
			$this->message = "Error al actualizar";
		}
		else {
			$consulta->execute();
			$this->message = "Actualizado correctamente";
		}
	}
	public function Delete(){
		global $lang;
		$model = new Conexion;
		$conexion = $model->conectar();
		$deleteFrom = $this->deleteFrom;
		$condition = $this->condition;
		if ($condition != '') {
			$condition = " WHERE " . $condition;
		}
		$sql = "DELETE FROM $deleteFrom $condition";
		error_log($sql);
		
		$querySincro = "insert into sincronizacion(Consulta) values(\"$sql\");";
		error_log($querySincro);
		$nose = $conexion->prepare($querySincro);
		if (!$nose) {
		    $error_log("Fallo sincronizacion");}
		else {
			$nose->execute();
		}
		
		//echo "<br>" . $sql . "<br>";
		$consulta = $conexion->prepare($sql);
		if (!$consulta) {
			$this->message = "Error al eliminar";
		}
		else {
			$OK = $consulta->execute();
			$this->message = "Eliminado correctamente";
			if (!$OK) {
				return false;
			}
		}
		return true;
	}
}
?>
