[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of TubePress (http://tubepress.org)
|
---|
| 6 | *
|
---|
| 7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * TubePress is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | function_exists('tubepress_load_classes')
|
---|
| 23 | || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
|
---|
| 24 | tubepress_load_classes(array('org_tubepress_pagination_PaginationService',
|
---|
| 25 | 'org_tubepress_options_category_Display',
|
---|
| 26 | 'net_php_pear_Net_URL2',
|
---|
| 27 | 'org_tubepress_message_MessageService',
|
---|
| 28 | 'org_tubepress_querystring_QueryStringService',
|
---|
| 29 | 'org_tubepress_options_manager_OptionsManager'));
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * General purpose cache for TubePress
|
---|
| 33 | */
|
---|
| 34 | class org_tubepress_pagination_DiggStylePaginationService implements org_tubepress_pagination_PaginationService
|
---|
| 35 | {
|
---|
| 36 | private $_messageService;
|
---|
| 37 | private $_tpom;
|
---|
| 38 | private $_queryStringService;
|
---|
| 39 |
|
---|
| 40 | public function getHtml($vidCount)
|
---|
| 41 | {
|
---|
| 42 | $currentPage = $this->_queryStringService->getPageNum($_GET);
|
---|
| 43 | $vidsPerPage = $this->_tpom->
|
---|
| 44 | get(org_tubepress_options_category_Display::RESULTS_PER_PAGE);
|
---|
| 45 |
|
---|
| 46 | $newurl = new net_php_pear_Net_URL2($this->_queryStringService->getFullUrl($_SERVER));
|
---|
| 47 | $newurl->unsetQueryVariable("tubepress_page");
|
---|
| 48 |
|
---|
| 49 | return $this->_diggStyle($this->_messageService, $currentPage, $vidCount,
|
---|
| 50 | $vidsPerPage, 1, $newurl->getURL(),
|
---|
| 51 | "tubepress_page");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public function setMessageService(org_tubepress_message_MessageService $messageService) { $this->_messageService = $messageService; }
|
---|
| 55 | public function setQueryStringService(org_tubepress_querystring_QueryStringService $queryStringService) { $this->_queryStringService = $queryStringService; }
|
---|
| 56 | public function setOptionsManager(org_tubepress_options_manager_OptionsManager $tpom) { $this->_tpom = $tpom; }
|
---|
| 57 |
|
---|
| 58 | //function to return the pagination string
|
---|
| 59 | private function _diggStyle(org_tubepress_message_MessageService $messageService, $page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
|
---|
| 60 | {
|
---|
| 61 | //defaults
|
---|
| 62 | if(!$adjacents) $adjacents = 1;
|
---|
| 63 | if(!$limit) $limit = 15;
|
---|
| 64 | if(!$page) $page = 1;
|
---|
| 65 | if(!$targetpage) $targetpage = "/";
|
---|
| 66 |
|
---|
| 67 | //other vars
|
---|
| 68 | $prev = $page - 1; //previous page is page - 1
|
---|
| 69 | $next = $page + 1; //next page is page + 1
|
---|
| 70 | $lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
|
---|
| 71 | $lpm1 = $lastpage - 1; //last page minus 1
|
---|
| 72 |
|
---|
| 73 | /*
|
---|
| 74 | Now we apply our rules and draw the pagination object.
|
---|
| 75 | We're actually saving the code to a variable in case we want to draw it more than once.
|
---|
| 76 | */
|
---|
| 77 | $pagination = "";
|
---|
| 78 |
|
---|
| 79 | $url = new net_php_pear_Net_URL2($targetpage);
|
---|
| 80 |
|
---|
| 81 | if($lastpage > 1)
|
---|
| 82 | {
|
---|
| 83 | $pagination .= "<div class=\"pagination\"";
|
---|
| 84 |
|
---|
| 85 | $pagination .= ">";
|
---|
| 86 |
|
---|
| 87 | //previous button
|
---|
| 88 | if ($page > 1) {
|
---|
| 89 | $url->setQueryVariable($pagestring, $prev);
|
---|
| 90 | $newurl = $url->getURL();
|
---|
| 91 | $pagination .= "<a href=\"$newurl\">" . $messageService->_("prev") . "</a>";
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | $pagination .= "<span class=\"disabled\">" . $messageService->_("prev") . "</span>";
|
---|
| 95 |
|
---|
| 96 | //pages
|
---|
| 97 | if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
|
---|
| 98 | {
|
---|
| 99 | for ($counter = 1; $counter <= $lastpage; $counter++)
|
---|
| 100 | {
|
---|
| 101 | if ($counter == $page)
|
---|
| 102 | $pagination .= "<span class=\"current\">$counter</span>";
|
---|
| 103 | else {
|
---|
| 104 | $url->setQueryVariable($pagestring, $counter);
|
---|
| 105 | $newurl = $url->getURL();
|
---|
| 106 | $pagination .= "<a href=\"$newurl\">$counter</a>";
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
|
---|
| 111 | {
|
---|
| 112 | //close to beginning; only hide later pages
|
---|
| 113 | if($page < 1 + ($adjacents * 3))
|
---|
| 114 | {
|
---|
| 115 | for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
|
---|
| 116 | {
|
---|
| 117 | if ($counter == $page)
|
---|
| 118 | $pagination .= "<span class=\"current\">$counter</span>";
|
---|
| 119 | else {
|
---|
| 120 | $url->setQueryVariable($pagestring, $counter);
|
---|
| 121 | $newurl = $url->getURL();
|
---|
| 122 | $pagination .= "<a href=\"$newurl\">$counter</a>";
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | $pagination .= "...";
|
---|
| 126 | $url->setQueryVariable($pagestring, $lpm1);
|
---|
| 127 | $newurl = $url->getURL();
|
---|
| 128 | $pagination .= " <a href=\"$newurl\">$lpm1</a>";
|
---|
| 129 | $url->setQueryVariable($pagestring, $lastpage);
|
---|
| 130 | $newurl = $url->getURL();
|
---|
| 131 | $pagination .= "<a href=\"$newurl\">$lastpage</a>";
|
---|
| 132 | }
|
---|
| 133 | //in middle; hide some front and some back
|
---|
| 134 | elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
|
---|
| 135 | {
|
---|
| 136 | $url->setQueryVariable($pagestring, 1);
|
---|
| 137 | $newurl = $url->getURL();
|
---|
| 138 | $pagination .= "<a href=\"$newurl\">1</a>";
|
---|
| 139 | $url->setQueryVariable($pagestring, 2);
|
---|
| 140 | $newurl = $url->getURL();
|
---|
| 141 | $pagination .= "<a href=\"$newurl\">2</a>";
|
---|
| 142 | $pagination .= "...";
|
---|
| 143 | for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
|
---|
| 144 | {
|
---|
| 145 | if ($counter == $page)
|
---|
| 146 | $pagination .= "<span class=\"current\">$counter</span>";
|
---|
| 147 | else {
|
---|
| 148 | $url->setQueryVariable($pagestring, $counter);
|
---|
| 149 | $newurl = $url->getURL();
|
---|
| 150 | $pagination .= " <a href=\"$newurl\">$counter</a>";
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | $pagination .= "...";
|
---|
| 154 |
|
---|
| 155 | $url->setQueryVariable($pagestring, $lpm1);
|
---|
| 156 | $newurl = $url->getURL();
|
---|
| 157 | $pagination .= " <a href=\"$newurl\">$lpm1</a>";
|
---|
| 158 | $url->setQueryVariable($pagestring, $lastpage);
|
---|
| 159 | $newurl = $url->getURL();
|
---|
| 160 | $pagination .= " <a href=\"$newurl\">$lastpage</a>";
|
---|
| 161 | }
|
---|
| 162 | //close to end; only hide early pages
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | $url->setQueryVariable($pagestring, 1);
|
---|
| 166 | $newurl = $url->getURL();
|
---|
| 167 | $pagination .= "<a href=\"$newurl\">1</a>";
|
---|
| 168 | $url->setQueryVariable($pagestring, 2);
|
---|
| 169 | $newurl = $url->getURL();
|
---|
| 170 | $pagination .= "<a href=\"$newurl\">2</a>";
|
---|
| 171 | $pagination .= "...";
|
---|
| 172 | for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
|
---|
| 173 | {
|
---|
| 174 | if ($counter == $page)
|
---|
| 175 | $pagination .= "<span class=\"current\">$counter</span>";
|
---|
| 176 | else {
|
---|
| 177 | $url->setQueryVariable($pagestring, $counter);
|
---|
| 178 | $newurl = $url->getURL();
|
---|
| 179 | $pagination .= " <a href=\"$newurl\">$counter</a>";
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | //next button
|
---|
| 186 | if ($page < $counter - 1) {
|
---|
| 187 | $url->setQueryVariable($pagestring, $next);
|
---|
| 188 | $newurl = $url->getURL();
|
---|
| 189 | $pagination .= "<a href=\"$newurl\">" . $messageService->_("next") . "</a>";
|
---|
| 190 | } else {
|
---|
| 191 | $pagination .= "<span class=\"disabled\">" . $messageService->_("next") . "</span>";
|
---|
| 192 | }
|
---|
| 193 | $pagination .= "</div>\n";
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | return $pagination;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|