1
|
|
|
<?php |
2
|
|
|
use Illuminate\Support\Facades\Route; |
|
|
|
|
3
|
|
|
|
4
|
|
|
if(!function_exists('urlsafe_b64encode')){ |
5
|
|
|
function urlsafe_b64encode($string) { |
6
|
|
|
$data = base64_encode($string); |
7
|
|
|
$data = str_replace(array('+','/','='),array('-','_',''),$data); |
8
|
|
|
return $data; |
9
|
|
|
} |
10
|
|
|
} |
11
|
|
|
if(!function_exists('urlsafe_b64decode')){ |
12
|
|
|
function urlsafe_b64decode($string) { |
13
|
|
|
$data = str_replace(array('-','_'),array('+','/'),$string); |
14
|
|
|
$mod4 = strlen($data) % 4; |
15
|
|
|
if ($mod4) { |
16
|
|
|
$data .= substr('====', $mod4); |
17
|
|
|
} |
18
|
|
|
return base64_decode($data); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if(!function_exists('init_rapid_paginator_cache')){ |
23
|
|
|
/* This function is supposed to cache 'Form' values... |
24
|
|
|
* |
25
|
|
|
* @param Array $fields |
26
|
|
|
* @return Array $result |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
function init_rapid_paginator_cache($fields = null, $tab=1){ |
30
|
|
|
//init cache |
31
|
|
|
$cache = isset($fields) ? count($fields) > 0 ? [] : null : null; |
32
|
|
|
|
33
|
|
|
if($cache == [] || $cache == null){ |
34
|
|
|
$cache['sort'] = '>'; |
35
|
|
|
$cache['perPage'] = '10'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// If form is submitted... |
39
|
|
|
// Cache Form values |
40
|
|
|
if (request()->isMethod('POST')) { |
41
|
|
|
foreach ($fields as $fieldName) { |
|
|
|
|
42
|
|
|
if(request($fieldName)){ |
43
|
|
|
$cache[$fieldName] = request($fieldName); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
else // else we have to retrieve old cache from the state |
48
|
|
|
{ |
49
|
|
|
$state_array = null; |
50
|
|
|
|
51
|
|
|
// Decode The State |
52
|
|
View Code Duplication |
if(request('state') && request('tab') == $tab){ |
|
|
|
|
53
|
|
|
$state_base64 = request('state'); |
54
|
|
|
$state_decoded = urlsafe_b64decode($state_base64); |
55
|
|
|
$state_array = json_decode($state_decoded,true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Append the state cache key/value pairs to the new cache... |
59
|
|
|
if(isset($state_array['cache'])){ |
60
|
|
|
if (is_array($state_array['cache']) || is_object($state_array['cache'])) |
61
|
|
|
{ |
62
|
|
|
foreach ($state_array['cache'] as $key => $value) { |
63
|
|
|
$cache[$key] = $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $cache; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
if(!function_exists('rapid_paginator')){ |
73
|
|
|
/* Custom pagination System Based on RapidPagination package |
74
|
|
|
* @param Query $query |
75
|
|
|
* @param Array $field |
76
|
|
|
* @param Char $sort |
77
|
|
|
* @param Integer $sort |
78
|
|
|
* @param Boolean $seekable |
79
|
|
|
* @return Array $result |
80
|
|
|
*/ |
81
|
|
|
function rapid_paginator($query, $field = 'id', $appendQuery=false, $cache = null, $sort = '>', $perPage = 10, $tab=1, $seekable = true) |
82
|
|
|
{ |
83
|
|
|
if($cache == null) |
84
|
|
|
init_rapid_paginator_cache(null); |
85
|
|
|
/* |
86
|
|
|
** Setup Default values |
87
|
|
|
*/ |
88
|
|
|
/*if($sort == null) |
89
|
|
|
$sort = '>'; |
90
|
|
|
else |
91
|
|
|
$cache['sort'] = $sort; |
92
|
|
|
|
93
|
|
|
if($field == null) |
94
|
|
|
$field = 'id'; |
95
|
|
|
|
96
|
|
|
if($perPage == null) |
97
|
|
|
$perPage = 10; |
98
|
|
|
else |
99
|
|
|
$cache['perPage'] = $perPage;*/ |
100
|
|
|
|
101
|
|
|
/* |
102
|
|
|
** Extract Cursor from the State route parameter |
103
|
|
|
** Cursor is used as a reference to navigate to the next or previous 'pages'... |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
$state_array = null; |
107
|
|
|
|
108
|
|
|
// Decode the State |
109
|
|
View Code Duplication |
if(request('state') && request('tab') == $tab){ |
|
|
|
|
110
|
|
|
$state_base64 = request('state'); |
111
|
|
|
$state_decoded = urlsafe_b64decode($state_base64); |
112
|
|
|
$state_array = json_decode($state_decoded,true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$cursor = null; |
116
|
|
|
|
117
|
|
|
// Add cursor from state to the newCursor array |
118
|
|
|
if($state_array){ |
119
|
|
|
if(isset($state_array['cursor'][$field])) |
120
|
|
|
$cursor[$field] = $state_array['cursor'][$field]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
// Create a new paginator |
125
|
|
|
$paginator = $query->rapid_pagination() |
126
|
|
|
->limit($perPage); // Set Number of elements Per Page (default=10) |
127
|
|
|
|
128
|
|
|
// Sort by 'field'.. |
129
|
|
|
if($sort == '>' || $sort == null) |
130
|
|
|
$paginator = $paginator->orderBy($field); |
131
|
|
|
else |
132
|
|
|
$paginator = $paginator->orderByDesc($field); |
133
|
|
|
|
134
|
|
|
// Get 'Previous Cursor' to be able to navigate backwards |
135
|
|
|
if($seekable) |
136
|
|
|
$paginator = $paginator->seekable(); |
137
|
|
|
|
138
|
|
|
// If 'Next' Button is Clicked |
139
|
|
|
if(request()->direction == "next" || request()->direction == null){ |
140
|
|
|
$paginator = $paginator->forward(); // Use forward method to change the direction of the navigation |
141
|
|
|
} |
142
|
|
|
// If 'Previous' Button is Clicked |
143
|
|
|
else{ |
144
|
|
|
$paginator = $paginator->backward(); // Use backward method to change the direction of the navigation |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Navigation rules |
148
|
|
|
if($cursor != null){ |
149
|
|
|
$paginator = $paginator |
150
|
|
|
->paginate($cursor); |
151
|
|
|
} |
152
|
|
|
else{ |
153
|
|
|
$paginator = $paginator |
154
|
|
|
->paginate(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/* |
158
|
|
|
** Prepare a new State |
159
|
|
|
*/ |
160
|
|
|
|
161
|
|
|
// Extract cursors from paginator |
162
|
|
|
$paginatorArray = (array)$paginator; |
163
|
|
|
unset($paginatorArray['records']); // We don't need to encode records in the state |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
// Next and Previous buttons have different cursors that's why we need state for every button |
167
|
|
|
|
168
|
|
|
// Next Btn State... |
169
|
|
|
$state_next = [ |
170
|
|
|
'cursor' => $paginatorArray['nextCursor'], |
171
|
|
|
'cache' => $cache |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
// Previous Btn State... |
175
|
|
|
$state_prev = [ |
176
|
|
|
'cursor' => $paginatorArray['previousCursor'], |
177
|
|
|
'cache' => $cache |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
// Encode States |
181
|
|
|
$base64_next_state = urlsafe_b64encode(json_encode($state_next)); |
182
|
|
|
$base64_prev_state = urlsafe_b64encode(json_encode($state_prev)); |
183
|
|
|
|
184
|
|
|
//Set tab id |
185
|
|
|
$paginator->setTabID($tab); |
186
|
|
|
|
187
|
|
|
if($appendQuery){ |
188
|
|
|
$paginator->appends(request()->query()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Set paginator previous and next Urls |
192
|
|
|
$paginator->makePreviousUrl($base64_prev_state); |
193
|
|
|
$paginator->makeNextUrl($base64_next_state); |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
$result = [ |
198
|
|
|
'items' => $paginator, |
199
|
|
|
'cache' => $cache |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
return $result; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: