1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
6
|
|
|
use Apps\Model\Front\Profile\FormAvatarUpload; |
7
|
|
|
use Apps\Model\Front\Profile\FormIgnoreAdd; |
8
|
|
|
use Apps\Model\Front\Profile\FormIgnoreDelete; |
9
|
|
|
use Apps\Model\Front\Profile\FormPasswordChange; |
10
|
|
|
use Apps\Model\Front\Profile\FormSettings; |
11
|
|
|
use Apps\Model\Front\Profile\FormUserSearch; |
12
|
|
|
use Apps\Model\Front\Profile\FormWallPostDelete; |
13
|
|
|
use Apps\Model\Front\Profile\FormWallPost; |
14
|
|
|
use Extend\Core\Arch\FrontAppController; |
15
|
|
|
use Ffcms\Core\App; |
16
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
17
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
18
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
19
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
20
|
|
|
use Ffcms\Core\Helper\Serialize; |
21
|
|
|
use Ffcms\Core\Helper\Type\Str; |
22
|
|
|
use Apps\ActiveRecord\Profile as ProfileRecords; |
23
|
|
|
use Ffcms\Core\Helper\Url; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Profile - user profiles interaction |
28
|
|
|
* @package Apps\Controller\Front |
29
|
|
|
*/ |
30
|
|
|
class Profile extends FrontAppController |
31
|
|
|
{ |
32
|
|
|
const BLOCK_PER_PAGE = 10; |
33
|
|
|
|
34
|
|
|
public function actionIndex($filter_name, $filter_value = null) |
35
|
|
|
{ |
36
|
|
|
$records = null; |
37
|
|
|
|
38
|
|
|
// set current page and offset |
39
|
|
|
$page = (int)App::$Request->query->get('page'); |
40
|
|
|
$cfgs = Serialize::decode($this->application->configs); |
41
|
|
|
$userPerPage = (int)$cfgs['usersOnPage']; |
42
|
|
|
if ($userPerPage < 1) { |
43
|
|
|
$userPerPage = 1; |
44
|
|
|
} |
45
|
|
|
$offset = $page * $userPerPage; |
46
|
|
|
|
47
|
|
|
switch ($filter_name) { |
48
|
|
|
case 'rating': // rating list, order by rating DESC |
49
|
|
|
// check if rating is enabled |
50
|
|
|
if ((int)$cfgs['rating'] !== 1) { |
51
|
|
|
throw new NotFoundException(); |
52
|
|
|
} |
53
|
|
|
$records = (new ProfileRecords())->orderBy('rating', 'DESC'); |
54
|
|
|
break; |
55
|
|
|
case 'hobby': // search by hobby |
56
|
|
|
if (Str::likeEmpty($filter_value)) { |
57
|
|
|
throw new NotFoundException(); |
58
|
|
|
} |
59
|
|
|
$records = (new ProfileRecords())->where('hobby', 'like', '%' . $filter_value . '%'); |
60
|
|
|
break; |
61
|
|
|
case 'city': |
62
|
|
|
if (Str::likeEmpty($filter_value)) { |
63
|
|
|
throw new NotFoundException(); |
64
|
|
|
} |
65
|
|
|
$records = (new ProfileRecords())->where('city', '=', $filter_value); |
66
|
|
|
break; |
67
|
|
|
case 'born': |
68
|
|
|
if ($filter_value === null || !Obj::isLikeInt($filter_value)) { |
69
|
|
|
throw new NotFoundException(); |
70
|
|
|
} |
71
|
|
|
$records = (new ProfileRecords())->where('birthday', 'like', $filter_value . '-%'); |
72
|
|
|
break; |
73
|
|
|
case 'all': |
74
|
|
|
$records = (new ProfileRecords())->orderBy('id', 'DESC'); |
75
|
|
|
break; |
76
|
|
|
default: |
77
|
|
|
App::$Response->redirect('profile/index/all'); |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// build pagination |
82
|
|
|
$pagination = new SimplePagination([ |
83
|
|
|
'url' => ['profile/index', $filter_name, $filter_value], |
84
|
|
|
'page' => $page, |
85
|
|
|
'step' => $userPerPage, |
86
|
|
|
'total' => $records->count() |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return App::$View->render('index', [ |
90
|
|
|
'records' => $records->skip($offset)->take($userPerPage)->get(), |
91
|
|
|
'pagination' => $pagination, |
92
|
|
|
'id' => $filter_name, |
93
|
|
|
'add' => $filter_value, |
94
|
|
|
'ratingOn' => (int)$cfgs['rating'] |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Show user profile: data, wall posts, other features |
100
|
|
|
* @param $userId |
101
|
|
|
* @throws NotFoundException |
102
|
|
|
* @throws ForbiddenException |
103
|
|
|
*/ |
104
|
|
|
public function actionShow($userId) |
105
|
|
|
{ |
106
|
|
|
$cfg = Serialize::decode($this->application->configs); |
107
|
|
|
if ((int)$cfg['guestView'] !== 1 && !App::$User->isAuth()) { |
108
|
|
|
throw new ForbiddenException('You must be registered user to view other profile'); |
109
|
|
|
} |
110
|
|
|
// check if target exists |
111
|
|
|
if (!App::$User->isExist($userId)) { |
112
|
|
|
throw new NotFoundException('This profile is not exist'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User |
116
|
|
|
$viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User |
117
|
|
|
|
118
|
|
|
$wallModel = null; |
119
|
|
|
// if current user is auth - allow to post messages on wall current user |
120
|
|
|
if (App::$User->isAuth() && $viewerPersone->getRole()->can('global/write')) { |
121
|
|
|
$wallModel = new FormWallPost(); |
122
|
|
|
// check if request post is done and rules validated |
123
|
|
|
if ($wallModel->send() && $wallModel->validate()) { |
124
|
|
|
// maybe in blacklist? |
125
|
|
|
if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) { |
126
|
|
|
App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!')); |
127
|
|
|
} else { |
128
|
|
|
// check if message added |
129
|
|
|
if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) { |
|
|
|
|
130
|
|
|
App::$Session->getFlashBag()->add('success', __('The message was successful posted!')); |
131
|
|
|
} else { |
132
|
|
|
App::$Session->getFlashBag()->add('warning', __('Posting message was failed! You need to wait some time...')); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$query = $targetPersone->getWall(); // relation hasMany from users to walls |
139
|
|
|
// pagination and query params |
140
|
|
|
$wallPage = (int)App::$Request->query->get('page'); |
141
|
|
|
$wallItems = (int)$cfg['wallPostOnPage']; |
142
|
|
|
$wallOffset = $wallPage * $wallItems; |
143
|
|
|
|
144
|
|
|
// build pagination |
145
|
|
|
$wallPagination = new SimplePagination([ |
146
|
|
|
'url' => ['profile/show', $userId, null], |
147
|
|
|
'page' => $wallPage, |
148
|
|
|
'step' => $wallItems, |
149
|
|
|
'total' => $query->count() |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
// get wall messages |
153
|
|
|
$wallRecords = $query->orderBy('id', 'desc')->skip($wallOffset)->take($wallItems)->get(); |
154
|
|
|
|
155
|
|
|
return App::$View->render('show', [ |
156
|
|
|
'user' => $targetPersone, |
157
|
|
|
'viewer' => $viewerPersone, |
158
|
|
|
'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id), |
|
|
|
|
159
|
|
|
'wall' => !Obj::isObject($wallModel) ? null : $wallModel->export(), |
160
|
|
|
'notify' => App::$Session->getFlashBag()->all(), |
161
|
|
|
'wallRecords' => $wallRecords, |
162
|
|
|
'pagination' => $wallPagination, |
163
|
|
|
'ratingOn' => (int)$cfg['rating'] === 1 |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* User avatar management |
169
|
|
|
*/ |
170
|
|
|
public function actionAvatar() |
171
|
|
|
{ |
172
|
|
|
if (!App::$User->isAuth()) { |
173
|
|
|
throw new ForbiddenException('You must be authorized user!'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// get user identity and model object |
177
|
|
|
$user = App::$User->identity(); |
178
|
|
|
$model = new FormAvatarUpload(); |
179
|
|
|
|
180
|
|
|
// validate model post data |
181
|
|
|
if ($model->send()) { |
182
|
|
|
if ($model->validate()) { |
183
|
|
|
$model->copyFile($user); |
|
|
|
|
184
|
|
|
App::$Session->getFlashBag()->add('success', __('Avatar is successful changed')); |
185
|
|
|
} else { |
186
|
|
|
App::$Session->getFlashBag()->add('error', __('File upload is failed!')); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return App::$View->render('avatar', [ |
191
|
|
|
'user' => $user, |
192
|
|
|
'model' => $model->export() |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Allow post owners and targets delete |
198
|
|
|
* @param int $postId |
199
|
|
|
* @throws ForbiddenException |
200
|
|
|
* @throws NotFoundException |
201
|
|
|
*/ |
202
|
|
|
public function actionWalldelete($postId) |
203
|
|
|
{ |
204
|
|
|
// is user auth? |
205
|
|
|
if (!App::$User->isAuth()) { |
206
|
|
|
throw new ForbiddenException(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// is postId is integer? |
210
|
|
|
if (!Obj::isLikeInt($postId) || $postId < 1) { |
211
|
|
|
throw new NotFoundException(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// try to find the wall post |
215
|
|
|
$wallPost = \Apps\ActiveRecord\WallPost::find($postId); |
216
|
|
|
if (null === $wallPost || false === $wallPost) { |
217
|
|
|
throw new NotFoundException(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// get user and check if he can delete this post |
221
|
|
|
$user = App::$User->identity(); |
222
|
|
|
if ($wallPost->sender_id !== $user->id && $wallPost->target_id !== $user->id) { |
|
|
|
|
223
|
|
|
throw new ForbiddenException(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// check if submit sended |
227
|
|
|
$wallModel = new FormWallPostDelete($wallPost); |
|
|
|
|
228
|
|
|
if ($wallModel->send() && $wallModel->validate()) { |
229
|
|
|
$wallModel->make(); |
230
|
|
|
App::$Response->redirect('profile/show/' . $wallPost->target_id); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return App::$View->render('wall_delete', [ |
234
|
|
|
'post' => $wallPost, |
235
|
|
|
'model' => $wallModel->export() |
236
|
|
|
]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Show user messages (based on ajax, all in template) |
241
|
|
|
* @throws ForbiddenException |
242
|
|
|
*/ |
243
|
|
|
public function actionMessages() |
244
|
|
|
{ |
245
|
|
|
if (!App::$User->isAuth()) { |
246
|
|
|
throw new ForbiddenException(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return App::$View->render('messages'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* User profile settings |
254
|
|
|
* @throws ForbiddenException |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
// check if auth |
259
|
|
|
if (!App::$User->isAuth()) { |
260
|
|
|
throw new ForbiddenException(); |
261
|
|
|
} |
262
|
|
|
// get user object |
263
|
|
|
$user = App::$User->identity(); |
264
|
|
|
// create model and pass user object |
265
|
|
|
$model = new FormSettings($user); |
|
|
|
|
266
|
|
|
|
267
|
|
|
// check if form is submited |
268
|
|
|
if ($model->send() && $model->validate()) { |
269
|
|
|
$model->save(); |
270
|
|
|
App::$Session->getFlashBag()->add('success', __('Profile data are successful updated')); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// render view |
274
|
|
|
return App::$View->render('settings', [ |
275
|
|
|
'model' => $model->export() |
276
|
|
|
]); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Action change user password |
281
|
|
|
* @throws ForbiddenException |
282
|
|
|
*/ |
283
|
|
View Code Duplication |
public function actionPassword() |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
// check if user is authed |
286
|
|
|
if (!App::$User->isAuth()) { |
287
|
|
|
throw new ForbiddenException(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// get user object and create model with user object |
291
|
|
|
$user = App::$User->identity(); |
292
|
|
|
$model = new FormPasswordChange($user); |
|
|
|
|
293
|
|
|
|
294
|
|
|
// check if form is submited and validation is passed |
295
|
|
|
if ($model->send() && $model->validate()) { |
296
|
|
|
$model->make(); |
297
|
|
|
App::$Session->getFlashBag()->add('success', __('Password is successful changed')); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// set response output |
301
|
|
|
return App::$View->render('password', [ |
302
|
|
|
'model' => $model->export() |
303
|
|
|
]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Show users in blacklist and allow add new users |
308
|
|
|
* @throws ForbiddenException |
309
|
|
|
*/ |
310
|
|
|
public function actionIgnore() |
311
|
|
|
{ |
312
|
|
|
// check if not auth |
313
|
|
|
if (!App::$User->isAuth()) { |
314
|
|
|
throw new ForbiddenException(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// get user object and init model of it |
318
|
|
|
$user = App::$User->identity(); |
319
|
|
|
$model = new FormIgnoreAdd($user); |
|
|
|
|
320
|
|
|
|
321
|
|
|
// set user id from ?id= get param if form not sended |
322
|
|
|
if (!$model->send()) { |
323
|
|
|
$uid = (int)App::$Request->query->get('id'); |
324
|
|
|
if ($uid > 0) { |
325
|
|
|
$model->id = $uid; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// sended new block add? |
330
|
|
|
if ($model->send() && $model->validate()) { |
331
|
|
|
if ($model->save()) { |
332
|
|
|
App::$Session->getFlashBag()->add('success', __('User was successful blocked!')); |
333
|
|
|
} else { |
334
|
|
|
App::$Session->getFlashBag()->add('error', __('This user is always in ignore list')); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
// get blocked users |
339
|
|
|
$records = Blacklist::where('user_id', '=', $user->getId()); |
340
|
|
|
|
341
|
|
|
$page = (int)App::$Request->query->get('page'); |
342
|
|
|
$offset = $page * self::BLOCK_PER_PAGE; |
343
|
|
|
|
344
|
|
|
// build pagination |
345
|
|
|
$pagination = new SimplePagination([ |
346
|
|
|
'url' => ['profile/ignore'], |
347
|
|
|
'page' => $page, |
348
|
|
|
'step' => self::BLOCK_PER_PAGE, |
349
|
|
|
'total' => $records->count() |
350
|
|
|
]); |
351
|
|
|
|
352
|
|
|
return App::$View->render('ignore', [ |
353
|
|
|
'records' => $records->skip($offset)->take(self::BLOCK_PER_PAGE)->get(), |
354
|
|
|
'model' => $model->export(), |
355
|
|
|
'pagination' => $pagination |
356
|
|
|
]); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Show user logs |
361
|
|
|
* @throws ForbiddenException |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public function actionLog() |
365
|
|
|
{ |
366
|
|
|
// check if user is authorized |
367
|
|
|
if (!App::$User->isAuth()) { |
368
|
|
|
throw new ForbiddenException(); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// get user object |
372
|
|
|
$user = App::$User->identity(); |
373
|
|
|
|
374
|
|
|
// get log records |
375
|
|
|
$records = $user->getLogs(); |
376
|
|
|
if ($records !== null && $records->count() > 0) { |
377
|
|
|
$records = $records->orderBy('id', 'DESC'); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
return App::$View->render('log', [ |
381
|
|
|
'records' => $records |
382
|
|
|
]); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Unblock always blocked user |
387
|
|
|
* @param string $target_id |
388
|
|
|
* @throws ForbiddenException |
389
|
|
|
* @throws NotFoundException |
390
|
|
|
*/ |
391
|
|
|
public function actionUnblock($target_id) |
392
|
|
|
{ |
393
|
|
|
// check if user is auth |
394
|
|
|
if (!App::$User->isAuth()) { |
395
|
|
|
throw new ForbiddenException(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
// check if target is defined |
399
|
|
View Code Duplication |
if (!Obj::isLikeInt($target_id) || $target_id < 1 || !App::$User->isExist($target_id)) { |
|
|
|
|
400
|
|
|
throw new NotFoundException(); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
$user = App::$User->identity(); |
404
|
|
|
|
405
|
|
|
// check if target user in blacklist of current user |
406
|
|
|
if (!Blacklist::have($user->getId(), $target_id)) { |
407
|
|
|
throw new NotFoundException(); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
$model = new FormIgnoreDelete($user, $target_id); |
|
|
|
|
411
|
|
|
|
412
|
|
|
if ($model->send() && $model->validate()) { |
413
|
|
|
$model->make(); |
414
|
|
|
App::$Response->redirect(Url::to('profile/ignore')); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return App::$View->render('unblock', [ |
418
|
|
|
'model' => $model->export() |
419
|
|
|
]); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Search users |
424
|
|
|
*/ |
425
|
|
|
public function actionSearch() |
426
|
|
|
{ |
427
|
|
|
// create model object |
428
|
|
|
$model = new FormUserSearch(); |
429
|
|
|
$model->setSubmitMethod('GET'); |
430
|
|
|
|
431
|
|
|
// get app configs |
432
|
|
|
$cfgs = Serialize::decode($this->application->configs); |
433
|
|
|
|
434
|
|
|
$records = null; |
435
|
|
|
$pagination = null; |
436
|
|
|
// check if request is sended |
437
|
|
|
if ($model->send() && $model->validate()) { |
438
|
|
|
// get records from db |
439
|
|
|
$records = ProfileRecords::where('nick', 'like', '%' . $model->query . '%'); |
440
|
|
|
$page = (int)App::$Request->query->get('page'); |
441
|
|
|
$userPerPage = (int)$cfgs['usersOnPage']; |
442
|
|
|
if ($userPerPage < 1) { |
443
|
|
|
$userPerPage = 1; |
444
|
|
|
} |
445
|
|
|
$offset = $page * $userPerPage; |
446
|
|
|
// build pagination |
447
|
|
|
$pagination = new SimplePagination([ |
448
|
|
|
'url' => ['profile/search', null, null, [$model->getFormName().'[query]' => $model->query, $model->getFormName().'[submit]' => true]], |
449
|
|
|
'page' => $page, |
450
|
|
|
'step' => $userPerPage, |
451
|
|
|
'total' => $records->count() |
452
|
|
|
]); |
453
|
|
|
// make query finally |
454
|
|
|
$records = $records->skip($offset)->take($userPerPage)->get(); |
455
|
|
|
|
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
// display response |
459
|
|
|
return App::$View->render('search', [ |
460
|
|
|
'model' => $model->export(), |
461
|
|
|
'records' => $records, |
462
|
|
|
'pagination' => $pagination, |
463
|
|
|
'ratingOn' => (int)$cfgs['rating'] |
464
|
|
|
]); |
465
|
|
|
} |
466
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: