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

/* 
A domain Class to demonstrate RESTful web services
*/
Class Mobile {
	private $mobiles = array();
	
	public function getUsuario(){
	   if (isset($_REQUEST['usuario'])){$usuario=$_REQUEST['usuario'];}else{$usuario="";}
	   
			$query = "SELECT nomper as nombre,nomusu as usuario,conusu as clave FROM sisusu where nomusu='".$usuario."'" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
	}	
	
	public function getLicencia(){
		if (isset($_REQUEST['nit'])){$nit=$_REQUEST['nit'];}else{$nit="";}
	   $conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   $conng-> set_charset("utf8");
	   if($result = $conng->query("select * from licemp where nriper='".$nit."'")) {
   			$nr= $result->num_rows;
			if($nr>=1){
				$query = "SELECT nitemp as nit,nomcom as nomcomercial,nomemp as nomempresa,diremp as dirempresa FROM admemp where nitemp='".$nit."'" ;
				$dbcontroller = new DBController();
				$this->mobiles = $dbcontroller->executeSelectQuery($query);
				return $this->mobiles;
			}
	   }
	}
	
	public function getActividad(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
	   	if (isset($_REQUEST['actividad'])){$actividad=$_REQUEST['actividad'];}else{$actividad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawact(nomact,obsact) values('".$actividad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawact where estact!='".$syseli."' order by nomact" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	
	public function getEspecialidad(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
	   	if (isset($_REQUEST['especialidad'])){$especialidad=$_REQUEST['especialidad'];}else{$especialidad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawesp(nomesp,obsesp) values('".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawesp where estesp!='".$syseli."' order by nomesp" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}
	
	public function getProceso(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['proceso'])){$proceso=$_REQUEST['proceso'];}else{$proceso="";}
	   	if (isset($_REQUEST['especialidad'])){$especialidad=$_REQUEST['especialidad'];}else{$especialidad="";}
		if (isset($_REQUEST['observacion'])){$observacion=$_REQUEST['observacion'];}else{$observacion="";}
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT * FROM lawpro where estpro!='".$syseli."' order by nompro" ;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	
	public function getModulo(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['modulo'])){$modulo=$_REQUEST['modulo'];}else{$modulo="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			if($accion=="e"){
				$query = "SELECT moddet.*,modcmp.titmod,modcmp.obsmod,modcmp.nommod FROM moddet,modcmp where moddet.modulo=modcmp.nommod and moddet.estado!='".$syseli."' and moddet.modulo='".$modulo."' order by moddet.bloque,moddet.orden,moddet.nombre" ;
			}
			if($accion!="e"){
				$query = "SELECT moddet.*,modcmp.titmod,modcmp.obsmod,modcmp.nommod FROM moddet,modcmp where moddet.modulo=modcmp.nommod and moddet.estado!='".$syseli."' and moddet.modulo='".$modulo."' and moddet.chkpub=1 order by moddet.bloque,moddet.orden,moddet.nombre" ;
			}
			
			//echo $query;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}
	
	public function getRegistro(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['modulo'])){$modulo=$_REQUEST['modulo'];}else{$modulo="";}
		if (isset($_REQUEST['campo'])){$vcampo=$_REQUEST['campo'];}else{$vcampo="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if (isset($_REQUEST['jsondatos'])){$jsondatos=$_REQUEST['jsondatos'];}else{$jsondatos="";}
		if (isset($_REQUEST['jsoncampos'])){$jsoncampos=$_REQUEST['jsoncampos'];}else{$jsoncampos="";}
		if (isset($_REQUEST['accion'])){$accion=$_REQUEST['accion'];}else{$accion="";}
		if (isset($_REQUEST['json'])){$json=$_REQUEST['json'];}else{$json="";}
		if (isset($_REQUEST['j'])){$j=$_REQUEST['j'];}else{$j=0;}
		echo $jsondatos;
		if($tipo=="r"){
			$tabla="";
			$conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   		$conng-> set_charset("utf8");
			if($result = $conng->query("select tabla from modcmp where nommod='".$modulo."'")) {
   				$nr= $result->num_rows;
				if($nr>=1){
					while($row=mysqli_fetch_assoc($result)) {
			           	$tabla =  $row["tabla"];
            	   	} mysqli_free_result($result);
				}
			}
			if($tabla==""){
				return "ERROR";
			}
			//$jsoncampos=parseString($jsoncampos);
			//$jsondatos=parseString($jsondatos);
			
			
 			$dcod = json_decode($jsoncampos);
			//$prop =$dcod;//temporal
 			$prop = get_object_vars ( $dcod );
 			$ddat = json_decode($jsondatos);
			//$pdat =$ddat;//temporal
 			$pdat = get_object_vars ( $ddat );
 			if($accion=="e"){
				$i=0;
				while ($i < $j) {
					if($i>0 ){
						$campo=$campo.',';
					}
					$campo= $campo.$prop['c'.$i].'="'.$pdat[$prop['c'.$i]].'"';
					$i++;
				}
				$query=" update ".$tabla." set ".$campo." where Id='".$vcampo."'";
			}
			if($accion!="e"){
				$campo="insert into ".$tabla."(";
				$datos=" values(";
				$i=0;
				while ($i < $j) {
					if($i>0){
						$campo=$campo.',';
						$datos=$datos.',';
					}
					$campo= $campo.$prop['c'.$i];
					$datos=$datos.'"'.$pdat[$prop['c'.$i]].'"';
					$i++;
				}
				$campo= $campo.')';
				$datos= $datos.')';
				$query = $campo.$datos;
			}
			echo $query;
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$conng = mysqli_connect('66.70.255.14','netempire_elsycb','Tm3Y@1CtWn67','netempire_elsy');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query("select listasql,tabla from modcmp where nommod='".$modulo."'")) {
   				$nr= $result->num_rows;
				if($nr>=1){
					while($row=mysqli_fetch_assoc($result)) {
		            	$query =  $row["listasql"];
						$tabla = $row["tabla"];
						if($accion=="e"){
							$query="select * from ".$tabla." where estado!='Eliminado' and id='".$vcampo."'";							
						}

                	} mysqli_free_result($result);
					//echo $query;
					$dbcontroller = new DBController();
					$this->mobiles = $dbcontroller->executeSelectQuery($query);
					return $this->mobiles;
				}
			}
		}
		
	}
	
	public function getMenu(){
		$syseli="Eliminado";
		if (isset($_REQUEST['tipo'])){$tipo=$_REQUEST['tipo'];}else{$tipo="";}
		if (isset($_REQUEST['proyecto'])){$proyecto=$_REQUEST['proyecto'];}else{$proyecto="";}
		if($tipo=="r"){
			$query = "insert into lawpro(nompro,codesp,nomesp,obspro) values('".$proceso."','".$especialidad."','".$especialidad."','".$observacion."')";
			$dbcontroller = new DBController();
			$Resp = $dbcontroller->executeInsertQuery($query);
			if($Resp!="ok"){
				return "ERROR";
			}
		}
		if($tipo=="l"){
			$query = "SELECT nommod,titmod,obsmod,link,proyecto FROM modcmp order by nommod" ;
			if($proyecto!="Admempire"){
				$query = "SELECT nommod,titmod,obsmod,link,proyecto FROM modcmp where proyecto='".$proyecto."' order by nommod";
				}
			//echo $query;
			$dbcontroller = new DBController();
			$this->mobiles = $dbcontroller->executeSelectQuery($query);
			return $this->mobiles;
		}
		
	}	
	
	function parseString($string) {
			return '"'.$string.'"'; 
			
		   $string = str_replace("\\", "\\\\", $string);
		   $string = str_replace('/', "\\/", $string);
		   $string = str_replace('"', "\\".'"', $string); 
		   $string = str_replace("\b", "\\b", $string); 
		   $string = str_replace("\t", "\\t", $string); 
		   $string = str_replace("\n", "\\n", $string); 
		   $string = str_replace("\f", "\\f", $string); 
		   $string = str_replace("\r", "\\r", $string); 
		   $string = str_replace("\u", "\\u", $string); 
		   return '"'.$string.'"'; 
	}
	
	 function clear ($string) {
	 	$resultado = htmlspecialchars($cadena,ENT_QUOTES );
		$resultado = str_replace("\n","<br/>",$resultado );
		return  $resultado ;
	}
	
	function parseData($data) { 
		 
		if(is_array($data)) { 
			$parsed = array(); 
			foreach ($data as $key=>$value) array_push($parsed, $this->parseString($key).':'.$this->parseData($value));
			return '{'.implode(',', $parsed).'}'; 
		} else return $this->parseString($data); }


}
?>