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