1 | <?php |
||
2 | |||
3 | namespace Apps\Controller\Front\Profile; |
||
4 | |||
5 | use Apps\ActiveRecord\WallPost; |
||
6 | use Apps\Model\Front\Profile\FormWallPostDelete; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Arch\View; |
||
9 | use Ffcms\Core\Exception\ForbiddenException; |
||
10 | use Ffcms\Core\Exception\NotFoundException; |
||
11 | use Ffcms\Core\Helper\Type\Any; |
||
12 | use Ffcms\Core\Network\Response; |
||
13 | |||
14 | /** |
||
15 | * Trait ActionWallDelete |
||
16 | * @package Apps\Controller\Front\Profile |
||
17 | * @property Response $response |
||
18 | * @property View $view |
||
19 | */ |
||
20 | trait ActionWallDelete |
||
21 | { |
||
22 | /** |
||
23 | * Allow post owners and targets delete |
||
24 | * @param string $postId |
||
25 | * @return string |
||
26 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
27 | * @throws ForbiddenException |
||
28 | * @throws NotFoundException |
||
29 | */ |
||
30 | public function wallDelete(string $postId): ?string |
||
31 | { |
||
32 | // user is auth? |
||
33 | if (!App::$User->isAuth()) { |
||
34 | throw new ForbiddenException(); |
||
35 | } |
||
36 | |||
37 | // is postId is integer? |
||
38 | if (!Any::isInt($postId) || $postId < 1) { |
||
39 | throw new NotFoundException(); |
||
40 | } |
||
41 | |||
42 | // try to find the wall post |
||
43 | /** @var WallPost $wallPost */ |
||
44 | $wallPost = WallPost::find($postId); |
||
45 | if (!$wallPost) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
46 | throw new NotFoundException(); |
||
47 | } |
||
48 | |||
49 | // get user and check if he can delete this post |
||
50 | $user = App::$User->identity(); |
||
51 | if ($wallPost->sender_id !== $user->id && $wallPost->target_id !== $user->id) { |
||
52 | throw new ForbiddenException(); |
||
53 | } |
||
54 | |||
55 | // check if submit sended |
||
56 | $wallModel = new FormWallPostDelete($wallPost); |
||
57 | if ($wallModel->send() && $wallModel->validate()) { |
||
58 | $wallModel->make(); |
||
59 | $this->response->redirect('profile/show/' . $wallPost->target_id); |
||
60 | } |
||
61 | |||
62 | return $this->view->render('profile/wall_delete', [ |
||
63 | 'post' => $wallPost, |
||
64 | 'model' => $wallModel |
||
65 | ]); |
||
66 | } |
||
67 | } |
||
68 |