<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reg01_model extends Ion_auth_model {
	
    public function __construct()
    {
        parent::__construct();
	}
/*********************** IN_TEMP *******************************/
	/**
	 * in_temp
	 *
	 * @return object
	 * @author Ben Edmunds
	 **/
	public function reg01()
	{ 
		$this->trigger_events('reg01');

		$this->response = $this->db->get($this->tables['reg01']);

		return $this; 
	}

	
	/**
	 * View
	 *
	 * @return array //campos de un registro
	 * @author Ben Edmunds
	 **/

	public function view($tabla = NULL, $arraywhere = NULL) 
	{
		$this->trigger_events('view');
		
		$this->response = $this->db->select('*');
		$this->response = $this->db->from($this->tables[$tabla]);
		foreach ($arraywhere as $k => $v) {

			$this->response = $this->db->where($k, $v);
		}

		$this->response = $this->db->limit(1);


		$this->response = $this->db->get();

		return $this;
	}

	public function eliminar_reg($id_registro_lote)
	{
		$this->db->query("SELECT INDERT.ELIMINAR_REG('".$id_registro_lote."')");

		return TRUE;
	}

	public function concepto ($where1, $blanco = FALSE, $orderby = FALSE, $where2 = FALSE)
	{
		$query = "SELECT CONCEPTO, NOMBRE, DESCRIPCION, TIPOCONCEPTO FROM CONCEPTOS WHERE TIPOCONCEPTO = '".$where1."'";

		if ($where2 == TRUE) {
			$query = $query . " AND CONCEPTO = '" . $where2 . "'";
		}

		if ($blanco == TRUE) {
			$query = $query . " UNION SELECT '  ', 'DEJAR EN BLANCO', '', ''";
		}
		if ($orderby) {
			$query = $query . " ORDER BY ".$orderby;
		}
		$this->response = $this->db->query($query);

		$_SESSION['last_query'] = $this->db->last_query();

		return $this;
	}

	public function dropdown_data($tabla = NULL, $where = NULL, $order_by = NULL)
	{
		if ($where) {
			foreach ($where as $k => $v) {

				$this->response = $this->db->where($k, $v);
			}
		}
		
		if ($order_by) {
			$this->response = $this->db->order_by($order_by, 'ASC');
		}

		$this->response = $this->db->get($this->tables[$tabla]);

		return $this;
	}

	public function importar_foto ($camposvalores = FALSE)
	{
		$query = "INSERT INTO reg_fotos (id_registro_lote, dpto, distrito, barrio, colonia, area, ae, manzana, lote_a, lote_b, finca, padron, cuest, c_tipo_objeto, descrip_objeto, codigo_objeto, image, day) VALUES ('";

		$query = $query . $camposvalores['id_registro_lote']."',";
		$query = $query . $camposvalores['dpto'].",";
		$query = $query . $camposvalores['distrito'].",";
		$query = $query . $camposvalores['barrio'].",";
		$query = $query . $camposvalores['colonia'].",";
		$query = $query . $camposvalores['area'].",";
		$query = $query . $camposvalores['ae'].",'";
		$query = $query . $camposvalores['manzana']."','"; 
		$query = $query . $camposvalores['lote_a']."','";
		$query = $query . $camposvalores['lote_b']."','";
		$query = $query . $camposvalores['finca']."','";
		$query = $query . $camposvalores['padron']."',";
		$query = $query . $camposvalores['cuest'].",";
		$query = $query . $camposvalores['c_tipo_objeto'].",'"; 
		$query = $query . $camposvalores['descrip_objeto']."',";
		$query = $query . $camposvalores['codigo_objeto'].",";
		$query = $query . "lo_import('".$camposvalores['image']."'),";
		$query = $query . "current_date)";
		
		$importar = $this->db->query($query);

		if ($importar) {
			return true;			
		}else{
			return false;
		}
	}

	public function obtener_fotos($id_registro_lote)
	{
		$this->response = $this->db->get_where('reg_fotos',array('id_registro_lote' => $id_registro_lote));
		return $this;
	}

	public function cambiar_clave($id_registro_lote, $array)
	{
		//return "SELECT indert.prueba_array('". $id_registro_lote . "', string_to_array('" . $array['dpto'] . "," . $array['distrito'] . "," . $array['barrio'] . "," . $array['colonia'] . "," . $array['area'] . "," . $array['ae'] . "," . $array['manzana'] . "," . $array['lote_a'] . "," . $array['lote_b'] . "," . $array['finca'] . "," . $array['padron'] . "," . $array['cuest']."',','));";

		$query = $this->db->query("SELECT indert.cuest_cambio_pk('". $id_registro_lote . "', string_to_array('" . $array['dpto'] . "," . $array['distrito'] . "," . $array['barrio'] . "," . $array['colonia'] . "," . $array['area'] . "," . $array['ae'] . "," . $array['manzana'] . "," . $array['lote_a'] . "," . $array['lote_b'] . "," . $array['finca'] . "," . $array['padron'] . "," . $array['cuest'] . "," . $array['verificador']."',','));");

		if ($query) {
			return $query;
		}else{
			return false;
		}
	}

	public function exportar_foto($id_foto, $id_registro_lote, $ruta)
	{
		//$query = "SELECT lo_export(image, '".$ruta."') FROM REG_FOTOS WHERE id_foto = ".$id_foto."AND id_registro_lote = '".$id_registro_lote."';";
		$query = $this->db->query("SELECT lo_export(image, '".$ruta."') FROM REG_FOTOS WHERE id_foto = ".$id_foto."AND id_registro_lote = '".$id_registro_lote."';");

		return $this->db->last_query();
		if ($query) {
			return true;
		}else{
			return false;
		}
	}

	public function viewlist_abm($tabla = NULL, $id_registro_lote = NULL, $orderby = NULL) 
	{
		$this->trigger_events('viewlist_abm');

		$this->response = $this->db->select('*');
		$this->response = $this->db->from($this->tables[$tabla]);

		$this->response = $this->db->where('id_registro_lote', $id_registro_lote);

		if ($orderby != NULL) {
			$this->response = $this->db->order_by($orderby);
		}

		$this->response = $this->db->get();

		return $this;



		
		if (isset($id_registro_lote))
		{
			$this->response = $this->db->get_where($this->tables[$tabla],array('id_registro_lote' => $id_registro_lote));	
		}

		return $this;
	}
	
	/**
	 * contador_registros
	 *	Obtiene la cantidad de registro de una tabla y condición dada
	 * @return int
	 * @author Diego Ferreira
	 **/
	public function contador_registros ($tabla = NULL, $arraywhere = NULL)
	{
		foreach ($arraywhere as $k => $v) {

			$this->db->where($k, $v);
		}
		$this->db->from($this->tables[$tabla]);
		return $this->db->count_all_results();
	}

	/**
	 * valor_maximo
	 *	Obtiene el valor máximo de una columna y tabla específica
	 * @return int
	 * @author Diego Ferreira
	 **/
	public function valor_maximo ($tabla = NULL, $columna = NULL, $arraywhere = NULL)
	{
		$this->db->select("MAX(".$columna.") max");

		foreach ($arraywhere as $k => $v) {

			$this->db->where($k, $v);
		}

		$this->db->from($this->tables[$tabla]);

		return $this->db->get();
	}

	/**
	 * actualizar_cuest
	 *	Función encargada de actualizar el cuestionario en la tabla y con la condición propuesta
	 * @return bool
	 * @author Diego Ferreira
	 **/
    public function actualizar_cuest($where = NULL, $tabla = NULL, $arraycampos = NULL)
	{
		if (empty($arraycampos)) return FALSE;

		$this->db->update($this->tables[$tabla], $arraycampos, $where);

		$this->set_message('in_temp_update_successful');

		return TRUE;
	}

	/**
	 * create_reg
	 *	Función encargada de crear un nuevo cuestionario
	 * @return bool
	 * @author Diego Ferreira
	 **/
	public function create_reg($arraycampos)
	{
		if ($arraycampos) {
			$this->db->query("SELECT INDERT.ADICIONAR_REG(". $arraycampos['dpto'].",". $arraycampos['distrito'].",". $arraycampos['barrio'].",". $arraycampos['colonia'].",". $arraycampos['area'].",". $arraycampos['ae'].",'". $arraycampos['manzana']."','". $arraycampos['lote_a']."','". $arraycampos['lote_b']."','". $arraycampos['finca']."','". $arraycampos['padron']."',". $arraycampos['cuest'].",". $arraycampos['verificador'].")");
		}
		
		return TRUE;
	}

	/**
	 * crear_registro
	 *	Función encargada de crear un nuevo registro en la tabla y con la condición propuesta
	 * @return bool
	 * @author Diego Ferreira
	 **/
	public function crear_registro($tabla = NULL, $arraycampos = NULL)
	{
		if (empty($arraycampos)) return FALSE;

		$this->db->insert($this->tables[$tabla], $arraycampos);

		$this->set_message('in_temp_update_successful');

		return TRUE;
	}

	/**
	 * eliminar_registro
	 *	Función encargada de eliminar un registro dada la tabla y la condición de la misma
	 * @return bool
	 * @author Diego Ferreira
	 **/
	public function eliminar_registro($tabla = NULL, $arraywhere){

		$eliminar = $this->db->delete($tabla, $arraywhere);

		if ($eliminar) {
			return TRUE;
		}else{
			return FALSE;
		}

	}

	/**
	 * validar_pestanha
	 *	Función encargada de llamar a la función de base de datos encargada de validar la pestaña modificada
	 * @return bool
	 * @author Diego Ferreira
	 **/
	public function validar_pestanha($id_registro_lote, $id_pestanha)
	{
		return $this->db->query("SELECT indert.validar_cuestionario('" . $id_registro_lote . "','" . $id_pestanha . "');");
	}

	/**
	 * actualizar_reg09
	 *	Función encargada de crear un registro en reg09 si no existe
	 * @return bool
	 * @author Diego Ferreira
	 **/
	public function actualizar_reg09($id_registro_lote)
	{
		return $this->db->query("SELECT indert.actualizar_reg09('" . $id_registro_lote . "');");
	}

	/**
	 * obtener_errores
	 *	Función encargada de obtener los errores de la tabla public.reg_errores
	 * @return array
	 * @author Diego Ferreira
	 **/
	public function obtener_errores($id_registro_lote, $id_pestanha)
	{
		return $this->db->get_where("reg_errores",array('id_registro_lote' => $id_registro_lote,
														'pestanha'		   => $id_pestanha
														));
	}
}
	