<?php
/* 
 * Generated by CRUDigniter v3.2 
 * www.crudigniter.com
 */
 
class Comentarioqueja extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Comentarioqueja_model');
        $this->load->model('Queja_model');
    } 

    /*
     * Listing of comentariosquejas no hay un listado independiente
     */
    function index()
    {
        $params['limit'] = RECORDS_PER_PAGE; 
        $params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;
        
        $config = $this->config->item('pagination');
        $config['base_url'] = site_url('comentarioqueja/index?');
        $config['total_rows'] = $this->Comentarioqueja_model->get_all_comentariosquejas_count();
        $this->pagination->initialize($config);

        $data['comentariosquejas'] = $this->Comentarioqueja_model->get_all_comentariosquejas($params);
        
        $data['_view'] = 'comentarioqueja/index';
        $this->load->view('layouts/main',$data);
    }

    /*
     * Adding a new comentarioqueja
     */
    function add($id_queja)
    {   
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };

        // check if the queja exists before trying to edit it
        $data['queja'] = $this->Queja_model->get_queja($id_queja);
        
        if(isset($data['queja']['id']))
        {
            $breadcrumb = array(
                "Quejas" => base_url('queja'),
                $data['queja']['titulo'] => base_url('queja/detail/'.$id_queja),
                "Agregar" => ""
            );
            $data['breadcrumb'] = $breadcrumb;

            $this->load->library('form_validation');

            $this->form_validation->set_rules('contenido','Contenido','required');
            
            if($this->form_validation->run())     
            {   
                $params = array(
                    'id_queja' => $id_queja,
                    'id_autor' =>  $this->session->userdata('id'),
                    'fecha_registro' => date("Y-m-d H:i:s"),
                    'fecha_modificacion' => date("Y-m-d H:i:s"),
                    'estado' => 1,
                    'contenido' => $this->input->post('contenido'),
                );
                
                $comentarioqueja_id = $this->Comentarioqueja_model->add_comentarioqueja($params);
                redirect('queja/detail/'.$id_queja);
            }
            else
            {            
                    $data['controlador'] = 'comentarioqueja';
                    $data['contenido'] = 'comentarioqueja/add';
                    $data['titulo'] = 'Nuevo Comentario';
                    $this->load->view('templates/template_admin', $data);
            }
        }
        else 
        {
            show_error('The queja you are trying to edit does not exist.');
        }
    }  

    /*
     * Editing a comentarioqueja
     */
    function edit($id)
    {   
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };
        // check if the comentarioqueja exists before trying to edit it
        $data['comentarioqueja'] = $this->Comentarioqueja_model->get_comentarioqueja($id);
        
        if(isset($data['comentarioqueja']['id']))
        {

            $data['queja'] = $this->Queja_model->get_queja($data['comentarioqueja']['id_queja']);

            $breadcrumb = array(
                "Quejas" => base_url('queja'),
                $data['queja']['titulo'] => base_url('queja/detail/'.$data['comentarioqueja']['id_queja']),
                "Editar" => ""
            );
            $data['breadcrumb'] = $breadcrumb;

            $this->load->library('form_validation');

			$this->form_validation->set_rules('contenido','Contenido','required');
		
			if($this->form_validation->run())     
            {   
                $params = array(
					'id_queja' => $this->input->post('id_queja'),
					'id_autor' => $this->input->post('id_autor'),
					'fecha_registro' => $this->input->post('fecha_registro'),
					'fecha_modificacion' => date("Y-m-d H:i:s"),
					'estado' => $this->input->post('estado'),
					'contenido' => $this->input->post('contenido'),
                );

                $this->Comentarioqueja_model->update_comentarioqueja($id,$params);            
                 redirect('queja/detail/'.$data['comentarioqueja']['id_queja']);
            }
            else
            {
                    $data['controlador'] = 'comentarioqueja';
                    $data['contenido'] = 'comentarioqueja/edit';
                    $data['titulo'] = 'Editar Comentario';
                    $this->load->view('templates/template_admin', $data);
            }
        }
        else
            show_error('The comentarioqueja you are trying to edit does not exist.');
    } 

    /*
     * Deleting comentarioqueja
     */
    function remove($id)
    {
        $comentarioqueja = $this->Comentarioqueja_model->get_comentarioqueja($id);

        // check if the comentarioqueja exists before trying to delete it
        if(isset($comentarioqueja['id']))
        {
            $this->Comentarioqueja_model->delete_comentarioqueja($id);
            redirect('comentarioqueja/index');
        }
        else
            show_error('The comentarioqueja you are trying to delete does not exist.');
    }

    /*
     * Detalle comentarioqueja
     */
    function detail($id)
    {   
        if(!($this->session->userdata('id'))) 
        {
            redirect(base_url()."login");
        };

       // check if the comentarioqueja exists before trying to edit it
        $data['comentarioqueja'] = $this->Comentarioqueja_model->get_comentarioqueja($id);
        
        if(isset($data['comentarioqueja']['id']))
        {
           
            $data['queja'] = $this->Queja_model->get_queja($data['comentarioqueja']['id_queja']);

            $breadcrumb = array(
                "Quejas" => base_url('queja'),
                $data['queja']['titulo'] => base_url('queja/detail/'.$data['comentarioqueja']['id_queja']),
                "Detalle" => ""
            );
            $data['breadcrumb'] = $breadcrumb;

            $this->load->model('Usuario_model');
            $data['usuario'] = $this->Usuario_model->get_usuario($data['comentarioqueja']['id_autor']);

            $data['controlador'] = 'queja';
            $data['contenido'] = 'comentarioqueja/detail';
            $data['titulo'] = 'Detalle de Comentario de Queja';
            $this->load->view('templates/template_admin', $data);            
        }        
        else
            show_error('The comentarioqueja you are trying to view does not exist.');
    } 
    
}
