|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pan\User; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
use Pan\User\HTMLForm\UserLoginForm; |
|
8
|
|
|
use Pan\User\HTMLForm\CreateUserForm; |
|
9
|
|
|
use Pan\Post\Post; |
|
10
|
|
|
|
|
11
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
12
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
13
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserController implements ContainerInjectableInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use ContainerInjectableTrait; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var $data description |
|
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
|
|
private $currentUser; |
|
27
|
|
|
private $db; |
|
28
|
|
|
private $userId; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The initialize method is optional and will always be called before the |
|
32
|
|
|
* target method/action. This is a convienient method where you could |
|
33
|
|
|
* setup internal properties that are commonly used by several methods. |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function initialize() : void |
|
38
|
|
|
{ |
|
39
|
|
|
// Get the current user from session |
|
40
|
|
|
$session = $this->di->get("session"); |
|
41
|
|
|
// var_dump($session); |
|
42
|
|
|
$this->currentUser = $session->get("username"); |
|
43
|
|
|
|
|
44
|
|
|
// Connect the database |
|
45
|
|
|
$this->db = $this->di->get("db"); |
|
46
|
|
|
$this->db->connect(); |
|
47
|
|
|
if ($this->currentUser !=null) { |
|
48
|
|
|
$sql = "SELECT id from users where username = ?;"; |
|
49
|
|
|
$res = $this->db->executeFetchAll($sql, [$this->currentUser]); |
|
50
|
|
|
$this->userId = $res[0]->id; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Description. |
|
58
|
|
|
* |
|
59
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @throws Exception |
|
62
|
|
|
* |
|
63
|
|
|
* @return object as a response object |
|
64
|
|
|
*/ |
|
65
|
|
|
public function indexActionGet() : object |
|
66
|
|
|
{ |
|
67
|
|
|
$page = $this->di->get("page"); |
|
68
|
|
|
|
|
69
|
|
|
$title = "User Profile"; |
|
70
|
|
|
// var_dump($this->currentUser); |
|
71
|
|
|
if ($this->currentUser) { |
|
72
|
|
|
$user = new User(); |
|
73
|
|
|
$user->setDb($this->di->get("dbqb")); |
|
74
|
|
|
$res = $user->find("username", $this->currentUser); |
|
75
|
|
|
$avatar = get_gravatar($res->email); |
|
76
|
|
|
//Get Posts |
|
77
|
|
|
$user_id = $res->id; |
|
78
|
|
|
$sql = "SELECT * FROM posts WHERE user_id=?;"; |
|
79
|
|
|
$posts = $this->db->executeFetchAll($sql, [$user_id]); |
|
80
|
|
|
|
|
81
|
|
|
// var_dump($posts); |
|
82
|
|
|
//Get the answers for the user |
|
83
|
|
|
$sql = "SELECT * from comments WHERE user_id=? and answer=1;"; |
|
84
|
|
|
$answers = $this->db->executeFetchAll($sql, [$user_id]); |
|
85
|
|
|
$bonus = 0; |
|
86
|
|
|
foreach ($answers as $value) { |
|
87
|
|
|
// var_dump($value->accepted); |
|
88
|
|
|
if ($value->accepted==1) { |
|
89
|
|
|
$bonus += 1; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Get the comments for the user |
|
94
|
|
|
$sql = "SELECT * from comments WHERE user_id=? and answer=0;"; |
|
95
|
|
|
$comments = $this->db->executeFetchAll($sql, [$user_id]); |
|
96
|
|
|
// var_dump($comments); |
|
97
|
|
|
$reputation = count($posts)*3 + (count($answers)- $bonus)*3 + |
|
98
|
|
|
+ $bonus*100 + count($comments); |
|
99
|
|
|
$page->add("user/index", |
|
100
|
|
|
["current_user" => $this->currentUser, |
|
101
|
|
|
"avatar" => $avatar, |
|
102
|
|
|
"reputation" => $reputation, |
|
103
|
|
|
"items" => $posts, |
|
104
|
|
|
"answers" => $answers, |
|
105
|
|
|
"comments" => $comments, |
|
106
|
|
|
]); |
|
107
|
|
|
return $page->render(["title" => $title, ]); |
|
108
|
|
|
} |
|
109
|
|
|
$response = $this->di->get("response"); |
|
110
|
|
|
return $response->redirect("user/login"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function showActionGet(int $user_id) : object |
|
115
|
|
|
{ |
|
116
|
|
|
$page = $this->di->get("page"); |
|
117
|
|
|
|
|
118
|
|
|
$title = "User Profile"; |
|
119
|
|
|
|
|
120
|
|
|
$user = new User(); |
|
121
|
|
|
$user->setDb($this->di->get("dbqb")); |
|
122
|
|
|
$res = $user->find("id", $user_id); |
|
123
|
|
|
$avatar = get_gravatar($res->email); |
|
124
|
|
|
//Get Posts |
|
125
|
|
|
|
|
126
|
|
|
$sql = "SELECT * from posts WHERE user_id=?;"; |
|
127
|
|
|
$posts = $this->db->executeFetchAll($sql, [$user_id]); |
|
128
|
|
|
// var_dump($res->email); |
|
129
|
|
|
//Get the answers for the user |
|
130
|
|
|
$sql = "SELECT * from comments WHERE user_id=? and answer=1;"; |
|
131
|
|
|
$answers = $this->db->executeFetchAll($sql, [$user_id]); |
|
132
|
|
|
$bonus = 0; |
|
133
|
|
|
foreach ($answers as $value) { |
|
134
|
|
|
if ($value->accepted==1) { |
|
135
|
|
|
$bonus += 1; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Get the comments for the user |
|
140
|
|
|
$sql = "SELECT * from comments WHERE user_id=? and answer=0;"; |
|
141
|
|
|
$comments = $this->db->executeFetchAll($sql, [$user_id]); |
|
142
|
|
|
// var_dump($comments); |
|
143
|
|
|
$reputation = count($posts)*3 + (count($answers)- $bonus)*3 + |
|
144
|
|
|
+ $bonus*100 + count($comments); |
|
145
|
|
|
$page->add("user/profile", |
|
146
|
|
|
["user" => $res->username, |
|
147
|
|
|
"avatar" => $avatar, |
|
148
|
|
|
"reputation" => $reputation, |
|
149
|
|
|
"posts" => $posts, |
|
150
|
|
|
"answers" => $answers, |
|
151
|
|
|
"comments" => $comments, |
|
152
|
|
|
]); |
|
153
|
|
|
return $page->render(["title" => $title, ]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Description. |
|
158
|
|
|
* |
|
159
|
|
|
* @param datatype $variable Description |
|
160
|
|
|
* |
|
161
|
|
|
* @throws Exception |
|
162
|
|
|
* |
|
163
|
|
|
* @return object as a response object |
|
164
|
|
|
*/ |
|
165
|
|
|
public function loginAction() : object |
|
166
|
|
|
{ |
|
167
|
|
|
$page = $this->di->get("page"); |
|
168
|
|
|
$form = new UserLoginForm($this->di); |
|
169
|
|
|
$form->check(); |
|
170
|
|
|
|
|
171
|
|
|
$page->add("user/login", [ |
|
172
|
|
|
"content" => $form->getHTML(), |
|
173
|
|
|
]); |
|
174
|
|
|
|
|
175
|
|
|
return $page->render([ |
|
176
|
|
|
"title" => "A login page", |
|
177
|
|
|
]); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Description. |
|
182
|
|
|
* |
|
183
|
|
|
* @param datatype $variable Description |
|
184
|
|
|
* |
|
185
|
|
|
* @throws Exception |
|
186
|
|
|
* |
|
187
|
|
|
* @return object as a response object |
|
188
|
|
|
*/ |
|
189
|
|
|
public function logoutAction() : object |
|
190
|
|
|
{ |
|
191
|
|
|
$response = $this->di->get("response"); |
|
192
|
|
|
$session = $this->di->get("session"); |
|
193
|
|
|
$session->destroy(); |
|
194
|
|
|
return $response->redirect("user/login"); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Description. |
|
200
|
|
|
* |
|
201
|
|
|
* @param datatype $variable Description |
|
202
|
|
|
* |
|
203
|
|
|
* @throws Exception |
|
204
|
|
|
* |
|
205
|
|
|
* @return object as a response object |
|
206
|
|
|
*/ |
|
207
|
|
|
public function createAction() : object |
|
208
|
|
|
{ |
|
209
|
|
|
$page = $this->di->get("page"); |
|
210
|
|
|
$form = new CreateUserForm($this->di); |
|
211
|
|
|
$form->check(); |
|
212
|
|
|
|
|
213
|
|
|
$page->add("user/create", [ |
|
214
|
|
|
"form" => $form->getHTML(), |
|
215
|
|
|
]); |
|
216
|
|
|
|
|
217
|
|
|
return $page->render([ |
|
218
|
|
|
"title" => "Register User", |
|
219
|
|
|
]); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|