1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Front\Profile; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
6
|
|
|
use Apps\Model\Front\Profile\FormIgnoreAdd; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Arch\View; |
9
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
10
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
11
|
|
|
use Ffcms\Core\Network\Response; |
12
|
|
|
use Ffcms\Core\Network\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait ActionIgnore |
16
|
|
|
* @package Apps\Controller\Front\Profile |
17
|
|
|
* @property View $view |
18
|
|
|
* @property Request $request |
19
|
|
|
* @property Response $response |
20
|
|
|
*/ |
21
|
|
|
trait ActionIgnore |
22
|
|
|
{ |
23
|
|
|
private $_blockPerPage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Show users in blacklist and allow add new users |
27
|
|
|
* @return string |
28
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
29
|
|
|
* @throws ForbiddenException |
30
|
|
|
*/ |
31
|
|
|
public function ignore() |
32
|
|
|
{ |
33
|
|
|
// check if not auth |
34
|
|
|
if (!App::$User->isAuth()) { |
35
|
|
|
throw new ForbiddenException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// get user object and init model of it |
39
|
|
|
$user = App::$User->identity(); |
40
|
|
|
$model = new FormIgnoreAdd($user); |
41
|
|
|
|
42
|
|
|
// set user id from ?id= get param if form not sended |
43
|
|
|
if (!$model->send()) { |
44
|
|
|
$uid = (int)$this->request->query->get('id'); |
45
|
|
|
if ($uid > 0) { |
46
|
|
|
$model->id = $uid; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// sended new block add? |
51
|
|
View Code Duplication |
if ($model->send() && $model->validate()) { |
|
|
|
|
52
|
|
|
if ($model->save()) { |
53
|
|
|
App::$Session->getFlashBag()->add('success', __('User was successful blocked!')); |
54
|
|
|
} else { |
55
|
|
|
App::$Session->getFlashBag()->add('error', __('This user is always in ignore list')); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// get blocked users |
60
|
|
|
$query = Blacklist::where('user_id', '=', $user->getId()); |
61
|
|
|
|
62
|
|
|
$page = (int)$this->request->query->get('page'); |
63
|
|
|
$offset = $page * $this->_blockPerPage; |
64
|
|
|
|
65
|
|
|
// build pagination |
66
|
|
|
$pagination = new SimplePagination([ |
67
|
|
|
'url' => ['profile/ignore'], |
68
|
|
|
'page' => $page, |
69
|
|
|
'step' => $this->_blockPerPage, |
70
|
|
|
'total' => $query->count() |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
// get records as object |
74
|
|
|
$records = $query->with(['targetUser', 'targetUser.profile']) |
75
|
|
|
->skip($offset) |
76
|
|
|
->take($this->_blockPerPage) |
77
|
|
|
->get(); |
78
|
|
|
|
79
|
|
|
// render output view |
80
|
|
|
return $this->view->render('ignore', [ |
81
|
|
|
'records' => $records, |
82
|
|
|
'model' => $model, |
83
|
|
|
'pagination' => $pagination |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.