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

class In_temp extends Admin_Controller {

    public function __construct()
    {
        parent::__construct();

		$this->load->model('man_espacial/in_temp/in_temp_model');
        $this->lang->load('in_temp/in_temp');

        /* Title Page :: Common */
        $this->page_title->push(lang('menu_in_temp'));
        $this->data['pagetitle'] = $this->page_title->show();

        if ($_SESSION['id_modulo'] != 'IN_TEMP') {

			$_SESSION['arraywhere_in_temp'] = NULL;
			$_SESSION['arraywhere_in_temp_busq'] = NULL;
			$_SESSION['id_modulo'] = 'IN_TEMP';
		}

        /* Breadcrumbs :: Common */
        $this->breadcrumbs->unshift(1, lang('menu_in_temp'), 'admin/in_temp');
    }


	public function index()
	{

		

        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
            if ($_POST) {

				if ($_POST['dpto']) 
				{
					$arraywhere['dpto'] 			= $_POST['dpto'];
					$arraywherebusq['dpto'] 			= $_POST['dpto'];
				}
				if ($_POST['dpto_desc']) 
				{
					$arraywhere['dpto_desc'] 		= $_POST['dpto_desc'];
					$arraywherebusq[' lower(dpto_desc) like'] 			= '%'.strtolower($_POST['dpto_desc']).'%';
				}
				if ($_POST['distrito']) 
				{
					$arraywhere['distrito'] 		= $_POST['distrito'];
					$arraywherebusq['distrito'] 		= $_POST['distrito'];
				}
				if ($_POST['dist_desc']) 
				{
					$arraywhere['dist_desc'] 		= $_POST['dist_desc'];
					$arraywherebusq[' lower(dist_desc) like'] 			= '%'.strtolower($_POST['dist_desc']).'%';
				}
				if ($_POST['colonia']) 
				{
					$arraywhere['colonia'] 			= $_POST['colonia'];
					$arraywherebusq['colonia'] 			= intval($_POST['colonia']);
				}
				if ($_POST['col_desc']) 
				{
					$arraywhere['col_desc'] 		= $_POST['col_desc'];
					$arraywherebusq[' lower(col_desc) like'] 			= '%'.strtolower($_POST['col_desc']).'%';
				}
				if ($_POST['mz_agr']) 
				{
					$arraywhere['mz_agr'] 			= $_POST['mz_agr'];
					$arraywherebusq[' lower(mz_agr) like'] 			= '%'.strtolower($_POST['mz_agr']).'%';
				}
				if ($_POST['lote_agr']) 
				{
					$arraywhere['lote_agr'] 		= $_POST['lote_agr'];
					$arraywherebusq[' lower(lote_agr) like'] 			= '%'.strtolower($_POST['lote_agr']).'%';
				}

				$_SESSION['arraywhere_in_temp'] 	= $arraywhere;
				$_SESSION['arraywhere_in_temp_busq']= $arraywherebusq;

				redirect('man_espacial/in_temp/in_temp/index');				
			}

			 // ----- Paginacion Generica 
			$mi_paginacion = $this->ion_auth->paginacion_generica(
				12, 						// resultados por por_pagina, 
				5, 							// segmento uri a leer, 
				5, 							// cantidad_enlaces,
				base_url(). 'man_espacial/in_temp/in_temp/index/',  // url completa de la pagina paginada
				'in_temp',		// scheme y nombre de la tabla, para la consulta
				$_SESSION['arraywhere_in_temp_busq']
				);

			$this->data['pagination'] 		= $mi_paginacion['pagination'];

			$this->data['in_temp']	  		= $this->ion_auth_model->segmentador_paginas(
 				$mi_paginacion['per_page'], 	// resultados por pagina
 				5,								// segmento uri a leer
 				'in_temp',					// el nombre de la tabla a consultar $nombre_tabla
 				$_SESSION['arraywhere_in_temp_busq']
 				)->result();
			// ----- Fin paginacion

            /* Breadcrumbs */
            $this->data['breadcrumb'] = $this->breadcrumbs->show();

            /* Load Template */
            $this->template->admin_render('man_espacial/in_temp/index', $this->data);
        }
    }


	public function create()
	{
		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}

        /* Breadcrumbs */
        $this->breadcrumbs->unshift(2, lang('menu_in_temp'), 'in_temp/create');
        $this->data['breadcrumb'] = $this->breadcrumbs->show();

		/* Validate form input */
		$this->form_validation->set_rules('col_desc', 'lang:create_in_temp_validation_name_label', 'required|alpha_dash');

		if ($this->form_validation->run() == TRUE)
		{
			$new_in_temp_id = $this->in_temp_model->create_in_temp($this->input->post('dpto'), $this->input->post('distrito'), $this->input->post('colonia'), $this->input->post('mz_agr'), $this->input->post('lote_agr'));
			if ($new_in_temp_id)
			{
				$this->session->set_flashdata('message', $this->ion_auth->messages());
				redirect('in_temp/in_temp', 'refresh');
			}
		}
		else
		{
            $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
	
			$this->data['dpto'] = array(
				'name'  => 'dpto',
				'id'    => 'dpto',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('dpto')
			);
			$this->data['distrito'] = array(
				'name'  => 'distrito',
				'id'    => 'distrito',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('distrito')
			);
			$this->data['colonia'] = array(
				'name'  => 'colonia',
				'id'    => 'colonia',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('colonia')
			);
			$this->data['mz_agr'] = array(
				'name'  => 'mz_agr',
				'id'    => 'mz_agr',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('mz_agr')
			);
			$this->data['lote_agr'] = array(
				'name'  => 'lote_agr',
				'id'    => 'lote_agr',
				'type'  => 'text',
                'class' => 'form-control',
				'value' => $this->form_validation->set_value('lote_agr')
			);
            /* Load Template */
            $this->template->admin_render('in_temp/create', $this->data);
		}
	}


	public function delete()
	{
        if ( ! $this->ion_auth->logged_in())
        {
            redirect('auth/login', 'refresh');
        }
        elseif ( ! $this->ion_auth->is_admin())
		{
            return show_error('You must be an administrator to view this page.');
        }
        else
        {
            $this->load->view('in_temp/delete');
        }
	}


	public function edit($id)
	{
		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin() OR ! $id OR empty($id))
		{
			redirect('auth', 'refresh');
		}

        /* Breadcrumbs */
        $this->breadcrumbs->unshift(2, lang('menu_in_temp_edit'), 'in_temp/edit');
        $this->data['breadcrumb'] = $this->breadcrumbs->show();

        /* Variables */
		$in_temp = $this->in_temp_model->view($id)->row();

		/* Validate form input */
        $this->form_validation->set_rules('mz_agr', $this->lang->line('in_temp_dpto'), 'required');
		$this->form_validation->set_rules('lote_agr', $this->lang->line('edit_in_temp_validation_dpto_desc_label'), 'required');

		if (isset($_POST) && ! empty($_POST))
		{
			if ($this->form_validation->run() == TRUE)
			{
				$arraycampos = array(
					'dpto' 		=> $in_temp->dpto,
					'distrito' 	=> $in_temp->distrito,
					'colonia' 	=> $in_temp->colonia,
					'mz_agr' 	=> $_POST['mz_agr'],
					'lote_agr' 	=> $_POST['lote_agr'],
					'mapear' 	=> $_POST['mapear']
				);
				$in_temp_update = $this->in_temp_model->update_in_temp($id, $arraycampos);

				if ($in_temp_update)
				{
					$this->session->set_flashdata('message', $this->lang->line('edit_in_temp_saved'));
				}
				else
				{
					$this->session->set_flashdata('message', $this->ion_auth->errors());
				}

				redirect('man_espacial/in_temp/in_temp');
			}
		}

        $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
        $this->data['in_temp']   = $in_temp;

		$readonly = $this->config->item('admin_group', 'ion_auth') === $in_temp->col_desc ? 'readonly' : '';

		$this->data['mz_agr'] = array(
			'type'     => 'text',
			'name'     => 'mz_agr',
			'id'       => 'mz_agr',
			'value'    => $this->form_validation->set_value('in_temp_mz_agr', $in_temp->mz_agr),
			'class'    => 'form-control'
		);

		$this->data['lote_agr'] = array(
			'type'     => 'text',
			'name'     => 'lote_agr',
			'id'       => 'lote_agr',
			'value'    => $this->form_validation->set_value('in_temp_lote_agr', $in_temp->lote_agr),
			'class'    => 'form-control'
		);

		$mapear = $this->ion_auth_model->traer_concepto('MAPEAR','concepto')->result_array();
		//inserta en un array asociativo para poder utilizarlo en el form_dropdown
		foreach ($mapear as $map) {
			$mapears[$map['concepto']] = $map['concepto'].' - '.$map['nombre'];
		}
		$this->data['mapear'] 		= $mapears;
		$this->data['mapear_sel'] 	= $in_temp->mapear;

        /* Load Template */
        $this->template->admin_render('man_espacial/in_temp/edit', $this->data);
	}
	public function verparcela ($objectid) {
	
		$bbox = $this->in_temp_model->bb($objectid);  
		if ($bbox)
		{
			$data['campo1'] = $bbox[0]['campo1'];
        	$data['campo2'] = $bbox[0]['campo2'];
        	$data['campo3'] = $bbox[0]['campo3'];
        	$data['campo4'] = $bbox[0]['campo4'];

        	$data['concat'] = $bbox[0]['campo1']. ',' . $bbox[0]['campo2'] . ',' .$bbox[0]['campo3']. ',' . $bbox[0]['campo4'];
			
        	$i_jameuge = $_SESSION['url_i3geo'].'/i3geo/ms_criamapa.php?layers=tempo';
        	$i_jameuge = $i_jameuge . "&map_layer_tempo_filter=objectid=" . $objectid;
        	$i_jameuge = $i_jameuge . "&mapext=". $data['concat'];

			redirect($i_jameuge);
		}
		else
		{
			$this->session->set_flashdata('message', $this->ion_auth->errors());
		}

	}
	public function verparcela1 ($objectid) {
	
		$bbox = $this->in_temp_model->boundary_box($objectid)->row();
		if ($bbox)
		{
        	$i_jameuge = $_SESSION['url_i3geo'].'/i3geo/ms_criamapa.php?layers=tempo';
        	$i_jameuge = $i_jameuge . "&map_layer_tempo_filter=objectid=" . $objectid;
        	$i_jameuge = $i_jameuge . "&mapext=". $bbox->bb;

			redirect($i_jameuge);
		}
		else
		{
			$this->session->set_flashdata('message', $this->ion_auth->errors());
		}

	}

	public function informe_deslinde () {
		
		header("Location: ".$_SESSION['url_i3geo']."/i3geo/ferramentas/imprimir/imprimir_A4_informe_deslinde.php?mapfile=C:/ms4w/Apache/htdocs/i3geo/aplicmap/imprimir_A4_plano_individual.map&interface=googlemaps&objectid=41141&dpto=10&distrito=06&colonia=12&mz_agr=I&lote_agr=2I&usr=1");
	}
}