1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Schanihbg\User; |
4
|
|
|
|
5
|
|
|
use \Anax\Configure\ConfigureInterface; |
6
|
|
|
use \Anax\Configure\ConfigureTrait; |
7
|
|
|
use \Anax\DI\InjectionAwareInterface; |
8
|
|
|
use \Anax\Di\InjectionAwareTrait; |
9
|
|
|
use \Schanihbg\User\HTMLForm\UserLoginForm; |
10
|
|
|
use \Schanihbg\User\HTMLForm\CreateUserForm; |
11
|
|
|
use \Schanihbg\User\HTMLForm\UpdateUserForm; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A controller class. |
15
|
|
|
*/ |
16
|
|
|
class UserController implements |
17
|
|
|
ConfigureInterface, |
18
|
|
|
InjectionAwareInterface |
19
|
|
|
{ |
20
|
|
|
use ConfigureTrait, |
21
|
|
|
InjectionAwareTrait; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var $data description |
27
|
|
|
*/ |
28
|
|
|
//private $data; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Description. |
34
|
|
|
* |
35
|
|
|
* @throws Exception |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function getIndex() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$title = "User profile"; |
42
|
|
|
$view = $this->di->get("view"); |
43
|
|
|
$pageRender = $this->di->get("pageRender"); |
44
|
|
|
|
45
|
|
|
$view->add("user/profile"); |
46
|
|
|
|
47
|
|
|
$pageRender->renderPage(["title" => $title]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Description. |
54
|
|
|
* |
55
|
|
|
* @throws Exception |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function getPostLogin() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$title = "A login page"; |
62
|
|
|
$view = $this->di->get("view"); |
63
|
|
|
$pageRender = $this->di->get("pageRender"); |
64
|
|
|
$form = new UserLoginForm($this->di); |
65
|
|
|
|
66
|
|
|
$form->check(); |
67
|
|
|
|
68
|
|
|
$data = [ |
69
|
|
|
"content" => $form->getHTML(), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$view->add("default2/article", $data); |
73
|
|
|
|
74
|
|
|
$pageRender->renderPage(["title" => $title]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Description. |
81
|
|
|
* |
82
|
|
|
* @throws Exception |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function getPostCreateUser() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$title = "A create user page"; |
89
|
|
|
$view = $this->di->get("view"); |
90
|
|
|
$pageRender = $this->di->get("pageRender"); |
91
|
|
|
$form = new CreateUserForm($this->di); |
92
|
|
|
|
93
|
|
|
$form->check(); |
94
|
|
|
|
95
|
|
|
$data = [ |
96
|
|
|
"content" => $form->getHTML(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$view->add("default2/article", $data); |
100
|
|
|
|
101
|
|
|
$pageRender->renderPage(["title" => $title]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Handler with form to update a user. |
106
|
|
|
* |
107
|
|
|
* @param datatype $id User ID |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function getPostUpdateUser($id) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$title = "Update an item"; |
114
|
|
|
$view = $this->di->get("view"); |
115
|
|
|
$pageRender = $this->di->get("pageRender"); |
116
|
|
|
$form = new UpdateUserForm($this->di, $id); |
117
|
|
|
|
118
|
|
|
$form->check(); |
119
|
|
|
|
120
|
|
|
$data = [ |
121
|
|
|
"form" => $form->getHTML(), |
122
|
|
|
"id" => $id, |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$view->add("user/update", $data); |
126
|
|
|
|
127
|
|
|
$pageRender->renderPage(["title" => $title]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Description. |
132
|
|
|
* |
133
|
|
|
* @param datatype $input What user to login. |
134
|
|
|
* |
135
|
|
|
* @throws Exception |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function loginUser($input) |
140
|
|
|
{ |
141
|
|
|
$this->di->get("session")->set("userLoggedIn", $input); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Description. |
146
|
|
|
* |
147
|
|
|
* @throws Exception |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function logoutUser() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$title = "Logout page"; |
154
|
|
|
$view = $this->di->get("view"); |
155
|
|
|
$pageRender = $this->di->get("pageRender"); |
156
|
|
|
|
157
|
|
|
$view->add("user/logout"); |
158
|
|
|
|
159
|
|
|
$pageRender->renderPage(["title" => $title]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Description. |
164
|
|
|
* |
165
|
|
|
* @param datatype $userInput What user to check ID. |
166
|
|
|
* |
167
|
|
|
* @throws Exception |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public function getIDByUser($userInput) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$user = new User(); |
174
|
|
|
$user->setDb($this->di->get("database")); |
175
|
|
|
$user->find("acronym", $userInput); |
176
|
|
|
|
177
|
|
|
return $user; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Description. |
182
|
|
|
* |
183
|
|
|
* @throws Exception |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function adminControl() |
188
|
|
|
{ |
189
|
|
|
$title = "Admin Control"; |
190
|
|
|
$view = $this->di->get("view"); |
191
|
|
|
$pageRender = $this->di->get("pageRender"); |
192
|
|
|
|
193
|
|
|
$sql = "SELECT * FROM `User`"; |
194
|
|
|
|
195
|
|
|
$data = $this->di->get("database")->executeFetchAll($sql); |
196
|
|
|
|
197
|
|
|
$view->add("user/admin", ["content" => $data]); |
198
|
|
|
|
199
|
|
|
$pageRender->renderPage(["title" => $title]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Delete user |
204
|
|
|
* |
205
|
|
|
* @param datatype $id User ID. |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public function removeUser($id) |
210
|
|
|
{ |
211
|
|
|
$sql = "DELETE FROM `User` WHERE id = ?"; |
212
|
|
|
$this->di->get("database")->execute($sql, [$id]); |
213
|
|
|
$this->di->get("response")->redirect($this->di->get("url")->create("user/admin")); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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.