1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Models\Language; |
6
|
|
|
use App\Models\RolePermission; |
7
|
|
|
use App\Models\User; |
8
|
|
|
use Exception; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
use Illuminate\Support\Facades\File; |
13
|
|
|
use Intervention\Image\Facades\Image; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class BaseService |
17
|
|
|
* |
18
|
|
|
* @package App\Services |
19
|
|
|
*/ |
20
|
|
|
class BaseService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Apply search |
24
|
|
|
* |
25
|
|
|
* @param $builder |
26
|
|
|
* @param $term |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function applySearch($builder, $term) |
31
|
|
|
{ |
32
|
|
|
$builder->where(function ($query) use ($term) { |
33
|
|
|
foreach ($query->getModel()->getSearchable() as $searchColumn) { |
34
|
|
|
if (in_array($searchColumn, $query->getModel()->getEncrypted())) { |
35
|
|
|
$query->orWhereEncrypted($searchColumn, '%' . $term . '%'); |
36
|
|
|
} else { |
37
|
|
|
$query->orWhere($searchColumn, 'LIKE', '%' . $term . '%'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
return $builder; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Apply sort params. |
47
|
|
|
* |
48
|
|
|
* @param Request $request |
49
|
|
|
* @param $builder |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function applySortParams(Request $request, $builder) |
54
|
|
|
{ |
55
|
|
|
if ($request->has('sortColumn') || $request->has('sortOrder')) { |
56
|
|
|
$sortColumn = strtolower($request->get('sortColumn', 'id')); |
57
|
|
|
$sortOrder = strtolower($request->get('sortOrder', 'asc')); |
58
|
|
|
|
59
|
|
|
if (in_array($sortColumn, $builder->getModel()->getSortable()) && in_array($sortOrder, ['asc', 'desc'])) { |
60
|
|
|
if (in_array($sortColumn, $builder->getModel()->getEncrypted())) { |
61
|
|
|
return $builder->orderByEncrypted($sortColumn, $sortOrder); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $builder->orderBy($sortColumn, $sortOrder); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $builder; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get pagination offset and limit. |
73
|
|
|
* |
74
|
|
|
* @param Request $request |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getPaginationParams(Request $request) |
79
|
|
|
{ |
80
|
|
|
$limit = 10; |
81
|
|
|
if ($request->has('limit')) { |
82
|
|
|
$requestLimit = (int)$request->get('limit'); |
83
|
|
|
|
84
|
|
|
if ($requestLimit > 0) { |
85
|
|
|
$limit = $requestLimit; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$offset = 0; |
90
|
|
|
$page = 1; |
91
|
|
|
|
92
|
|
|
if ($request->has('page')) { |
93
|
|
|
$requestPage = (int)$request->get('page'); |
94
|
|
|
|
95
|
|
|
if ($requestPage > 1) { |
96
|
|
|
$page = $requestPage; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$offset = ($page - 1) * $limit; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return [ |
103
|
|
|
'page' => $page, |
104
|
|
|
'offset' => $offset, |
105
|
|
|
'limit' => $limit |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get pagination data. |
111
|
|
|
* |
112
|
|
|
* @param $builder |
113
|
|
|
* @param $page |
114
|
|
|
* @param $limit |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getPaginationData($builder, $page, $limit) |
119
|
|
|
{ |
120
|
|
|
$totalEntries = $builder->count(); |
121
|
|
|
|
122
|
|
|
$totalPages = ceil($totalEntries / $limit); |
123
|
|
|
|
124
|
|
|
return [ |
125
|
|
|
'currentPage' => $page > $totalPages ? $totalPages : $page, |
126
|
|
|
'totalPages' => $totalPages, |
127
|
|
|
'limit' => $limit, |
128
|
|
|
'totalEntries' => $totalEntries |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get language to use. |
134
|
|
|
* |
135
|
|
|
* @param Request $request |
136
|
|
|
* |
137
|
|
|
* @return Language |
138
|
|
|
* |
139
|
|
|
* @throws Exception |
140
|
|
|
*/ |
141
|
|
|
public function getLanguage(Request $request) |
142
|
|
|
{ |
143
|
|
|
if ($request->has('language')) { |
144
|
|
|
$language = Language::where('code', strtolower($request->get('language')))->first(); |
145
|
|
|
|
146
|
|
|
if ($language) { |
147
|
|
|
return $language; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** @var User|null $user */ |
152
|
|
|
$user = Auth::user(); |
153
|
|
|
|
154
|
|
|
if ($user) { |
|
|
|
|
155
|
|
|
$language = Language::where('id', $user->language_id)->first(); |
156
|
|
|
|
157
|
|
|
if ($language) { |
158
|
|
|
return $language; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$language = Language::where('code', env('APP_LOCALE'))->first(); |
163
|
|
|
|
164
|
|
|
if ($language) { |
165
|
|
|
return $language; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
throw new Exception('Application is bad configured!'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check if a logged user has permission on action |
173
|
|
|
* |
174
|
|
|
* @param $userId |
175
|
|
|
* @param $permissionId |
176
|
|
|
* |
177
|
|
|
* @return RolePermission |
178
|
|
|
*/ |
179
|
|
|
public function getUserPermissionActions($userId, $permissionId) |
180
|
|
|
{ |
181
|
|
|
return $rolePermission = Cache::tags(['permissions']) |
|
|
|
|
182
|
|
|
->remember( |
183
|
|
|
'permission' . $userId . $permissionId, |
184
|
|
|
env('CACHE_PERIOD'), |
185
|
|
|
function () use ($userId, $permissionId) { |
186
|
|
|
return RolePermission::where('permission_id', $permissionId) |
187
|
|
|
->whereHas('role', function ($query) use ($userId) { |
188
|
|
|
$query->whereHas('users', function ($query) use ($userId) { |
189
|
|
|
$query->where('id', $userId); |
190
|
|
|
}); |
191
|
|
|
})->first(); |
192
|
|
|
} |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Process images. |
198
|
|
|
* |
199
|
|
|
* @param $path |
200
|
|
|
* @param $image |
201
|
|
|
* @param $name |
202
|
|
|
* @param bool $generateAvatar |
203
|
|
|
* @param bool $onlyAvatar |
204
|
|
|
* |
205
|
|
|
* @return false|string |
206
|
|
|
*/ |
207
|
|
|
public function processImage($path, $image, $name, $generateAvatar = false, $onlyAvatar = false) |
208
|
|
|
{ |
209
|
|
|
$pictureData = []; |
210
|
|
|
|
211
|
|
|
if ($generateAvatar) { |
212
|
|
|
$avatarImage = Image::make($image)->resize(200, 200, function ($constraint) { |
213
|
|
|
$constraint->aspectRatio(); |
214
|
|
|
$constraint->upsize(); |
215
|
|
|
}); |
216
|
|
|
|
217
|
|
|
$color = $this->getColorAverage($avatarImage); |
218
|
|
|
|
219
|
|
|
$avatarCanvas = Image::canvas(200, 200, $color); |
220
|
|
|
|
221
|
|
|
$avatarCanvas->insert($avatarImage, 'center'); |
222
|
|
|
|
223
|
|
|
$avatarPath = $path . 'avatar/'; |
224
|
|
|
File::makeDirectory($avatarPath, 0777, true, true); |
225
|
|
|
|
226
|
|
|
$avatarCanvas->save($avatarPath . $name, 100); |
227
|
|
|
|
228
|
|
|
$pictureData['avatar'] = $avatarPath . $name; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($onlyAvatar) { |
232
|
|
|
return json_encode($pictureData); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$mediumImage = Image::make($image)->resize(1024, 768, function ($constraint) { |
236
|
|
|
$constraint->aspectRatio(); |
237
|
|
|
$constraint->upsize(); |
238
|
|
|
}); |
239
|
|
|
|
240
|
|
|
$mediumPath = $path . 'medium/'; |
241
|
|
|
File::makeDirectory($mediumPath, 0777, true, true); |
242
|
|
|
|
243
|
|
|
$mediumImage->save($mediumPath . $name, 100); |
244
|
|
|
$pictureData['medium'] = $mediumPath . $name; |
245
|
|
|
|
246
|
|
|
$originalMaxImage = Image::make($image)->resize(1920, 1080, function ($constraint) { |
247
|
|
|
$constraint->aspectRatio(); |
248
|
|
|
$constraint->upsize(); |
249
|
|
|
}); |
250
|
|
|
|
251
|
|
|
$originalPath = $path . 'original/'; |
252
|
|
|
File::makeDirectory($originalPath, 0777, true, true); |
253
|
|
|
|
254
|
|
|
$originalMaxImage->save($originalPath . $name, 100); |
255
|
|
|
$pictureData['original'] = $originalPath . $name; |
256
|
|
|
|
257
|
|
|
return json_encode($pictureData); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get average image color. |
262
|
|
|
* |
263
|
|
|
* @param $image |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
private function getColorAverage($image) |
267
|
|
|
{ |
268
|
|
|
$image = clone $image; |
269
|
|
|
|
270
|
|
|
$color = $image->limitColors(1)->pickColor(0, 0); |
271
|
|
|
$image->destroy(); |
272
|
|
|
|
273
|
|
|
return $color; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|