<?php

class TieAli{
	var  $tabla = '0';
	var  $id_tipali = '0';
	var  $cve_tipali = '0';
	var  $des_tipali = '0';
	var  $usuario = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $lastInsId = '0';

	function TieAli($data){
		$this->tabla='rctipali';
		$this->resetData($data);
	}

	function resetData($data){
		$this->id_tipali=$this->checkData($data['idTipAli'],'');
		$this->cve_tipali=$this->checkData($data['cveTipAli'],'');
		$this->des_tipali=$this->checkData($data['desTipAli'],'');
		$this->usuario=$this->checkData($data['usuario'],$_SESSION['num_usu']);
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET cve_tipali='$this->cve_tipali', des_tipali='$this->des_tipali', usuario='$this->usuario', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET cve_tipali='$this->cve_tipali', des_tipali='$this->des_tipali', usuario='$this->usuario', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor' WHERE id_tipali='$this->id_tipali'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function busReg(){
		$sq_retrieve="SELECT * FROM $this->tabla WHERE id_tipali='$this->id_tipali'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}

	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE id_tipali='$this->id_tipali'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}

}
?>