1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment; |
4
|
|
|
|
5
|
|
|
use \Nicklas\Comment\HTMLForm\Admin\EditUserForm; |
6
|
|
|
use \Nicklas\Comment\HTMLForm\Admin\CreateUserForm2; |
7
|
|
|
|
8
|
|
|
// MODULES |
9
|
|
|
use \Nicklas\Comment\Modules\User; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A controller class. |
13
|
|
|
*/ |
14
|
|
|
class AdminController extends UserController |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get all users |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
2 |
|
public function getUsers() |
24
|
|
|
{ |
25
|
2 |
|
$user = new User($this->di->get("db")); |
26
|
2 |
|
return $user->getAllUsers(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* check if user is logged in |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
1 |
|
public function checkIsAdmin() |
35
|
|
|
{ |
36
|
1 |
|
$this->checkIsLogin(); |
37
|
|
|
|
38
|
1 |
|
$user = new User($this->di->get("db")); |
39
|
1 |
|
$user = $user->getUser($this->di->get("session")->get("user")); |
40
|
|
|
|
41
|
1 |
View Code Duplication |
if ($user->authority != "admin") { |
|
|
|
|
42
|
|
|
$views = [ |
43
|
1 |
|
["comment/admin/fail", [], "main"] |
44
|
1 |
|
]; |
45
|
1 |
|
$this->di->get("pageRenderComment")->renderPage([ |
46
|
1 |
|
"views" => $views, |
47
|
|
|
"title" => "Not authorized" |
48
|
1 |
|
]); |
49
|
1 |
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Description. |
55
|
|
|
* |
56
|
|
|
* @param datatype $variable Description |
|
|
|
|
57
|
|
|
* |
58
|
|
|
* @throws Exception |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
1 |
|
public function getUsersIndex() |
63
|
|
|
{ |
64
|
|
|
$views = [ |
65
|
1 |
|
["comment/admin/navbar", [], "main"], |
66
|
1 |
|
["comment/admin/crud/view-all", ["users" => $this->getUsers()], "main"] |
67
|
1 |
|
]; |
68
|
|
|
|
69
|
1 |
|
$this->di->get("pageRenderComment")->renderPage([ |
70
|
1 |
|
"views" => $views, |
71
|
|
|
"title" => "A collection of users" |
72
|
1 |
|
]); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Description. |
78
|
|
|
* |
79
|
|
|
* @param datatype $variable Description |
|
|
|
|
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
1 |
View Code Duplication |
public function getPostAdminEditUser($id) |
|
|
|
|
86
|
|
|
{ |
87
|
1 |
|
$form = new EditUserForm($this->di, $id); |
88
|
1 |
|
$form->check(); |
89
|
|
|
|
90
|
|
|
$views = [ |
91
|
1 |
|
["comment/admin/navbar", [], "main"], |
92
|
1 |
|
["comment/admin/crud/edit", ["form" => $form->getHTML()], "main"] |
93
|
1 |
|
]; |
94
|
|
|
|
95
|
1 |
|
$this->di->get("pageRenderComment")->renderPage([ |
96
|
1 |
|
"views" => $views, |
97
|
|
|
"title" => "Edit user" |
98
|
1 |
|
]); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Description. |
103
|
|
|
* |
104
|
|
|
* @param datatype $variable Description |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @throws Exception |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function getPostAdminCreateUser() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$form = new CreateUserForm2($this->di); |
113
|
|
|
$form->check(); |
114
|
|
|
|
115
|
|
|
$views = [ |
116
|
|
|
["comment/admin/navbar", [], "main"], |
117
|
|
|
["comment/admin/crud/edit", ["form" => $form->getHTML()], "main"] |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$this->di->get("pageRenderComment")->renderPage([ |
121
|
|
|
"views" => $views, |
122
|
|
|
"title" => "Create user" |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.