Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
32 / 34 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| PaginationHelper | |
93.94% |
31 / 33 |
|
0.00% |
0 / 1 |
10.02 | |
0.00% |
0 / 1 |
| render | |
93.94% |
31 / 33 |
|
0.00% |
0 / 1 |
10.02 | |||
| 1 | <?php |
| 2 | |
| 3 | App::uses('AppHelper', 'View/Helper'); |
| 4 | |
| 5 | /** |
| 6 | * PaginationHelper - Generates pagination controls |
| 7 | * |
| 8 | * Renders page navigation with Previous/Next buttons, page numbers, |
| 9 | * and ellipsis for large page counts. Automatically preserves query |
| 10 | * parameters and generates anchor links for smooth scrolling. |
| 11 | */ |
| 12 | class PaginationHelper extends AppHelper |
| 13 | { |
| 14 | /** |
| 15 | * Render pagination controls |
| 16 | * |
| 17 | * @param int $currentPage Current page number (1-indexed) |
| 18 | * @param int $totalPages Total number of pages |
| 19 | * @param string $paramName Query parameter name (e.g., 'activity_page') |
| 20 | * @return string HTML pagination controls |
| 21 | */ |
| 22 | public static function render($currentPage, $totalPages, $paramName) |
| 23 | { |
| 24 | if ($totalPages <= 1) |
| 25 | return ''; |
| 26 | |
| 27 | // Auto-generate anchor ID from param name (e.g., 'activity_page' -> '#pagination-activity') |
| 28 | $anchorId = '#pagination-' . str_replace('_page', '', $paramName); |
| 29 | $divId = 'pagination-' . str_replace('_page', '', $paramName); |
| 30 | |
| 31 | // Build query string preserving other pagination parameters |
| 32 | $queryParams = $_GET; |
| 33 | unset($queryParams[$paramName]); // Remove current param, will add it back with new value |
| 34 | $baseQuery = empty($queryParams) ? '?' : '?' . http_build_query($queryParams) . '&'; |
| 35 | |
| 36 | // Style variables for easy customization |
| 37 | $containerStyle = 'margin:15px 0; text-align:center;'; |
| 38 | $ellipsisStyle = 'margin:0 5px; color:#999;'; |
| 39 | |
| 40 | $output = '<div id="' . $divId . '" style="' . $containerStyle . '">'; |
| 41 | $output .= '<span class="paginator-info">Page ' . $currentPage . ' of ' . $totalPages . '</span>'; |
| 42 | |
| 43 | // Previous button |
| 44 | if ($currentPage > 1) |
| 45 | $output .= '<a class="paginator-link" href="' . $baseQuery . $paramName . '=' . ($currentPage - 1) . $anchorId . '">« Previous</a>'; |
| 46 | |
| 47 | // Show page numbers with reduced range for tighter pagination |
| 48 | $pages = []; |
| 49 | $pages[] = 1; |
| 50 | for ($i = max(2, $currentPage - 1); $i <= min($totalPages - 1, $currentPage + 1); $i++) |
| 51 | $pages[] = $i; |
| 52 | if ($totalPages > 1) |
| 53 | $pages[] = $totalPages; |
| 54 | $pages = array_unique($pages); |
| 55 | sort($pages); |
| 56 | |
| 57 | // Render page numbers with ellipsis |
| 58 | $lastPage = 0; |
| 59 | foreach ($pages as $page) |
| 60 | { |
| 61 | if ($page > $lastPage + 1) |
| 62 | $output .= '<span style="' . $ellipsisStyle . '">...</span>'; |
| 63 | if ($page == $currentPage) |
| 64 | $output .= '<span class="active-paginator-link">' . $page . '</span>'; |
| 65 | else |
| 66 | $output .= '<a class="paginator-link" href="' . $baseQuery . $paramName . '=' . $page . $anchorId . '">' . $page . '</a>'; |
| 67 | $lastPage = $page; |
| 68 | } |
| 69 | |
| 70 | // Next button |
| 71 | if ($currentPage < $totalPages) |
| 72 | $output .= '<a class="paginator-link" href="' . $baseQuery . $paramName . '=' . ($currentPage + 1) . $anchorId . '">Next »</a>'; |
| 73 | |
| 74 | $output .= '</div>'; |
| 75 | return $output; |
| 76 | } |
| 77 | } |