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

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

		// run each where that was passed
		if (isset($this->_ion_where) && !empty($this->_ion_where))
		{
			foreach ($this->_ion_where as $where)
			{
				$this->db->where($where);
			}
			$this->_ion_where = array();
		}

		if (isset($this->_ion_limit) && isset($this->_ion_offset))
		{
			$this->db->limit($this->_ion_limit, $this->_ion_offset);

			$this->_ion_limit  = NULL;
			$this->_ion_offset = NULL;
		}
		else if (isset($this->_ion_limit))
		{
			$this->db->limit($this->_ion_limit);

			$this->_ion_limit  = NULL;
		}

		// set the order
		if (isset($this->_ion_order_by) && isset($this->_ion_order))
		{
			$this->db->order_by($this->_ion_order_by, $this->_ion_order);
		}

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

		return $this;
	}

	public function departamentos()
	{
		$this->response = $this->db->get($this->tables['dpto']);

		return $this;
	}
	public function distrito($dpto = null)
	{
		$this->response = $this->db->query("SELECT * FROM INDERT.DISTRITOS WHERE dpto = $dpto");

		return $this;
	}

	/**
	 * geocolonia
	 *
	 * @return object
	 * @author Ben Edmunds
	 **/
	public function view($id = NULL)
	{
		$this->trigger_events('in_temps');

		if (isset($id))
		{
			$this->where($this->tables['in_temp'].'.gid', $id);
		}

		$this->limit(1);
		$this->order_by('gid', 'col_desc');

		return $this->view_all();
	}

	/**
	 * create_group
	 *
	 * @author aditya menon
	*/
	public function create_in_temp($in_temp_dpto = '', $in_temp_distrito = '', $in_temp_colonia = '', $in_temp_mz_agr = '', $in_temp_lote_agr = '', $additional_data = array())
	{
		// bail if the dpto_cod was not passed ** Se utiliza la etiqueta de lenguaje spanish
		if(!$in_temp_dpto)
		{
			$this->set_error('in_temp_dpto_required');
			return FALSE;
		}
		// bail if the dist_cod was not passed
		if(!$in_temp_distrito)
		{
			$this->set_error('in_temp_distrito_required');
			return FALSE;
		}
		// bail if the col_cod was not passed
		if(!$in_temp_colonia)
		{
			$this->set_error('in_temp_colonia_required');
			return FALSE;
		}
		// bail if the id_codgeo was not passed
		if(!$in_temp_mz_agr)
		{
			$this->set_error('in_temp_mz_agr_required');
			return FALSE;
		}
		// bail if the col_desc was not passed
		if(!$in_temp_lote_agr)
		{
			$this->set_error('in_temp_lote_agr_required');
			return FALSE;
		}		
		// bail if the group name already exists
		$existing_in_temp = $this->db->get_where($this->tables['in_temp'], array('colonia' => $in_temp_colonia))->num_rows();
		$existing_in_temp = $this->db->get_where($this->tables['in_temp'], array('lote_agr' => $in_temp_lote_agr))->num_rows();
		$existing_in_temp = $this->db->get_where($this->tables['in_temp'], array('mz_agr' => $in_temp_mz_agr))->num_rows();
		if($existing_in_temp !== 0)
		{
			$this->set_error('in_temp_already_exists');
			return FALSE;
		}
		
		$data = array('dpto'=>$in_temp_dpto, 'distrito'=>$in_temp_distrito, 'colonia'=>$in_temp_colonia, 'mz_agr'=>$in_temp_mz_agr, 'lote_agr'=>$in_temp_lote_agr);
		
		// filter out any data passed that doesnt have a matching column in the groups table
		// and merge the set group data and the additional data
		if (!empty($additional_data)) $data = array_merge($this->_filter_data($this->tables['in_temp'], $additional_data), $data);

		$this->trigger_events('extra_in_temp_set');

		// insert the new group
		$this->db->insert($this->tables['in_temp'], $data);
		$in_temp_gid = $this->db->insert_id();

		// report success
		$this->set_message('in_temp_creation_successful');
		// return the brand new group id
		return $in_temp_gid;
	}
	
	/**
	 * update_gepcolonia
	 *
	 * @return bool
	 * @author aditya menon
	 **/

    public function update_in_temp($id, $arraycampos)
	{
		if (empty($in_temp_id)) return FALSE;

		$data = array();

			$existing_in_temp = $this->db->get_where($this->tables['in_temp'], $arraycampos)->row();
			if(isset($existing_in_temp->id) && $existing_in_temp->id != $in_temp_id)
			{
				$this->set_error('in_temp_already_exists');
				return FALSE;
			}

		$this->db->update($this->tables['in_temp'], $arraycampos, array('objectid' => $id));

		$this->set_message('in_temp_update_successful');

		return TRUE;
	}
	public function bb($objectid)
	{
			$i_sql = "select ";
			$i_sql = $i_sql. "(regexp_split_to_array(bb, E','))[1] as campo1,";
			$i_sql = $i_sql. "(regexp_split_to_array(bb, E','))[2] as campo2,";
			$i_sql = $i_sql. "(regexp_split_to_array(bb, E','))[3] as campo3,";
			$i_sql = $i_sql. "(regexp_split_to_array(bb, E','))[4] as campo4";
			$i_sql = $i_sql. " from (";
				
			$i_sql = $i_sql . " SELECT ";
            $i_sql = $i_sql . "replace(";
            $i_sql = $i_sql . "replace(substr(cast(a.bb as varchar),5),' ',',')";
            $i_sql = $i_sql . ",')','') as bb ";
            $i_sql = $i_sql . "FROM (select ST_Extent(the_geom) as bb, objectid, lote_agr, mz_agr, dpto, distrito, colonia ";
            $i_sql = $i_sql . "from i3geo.in_temp ";
            $i_sql = $i_sql . "where objectid=" . $objectid . " ";
            $i_sql = $i_sql . "group by objectid, lote_agr, mz_agr, dpto, distrito, colonia) as a) as t1;";

            $response = $this->db->query($i_sql)->result_array();

            return $response;

	}
	public function boundary_box($objectid)
	{
		$this->trigger_events('in_temps');

		if (isset($objectid))
		{
			$this->where($this->tables['v_bb_parcelas'].'.objectid', $objectid);
		}

		$this->limit(1);
		$this->response = $this->db->get($this->tables['v_bb_parcelas']);
		return $this;
	}
}