<?php

if (!function_exists('get_limit_offset_order_and_filters')) {

    function get_limit_offset_order_and_filters($default_order = array()) {
        $param_page_name = 'pagina';
        $param_page_by_page = 'by_page';
        $param_order_name = 'ordenamiento';
        $param_filter_name = 'buscar';
        $CI = & get_instance();
        $data_array = array();
        $data_array['offset'] = (($CI->get($param_page_name) - 1) * $CI->get($param_page_by_page));
        if ((int) $CI->get($param_page_by_page) > 0) {
            $data_array['limit'] = $CI->get($param_page_by_page);
        }
        //para el ordenamiento
        if ($CI->get($param_order_name) !== NULL) {
            $data_array['orders'] = json_decode(urldecode($CI->get($param_order_name)), TRUE);
        } else {
            $data_array['orders'] = $default_order;
        }
        //para los filtros
        if ($CI->get($param_filter_name) !== NULL) {
            $data_array['filters'] = json_decode(urldecode($CI->get($param_filter_name)), TRUE);
        } else {
            $data_array['filters'] = array();
        }
        return $data_array;
    }

}

if (!function_exists('get_order_and_filters_from_text')) {

    function get_order_and_filters_from_text($default_order = array()) {
        $CI = & get_instance();
        $data_array = array();
        //para el ordenamiento
        $data_array['orders'] = $default_order;
        //para los filtros
        if ($CI->input->get('filtros') !== NULL) {
            $data_array['filters'] = json_decode(urldecode($CI->input->get('filtros')), TRUE);
        } else {
            $data_array['filters'] = array();
        }
        return $data_array;
    }

}

if (!function_exists('get_filters')) {

    function get_filters() {
        $param_filter_name = 'buscar';
        $CI = & get_instance();
        $data_array = array();
        //para los filtros
        if ($CI->get($param_filter_name) !== NULL) {
            $data_array['filters'] = json_decode(urldecode($CI->get($param_filter_name)), TRUE);
        } else {
            $data_array['filters'] = array();
        }
        return $data_array;
    }

}

if (!function_exists('get_order_and_filters_from_text')) {

    function get_order_and_filters_from_text($default_order = array()) {
        $CI = & get_instance();
        $data_array = array();
        //para el ordenamiento
        $data_array['orders'] = $default_order;
        //para los filtros
        if ($CI->input->get('filtros') !== NULL) {
            $data_array['filters'] = json_decode(urldecode($CI->input->get('filtros')), TRUE);
        } else {
            $data_array['filters'] = array();
        }
        return $data_array;
    }

}
