| 1 | <?php
 | 
  
    | 2 | class Zend_View_Helper_DiggPagination
 | 
  
    | 3 | {
 | 
  
    | 4 |     /**
 | 
  
    | 5 |      * Builds & returns a variable containing the HTML code to display pagination links based on given params
 | 
  
    | 6 |      *
 | 
  
    | 7 |      * @param int $page - the current page
 | 
  
    | 8 |      * @param int $totalitems - the number of items to paginate (not total number of pages)
 | 
  
    | 9 |      * @param int $limit - the number of items per page
 | 
  
    | 10 |      * @param int $adjacents - the number of page links to put adjacent to the current page
 | 
  
    | 11 |      * @param string $targetpage - URL to the web page requiring pagination links e.g. /module/controller/action/id/abc123123/
 | 
  
    | 12 |      * @param string $pagestring - the URL params to pass the new page value e.g. page/ (the number is inserted at the end of the string)
 | 
  
    | 13 |      * @param string $cssClass - the class of the containing DIV tag for the returned pagination
 | 
  
    | 14 |      * @return void
 | 
  
    | 15 |      * @author Stranger Studios, adapted by Rich Milns
 | 
  
    | 16 |      * @see http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
 | 
  
    | 17 |      */
 | 
  
    | 18 |     public function diggPagination($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "page/", $cssClass='diggPagination')
 | 
  
    | 19 |     {
 | 
  
    | 20 |         //defaults
 | 
  
    | 21 |         if(!$adjacents) $adjacents = 1;
 | 
  
    | 22 |         if(!$limit) $limit = 15;
 | 
  
    | 23 |         if(!$page) $page = 1;
 | 
  
    | 24 |         if(!$targetpage) $targetpage = "/";
 | 
  
    | 25 | 
 | 
  
    | 26 |         //other vars
 | 
  
    | 27 |         $prev = $page - 1;                                    //previous page is page - 1
 | 
  
    | 28 |         $next = $page + 1;                                    //next page is page + 1
 | 
  
    | 29 |         $lastpage = ceil($totalitems / $limit);                //lastpage is = total items / items per page, rounded up.
 | 
  
    | 30 |         $lpm1 = $lastpage - 1;                                //last page minus 1
 | 
  
    | 31 | 
 | 
  
    | 32 |         /*
 | 
  
    | 33 |             Now we apply our rules and draw the pagination object.
 | 
  
    | 34 |             We're actually saving the code to a variable in case we want to draw it more than once.
 | 
  
    | 35 |         */
 | 
  
    | 36 |         $pagination = "";
 | 
  
    | 37 |         if($lastpage > 1)
 | 
  
    | 38 |         {
 | 
  
    | 39 |             $pagination .= "<div class=\"" . $cssClass . "\"";
 | 
  
    | 40 |             if($margin || $padding)
 | 
  
    | 41 |             {
 | 
  
    | 42 |                 $pagination .= " style=\"";
 | 
  
    | 43 |                 if($margin)
 | 
  
    | 44 |                     $pagination .= "margin: $margin;";
 | 
  
    | 45 |                 if($padding)
 | 
  
    | 46 |                     $pagination .= "padding: $padding;";
 | 
  
    | 47 |                 $pagination .= "\"";
 | 
  
    | 48 |             }
 | 
  
    | 49 |             $pagination .= ">";
 | 
  
    | 50 | 
 | 
  
    | 51 |             //previous button
 | 
  
    | 52 |             if ($page > 1)
 | 
  
    | 53 |                 $pagination .= "<a href=\"$targetpage$pagestring$prev\">« prev</a>";
 | 
  
    | 54 |             else
 | 
  
    | 55 |                 $pagination .= "<span class=\"disabled\">« prev</span>";
 | 
  
    | 56 | 
 | 
  
    | 57 |             //pages
 | 
  
    | 58 |             if ($lastpage < 7 + ($adjacents * 2))    //not enough pages to bother breaking it up
 | 
  
    | 59 |             {
 | 
  
    | 60 |                 for ($counter = 1; $counter <= $lastpage; $counter++)
 | 
  
    | 61 |                 {
 | 
  
    | 62 |                     if ($counter == $page)
 | 
  
    | 63 |                         $pagination .= "<span class=\"current\">$counter</span>";
 | 
  
    | 64 |                     else
 | 
  
    | 65 |                         $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
 | 
  
    | 66 |                 }
 | 
  
    | 67 |             }
 | 
  
    | 68 |             elseif($lastpage >= 7 + ($adjacents * 2))    //enough pages to hide some
 | 
  
    | 69 |             {
 | 
  
    | 70 |                 //close to beginning; only hide later pages
 | 
  
    | 71 |                 if($page < 1 + ($adjacents * 3))
 | 
  
    | 72 |                 {
 | 
  
    | 73 |                     for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
 | 
  
    | 74 |                     {
 | 
  
    | 75 |                         if ($counter == $page)
 | 
  
    | 76 |                             $pagination .= "<span class=\"current\">$counter</span>";
 | 
  
    | 77 |                         else
 | 
  
    | 78 |                             $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
 | 
  
    | 79 |                     }
 | 
  
    | 80 |                     $pagination .= "<span class=\"elipses\">...</span>";
 | 
  
    | 81 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
 | 
  
    | 82 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
 | 
  
    | 83 |                 }
 | 
  
    | 84 |                 //in middle; hide some front and some back
 | 
  
    | 85 |                 elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
 | 
  
    | 86 |                 {
 | 
  
    | 87 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
 | 
  
    | 88 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
 | 
  
    | 89 |                     $pagination .= "<span class=\"elipses\">...</span>";
 | 
  
    | 90 |                     for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
 | 
  
    | 91 |                     {
 | 
  
    | 92 |                         if ($counter == $page)
 | 
  
    | 93 |                             $pagination .= "<span class=\"current\">$counter</span>";
 | 
  
    | 94 |                         else
 | 
  
    | 95 |                             $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
 | 
  
    | 96 |                     }
 | 
  
    | 97 |                     $pagination .= "...";
 | 
  
    | 98 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
 | 
  
    | 99 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
 | 
  
    | 100 |                 }
 | 
  
    | 101 |                 //close to end; only hide early pages
 | 
  
    | 102 |                 else
 | 
  
    | 103 |                 {
 | 
  
    | 104 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
 | 
  
    | 105 |                     $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
 | 
  
    | 106 |                     $pagination .= "<span class=\"elipses\">...</span>";
 | 
  
    | 107 |                     for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
 | 
  
    | 108 |                     {
 | 
  
    | 109 |                         if ($counter == $page)
 | 
  
    | 110 |                             $pagination .= "<span class=\"current\">$counter</span>";
 | 
  
    | 111 |                         else
 | 
  
    | 112 |                             $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
 | 
  
    | 113 |                     }
 | 
  
    | 114 |                 }
 | 
  
    | 115 |             }
 | 
  
    | 116 | 
 | 
  
    | 117 |             //next button
 | 
  
    | 118 |             if ($page < $counter - 1)
 | 
  
    | 119 |                 $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next »</a>";
 | 
  
    | 120 |             else
 | 
  
    | 121 |                 $pagination .= "<span class=\"disabled\">next »</span>";
 | 
  
    | 122 |             $pagination .= "</div>\n";
 | 
  
    | 123 |         }
 | 
  
    | 124 | 
 | 
  
    | 125 |         return $pagination;
 | 
  
    | 126 | 
 | 
  
    | 127 |     }
 | 
  
    | 128 | }
 |