Skip to content
Snippets Groups Projects
class.pagenate.php 4.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jared Hancock's avatar
    Jared Hancock committed
    <?php
    /*********************************************************************
        class.format.php
    
        Pagenation  support class
    
        Peter Rotich <peter@osticket.com>
    
        Copyright (c)  2006-2013 osTicket
    
    Jared Hancock's avatar
    Jared Hancock committed
        http://www.osticket.com
    
        Released under the GNU General Public License WITHOUT ANY WARRANTY.
        See LICENSE.TXT for details.
    
        vim: expandtab sw=4 ts=4 sts=4:
    **********************************************************************/
    
    Jared Hancock's avatar
    Jared Hancock committed
    class PageNate {
    
        var $start;
        var $limit;
    
        var $slack = 0;
    
        var $total;
        var $page;
        var $pages;
    
        function PageNate($total,$page,$limit=20,$url='') {
            $this->total = intval($total);
            $this->limit = max($limit, 1 );
            $this->page  = max($page, 1 );
            $this->start = max((($page-1)*$this->limit),0);
            $this->pages = ceil( $this->total / $this->limit );
    
            if (($this->limit > $this->total) || ($this->page>ceil($this->total/$this->limit))) {
                $this->start = 0;
            }
            if (($this->limit-1)*$this->start > $this->total) {
                $this->start -= $this->start % $this->limit;
            }
            $this->setURL($url);
        }
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function setURL($url='',$vars='') {
            if ($url) {
                if (strpos($url, '?')===false)
                    $url .= '?';
            } else {
             $url = THISPAGE.'?';
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if ($vars && is_array($vars))
                $vars = Http::build_query($vars);
    
            $this->url = $url.$vars;
    
    Jared Hancock's avatar
    Jared Hancock committed
    
        function getStart() {
    
            return max($this->start - $this->slack, 0);
    
    Jared Hancock's avatar
    Jared Hancock committed
        }
    
        function getLimit() {
            return $this->limit;
        }
    
        function setSlack($count) {
            $this->slack = $count;
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getNumPages(){
            return $this->pages;
        }
    
    Jared Hancock's avatar
    Jared Hancock committed
        function getPage() {
            return ceil(($this->start+1)/$this->limit);
        }
    
        function showing() {
            $html = '';
    
            $start = $this->getStart() + 1;
    
            $end = min($start + $this->limit + $this->slack - 1, $this->total);
            if ($end < $this->total) {
                $to= $end;
    
            } else {
                $to= $this->total;
            }
    
            $html="&nbsp;".__('Showing')."&nbsp;&nbsp;";
    
            if ($this->total > 0) {
    
                $html .= sprintf(__('%1$d - %2$d of %3$d' /* Used in pagination output */),
    
                   $start, $end, $this->total);
    
            }else{
                $html .= " 0 ";
            }
            return $html;
        }
    
        function getPageLinks($hash=false, $pjax=false) {
    
            $html                 = '';
            $file                =$this->url;
            $displayed_span     = 5;
    
            $total_pages         = ceil( ($this->total - $this->slack) / $this->limit );
    
            $this_page             = ceil( ($this->start+1) / $this->limit );
    
    Jared Hancock's avatar
    Jared Hancock committed
    
            $last=$this_page-1;
            $next=$this_page+1;
    
            $start_loop         = floor($this_page-$displayed_span);
    
    Jared Hancock's avatar
    Jared Hancock committed
            $stop_loop          = ceil($this_page + $displayed_span);
    
    Jared Hancock's avatar
    Jared Hancock committed
            $stopcredit    =($start_loop<1)?0-$start_loop:0;
            $startcredit   =($stop_loop>$total_pages)?$stop_loop-$total_pages:0;
    
    Jared Hancock's avatar
    Jared Hancock committed
            $start_loop =($start_loop-$startcredit>0)?$start_loop-$startcredit:1;
            $stop_loop  =($stop_loop+$stopcredit>$total_pages)?$total_pages:$stop_loop+$stopcredit;
    
            if($start_loop>1){
                $lastspan=($start_loop-$displayed_span>0)?$start_loop-$displayed_span:1;
                $html .= "\n<a href=\"$file&p=$lastspan\" ><strong>&laquo;</strong></a>";
            }
    
            for ($i=$start_loop; $i <= $stop_loop; $i++) {
                $page = ($i - 1) * $this->limit;
    
                $href = "{$file}&amp;p={$i}";
                if ($hash)
                    $href .= '#'.$hash;
    
                if ($i == $this_page) {
                    $html .= "\n<b>[$i]</b>";
    
                }
                elseif ($pjax) {
                    $html .= " <a href=\"{$href}\" data-pjax-container=\"{$pjax}\"><b>$i</b></a>";
    
                    $html .= "\n<a href=\"{$href}\" ><b>$i</b></a>";
    
    Jared Hancock's avatar
    Jared Hancock committed
            if($stop_loop<$total_pages){
                $nextspan=($stop_loop+$displayed_span>$total_pages)?$total_pages-$displayed_span:$stop_loop+$displayed_span;
    
                $href = "{$file}&amp;p={$nextspan}";
                if ($hash)
                    $href .= '#'.$hash;
                $html .= "\n<a href=\"{$href}\" ><strong>&raquo;</strong></a>";
    
        function paginate(QuerySet $qs) {
    
            $start = $this->getStart();
    
            $end = min($start + $this->getLimit() + $this->slack + ($start > 0 ? $this->slack : 0), $this->total);
            return $qs->limit($end-$start)->offset($start);