<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 10/13/2015
 * Time: 10:28 AM
 */
if(!function_exists('t')) {
    function t($str) {
        return $str;
    }
}

class Paginator {
    public $total_records = 0;
    public $options = array(
        "base_url"  => "",
        "request_parameter_name" => "filter",
        "records_per_page" => 25,
        "show_all" => 1
    );

    public $params = array(
        "i_from"    => 0,
        "i_limit"   => 25
    );

    function __construct($options = null)
    {
        if( !is_null($options) ) $this->options = array_merge($this->options, $options);
        //echo '<pre>', var_export($this->options, true), '</pre>';
    }

    public function get_json($total, $params, $options = null) {
        $json = array();
        $err = array();

        $this->total_records = intval($total);
        $this->params = array_merge($this->params, $params);

        if( !is_null($options)) $this->options = array_merge($this->options, $options);

        if($total > $this->options['records_per_page'] && $this->options['base_url'] == "") {
            $err [] = "Please provide the [base_url] option.";
        }

        if(count($err) == 0) {
            $from = $this->params['i_from'];
            $limit = $this->params['i_limit'];
            $base_url = $this->options['base_url'];
            $filter = $this->options['request_parameter_name'];

            $page = $limit;

            $showFrom = $from;
            $showTo = $from + $page;
            if ($showTo > $this->total_records) $showTo = $this->total_records;

            $nrpages = intval($this->total_records / $page);
            if ($this->total_records / $page > $nrpages) $nrpages += 1;

            $json['total_records'] = $this->total_records;
            $json['pages'] = $nrpages;

            if($nrpages > 1) {
                $link_url = $base_url;
                $P = ($from + $page) / $page;

                // Current Page Links
                $current = $this->params;
                $current['i_from'] = $P * $page;
                $json['current_page'] = $P;
                $json['current_url'] = $link_url . "?$filter=" . base64_encode(http_build_query($current));
                if($P == 1) {
                    $json['current_url'] = $link_url;
                }


                // Previous Page URL
                if($P - 1 > 0) {
                    $previous = $this->params;
                    $previous['i_from'] = ($P - 1) * $page;
                    $json['prev_page'] = $P - 1;
                    $json['prev_url'] = $link_url . "?$filter=" . base64_encode(http_build_query($previous));
                    $json['prev_data_url'] = $link_url . "?$filter=" . base64_encode(http_build_query($previous)) . '&type=json';
                    if($P - 1 == 1) {
                        $json['prev_url'] = $link_url;
                        $json['prev_data_url'] = $link_url . '?type=json';;
                    }
                }
                else {
                    $json['prev_page'] = '';
                    $json['prev_url'] = '';
                    $json['prev_data_url'] = '';
                }

                // Next Page URL
                if($P + 1 <= $nrpages) {
                    $next = $this->params;
                    $next['i_from'] = ($P + 1) * $page;
                    $json['next_page'] = $P + 1;
                    $json['next_url'] = $link_url . "?$filter=" . base64_encode(http_build_query($next));
                    $json['next_data_url'] = $link_url . "?$filter=" . base64_encode(http_build_query($next)) . '&type=json';
                }
                else {
                    $json['next_page'] = '';
                    $json['next_url'] = '';
                    $json['next_data_url'] = '';
                }
            }
        }

        return $json;

    }

    public function paginate($total, $params, $options = null) {
        ob_start();

        $json = array();
        $return_json = (isset($this->options['type']) && $this->options['type'] == 1 ) ? true : false;
        if($options != null) {
            if(isset($options['type'])) {
                $return_json = $options['type'] == 1 ? true : false;
            }
        }
        $err = array();

        $this->total_records = intval($total);
        $this->params = array_merge($this->params, $params);

        if( !is_null($options)) $this->options = array_merge($this->options, $options);

        if($total > $this->options['records_per_page'] && $this->options['base_url'] == "") {
            $err [] = "Please provide the [base_url] option.";
        }

        if(count($err) == 0) {

            $from = $this->params['i_from'];
            $limit = $this->params['i_limit'];
            $base_url = $this->options['base_url'];
            $filter = $this->options['request_parameter_name'];

            $page = $limit;

            $showFrom = $from;
            $showTo = $from + $page;
            if ($showTo > $this->total_records) $showTo = $this->total_records;

            $nrpages = intval($this->total_records / $page);
            if ($this->total_records / $page > $nrpages) $nrpages += 1;

            $json['total_records'] = $this->total_records;
            $json['pages'] = $nrpages;

            ?>

            <div class="paginator">
            <div class="text-left col-xs-10 col-sm-10 box_nopad_lr pagination-results-text">
                <?php
                if($this->total_records > 0) {
                    if ($limit > 10000) {
                        ?>
                        <span><b><?php echo $this->total_records . '</b> records. Showing all <b>' . $this->total_records; ?></b></span>
                    <?php
                    } else {
                        ?>
                        <span><b><?php echo $this->total_records . '</b> records. Showing <b>' . ($showFrom + 1) . ' - ' . $showTo; ?></b></span>
                    <?php
                    }
                }
                else {
                    ?>
                    <span><b><?php echo $this->total_records . '</b> records.'; ?></span>
                <?php
                }
                ?>
            </div>
            <?php

            if ($nrpages > 1) {

                $pages_to_show = 5;
                $prev_text = "&laquo;";
                $next_text = "&raquo;";

                $P = ($from + $page) / $page;

                $plink = array(
                    "prev" => array(),
                    "left" => array(),
                    "middle" => array(),
                    "right" => array(),
                    "next" => array()
                );
                //echo '<pre>', print_r($plinks), '</pre>';

                $link_url = $base_url;


                if ($P + 3 > $pages_to_show) {
                    for ($i = 3; $i >= 1; $i--) {
                        $cur_p = $P - $i;
                        if($cur_p > 0) {
                            $cur_from = ($cur_p - 1) * $page;
                            $cur_params = $this->params;
                            $cur_params['i_from'] = $cur_from;
                            $cur_qs = base64_encode(http_build_query($cur_params));
                            $plink['left'][] = array(
                                "href" => $link_url . "&$filter=" . $cur_qs,
                                "rel" => $cur_from,
                                "text" => $cur_p,
                                "page" => $cur_p
                            );
                        }
                    }

                    $cur_p = $P;
                    $cur_from = ($cur_p - 1) * $page;
                    $cur_params = $this->params;
                    $cur_params['i_from'] = $cur_from;
                    $cur_qs = base64_encode(http_build_query($cur_params));
                    $plink['middle'][] = array(
                        "href" => $link_url . "&$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => $cur_p,
                        "page" => $cur_p
                    );
                    if ($nrpages > $P) {

                        for ($i = 1; $i <= ($P + 3 <= $nrpages ? 3 : $nrpages - $P); $i++) {
                            $cur_p = $P + $i;
                            $cur_from = ($cur_p - 1) * $page;
                            $cur_params = $this->params;
                            $cur_params['i_from'] = $cur_from;
                            $cur_qs = base64_encode(http_build_query($cur_params));
                            $plink['right'][] = array(
                                "href" => $link_url . "&$filter=" . $cur_qs,
                                "rel" => $cur_from,
                                "text" => $cur_p,
                                "page" => $cur_p
                            );
                        }
                    }
                } else {
                    for ($i = 1; $i <= (($nrpages < $pages_to_show) ? $nrpages : $pages_to_show); $i++) {
                        $cur_p = $i;
                        $cur_from = ($cur_p - 1) * $page;
                        $cur_params = $this->params;
                        $cur_params['i_from'] = $cur_from;
                        $cur_qs = base64_encode(http_build_query($cur_params));
                        $plink['left'][] = array(
                            "href" => $link_url . "&$filter=" . $cur_qs,
                            "rel" => $cur_from,
                            "text" => $cur_p,
                            "page" => $cur_p
                        );
                    }


                }

                if ($P > 1) {
                    $cur_p = $P - 1;
                    $cur_from = ($cur_p - 1) * $page;
                    $cur_params = $this->params;
                    $cur_params['i_from'] = $cur_from;
                    $cur_qs = base64_encode(http_build_query($cur_params));
                    $plink['prev'][] = array(
                        "href" => $link_url . "&$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => '&laquo; ' . t('Prev'),
                        "page" => $cur_p
                    );
                }

                if ($nrpages > $P) {
                    $cur_p = $P + 1;
                    $cur_from = ($cur_p - 1) * $page;
                    $cur_params = $this->params;
                    $cur_params['i_from'] = $cur_from;
                    $cur_qs = base64_encode(http_build_query($cur_params));
                    $plink['next'][] = array(
                        "href" => $link_url . "&$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => t('Next') . ' &raquo;',
                        "page" => $cur_p
                    );
                }


                ?>
                <div class="col-xs-10 col-sm-10 box_nopad_lr  pagination-results-pages text-left">
                    <ul class="pagination pagination-sm pagination_nomargin pagination-container">
                        <?php
                        if (isset($plink['prev']) && count($plink['prev']) > 0) {
                            $ld = $plink['prev'][0];
                            echo '<li><a href="' . $ld['href'] . '" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
                        } else {
                            echo '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
                        }

                        $margins = array(
                            "left" => array(),
                            "right" => array(),
                        );
                        $margin_left_page = 0;
                        $margin_right_page = 0;
                        if ($P >= 3) {

                            for ($i = 1; $i <= 1; $i++) {
                                $cur_p = $i;
                                $cur_from = ($cur_p - 1) * $page;
                                $cur_params = $this->params;
                                $cur_params['i_from'] = $cur_from;
                                $cur_qs = base64_encode(http_build_query($cur_params));
                                $margins['left'][] = array(
                                    "href" => $link_url . "&$filter=" . $cur_qs,
                                    "rel" => $cur_from,
                                    "text" => $cur_p,
                                    "page" => $cur_p
                                );

                                $margin_left_page = $cur_p;
                            }

                            foreach ($margins['left'] as $i => $ld) {
                                echo '<li><a href="' . $ld['href'] . '">' . $ld['text'] . '</a></li>';
                            }
                            echo '<li><a class="dots"><span>..</span></a></li>';

                        }

                        if ($P + 3 < $nrpages) {

                            for ($i = 1; $i <= 1; $i++) {
                                $cur_p = $nrpages;
                                $cur_from = ($cur_p - 1) * $page;
                                $cur_params = $this->params;
                                $cur_params['i_from'] = $cur_from;
                                $cur_qs = base64_encode(http_build_query($cur_params));
                                $margins['right'][] = array(
                                    "href" => $link_url . "&$filter=" . $cur_qs,
                                    "rel" => $cur_from,
                                    "text" => $cur_p,
                                    "page" => $cur_p
                                );

                                $margin_right_page = $cur_p;
                            }
                        }


                        foreach ($plink as $pos_label => $pos) {
                            foreach ($pos as $i => $ld) {
                                if (
                                    $pos_label != "prev"
                                    && $pos_label != "next"
                                    && $margin_left_page != $ld['page']
                                    && $margin_right_page != $ld['page']
                                ) {
                                    if ($ld['page'] == $P) {
                                        echo '<li class="active"><a href="#">' . $ld['text'] . '<span class="sr-only">(current)</span></a></li>';
                                    } else {
                                        echo '<li><a href="' . $ld['href'] . '">' . $ld['text'] . '</a></li>';
                                    }
                                }
                            }
                        }

                        if (count($margins['right']) > 0) {
                            echo '<li><a class="dots"><span>..</span></a></li>';
                            foreach ($margins['right'] as $i => $ld) {
                                echo '<li><a href="' . $ld['href'] . '">' . $ld['text'] . '</a></li>';
                            }

                        }

                        if (isset($plink['next']) && count($plink['next']) > 0) {
                            $ld = $plink['next'][0];
                            echo '<li><a href="' . $ld['href'] . '" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
                        } else {
                            echo '<li class="disabled"><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
                        }

                        // Show all link
                        $cur_params = $this->params;
                        $cur_params['i_from'] = 0;
                        $cur_params['i_limit'] = 10000000;
                        $cur_qs = base64_encode(http_build_query($cur_params));
                        $all_href = $link_url . "&$filter=" . $cur_qs;
                        if($this->options['show_all'] == 1 ) echo '<li><a href="' . $all_href . '">All</a></li>';
                        ?>
                    </ul>
                </div>

            <?php
            }
            echo '</div>';
        }
        else {
            ?>
            <div class="alert alert-danger">Dev.Error:<br><?php echo implode("<br>", $err);?></div>
            <?php
        }
        $pagination_code = ob_get_contents();
        ob_end_clean();

        return $pagination_code;
    }
}