|
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 urlsafe_b64decode($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){ |
|
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')){ |
|
|
|
|
|
|
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
|
|
|
foreach ($state_array['cache'] as $key => $value) { |
|
61
|
|
|
$cache[$key] = $value; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $cache; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
if(!function_exists('rapid_paginator')){ |
|
70
|
|
|
/* Custom pagination System Based on RapidPagination package |
|
71
|
|
|
* @param Query $query |
|
72
|
|
|
* @param Array $field |
|
73
|
|
|
* @param Char $sort |
|
74
|
|
|
* @param Integer $sort |
|
75
|
|
|
* @param Boolean $seekable |
|
76
|
|
|
* @return Array $result |
|
77
|
|
|
*/ |
|
78
|
|
|
function rapid_paginator($query, $field = 'id', $cache = null, $sort = '>', $perPage = 10, $seekable = true) |
|
79
|
|
|
{ |
|
80
|
|
|
if($cache == null) |
|
81
|
|
|
init_rapid_paginator_cache(null); |
|
82
|
|
|
/* |
|
83
|
|
|
** Setup Default values |
|
84
|
|
|
*/ |
|
85
|
|
|
if($sort == null) |
|
86
|
|
|
$sort = '>'; |
|
87
|
|
|
|
|
88
|
|
|
if($field == null) |
|
89
|
|
|
$field = 'id'; |
|
90
|
|
|
|
|
91
|
|
|
if($perPage == null) |
|
92
|
|
|
$perPage = 10; |
|
93
|
|
|
|
|
94
|
|
|
/* |
|
95
|
|
|
** Extract Cursor from the State route parameter |
|
96
|
|
|
** Cursor is used as a reference to navigate to the next or previous 'pages'... |
|
97
|
|
|
*/ |
|
98
|
|
|
|
|
99
|
|
|
$state_array = null; |
|
100
|
|
|
|
|
101
|
|
|
// Decode the State |
|
102
|
|
View Code Duplication |
if(request('state')){ |
|
|
|
|
|
|
103
|
|
|
$state_base64 = request('state'); |
|
104
|
|
|
$state_decoded = urlsafe_b64decode($state_base64); |
|
105
|
|
|
$state_array = json_decode($state_decoded,true); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$cursor = null; |
|
109
|
|
|
|
|
110
|
|
|
// Add cursor from state to the newCursor array |
|
111
|
|
|
if($state_array){ |
|
112
|
|
|
if(isset($state_array['cursor'][$field])) |
|
113
|
|
|
$cursor[$field] = $state_array['cursor'][$field]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
// Create a new paginator |
|
118
|
|
|
$paginator = $query->rapid_pagination() |
|
119
|
|
|
->limit($perPage); // Set Number of elements Per Page (default=10) |
|
120
|
|
|
|
|
121
|
|
|
// Sort by 'field'.. |
|
122
|
|
|
if($sort == '>' || $sort == null) |
|
123
|
|
|
$paginator = $paginator->orderBy($field); |
|
124
|
|
|
else |
|
125
|
|
|
$paginator = $paginator->orderByDesc($field); |
|
126
|
|
|
|
|
127
|
|
|
// Get 'Previous Cursor' to be able to navigate backwards |
|
128
|
|
|
if($seekable) |
|
129
|
|
|
$paginator = $paginator->seekable(); |
|
130
|
|
|
|
|
131
|
|
|
// If 'Next' Button is Clicked |
|
132
|
|
|
if(request()->direction == "next" || request()->direction == null){ |
|
133
|
|
|
$paginator = $paginator->forward(); // Use forward method to change the direction of the navigation |
|
134
|
|
|
} |
|
135
|
|
|
// If 'Previous' Button is Clicked |
|
136
|
|
|
else{ |
|
137
|
|
|
$paginator = $paginator->backward(); // Use backward method to change the direction of the navigation |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Navigation rules |
|
141
|
|
|
if($cursor != null){ |
|
142
|
|
|
$paginator = $paginator |
|
143
|
|
|
->paginate($cursor); |
|
144
|
|
|
} |
|
145
|
|
|
else{ |
|
146
|
|
|
$paginator = $paginator |
|
147
|
|
|
->paginate(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/* |
|
151
|
|
|
** Prepare a new State |
|
152
|
|
|
*/ |
|
153
|
|
|
|
|
154
|
|
|
// Extract cursors from paginator |
|
155
|
|
|
$paginatorArray = (array)$paginator; |
|
156
|
|
|
unset($paginatorArray['records']); // We don't need to encode records in the state |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
// Next and Previous buttons have different cursors that's why we need state for every button |
|
160
|
|
|
|
|
161
|
|
|
// Next Btn State... |
|
162
|
|
|
$state_next = [ |
|
163
|
|
|
'cursor' => $paginatorArray['nextCursor'], |
|
164
|
|
|
'cache' => $cache |
|
165
|
|
|
]; |
|
166
|
|
|
|
|
167
|
|
|
// Previous Btn State... |
|
168
|
|
|
$state_prev = [ |
|
169
|
|
|
'cursor' => $paginatorArray['previousCursor'], |
|
170
|
|
|
'cache' => $cache |
|
171
|
|
|
]; |
|
172
|
|
|
|
|
173
|
|
|
// Encode States |
|
174
|
|
|
$base64_next_state = urlsafe_b64encode(json_encode($state_next)); |
|
175
|
|
|
$base64_prev_state = urlsafe_b64encode(json_encode($state_prev)); |
|
176
|
|
|
|
|
177
|
|
|
// Set paginator previous and next Urls |
|
178
|
|
|
$paginator->makePreviousUrl($base64_prev_state); |
|
179
|
|
|
$paginator->makeNextUrl($base64_next_state); |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
$result = [ |
|
183
|
|
|
'items' => $paginator, |
|
184
|
|
|
'cache' => $cache |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
|
|
return $result; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: