<?php
require_once '../MVC/pdoparameters.php';
/*
*@Autor DNDSIT 
*/
class PDOCONTROLLER{
	private $cadenaconex;
	private $conex;
	
	public function __construct(){
		$conex = $this->connectDB();
		if(!empty($conex)) {
			$this->conex = $conex;			
		}
	}
	function connectDB() {
		try{
			$this->cadenaconex="mysql:host=".DB_SERVER.";port=".DB_PORT.";dbname=".DB_DBNAME.";charset=".DB_CHARSET;
			$options=[
				PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
				PDO::ATTR_EMULATE_PREPARES => false,
			];
			$pdo = new PDO($this->cadenaconex,DB_USER,DB_PASS,$options);
			return $pdo;
		}catch(Exception $ex){
			echo "Error conexions".$ex->getMessage();
			exit;
		}
	}
	

	public function executeconsulta($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$consulta=$this->conex->prepare($sql);
				$consulta->execute($valores);
				if(intval($consulta->errorCode())===0){
					return $consulta->fetchAll(PDO::FETCH_ASSOC);
				}else{
					return intval($consulta->errorCode());
				}
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}

	public function execute_insert($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$insert=$this->conex->prepare($sql);
				$insert->execute($valores);
				$idinsert=$this->conex->lastInsertId();
				return $idinsert;
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}

	public function execute_eliminar($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$elimina=$this->conex->prepare($sql);
				$elimina->execute($valores);
				return "procesado";
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}
}


class PDOECOSCORP{
	private $cadenaconex;
	private $conex;
	private $host = "66.70.255.14";
	private $port = "3306"
	private $user = "netempire_elsycb";
	private $password = "Tm3Y@1CtWn67";
	private $database = "netempire_elsy";
	private $charset ="utf8";

	public function __construct(){
		$conex = $this->connectDB();
		if(!empty($conex)) {
			$this->conex = $conex;			
		}
	}
	function connectDB() {
		try{
			$this->cadenaconex="mysql:host=".$host.";port=".$port.";dbname=".$database.";charset=".$charset;
			$options=[
				PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
				PDO::ATTR_EMULATE_PREPARES => false,
			];
			$pdo = new PDO($this->cadenaconex,$user,$password,$options);
			return $pdo;
		}catch(Exception $ex){
			echo "Error conexions".$ex->getMessage();
			exit;
		}
	}
	

	public function executeconsulta($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$consulta=$this->conex->prepare($sql);
				$consulta->execute($valores);
				if(intval($consulta->errorCode())===0){
					return $consulta->fetchAll(PDO::FETCH_ASSOC);
				}else{
					return intval($consulta->errorCode());
				}
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}

	public function execute_insert($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$insert=$this->conex->prepare($sql);
				$insert->execute($valores);
				$idinsert=$this->conex->lastInsertId();
				return $idinsert;
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}

	public function execute_eliminar($sql="",$valores=array()){
		if($sql != "" && strlen($sql)>0){
			try{
				$elimina=$this->conex->prepare($sql);
				$elimina->execute($valores);
				return "procesado";
			}catch(exception $ex){
				return $ex->getMessage();
			}
		}else{
			echo "else";
			return 0;
		}
	}
}

?>