1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Users; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A controller for users and admin related events. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class UsersController implements \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use \Anax\DI\TInjectable, |
12
|
|
|
\Anax\MVC\TRedirectHelpers; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Initialize the controller. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function initialize() |
20
|
|
|
{ |
21
|
|
|
$this->users = new \Anax\Users\User(); |
|
|
|
|
22
|
|
|
$this->users->setDI($this->di); |
|
|
|
|
23
|
|
|
$this->questions = new \Anax\Questions\CQuestions(); |
|
|
|
|
24
|
|
|
$this->questions->setDI($this->di); |
|
|
|
|
25
|
|
|
$this->answers = new \Anax\Answers\CAnswers(); |
|
|
|
|
26
|
|
|
$this->answers->setDI($this->di); |
|
|
|
|
27
|
|
|
$this->comments = new \Anax\CommentDb\CommentsInDb(); |
|
|
|
|
28
|
|
|
$this->comments->setDI($this->di); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setup initial table for users. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function setupAction() |
37
|
|
|
{ |
38
|
|
|
$this->users->init(); |
39
|
|
|
} |
40
|
|
|
/** |
41
|
|
|
* Display user with id. |
42
|
|
|
* |
43
|
|
|
* @param int $id of user to display |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function profidAction($id = null) |
48
|
|
|
{ |
49
|
|
|
$user = $this->users->find($id); |
50
|
|
|
|
51
|
|
|
// Show users gravatar in big size somewhere here. |
52
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
53
|
|
|
'controller' => 'users', |
54
|
|
|
'action' => 'profile', |
55
|
|
|
'params' => [$user->acronym ], |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Display most active users. |
61
|
|
|
* Sum number of questions and answers contributed. |
62
|
|
|
* |
63
|
|
|
* @param int $id of user to display |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function mostactiveAction($count = 3) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$questions = $this->questions->countByUser(); |
70
|
|
|
$answers = $this->answers->countByUser(); |
71
|
|
|
$comments = $this->comments->countByUser(); |
72
|
|
|
$useractivity = array(); |
|
|
|
|
73
|
|
|
foreach ($questions as $qActivity) { |
74
|
|
|
$userActivity[$qActivity->user_id] = [ |
|
|
|
|
75
|
|
|
'activity' => $qActivity->Cnt, |
76
|
|
|
'user_id' => $qActivity->user_id, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
foreach ($answers as $Activity) { |
80
|
|
|
$userActivity[$Activity->user_id]['activity'] += $Activity->Cnt; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
foreach ($comments as $Activity) { |
83
|
|
|
$userActivity[$Activity->user_id]['activity'] += $Activity->Cnt; |
84
|
|
|
} |
85
|
|
|
arsort($userActivity); |
86
|
|
|
$mostActiveUsers = array_slice($userActivity, 0, 3, true); |
87
|
|
|
$all = array(); |
88
|
|
|
foreach ($mostActiveUsers as $user) { |
89
|
|
|
$all[] = $this->users->find($user['user_id'])->getProperties(); |
90
|
|
|
} |
91
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
92
|
|
|
'title' => 'Mest aktiva användare', |
93
|
|
|
'content' => '', |
94
|
|
|
]); |
95
|
|
|
$this->views->add('users/view_short', [ |
|
|
|
|
96
|
|
|
'users' => $all, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Display user with acronym. |
102
|
|
|
* |
103
|
|
|
* @param int $id of user to display |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function profileAction($acronym = null) |
108
|
|
|
{ |
109
|
|
|
$user = $this->users->query() |
110
|
|
|
->where('acronym = ' . "'$acronym'") |
111
|
|
|
->execute()[0]; |
112
|
|
|
// Get user questions |
113
|
|
|
// TODO: move queries to model? |
114
|
|
|
// TODO: use sql count function instead. |
115
|
|
|
$questions = $this->questions->query() |
116
|
|
|
->where('user_id = ' . "'$user->id'") |
117
|
|
|
->execute(); |
118
|
|
|
$nrOfQuestions = sizeof($questions); |
119
|
|
|
// Build route to user questions and send to view. |
120
|
|
|
$urlQuestions = $this->url->create('questions/list/questions/'.$acronym); |
|
|
|
|
121
|
|
|
|
122
|
|
|
// Get user answers |
123
|
|
|
$answers = $this->answers->query() |
124
|
|
|
->where('user_id = ' . "'$user->id'") |
125
|
|
|
->execute(); |
126
|
|
|
$nrOfAnswers = sizeof($answers); |
127
|
|
|
// Build route to user answers and send to view. |
128
|
|
|
$urlAnswers = $this->url->create('questions/list/answers/'.$acronym); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$gravatarSize = 120; |
131
|
|
|
$user->gravatar = \Anax\Users\User::getGravatar($user->email, $gravatarSize); |
132
|
|
|
$this->theme->setTitle("Byggare $acronym"); |
|
|
|
|
133
|
|
|
$this->views->add('users/view', [ |
|
|
|
|
134
|
|
|
'user' => $user, |
135
|
|
|
'urlQuestions' => $urlQuestions, |
136
|
|
|
'nrOfQuestions' => $nrOfQuestions, |
137
|
|
|
'urlAnswers' => $urlAnswers, |
138
|
|
|
'nrOfAnswers' => $nrOfAnswers, |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
// If user is logged in and profile is logged in users show additional links. |
142
|
|
|
if ($this->users->loggedIn()) { |
143
|
|
|
$loggedInUser = $this->users->loggedInUser(); |
144
|
|
|
if ($loggedInUser->id == $user->id) { |
145
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
146
|
|
|
'content' => "Hej {$user->name}. Vad vill du göra? ", |
147
|
|
|
'links' => [ |
148
|
|
|
[ |
149
|
|
|
'href' => $this->url->create('questions/ask'), |
|
|
|
|
150
|
|
|
'text' => "Fråga en fråga", |
151
|
|
|
], |
152
|
|
|
[ |
153
|
|
|
'href' => $this->url->create('users/logout'), |
|
|
|
|
154
|
|
|
'text' => "Logga ut mig", |
155
|
|
|
], |
156
|
|
|
[ |
157
|
|
|
'href' => $this->url->create("users/update/{$user->id}"), |
|
|
|
|
158
|
|
|
'text' => "Redigera min profil", |
159
|
|
|
], |
160
|
|
|
[ |
161
|
|
|
'href' => $this->url->create('users/add'), |
|
|
|
|
162
|
|
|
'text' => "Lägg till ny användare", |
163
|
|
|
], |
164
|
|
|
], |
165
|
|
|
]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* List all users. |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function listAction() |
176
|
|
|
{ |
177
|
|
|
$all = $this->users->findAll(); |
178
|
|
|
$gravatarSize = 80; |
179
|
|
|
// TODO: move below to User model? |
180
|
|
|
foreach ($all as $user) { |
181
|
|
|
$user->gravatar = \Anax\Users\User::getGravatar($user->email, $gravatarSize); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Display logged in user. |
185
|
|
|
if ($this->users->loggedIn()) { |
186
|
|
|
$user = $this->users->loggedInUser(); |
187
|
|
|
// Show users gravatar in big size somewhere here. |
188
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
189
|
|
|
'controller' => 'users', |
190
|
|
|
'action' => 'profile', |
191
|
|
|
'params' => [$user->acronym ], |
192
|
|
|
// 'params' => [$user['acronym'] ], |
193
|
|
|
]); |
194
|
|
|
} else { |
195
|
|
|
// Dispatch to login Form |
196
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
197
|
|
|
'controller' => 'users', |
198
|
|
|
'action' => 'login', |
199
|
|
|
'params' => [ ], |
200
|
|
|
]); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->theme->setTitle("Byggare"); |
|
|
|
|
204
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
205
|
|
|
'users' => $all, |
206
|
|
|
'title' => "Alla byggare", |
207
|
|
|
]); |
208
|
|
|
} |
209
|
|
|
/** |
210
|
|
|
* List all users for admin purpose. |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function listadminAction() |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$all = $this->users->findAll(); |
217
|
|
|
|
218
|
|
|
$this->theme->setTitle("List all users"); |
|
|
|
|
219
|
|
|
$this->views->add('users/list-admin', [ |
|
|
|
|
220
|
|
|
'users' => $all, |
221
|
|
|
'title' => "Administrate all users", |
222
|
|
|
]); |
223
|
|
|
} |
224
|
|
|
/** |
225
|
|
|
* List user with acronym. |
226
|
|
|
* |
227
|
|
|
* @param int $id of user to display |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function acronymAction($acronym = null) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$user = $this->users->query() |
234
|
|
|
->where('acronym = ' . "'$acronym'") |
235
|
|
|
->execute()[0]; |
236
|
|
|
$this->theme->setTitle("View user with acronym"); |
|
|
|
|
237
|
|
|
$this->views->add('users/view', [ |
|
|
|
|
238
|
|
|
'user' => $user, |
239
|
|
|
]); |
240
|
|
|
} |
241
|
|
|
/** |
242
|
|
|
* List user with id for admin purpose. |
243
|
|
|
* |
244
|
|
|
* @param int $id of user to display |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function idAction($id = null) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$user = $this->users->find($id); |
251
|
|
|
|
252
|
|
|
$this->theme->setTitle("View user with id"); |
|
|
|
|
253
|
|
|
$this->views->add('users/view', [ |
|
|
|
|
254
|
|
|
'user' => $user, |
255
|
|
|
]); |
256
|
|
|
} |
257
|
|
|
/** |
258
|
|
|
* Display user as card. |
259
|
|
|
* |
260
|
|
|
* @param int $id of user to display |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public function cardAction($id = null, $q_or_a = '', $text = '') |
265
|
|
|
{ |
266
|
|
|
$gravatarSize = 40; |
267
|
|
|
|
268
|
|
|
$user = $this->users->find($id)->getProperties(); |
269
|
|
|
$gravatar = \Anax\Users\User::getGravatar($user['email'], $gravatarSize); |
270
|
|
|
$profileUrl = $this->url->create("users/profile/{$user['acronym']}"); |
|
|
|
|
271
|
|
|
|
272
|
|
|
$this->views->add('users/card', [ |
|
|
|
|
273
|
|
|
'user' => $user, |
274
|
|
|
'q_or_a' => $q_or_a, |
275
|
|
|
'gravatar' => $gravatar, |
276
|
|
|
'text' => $text, |
277
|
|
|
'profileUrl' => $profileUrl, |
278
|
|
|
]); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Add new user. |
283
|
|
|
* |
284
|
|
|
* @param string $acronym of user to add. |
285
|
|
|
* |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function addAction($acronym = null) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
if ($this->users->loggedIn()) { |
291
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
292
|
|
|
$form = $this->createAddUserForm(); |
293
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
294
|
|
|
$this->di->theme->setTitle("Add user"); |
295
|
|
|
$this->di->views->add('default/page', [ |
296
|
|
|
'title' => "Add user", |
297
|
|
|
'content' => $form->getHTML() |
298
|
|
|
]); |
299
|
|
|
} else { |
300
|
|
|
$this->redirectTo($this->url->create('users/login')); |
|
|
|
|
301
|
|
|
} |
302
|
|
|
} |
303
|
|
View Code Duplication |
private function createAddUserForm() |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
return $this->di->form->create([], [ |
306
|
|
|
'name' => [ |
307
|
|
|
'type' => 'text', |
308
|
|
|
'label' => 'Name of person:', |
309
|
|
|
'required' => true, |
310
|
|
|
'validation' => ['not_empty'], |
311
|
|
|
], |
312
|
|
|
'acronym' => [ |
313
|
|
|
'type' => 'text', |
314
|
|
|
'label' => 'Acronym of person:', |
315
|
|
|
'required' => true, |
316
|
|
|
'validation' => ['not_empty'], |
317
|
|
|
], |
318
|
|
|
'email' => [ |
319
|
|
|
'type' => 'text', |
320
|
|
|
'required' => true, |
321
|
|
|
'validation' => ['not_empty', 'email_adress'], |
322
|
|
|
], |
323
|
|
|
'submit' => [ |
324
|
|
|
'type' => 'submit', |
325
|
|
|
'callback' => [$this, 'callbackSubmitAddUser'], |
326
|
|
|
], |
327
|
|
|
// 'submit-fail' => [ |
328
|
|
|
// 'type' => 'submit', |
329
|
|
|
// 'callback' => [$this, 'callbackSubmitFailAddUser'], |
330
|
|
|
// ], |
331
|
|
|
]); |
332
|
|
|
} |
333
|
|
|
/** |
334
|
|
|
* Callback for submit-button. |
335
|
|
|
* |
336
|
|
|
*/ |
337
|
|
|
public function callbackSubmitAddUser($form) |
338
|
|
|
{ |
339
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
340
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
341
|
|
|
$acronym = $form->Value('acronym'); |
342
|
|
|
// Check for duplicate acronym. Die if exists. |
343
|
|
|
$all = $this->users->query() |
344
|
|
|
->where("acronym = '$acronym'") |
345
|
|
|
->execute(); |
346
|
|
|
if (count($all)!=0) { |
347
|
|
|
die("User with acronym $acronym already registered."); |
348
|
|
|
} |
349
|
|
|
// Save user data to database |
350
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
351
|
|
|
unset($this->users->session); |
352
|
|
|
$this->users->save([ |
353
|
|
|
'acronym' => $form->Value('acronym'), |
354
|
|
|
'email' => $form->Value('email'), |
355
|
|
|
'name' => $form->Value('name'), |
356
|
|
|
'password' => md5($acronym), |
357
|
|
|
'created' => $now, |
358
|
|
|
'active' => $now, |
359
|
|
|
]); |
360
|
|
|
|
361
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
362
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('email') . "</b></p>"); |
363
|
|
|
// $form->AddOutput("<p><b>Acronym: " . $form->Value('acronym') . "</b></p>"); |
364
|
|
|
$form->saveInSession = false; |
365
|
|
|
return true; |
366
|
|
|
} |
367
|
|
|
/** |
368
|
|
|
* Callback for submit-button. |
369
|
|
|
* |
370
|
|
|
*/ |
371
|
|
|
public function callbackSubmitFailAddUser($form) |
372
|
|
|
{ |
373
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
/** |
377
|
|
|
* Callback What to do if the form was submitted? |
378
|
|
|
* |
379
|
|
|
*/ |
380
|
|
|
public function callbackSuccess($form) |
381
|
|
|
{ |
382
|
|
|
$form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
383
|
|
|
$this->redirectTo('users/list/'); |
384
|
|
|
} |
385
|
|
|
/** |
386
|
|
|
* Callback What to do when form could not be processed? |
387
|
|
|
* |
388
|
|
|
*/ |
389
|
|
|
public function callbackFail($form) |
390
|
|
|
{ |
391
|
|
|
$form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
392
|
|
|
$this->redirectTo(); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Update user. |
398
|
|
|
* |
399
|
|
|
* @param string $acronym of user to update. |
|
|
|
|
400
|
|
|
* |
401
|
|
|
* @return void |
402
|
|
|
*/ |
403
|
|
|
public function updateAction($id = null) |
404
|
|
|
{ |
405
|
|
|
if ($this->users->loggedIn()) { |
406
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
407
|
|
|
// Check if valid entry exists. |
408
|
|
|
$all = $this->users->query() |
409
|
|
|
->where("id = '$id'") |
410
|
|
|
->execute(); |
411
|
|
|
if (count($all)!=1) { |
412
|
|
|
die("User with id $id not found."); |
413
|
|
|
} |
414
|
|
|
$user = $this->users->find($id); |
415
|
|
|
unset($user->session); |
416
|
|
|
unset($user->gravatar); |
417
|
|
|
$form = $this->createUpdateUserForm($user); |
418
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
419
|
|
|
$this->di->theme->setTitle("Updatera profil"); |
420
|
|
|
$this->di->views->add('default/page', [ |
421
|
|
|
'title' => "Updatera profil", |
422
|
|
|
'content' => $form->getHTML() |
423
|
|
|
]); |
424
|
|
|
} else { |
425
|
|
|
$this->redirectTo($this->url->create('users/login')); |
|
|
|
|
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
private function createUpdateUserForm($user = null) |
429
|
|
|
{ |
430
|
|
|
return $this->di->form->create([], [ |
431
|
|
|
'name' => [ |
432
|
|
|
'type' => 'text', |
433
|
|
|
'value' => $user->name, |
434
|
|
|
'label' => 'Name of person:', |
435
|
|
|
'required' => true, |
436
|
|
|
'validation' => ['not_empty'], |
437
|
|
|
], |
438
|
|
|
'acronym' => [ |
439
|
|
|
'type' => 'text', |
440
|
|
|
'value' => $user->acronym, |
441
|
|
|
'label' => 'Acronym of person:', |
442
|
|
|
'required' => true, |
443
|
|
|
'validation' => ['not_empty'], |
444
|
|
|
], |
445
|
|
|
'email' => [ |
446
|
|
|
'type' => 'text', |
447
|
|
|
'value' => $user->email, |
448
|
|
|
'required' => true, |
449
|
|
|
'validation' => ['not_empty', 'email_adress'], |
450
|
|
|
], |
451
|
|
|
'id' => [ |
452
|
|
|
'type' => 'hidden', |
453
|
|
|
'value' => $user->id, |
454
|
|
|
], |
455
|
|
|
'submit' => [ |
456
|
|
|
'type' => 'submit', |
457
|
|
|
'label' => 'Uppdatera', |
458
|
|
|
'callback' => [$this, 'callbackSubmitUpdateUser'], |
459
|
|
|
], |
460
|
|
|
'submit-fail' => [ |
461
|
|
|
'type' => 'submit', |
462
|
|
|
'callback' => [$this, 'callbackSubmitFailUpdateUser'], |
463
|
|
|
], |
464
|
|
|
]); |
465
|
|
|
} |
466
|
|
|
public function callbackSubmitUpdateUser($form) |
467
|
|
|
{ |
468
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
469
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
470
|
|
|
// Handle update to duplicate acronym. Die if other user already has acronym. |
471
|
|
|
$id = $form->Value('id'); |
472
|
|
|
$acronym = $form->Value('acronym'); |
473
|
|
|
$all = $this->users->query() |
474
|
|
|
->where("id != '$id'") |
475
|
|
|
->andwhere("acronym = '$acronym'") |
476
|
|
|
->execute(); |
477
|
|
|
if (count($all)==1) { |
478
|
|
|
die("User with acronym $acronym alredy defined for other user. "); |
479
|
|
|
} |
480
|
|
|
// die(); |
481
|
|
|
// Save user data to database |
482
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
|
|
|
|
483
|
|
|
$this->users->save([ |
484
|
|
|
'acronym' => $form->Value('acronym'), |
485
|
|
|
'email' => $form->Value('email'), |
486
|
|
|
'name' => $form->Value('name'), |
487
|
|
|
// 'password' => md5($acronym, PASSWORD_DEFAULT), |
488
|
|
|
// 'created' => $now, |
489
|
|
|
// 'active' => $now, |
490
|
|
|
]); |
491
|
|
|
|
492
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
493
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('email') . "</b></p>"); |
494
|
|
|
// $form->AddOutput("<p><b>Phone: " . $form->Value('acronym') . "</b></p>"); |
495
|
|
|
$form->saveInSession = false; |
496
|
|
|
return true; |
497
|
|
|
} |
498
|
|
|
/** |
499
|
|
|
* Callback for submit-button. |
500
|
|
|
* |
501
|
|
|
*/ |
502
|
|
|
public function callbackSubmitFailUpdateUser($form) |
503
|
|
|
{ |
504
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
505
|
|
|
return false; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
|
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Delete user. |
512
|
|
|
* |
513
|
|
|
* @param integer $id of user to delete. |
514
|
|
|
* |
515
|
|
|
* @return void |
516
|
|
|
*/ |
517
|
|
|
public function deleteAction($id = null) |
518
|
|
|
{ |
519
|
|
|
if (!isset($id)) { |
520
|
|
|
die("Missing id"); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$res = $this->users->delete($id); |
|
|
|
|
524
|
|
|
|
525
|
|
|
$this->redirectTo('users/list/'); |
526
|
|
|
} |
527
|
|
|
/** |
528
|
|
|
* Delete (soft) user. |
529
|
|
|
* |
530
|
|
|
* @param integer $id of user to delete. |
531
|
|
|
* |
532
|
|
|
* @return void |
533
|
|
|
*/ |
534
|
|
View Code Duplication |
public function softDeleteAction($id = null) |
|
|
|
|
535
|
|
|
{ |
536
|
|
|
if (!isset($id)) { |
537
|
|
|
die("Missing id"); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
541
|
|
|
|
542
|
|
|
$user = $this->users->find($id); |
543
|
|
|
|
544
|
|
|
$user->deleted = $now; |
545
|
|
|
$user->save(); |
546
|
|
|
|
547
|
|
|
$this->redirectTo('users/list/'); |
548
|
|
|
} |
549
|
|
View Code Duplication |
public function undoDeleteAction($id = null) |
|
|
|
|
550
|
|
|
{ |
551
|
|
|
if (!isset($id)) { |
552
|
|
|
die("Missing id"); |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
$user = $this->users->find($id); |
556
|
|
|
|
557
|
|
|
$user->deleted = null; |
558
|
|
|
$user->save(); |
559
|
|
|
|
560
|
|
|
$this->redirectTo('users/id/' . $id); |
561
|
|
|
} |
562
|
|
View Code Duplication |
public function activateAction($id = null) |
|
|
|
|
563
|
|
|
{ |
564
|
|
|
if (!isset($id)) { |
565
|
|
|
die("Missing id"); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
$user = $this->users->find($id); |
569
|
|
|
|
570
|
|
|
$user->active = gmdate('Y-m-d H:i:s'); |
571
|
|
|
$user->save(); |
572
|
|
|
|
573
|
|
|
$this->redirectTo('users/id/' . $id); |
574
|
|
|
} |
575
|
|
View Code Duplication |
public function deactivateAction($id = null) |
|
|
|
|
576
|
|
|
{ |
577
|
|
|
if (!isset($id)) { |
578
|
|
|
die("Missing id"); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
$user = $this->users->find($id); |
582
|
|
|
|
583
|
|
|
$user->active = null; |
584
|
|
|
$user->save(); |
585
|
|
|
|
586
|
|
|
$this->redirectTo('users/id/' . $id); |
587
|
|
|
} |
588
|
|
|
/** |
589
|
|
|
* List all active and not deleted users. |
590
|
|
|
* |
591
|
|
|
* @return void |
592
|
|
|
*/ |
593
|
|
View Code Duplication |
public function activeAction() |
|
|
|
|
594
|
|
|
{ |
595
|
|
|
$all = $this->users->query() |
596
|
|
|
->where('active IS NOT NULL') |
597
|
|
|
->andWhere('deleted is NULL') |
598
|
|
|
->execute(); |
599
|
|
|
|
600
|
|
|
$this->theme->setTitle("Users that are active"); |
|
|
|
|
601
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
602
|
|
|
'users' => $all, |
603
|
|
|
'title' => "Users that are active", |
604
|
|
|
]); |
605
|
|
|
} |
606
|
|
|
/** |
607
|
|
|
* List all inactive and not deleted users. |
608
|
|
|
* |
609
|
|
|
* @return void |
610
|
|
|
*/ |
611
|
|
View Code Duplication |
public function inactiveAction() |
|
|
|
|
612
|
|
|
{ |
613
|
|
|
$all = $this->users->query() |
614
|
|
|
->where('active IS NULL') |
615
|
|
|
->andWhere('deleted is NULL') |
616
|
|
|
->execute(); |
617
|
|
|
|
618
|
|
|
$this->theme->setTitle("Users that are inactive"); |
|
|
|
|
619
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
620
|
|
|
'users' => $all, |
621
|
|
|
'title' => "Users that are inactive", |
622
|
|
|
]); |
623
|
|
|
} |
624
|
|
|
/** |
625
|
|
|
* List all active and not deleted users. |
626
|
|
|
* |
627
|
|
|
* @return void |
628
|
|
|
*/ |
629
|
|
View Code Duplication |
public function wastebasketAction() |
|
|
|
|
630
|
|
|
{ |
631
|
|
|
$all = $this->users->query() |
632
|
|
|
->where('deleted is NOT NULL') |
633
|
|
|
->execute(); |
634
|
|
|
|
635
|
|
|
$this->theme->setTitle("Users that are in wastebasket"); |
|
|
|
|
636
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
637
|
|
|
'users' => $all, |
638
|
|
|
'title' => "Users that are in wastebasket", |
639
|
|
|
]); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Logout user. |
644
|
|
|
* |
645
|
|
|
* @return void |
646
|
|
|
*/ |
647
|
|
|
public function logoutAction() |
648
|
|
|
{ |
649
|
|
|
$this->session->set('user_logged_in', null); |
|
|
|
|
650
|
|
|
$this->redirectTo($_SERVER['HTTP_REFERER']); |
651
|
|
|
} |
652
|
|
|
/** |
653
|
|
|
* Login user. |
654
|
|
|
* |
655
|
|
|
* @return void |
656
|
|
|
*/ |
657
|
|
|
public function loginAction() |
658
|
|
|
{ |
659
|
|
|
// TODO: Need to sweep session? How? |
660
|
|
|
// Set saveInSession = false instead. |
661
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
662
|
|
|
$form = $this->createLoginForm(); |
663
|
|
|
$form->check([$this, 'callbackLoginSuccess'], [$this, 'callbackLoginSuccess']); |
664
|
|
|
$this->di->theme->setTitle("Logga in"); |
665
|
|
|
$this->di->views->add('default/page', [ |
666
|
|
|
'title' => "Logga in", |
667
|
|
|
'content' => $form->getHTML() |
668
|
|
|
]); |
669
|
|
|
} |
670
|
|
View Code Duplication |
private function createLoginForm() |
|
|
|
|
671
|
|
|
{ |
672
|
|
|
return $this->di->form->create([], [ |
673
|
|
|
'user' => [ |
674
|
|
|
'type' => 'text', |
675
|
|
|
'label' => 'Användarnamn:', |
676
|
|
|
'required' => true, |
677
|
|
|
'validation' => ['not_empty'], |
678
|
|
|
], |
679
|
|
|
'password' => [ |
680
|
|
|
'type' => 'password', |
681
|
|
|
'label' => 'Lösenord:', |
682
|
|
|
'required' => true, |
683
|
|
|
'validation' => ['not_empty'], |
684
|
|
|
], |
685
|
|
|
'submit' => [ |
686
|
|
|
'type' => 'submit', |
687
|
|
|
'callback' => [$this, 'callbackSubmitLogin'], |
688
|
|
|
], |
689
|
|
|
'submit-fail' => [ |
690
|
|
|
'type' => 'submit', |
691
|
|
|
'callback' => [$this, 'callbackSubmitFailLogin'], |
692
|
|
|
], |
693
|
|
|
]); |
694
|
|
|
} |
695
|
|
|
/** |
696
|
|
|
* Callback for submit-button. |
697
|
|
|
* |
698
|
|
|
*/ |
699
|
|
|
public function callbackSubmitLogin($form) |
700
|
|
|
{ |
701
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
702
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
703
|
|
|
// Authenticate user. |
704
|
|
|
// Check if user exists |
705
|
|
|
// Check if password matches hash |
706
|
|
|
$userName = $form->Value('user'); |
707
|
|
|
$password = $form->Value('password'); |
708
|
|
|
$user = $this->users->query() |
709
|
|
|
->where("acronym = '$userName'") |
710
|
|
|
->execute(); |
711
|
|
|
if (sizeof($user)==1) { |
712
|
|
|
// Set user in session if successful authentication |
713
|
|
|
if (md5($password)==$user[0]->password) { |
714
|
|
|
$this->session->set('user_logged_in', $userName); |
|
|
|
|
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
// $form->AddOutput("<p><b>Användare: " . $form->Value('user') . "</b></p>"); |
719
|
|
|
// $form->AddOutput("<p><b>Lösenord: " . $form->Value('password') . "</b></p>"); |
720
|
|
|
$form->saveInSession = false; |
721
|
|
|
return true; |
722
|
|
|
} |
723
|
|
|
/** |
724
|
|
|
* Callback for submit-button. |
725
|
|
|
* |
726
|
|
|
*/ |
727
|
|
|
public function callbackSubmitFailLogin($form) |
|
|
|
|
728
|
|
|
{ |
729
|
|
|
// TODO: Remove this? |
730
|
|
|
// $form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
731
|
|
|
return false; |
732
|
|
|
} |
733
|
|
|
/** |
734
|
|
|
* Callback What to do if the form was submitted? |
735
|
|
|
* |
736
|
|
|
*/ |
737
|
|
|
public function callbackLoginSuccess($form) |
|
|
|
|
738
|
|
|
{ |
739
|
|
|
// $form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
740
|
|
|
// Redirect to page posted from. |
741
|
|
|
$this->redirectTo('users/list'); |
742
|
|
|
} |
743
|
|
|
/** |
744
|
|
|
* Callback What to do when form could not be processed? |
745
|
|
|
* |
746
|
|
|
*/ |
747
|
|
|
public function callbackLoginFail($form) |
748
|
|
|
{ |
749
|
|
|
$form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
750
|
|
|
// Redirect to comment form. |
751
|
|
|
// $this->redirectTo(); |
752
|
|
|
} |
753
|
|
|
} |
754
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: