<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 4/23/2015
 * Time: 2:57 PM
 */
class Paginator {

    public $total_records = 0;
    public $options = array(
        "base_url"  => "",
        "request_parameter_name" => "filter",
        "records_per_page" => 25
    );
    public $qs_separator = '?';

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

    function __construct($options = null)
    {
        if($options != null) $this->options = array_merge($this->options, $options);
    }


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

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

        if($options != null) $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'];
            
            if(strpos($base_url, '?') !== false) $this->qs_separator = '&';

            $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;
            ?>

            <div class="search-results-pagination text-left">
                <div class="search-results-num">
                    <?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 = 10;
                $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 + 5 > $pages_to_show) {
                    for ($i = 5; $i >= 1; $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(json_encode($cur_params));
                        $plink['left'][] = array(
                            "href" => $link_url . $this->qs_separator . "$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(json_encode($cur_params));
                    $plink['middle'][] = array(
                        "href" => $link_url . $this->qs_separator . "$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => $cur_p,
                        "page" => $cur_p
                    );
                    if ($nrpages > $P) {

                        for ($i = 1; $i <= ($P + 5 <= $nrpages ? 5 : $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(json_encode($cur_params));
                            $plink['right'][] = array(
                                "href" => $link_url . $this->qs_separator . "$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(json_encode($cur_params));
                        $plink['left'][] = array(
                            "href" => $link_url . $this->qs_separator . "$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(json_encode($cur_params));
                    $plink['prev'][] = array(
                        "href" => $link_url . $this->qs_separator . "$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => '&laquo; ' . ('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(json_encode($cur_params));
                    $plink['next'][] = array(
                        "href" => $link_url . $this->qs_separator . "$filter=" . $cur_qs,
                        "rel" => $cur_from,
                        "text" => ('Next') . ' &raquo;',
                        "page" => $cur_p
                    );
                }


                ?>
                <nav>
                    <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 >= 5) {

                            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(json_encode($cur_params));
                                $margins['left'][] = array(
                                    "href" => $link_url . $this->qs_separator . "$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 href="#" 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(json_encode($cur_params));
                                $margins['right'][] = array(
                                    "href" => $link_url . $this->qs_separator . "$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 href="#" 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>';
                        }

                        ?>
                    </ul>
                </nav>

            <?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;
    }
    
}