|
|
<?php
|
|
|
abstract class ModelBase extends ModelAbstract{
|
|
|
|
|
|
protected $db;
|
|
|
public $fieldArray = array();
|
|
|
public $strQuery = "";
|
|
|
public $tableName; //nombre de la tabla con la que trabajará cada 'modelo' de datos
|
|
|
public $id = array();
|
|
|
|
|
|
public function __construct(){
|
|
|
$this->db = SPDO::singleton();
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
Autor: Carlos Augusto Espinoza Galarza
|
|
|
Descripcion: Generamos la clase Pagina con los tres principales pilares
|
|
|
Update - Deleted - Insert
|
|
|
Fecha Creación: 28/02/2009
|
|
|
Modificado: Noe Medina Arce
|
|
|
Descripcion: Adecuar funciones para trabajar en singleton
|
|
|
Fecha Modificación: 03/08/2010
|
|
|
*/
|
|
|
|
|
|
//Establecemos el valor de la tabla a emplear
|
|
|
public function setTableName($valor=NULL){
|
|
|
$this->tableName = $valor;
|
|
|
}
|
|
|
|
|
|
public function setFieldArray($valuesArray=NULL){
|
|
|
if(!is_null($valuesArray) && (count($valuesArray) > 0)){
|
|
|
$this->fieldArray = $valuesArray;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function setIdKey($value = NULL){
|
|
|
$this->id['key'] = $value;
|
|
|
}
|
|
|
|
|
|
public function setIdValue($value = NULL){
|
|
|
$this->id['value'] = $value;
|
|
|
}
|
|
|
|
|
|
public function setQuery($value=NULL){
|
|
|
$this->strQuery = $value;
|
|
|
}
|
|
|
|
|
|
//obtenemos el valor de la tabla que estamos trabajando
|
|
|
public function getTableName(){
|
|
|
return $this->tableName;
|
|
|
}
|
|
|
|
|
|
public function getQuery(){
|
|
|
return $this->strQuery;
|
|
|
}
|
|
|
|
|
|
public function execute(){
|
|
|
$query = $this->db->prepare($this->strQuery);
|
|
|
|
|
|
//$query = $this->db->prepare('select * from items');
|
|
|
$query->execute();
|
|
|
//$this->db->execute();
|
|
|
return $query;
|
|
|
//return $this->strQuery;
|
|
|
}
|
|
|
|
|
|
/*Ahora vamaos hacer los 3 Procesos Básicos Insert-Update-Deleted*/
|
|
|
public function insert($datos=""){
|
|
|
if(!empty($datos) && is_array($datos)){
|
|
|
$arr_atributos = $datos;
|
|
|
}else{
|
|
|
$arr_atributos = $this->fieldArray;
|
|
|
}
|
|
|
|
|
|
$count_atributos = count($arr_atributos);
|
|
|
if ($count_atributos!=0){
|
|
|
$cad_insert = "";//-->inicializamos en vacio
|
|
|
$cont_aux = 1;
|
|
|
/**** Generamos la cadena de inserccion ***/
|
|
|
$cad_values="";
|
|
|
$cad_insert.="INSERT INTO ".$this->tableName." (";
|
|
|
foreach ($arr_atributos as $key=>$value){
|
|
|
if ($cont_aux==$count_atributos){
|
|
|
$cad_insert.=$key;
|
|
|
$value1=trim($value);
|
|
|
if (empty($value1))
|
|
|
$cad_values.="'NULL'";
|
|
|
else
|
|
|
$cad_values.="'".$value1."'";
|
|
|
}else{
|
|
|
$cad_insert.=$key.",";
|
|
|
$value1=trim($value);
|
|
|
if (empty($value1))
|
|
|
$cad_values.="'NULL',";
|
|
|
else
|
|
|
$cad_values.="'".$value1."',";
|
|
|
}
|
|
|
$cont_aux++;
|
|
|
}
|
|
|
$cad_insert.=") ";
|
|
|
$cad_insert.=" VALUES (";
|
|
|
$cad_insert.=$cad_values;
|
|
|
$cad_insert.=")";
|
|
|
$this->setQuery($cad_insert);
|
|
|
$resultado = $this->execute();
|
|
|
if($resultado){
|
|
|
$id = $this->db->lastInsertId();
|
|
|
$mensaje = " Insercion satisfactoria ";
|
|
|
}else{
|
|
|
$id = 0;
|
|
|
$mensaje = " Error en la cadena de insercion";
|
|
|
}
|
|
|
}else{
|
|
|
$id = 0;
|
|
|
$mensaje = "La sentencia ha venido vacia no se puede insertar los datos";
|
|
|
//exit();
|
|
|
}
|
|
|
|
|
|
return array($resultado,$id,$mensaje);
|
|
|
|
|
|
}
|
|
|
|
|
|
public function update($datos=""){
|
|
|
if(!empty($datos) && is_array($datos)){
|
|
|
$arr_atributos = $datos;
|
|
|
}else{
|
|
|
$arr_atributos = $this->fieldArray;
|
|
|
}
|
|
|
$count_atributos = count($arr_atributos);
|
|
|
if ($count_atributos!=0){
|
|
|
$cad_update = "";//-->inicializamos en vacio
|
|
|
$cont_aux = 1;
|
|
|
/****Generamos la cadena de inserccion***/
|
|
|
$cad_values="";
|
|
|
$cad_update.="UPDATE ".$this->tableName." ";
|
|
|
$cad_update.=" SET ";
|
|
|
foreach ($arr_atributos as $key=>$value){
|
|
|
if ($cont_aux==$count_atributos){
|
|
|
$value1=trim($value);
|
|
|
if (empty($value1))
|
|
|
$cad_update.=$key." = '' ";
|
|
|
else
|
|
|
$cad_update.=$key." = '".$value."' ";
|
|
|
}
|
|
|
else{
|
|
|
$value1=trim($value);
|
|
|
if (empty($value1))
|
|
|
$cad_update.=$key." = '".$value."', ";
|
|
|
else
|
|
|
$cad_update.=$key." = '".$value."', ";
|
|
|
}
|
|
|
$cont_aux++;
|
|
|
}
|
|
|
$cad_update.=" WHERE ".$this->id['key']." = ".$this->id['value']."";
|
|
|
$this->setQuery($cad_update);
|
|
|
$resultado = $this->execute();
|
|
|
if($resultado){
|
|
|
$mensaje = " Datos actualizados satisfactoriamente";
|
|
|
}else{
|
|
|
$mensaje = " Problemas al actualizar los datos";
|
|
|
}
|
|
|
}
|
|
|
else{
|
|
|
$mensaje = "La sentencia ha venido vacia no se puede actualizar los datos";
|
|
|
|
|
|
}
|
|
|
return array ($resultado,$mensaje);
|
|
|
}
|
|
|
|
|
|
public function delete($id=""){
|
|
|
$resultado = "";
|
|
|
$mensaje = "";
|
|
|
if(!empty($id)){
|
|
|
$this->setIdValue($id);
|
|
|
}
|
|
|
$arr_atributos = $this->fieldArray;
|
|
|
|
|
|
$existe_pk = $this->validateId($this->id['value']);
|
|
|
if ($existe_pk==1){
|
|
|
$cad_delete = "";
|
|
|
$cad_delete.= " DELETE FROM ".$this->tableName." ";
|
|
|
$cad_delete.= " WHERE ".$this->id['key']." = ".$this->id['value']."";
|
|
|
$this->setQuery($cad_delete);
|
|
|
$resultado = $this->execute();
|
|
|
if($resultado){
|
|
|
$mensaje = " registro eliminado satisfactoriamente ";
|
|
|
}else{
|
|
|
$mensaje = " El registro no pudo ser eliminado ";
|
|
|
}
|
|
|
}else{
|
|
|
$mensaje = "El registro que desea Eliminar no existe en nuestra Base de Datos o Está en Blanco la llave.";
|
|
|
}
|
|
|
|
|
|
return array($resultado, $mensaje);
|
|
|
}
|
|
|
|
|
|
private function validateId(){
|
|
|
if (!empty($this->id['key']) && !empty($this->id['value'])){
|
|
|
$cad_verfi = "SELECT COUNT(".$this->id['key'].") as total FROM ".$this->tableName." WHERE ".$this->id['key']." =".$this->id['value'];
|
|
|
$this->setQuery($cad_verfi);
|
|
|
//$resultado = $this->execute();
|
|
|
$row = $this->loadObjectList();
|
|
|
$total_registros = $row[0]->total;
|
|
|
if ($total_registros!=0)
|
|
|
return "1"; //--> existe
|
|
|
else
|
|
|
return "0"; //--> no existe
|
|
|
}
|
|
|
else{
|
|
|
return "0"; //--> no existe
|
|
|
}
|
|
|
}
|
|
|
public function getObjectList(){
|
|
|
|
|
|
$stmt=$this->setQuery($this->strQuery);
|
|
|
return $this->loadObjectList();
|
|
|
}
|
|
|
|
|
|
public function getObject(){
|
|
|
|
|
|
$stmt=$this->setQuery($this->strQuery);
|
|
|
if( $result = $this->execute() ){
|
|
|
$obj = $result->fetchObject();
|
|
|
return $obj;
|
|
|
|
|
|
}else{
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public function loadObjectList(){
|
|
|
if ( ! ( $cur = $this->execute() ) ){
|
|
|
return null;
|
|
|
}
|
|
|
$array = array();
|
|
|
while ( $obj = $cur->fetchObject() ){
|
|
|
//fetchObject()
|
|
|
$array[] = $obj;
|
|
|
}
|
|
|
return $array;
|
|
|
}
|
|
|
//NOE MEDINA ARCE 20100507
|
|
|
|
|
|
public function getPager($datos = array("total" => 0, "inicio" => 0, "pagina" => 5)){
|
|
|
|
|
|
$num_registros = $datos['total']; //total registros
|
|
|
$tam_pag = $datos['pagina'];// tamaño de pagina a mostrar
|
|
|
$inicio = $datos['inicio'];// inicio establecido de pagina a mostrar
|
|
|
|
|
|
//Generando el arreglo paginas.
|
|
|
if(is_numeric($num_registros)){
|
|
|
if($num_registros < $tam_pag){
|
|
|
$arreglo_paginas[0] = 0;
|
|
|
}else{
|
|
|
$num_paginas = $num_registros / $tam_pag;
|
|
|
$num_paginas = floor($num_paginas);
|
|
|
if(($num_registros%$tam_pag)>0){
|
|
|
$num_paginas++;
|
|
|
}
|
|
|
$arreglo_paginas = "";
|
|
|
$acumulado = 0;
|
|
|
for($i=0;$num_paginas>$i;$i++){
|
|
|
$arreglo_paginas[$i] = $acumulado;
|
|
|
$acumulado = $acumulado + $tam_pag;
|
|
|
}
|
|
|
}
|
|
|
}else{
|
|
|
$arreglo_paginas = "error";
|
|
|
|
|
|
}
|
|
|
$paginas = $arreglo_paginas;
|
|
|
|
|
|
//Generando el arreglo navegador
|
|
|
$b = count($arreglo_paginas);
|
|
|
//echo("cantidad de paginas $b");
|
|
|
$pagina_inicio = $arreglo_paginas[0];
|
|
|
for($a=0;$b>$a;$a++){
|
|
|
if($arreglo_paginas[$a] == $inicio){
|
|
|
@$pagina_anterior = $arreglo_paginas[$a-1];
|
|
|
@$pagina_siguiente = $arreglo_paginas[$a+1];
|
|
|
if($a==0){
|
|
|
@$pagina_anterior = $arreglo_paginas[0];
|
|
|
@$pagina_siguiente = $arreglo_paginas[$a+1];
|
|
|
}
|
|
|
if($a==$b-1){
|
|
|
@$pagina_anterior = $arreglo_paginas[$a-1];
|
|
|
@$pagina_siguiente = $arreglo_paginas[$b-1];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//pagina final
|
|
|
$x = count($arreglo_paginas);
|
|
|
$pagina_final = $arreglo_paginas[$x-1];
|
|
|
//generando el arreglo
|
|
|
$navegador["inicio"] = $pagina_inicio;
|
|
|
$navegador["anterior"] = $pagina_anterior;
|
|
|
$navegador["siguiente"] = $pagina_siguiente;
|
|
|
$navegador["fin"] = $pagina_final;
|
|
|
|
|
|
return array($paginas,$navegador);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
?>
|