|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front\Profile; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
|
6
|
|
|
use Apps\ActiveRecord\WallPost; |
|
7
|
|
|
use Apps\Model\Front\Profile\FormWallPost; |
|
8
|
|
|
use Ffcms\Core\App; |
|
9
|
|
|
use Ffcms\Core\Arch\View; |
|
10
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
|
11
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
|
12
|
|
|
use Ffcms\Core\Network\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait ActionShow |
|
16
|
|
|
* @package Apps\Controller\Front\Profile |
|
17
|
|
|
* @property View $view |
|
18
|
|
|
* @property Response $response |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ActionShow |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Show user profile: data, wall posts, other features |
|
25
|
|
|
* @param string $userId |
|
26
|
|
|
* @return string |
|
27
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
|
28
|
|
|
* @throws NotFoundException |
|
29
|
|
|
* @throws ForbiddenException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function show(string $userId): ?string |
|
32
|
|
|
{ |
|
33
|
|
|
$cfg = $this->application->configs; |
|
34
|
|
|
if (!(bool)$cfg['guestView'] && !App::$User->isAuth()) { |
|
35
|
|
|
throw new ForbiddenException(__('You must login to view other profile')); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// check if target exists |
|
39
|
|
|
if (!App::$User->isExist($userId)) { |
|
40
|
|
|
throw new NotFoundException(__('This profile is not exist')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User |
|
44
|
|
|
$viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User |
|
45
|
|
|
|
|
46
|
|
|
$wallModel = null; |
|
47
|
|
|
// if current user is auth - allow to post messages on wall current user |
|
48
|
|
|
if (App::$User->isAuth() && $viewerPersone->role->can('global/write')) { |
|
49
|
|
|
$wallModel = new FormWallPost(); |
|
50
|
|
|
// check if request post is done and rules validated |
|
51
|
|
|
if ($wallModel->send() && $wallModel->validate()) { |
|
52
|
|
|
// maybe in blacklist? |
|
53
|
|
|
if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) { |
|
54
|
|
|
App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!')); |
|
55
|
|
|
} else { |
|
56
|
|
|
// check if message added |
|
57
|
|
|
if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) { |
|
|
|
|
|
|
58
|
|
|
App::$Session->getFlashBag()->add('success', __('The message was successful posted!')); |
|
59
|
|
|
} else { |
|
60
|
|
|
App::$Session->getFlashBag()->add('warning', __('Posting message was failed! Please, wait few seconds')); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// get wall posts by target user_id |
|
67
|
|
|
$wallQuery = WallPost::where('target_id', $targetPersone->getId()); |
|
68
|
|
|
|
|
69
|
|
|
// pagination and query params |
|
70
|
|
|
$wallPage = (int)$this->request->query->get('page'); |
|
71
|
|
|
$wallStep = (int)$cfg['wallPostOnPage']; |
|
72
|
|
|
$wallOffset = $wallPage * $wallStep; |
|
73
|
|
|
$wallTotalCount = $wallQuery->count(); |
|
74
|
|
|
|
|
75
|
|
|
// get wall messages as object |
|
76
|
|
|
$wallRecords = $wallQuery->with(['senderUser', 'senderUser.profile', 'senderUser.role']) |
|
77
|
|
|
->orderBy('id', 'desc') |
|
78
|
|
|
->skip($wallOffset) |
|
79
|
|
|
->take($wallStep) |
|
80
|
|
|
->get(); |
|
81
|
|
|
|
|
82
|
|
|
// render output view |
|
83
|
|
|
return $this->view->render('profile/show', [ |
|
84
|
|
|
'user' => $targetPersone, |
|
85
|
|
|
'viewer' => $viewerPersone, |
|
86
|
|
|
'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id), |
|
87
|
|
|
'wall' => $wallModel, |
|
88
|
|
|
'wallRecords' => $wallRecords, |
|
89
|
|
|
'pagination' => [ |
|
90
|
|
|
'step' => $wallStep, |
|
91
|
|
|
'total' => $wallTotalCount, |
|
92
|
|
|
'page' => $wallPage |
|
93
|
|
|
], |
|
94
|
|
|
'ratingOn' => (int)$cfg['rating'] === 1 |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|