<?php
class Paginator{
    // declare all inernal private variables only accessbile withen this class
    private $_conn;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;
    private $_row_start;
//constructor method is called automaticaly when object is instantiated with new keyword
    public function __construct($conn,$query)
    {
        $this->_conn = $conn;
        $this->_query = $query;

        // $rs = $this->_conn->query($this->_query);
        $rs = $this->_conn->prepare($this->_query);
        $rs->execute();
        // $this->_total = $rs->num_rows ;// total number of rows
        $this->_total = $rs->rowCount();
    }

    public function getData($limit = 10, $page = 1){
        $this->_limit = $limit ;
        $this->_page = $page;
        $results = array();
        if($this->_limit == 'all'){
            $query = $this->_query;
        }else{
            $this->_row_start = (($this->_page - 1) * $this->_limit);
            // add to original query
            $query = $this->_query." LIMIT {$this->_row_start}, $this->_limit";
        }
        //echo $query ; die;

        $rs = $this->_conn->prepare($query);
        $rs->execute();
        while($row = $rs->fetch(PDO::FETCH_ASSOC)){
            //starte this array in $result->data below
            // $results[] = $row;
            array_push($results, $row);
        }

        // return data as object, new stdClass() creates new empty object
        $result = new stdClass();
        $result->page       = $this->_page;
        $result->row_start  = $this->_row_start;
        $result->total      = $this->_total;
        $result->data       = $results;//$result->data = array

        //print_r($result(object));die;
        return $result; //object
    }

    //print links
    public function createLinks($links , $list_class){
        if($this->_limit == 'all'){
            return '';
        }
        //get last page number
        $last = ceil($this->_total / $this->_limit);

        //calcualte start or range for link printing
        $start = (($this->_page - $links)> 0) ? $this->_page - $links: 1 ;
        // end of range for link printing
        $end = (($this->_page + $links)< $last) ? $this->_page + $links: $last ;
        //debugging
        // echo '$total : '.$this->_total.' |' ; // total rows
        // echo '$row_start : '.$this->_row_start.'|'; // total rows
        // echo '$limit: '.$this->_limit.'|'; // total rows per query
        // echo '$start: '.$start.'|';
        // echo '$end: '.$end.'|';
        // echo '$last: '.$last.'|';
        // echo '$page: '.$this->_page .'|' ;//current page
        // echo '$links: '.$links.'<br />' ; //links

        //ul boot strap class -"pagination pagination-sm"
        $html = '<ul class="pagination">';
        $class = ($this->_page == 1)? "disabled":"page-item";// disable previous page link <<<

        //create the links and pass limit and page as $_GET parameters
        
        //$this->_page - 1 = previous page (<<< link)
        $previous_page = ($this->_page == 1)?
        '<li class="'.$class.'"><a class="page-link" href="">&laquo;</a></li>':// remove link from previous button
        '<li class="'.$class.'"><a class="page-link" href="?limit='.$this->_limit.'&page='.($this->_page - 1).'">&laquo;</a></li>';

        $html .= $previous_page ;
        if($start > 1)//print ... before (previous <<< link)
        {
            $html.='<li><a class="page-link" href="?limit='.$this->_limit.'&page=1">1</a></li>'; // print first page
            $html .='<li class="disabled"><span>...</span></li>';
        }

        //print all the numbered page links
        for ($i = $start ; $i <= $end; $i++)
        {
            $class = ($this->_page == $i)?"active":"";//highlight current page
            $html .='<li class="'.$class.'"><a class="page-link" href="?limit=' . $this->_limit.'&page='.$i.'">'.$i.'</a></li>';
        }

        if($end < $last){// print ... before next page (>>> link)
            $html .='<li class="disabled"><span>...</span></li>'; // print ... if not last page
            $html .= '<li><a class="page-link" href="?limit='.$this->_limit.'&page='.$last.'">'.$last.'</a></li>';
        }
        $class = ($this->_page == $last)? "disabled":""; // disable (>>> next page link)

        // $this ->_page + 1 = next page (>>> link)
        $next_page = ($this->_page == $last)?
        '<li class="'.$class.'"><a class="page-link" href="">&raquo;</a></li>'://remove link from next button
        '<li class="'.$class.'"><a class="page-link" href="?limit='.$this->_limit.'&page='.($this->_page + 1).'">&raquo;</a></li>' ;

        $html .=$next_page ;
        $html .='</ul>';

        return $html ;
    }
}