1+ <?php 
2+ 
3+ namespace  InfinityBrackets \Core ;
4+ /* 
5+  * This is not for sale but it's free to use for any web application 
6+  * 
7+  * @package    Infinity Brackets 
8+  * @author     John Vincent Bonza 
9+  * @copyright  2021 Infinity Brackets 
10+  * @license    Free 
11+  * @version    v3.0 
12+  */ 
13+ use  InfinityBrackets \Core \Pagination ;
14+ 
15+ class  Pagination {
16+     public  $ paginationControls  = "" ;
17+     public  $ maxControlsPerPage  = 5 ;
18+     public  $ current  = 1 ;
19+     public  $ last  = 1 ;
20+     public  $ queryString  = "" ;
21+ 
22+     public  function  __construct ($ config  = []) {
23+         if (array_key_exists ('current ' , $ config )) {
24+             $ this  ->current  = $ config ['current ' ];
25+         }
26+         if (array_key_exists ('last ' , $ config )) {
27+             $ this  ->last  = $ config ['last ' ];
28+         }
29+         if (array_key_exists ('queryString ' , $ config )) {
30+             $ this  ->queryString  .= $ config ['queryString ' ];
31+         }
32+         if (array_key_exists ('orderBy ' , $ config )) {
33+             $ this  ->queryString  .= '&orderBy= '  . $ config ['orderBy ' ];
34+         }
35+     }
36+     
37+     public  function  GeneratePagination () {
38+         $ this  ->queryString  .='&page= ' ;
39+         if ($ this  ->last  != 1 ) {
40+             if  ($ this  ->current  > 1 ) {
41+                 // First Page 
42+                 if ($ this  ->current  >= $ this  ->maxControlsPerPage  - 1 ) {
43+                     $ this  ->paginationControls  .= '<li class="paginate_button page-item"><a class="page-link" href=" '  . $ this  ->queryString  . '1"><span class="ib_pagination"><i class="fas fa-angle-double-left"></i></a></li> ' ;
44+                 }
45+                 // Before Active Page 
46+                 for ($ i  = $ this  ->current  - ($ this  ->maxControlsPerPage  - 1 ); $ i  < $ this  ->current ; $ i ++) {
47+                     if ($ i  > 0 ) {
48+                         if ($ this  ->current  - 3  < $ i ) {
49+                             $ this  ->paginationControls  .= '<li class="paginate_button page-item"><a class="page-link" href=" '  . $ this  ->queryString  . $ i  . '"><span class="ib_pagination"> '  . $ i  . '</a></li> ' ;
50+                         }
51+                     }
52+                 }
53+             }
54+             // Active Page 
55+             $ this  ->paginationControls  .= '<li class="paginate_button page-item active"><a class="page-link" href="javascript:void(0)"> '  . $ this  ->current  . '</a></li> ' ;
56+             // After Active Page 
57+             for  ($ i  = $ this  ->current  + 1 ; $ i  <= $ this  ->last ; $ i ++){
58+                 $ this  ->paginationControls  .= '<li class="paginate_button page-item"><a class="page-link" href=" '  . $ this  ->queryString  . $ i  . '"> '  . $ i  . '</a></li> ' ;
59+                 if ($ i  >= $ this  ->current  + 2 ) {
60+                     break ;
61+                 }
62+             }
63+             // this->last Page 
64+             if ($ this  ->last  >= $ this  ->current  + 3 ) {
65+                 $ this  ->paginationControls  .= '<li class="paginate_button page-item"><a class="page-link" href=" '  . $ this  ->queryString  . $ this  ->last  . '"><span class="ib_pagination"><i class="fas fa-angle-double-right"></i></a></li> ' ;
66+             }
67+         }
68+         return  $ this  ;
69+     }
70+ 
71+     public  function  Render () {
72+         return  $ this  ->paginationControls ;
73+     }
74+ 
75+     public  function  Paginate ($ total , $ limit , $ options  = []) {
76+         $ last  = ceil ($ total /$ limit );
77+         if ($ last  < 1 ){
78+             $ last  = 1 ;
79+         }
80+ 
81+         // Establish the $pagenum variable 
82+         $ page  = 1 ;
83+         $ link  = '?page= ' ;
84+ 
85+         // Configure options 
86+         if ($ options ) {
87+             // querystring 
88+             if (array_key_exists ('querystring ' , $ options )) {
89+                 $ link  = $ options ['querystring ' ] . '&page= ' ;
90+             }
91+             //page 
92+             if (array_key_exists ('page ' , $ options )) {
93+                 $ page  = $ options ['page ' ];
94+             }
95+         }
96+ 
97+         // Get page from URL vars if it is present, else it is = 1 
98+         if (isset ($ request ['page ' ])) {
99+             $ page  = preg_replace ('#[^0-9]# ' , '' , $ request ['page ' ]);
100+         }
101+         if  ($ page  < 1 ) { 
102+             $ page  = 1 ; 
103+         } else  if  ($ page  > $ last ) { 
104+             $ page  = $ last ; 
105+         }
106+ 
107+         $ pages  = [];
108+         $ current  = $ page ;
109+ 
110+         if ($ last  != 1 ) {
111+             // Previous button 
112+             if ($ page  == 1 ) {
113+                 $ pages [] = [
114+                     'page '  => NULL ,
115+                     'link '  => NULL ,
116+                     'type '  => 'previous ' 
117+                 ];
118+             } else  {
119+                 $ pages [] = [
120+                     'page '  => $ page  - 1 ,
121+                     'link '  => $ link  . ($ page  - 1 ),
122+                     'type '  => 'previous ' 
123+                 ];
124+             }
125+             if  ($ current  > 1 ) {
126+                 // Before Active Page 
127+                 for ($ i  = $ current  - ($ this  ->maxControlsPerPage  - 1 ); $ i  < $ current ; $ i ++) {
128+                     if ($ i  > 0 ) {
129+                         if ($ current  - 4  < $ i ) {
130+                             $ pages [] = [
131+                                 'page '  => $ i ,
132+                                 'link '  => $ link  . $ i ,
133+                                 'type '  => 'default ' 
134+                             ];
135+                         }
136+                     }
137+                 }
138+             }
139+ 
140+             // Current page 
141+             $ pages [] = [
142+                 'page '  => $ page ,
143+                 'link '  => $ link  . $ page ,
144+                 'type '  => 'active ' 
145+             ];
146+             
147+             // After Active Page 
148+             for  ($ i  = $ current  + 1 ; $ i  <= $ last ; $ i ++){
149+                 $ pages [] = [
150+                     'page '  => $ i ,
151+                     'link '  => $ link  . $ i ,
152+                     'type '  => 'default ' 
153+                 ];
154+                 if ($ i  >= $ current  + 3 ) {
155+                     break ;
156+                 }
157+             }
158+ 
159+             // Next button 
160+             if ($ page  == $ last ) {
161+                 $ pages [] = [
162+                     'page '  => NULL ,
163+                     'link '  => NULL ,
164+                     'type '  => 'next ' 
165+                 ];
166+             } else  {
167+                 $ pages [] = [
168+                     'page '  => $ page  + 1 ,
169+                     'link '  => $ link  . ($ page  + 1 ),
170+                     'type '  => 'next ' 
171+                 ];
172+             }
173+         }
174+ 
175+         return  Application::$ app ->ToObject ($ pages );
176+     }
177+ }
0 commit comments