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

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

		// 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['geocolonias']);
		
		return $this;
	}

	public function dropdown_data($tabla = NULL, $where = NULL)
	{
		$this->response = $this->db->get_where($this->tables[$tabla],$where);

		return $this;
	}
	
	public function buscar_geo($abuscar)
	{
        //usamos after para decir que empiece a buscar por
        //el principio de la cadena
        //ej SELECT poblacion from poblacion
        //WHERE poblacion LIKE '%$abuscar' limit 12
        //$this->db->select('geocolonias');

        //$this->db->like('poblacion',$abuscar,'after');

		$resultados = $this->db->get('geocoloniaS');

        //si existe algún resultado lo devolvemos
		if($resultados->num_rows() > 0)
		{

			return $resultados->result();

        //en otro caso devolvemos false
		}else{

			return FALSE;

		}

	}

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

		$this->response = $this->db->get_where($this->tables['geocolonias'], array('gid' => $gid));

		return $this;
	}

	/**
	 * create_geocolonia
	 *
	 * @author aditya menon
	*/
	public function create_geocolonia($arraycampos = NULL)
	{
		// bail if the dpto_cod was not passed ** Se utiliza la etiqueta de lenguaje spanish
		/*if(!$geocolonia_dpto_cod)
		{
			$this->set_error('geocolonia_dpto_cod_required');
			return FALSE;
		}
		// bail if the dist_cod was not passed
		if(!$geocolonia_dist_cod)
		{
			$this->set_error('geocolonia_dist_cod_required');
			return FALSE;
		}
		// bail if the col_cod was not passed
		if(!$geocolonia_col_cod)
		{
			$this->set_error('geocolonia_col_cod_required');
			return FALSE;
		}
		// bail if the id_codgeo was not passed
		if(!$geocolonia_id_codgeo)
		{
			$this->set_error('geocolonia_id_codgeo_required');
			return FALSE;
		}
		// bail if the col_desc was not passed
		if(!$geocolonia_col_desc)
		{
			$this->set_error('geocolonia_col_desc_required');
			return FALSE;
		}*/		
		// bail if the group name already exists
		$existing_geocolonia = $this->db->get_where($this->tables['geocolonias'], $arraycampos)->num_rows();

		
		if($existing_geocolonia !== 0)
		{
			$this->ion_auth->set_error('geocolonia_already_exists');
			return FALSE;
		}
		
		//$data = array('dpto_cod'=>$geocolonia_dpto_cod, 'dist_cod'=>$geocolonia_dist_cod, 'col_cod'=>$geocolonia_col_cod, 'id_codgeo'=>$geocolonia_id_codgeo, '//col_desc'=>$geocolonia_col_desc, 'col_desc'=>$geocolonia_col_desc);
		//
		//// 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['geocolonias'], $additional_data), $data);
//
		//$this->trigger_events('extra_geocolonia_set');

		// insert the new group
		$this->db->insert($this->tables['geocolonias'], $arraycampos);
		$geocolonia_gid = $this->db->insert_id();

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

	public function update_geocolonia($gid = FALSE, $arraycampos = NULL)
	{

		if (!empty($gid))
		{
			// we are changing the col_desc, so do some checks

			// bail if the geocolonia col_desc already exists
			$existing_geocolonia = $this->db->get_where($this->tables['geocolonias'], $arraycampos)->row();

			if(isset($existing_geocolonia->gid) && $existing_geocolonia->gid != $gid)
			{
				$this->set_error('geocolonia_already_exists');
				return FALSE;
			}
		}


        $this->db->update($this->tables['geocolonias'], $arraycampos, array('gid' => $gid));

        $this->set_message('geocolonias_update_successful');

        return TRUE;
    }

    public function eliminar_geocolonia($gid)
    {
    	$this->response = $this->db->delete($this->tables['geocolonias'],array('gid' => $gid));

    	return TRUE;
    }

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

    	return $this;
    }

    public function distritos()
    {
    	$this->response = $this->db->query("SELECT * FROM INDERT.DISTRITOS ORDER BY DISTRITO");
    	return $this;
    }

	//PAGINATION
    public function get_pagination($number_per_page)
    {
    	if ($this->uri->segment(5) == null | $this->uri->segment(5) == '') {

    		$this->response = $this->db->get($this->tables['geocolonias'],$number_per_page, 0);
    	}else
    	{
    		$this->response = $this->db->get($this->tables['geocolonias'],$number_per_page, $this->uri->segment(5));
    	}

    	return $this;
	}

	public function num_post()
	{			
		$number = $this->db->query("SELECT count(*) as number FROM indert.geocolonia")->row()->number; //rápido
		return intval($number);
	}
}