[795] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * Created on 02/07/2006 |
---|
| 4 | * |
---|
| 5 | * To change the template for this generated file go to |
---|
| 6 | * Window - Preferences - PHPeclipse - PHP - Code Templates |
---|
| 7 | */ |
---|
| 8 | /** |
---|
| 9 | * Database query results paging class |
---|
| 10 | * @author Sidnei Augusto Drovetto Junior |
---|
| 11 | * @version 1.0 |
---|
| 12 | * @package Workflow |
---|
| 13 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 14 | */ |
---|
| 15 | class Paging |
---|
| 16 | { |
---|
| 17 | /** |
---|
| 18 | * @var int $itemsPerPage Number of page itens |
---|
| 19 | * @access public |
---|
| 20 | */ |
---|
| 21 | var $itemsPerPage; |
---|
| 22 | /** |
---|
| 23 | * @var int $itemsCount Number of itens |
---|
| 24 | * @access public |
---|
| 25 | */ |
---|
| 26 | var $itemsCount; |
---|
| 27 | /** |
---|
| 28 | * @var int $numberOfPages Total pages |
---|
| 29 | * @access public |
---|
| 30 | */ |
---|
| 31 | var $numberOfPages; |
---|
| 32 | /** |
---|
| 33 | * @var int $currentPage Actual page |
---|
| 34 | * @access public |
---|
| 35 | */ |
---|
| 36 | var $currentPage; |
---|
| 37 | /** |
---|
| 38 | * @var array $items itens array |
---|
| 39 | * @access public |
---|
| 40 | */ |
---|
| 41 | var $items; |
---|
| 42 | /** |
---|
| 43 | * @var string $baseLink server Url base |
---|
| 44 | * @access public |
---|
| 45 | */ |
---|
| 46 | var $baseLink; |
---|
| 47 | /** |
---|
| 48 | * @var $newRequest |
---|
| 49 | * @access public |
---|
| 50 | */ |
---|
| 51 | var $newRequest; |
---|
| 52 | /** |
---|
| 53 | * @var int $nextItem number of next item |
---|
| 54 | * @access public |
---|
| 55 | */ |
---|
| 56 | var $nextItem; |
---|
| 57 | /** |
---|
| 58 | * @var int $originalRequest |
---|
| 59 | * @access public |
---|
| 60 | */ |
---|
| 61 | var $originalRequest; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * @var string $SQLOrdenacao |
---|
| 65 | * @access public |
---|
| 66 | */ |
---|
| 67 | var $SQLOrdenacao; |
---|
| 68 | /** |
---|
| 69 | * Initialize class Paging atributes |
---|
| 70 | * @acces public |
---|
| 71 | * @return void |
---|
| 72 | */ |
---|
| 73 | function initialize() |
---|
| 74 | { |
---|
| 75 | $this->currentPage = 0; |
---|
| 76 | $this->baseLink = $_SERVER['SCRIPT_NAME']; |
---|
| 77 | $this->items = array(); |
---|
| 78 | |
---|
| 79 | if (!is_null($this->originalRequest)) |
---|
| 80 | $this->parseRequest($this->originalRequest); |
---|
| 81 | } |
---|
| 82 | /** |
---|
| 83 | * Construtor |
---|
| 84 | * @access public |
---|
| 85 | * @return object |
---|
| 86 | */ |
---|
| 87 | function Paging($pItemsPerPage, $request = null) |
---|
| 88 | { |
---|
| 89 | $this->itemsPerPage = $pItemsPerPage; |
---|
| 90 | $this->originalRequest = $request; |
---|
| 91 | |
---|
| 92 | $this->initialize(); |
---|
| 93 | } |
---|
| 94 | /** |
---|
| 95 | * Parse Request |
---|
| 96 | * @param string $request request |
---|
| 97 | * @return void |
---|
| 98 | */ |
---|
| 99 | function parseRequest($request) |
---|
| 100 | { |
---|
| 101 | $this->currentPage = (isset($request['p_page'])) ? $request['p_page'] : 0; |
---|
| 102 | if (is_numeric($this->currentPage)) |
---|
| 103 | $this->currentPage = (int) $this->currentPage; |
---|
| 104 | else |
---|
| 105 | $this->currentPage = 0; |
---|
| 106 | |
---|
| 107 | $this->newRequest = $this->_cleanRequest("p_page", $request); |
---|
| 108 | |
---|
| 109 | $this->nextItem = $this->currentPage * $this->itemsPerPage; |
---|
| 110 | } |
---|
| 111 | /** |
---|
| 112 | * Restrict the number of itens |
---|
| 113 | * @var array $pItems array of page itens |
---|
| 114 | * @var int $totalItems number of total items |
---|
| 115 | * @return array page items |
---|
| 116 | * @access public |
---|
| 117 | */ |
---|
| 118 | function restrictItems($pItems, $totalItems = null) |
---|
| 119 | { |
---|
| 120 | $start = $this->nextItem; |
---|
| 121 | if (is_null($totalItems)) |
---|
| 122 | { |
---|
| 123 | $totalItems = count($pItems); |
---|
| 124 | $end = min($start + $this->itemsPerPage, $totalItems); |
---|
| 125 | $this->items = array(); |
---|
| 126 | for ($i = $start; $i < $end; $i++) |
---|
| 127 | $this->items[] = $pItems[$i]; |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | $this->items = $pItems; |
---|
| 131 | |
---|
| 132 | $this->itemsCount = $totalItems; |
---|
| 133 | return $this->items; |
---|
| 134 | } |
---|
| 135 | /** |
---|
| 136 | * Return pagination Result |
---|
| 137 | * @return array |
---|
| 138 | * @access public |
---|
| 139 | */ |
---|
| 140 | function paginationResult() |
---|
| 141 | { |
---|
| 142 | $this->numberOfPages = ceil(((double) $this->itemsCount / (double) $this->itemsPerPage)); |
---|
| 143 | $output = array(); |
---|
| 144 | if ($this->numberOfPages < 2) |
---|
| 145 | return $output; |
---|
| 146 | |
---|
| 147 | $charSeparator = empty($this->newRequest) ? "" : "&"; |
---|
| 148 | |
---|
| 149 | for ($i = 0; $i < $this->numberOfPages; $i++) |
---|
| 150 | { |
---|
| 151 | $page = $i; |
---|
| 152 | $start = ($i * $this->itemsPerPage) + 1; |
---|
| 153 | $end = min($start - 1 + $this->itemsPerPage, $this->itemsCount); |
---|
| 154 | $current = array( |
---|
| 155 | "link" => $this->baseLink . "?" . $this->newRequest . $charSeparator . "p_page=$page", |
---|
| 156 | "pageNumber" => $page + 1, |
---|
| 157 | "p_page" => $page, |
---|
| 158 | "start" => $start, |
---|
| 159 | "end" => $end); |
---|
| 160 | $output[] = $current; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return $output; |
---|
| 164 | } |
---|
| 165 | /** |
---|
| 166 | * Limit pagination result |
---|
| 167 | * @var int $numberofLinks number of links |
---|
| 168 | * @access public |
---|
| 169 | * @return array |
---|
| 170 | */ |
---|
| 171 | function limitedPaginationResult($numberOfLinks) |
---|
| 172 | { |
---|
| 173 | $allPaginationResults = $this->paginationResult(); |
---|
| 174 | $output = array(); |
---|
| 175 | if (count($allPaginationResults) == 0) |
---|
| 176 | return $output; |
---|
| 177 | |
---|
| 178 | $firstPage = max(0, $this->currentPage - $numberOfLinks); |
---|
| 179 | $lastPage = min($this->currentPage + $numberOfLinks, $this->numberOfPages); |
---|
| 180 | for ($i = $firstPage; $i < $lastPage; $i++) |
---|
| 181 | $output[] = $allPaginationResults[$i]; |
---|
| 182 | |
---|
| 183 | return $output; |
---|
| 184 | } |
---|
| 185 | /** |
---|
| 186 | * Pagination Result navigation |
---|
| 187 | * @access public |
---|
| 188 | * @return array |
---|
| 189 | */ |
---|
| 190 | function paginationResultNavigation() |
---|
| 191 | { |
---|
| 192 | $allPaginationResults = $this->paginationResult(); |
---|
| 193 | |
---|
| 194 | $output = array(); |
---|
| 195 | $output['currentPage'] = $this->currentPage + 1; |
---|
| 196 | |
---|
| 197 | if (($this->currentPage > 0) && isset($allPaginationResults[$this->currentPage - 1])) |
---|
| 198 | $output['previous'] = $allPaginationResults[$this->currentPage - 1]; |
---|
| 199 | |
---|
| 200 | if (($this->currentPage < ($this->numberOfPages - 1)) && isset($allPaginationResults[$this->currentPage + 1])) |
---|
| 201 | $output['next'] = $allPaginationResults[$this->currentPage + 1]; |
---|
| 202 | |
---|
| 203 | return $output; |
---|
| 204 | } |
---|
| 205 | /** |
---|
| 206 | * Common Links |
---|
| 207 | * @var int $numberOfLinks number of links |
---|
| 208 | * @return array $links array of common links |
---|
| 209 | * @access public |
---|
| 210 | */ |
---|
| 211 | function commonLinks($numberOfLinks = 10) |
---|
| 212 | { |
---|
| 213 | $links = array(); |
---|
| 214 | $paginationLinks = $this->limitedPaginationResult($numberOfLinks); |
---|
| 215 | $paginationNavigationLinks = $this->paginationResultNavigation(); |
---|
| 216 | |
---|
| 217 | if (count($paginationLinks) == 0) |
---|
| 218 | return $links; |
---|
| 219 | if (isset($paginationNavigationLinks['previous'])) |
---|
| 220 | { |
---|
| 221 | $tmp = $paginationNavigationLinks['previous']; |
---|
| 222 | $tmp['name'] = "anterior"; |
---|
| 223 | $tmp['do_link'] = true; |
---|
| 224 | $links[] = $tmp; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | for ($i = 0; $i < count($paginationLinks); $i++) |
---|
| 228 | { |
---|
| 229 | $tmp = $paginationLinks[$i]; |
---|
| 230 | $tmp['name'] = $tmp['pageNumber']; |
---|
| 231 | $tmp['do_link'] = ($paginationNavigationLinks['currentPage'] != $tmp['pageNumber']) ? true : false; |
---|
| 232 | $links[] = $tmp; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | if (isset($paginationNavigationLinks['next'])) |
---|
| 236 | { |
---|
| 237 | $tmp = $paginationNavigationLinks['next']; |
---|
| 238 | $tmp['name'] = "próximo"; |
---|
| 239 | $tmp['do_link'] = true; |
---|
| 240 | $links[] = $tmp; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | return $links; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Page Autolinks |
---|
| 248 | * @var int $numberOfLinks number of links |
---|
| 249 | * @var string $linkFormat link format |
---|
| 250 | * @var string $selectedFormat |
---|
| 251 | * @var string $separator separator |
---|
| 252 | * @var int $totalItems number of total items |
---|
| 253 | * @access public |
---|
| 254 | */ |
---|
| 255 | function autoLinks($numberOfLinks = 10, $linkFormat = null, $selectedFormat = null, $separator = " ") |
---|
| 256 | { |
---|
| 257 | $output = ""; |
---|
| 258 | $linkList = $this->commonLinks($numberOfLinks); |
---|
| 259 | |
---|
| 260 | if (is_null($linkFormat)) |
---|
| 261 | $linkFormat = '<a href="%link%">%name%</a>'; |
---|
| 262 | |
---|
| 263 | if (is_null($selectedFormat)) |
---|
| 264 | $selectedFormat = '<strong>%name%</strong>'; |
---|
| 265 | |
---|
| 266 | $correspondenceList = array( |
---|
| 267 | '%name%' => 'name', |
---|
| 268 | '%link%' => 'link', |
---|
| 269 | '%pageNumber%' => 'pageNumber', |
---|
| 270 | '%p_page%' => 'p_page', |
---|
| 271 | '%start%' => 'start', |
---|
| 272 | '%end%' => 'end'); |
---|
| 273 | |
---|
| 274 | $tmp = array(); |
---|
| 275 | |
---|
| 276 | foreach ($linkList as $link) |
---|
| 277 | { |
---|
| 278 | $format = $link['do_link'] ? $linkFormat : $selectedFormat; |
---|
| 279 | foreach ($correspondenceList as $find => $index) |
---|
| 280 | $format = str_replace($find, $link[$index], $format); |
---|
| 281 | $tmp[] = $format; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | $output = implode($separator, $tmp); |
---|
| 285 | |
---|
| 286 | return $output; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /** |
---|
| 290 | * Clean Request |
---|
| 291 | * @var string $remove |
---|
| 292 | * @var string $request |
---|
| 293 | * @return string |
---|
| 294 | * @access public |
---|
| 295 | */ |
---|
| 296 | function _cleanRequest($remove = "", $request = null) |
---|
| 297 | { |
---|
| 298 | if (is_null($request)) |
---|
| 299 | $request = $_GET; |
---|
| 300 | if (!is_array($remove)) |
---|
| 301 | $remove = array($remove); |
---|
| 302 | |
---|
| 303 | $tmp = array(); |
---|
| 304 | $flagAdd = true; |
---|
| 305 | |
---|
| 306 | /* remove selected variables from posted data */ |
---|
| 307 | foreach ($request as $key => $value) |
---|
| 308 | { |
---|
| 309 | $flagAdd = true; |
---|
| 310 | |
---|
| 311 | foreach ($remove as $rem) |
---|
| 312 | if (strcasecmp($key, $rem) == 0) |
---|
| 313 | $flagAdd = false; |
---|
| 314 | |
---|
| 315 | if ($flagAdd) |
---|
| 316 | $tmp[] = $key . "=" . urlencode("$value"); |
---|
| 317 | } |
---|
| 318 | return implode("&", $tmp); |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | ?> |
---|