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
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Setup initial table for users. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function setupAction() |
31
|
|
|
{ |
32
|
|
|
$this->users->init(); |
33
|
|
|
$this->redirectTo('users/list/'); |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* List all users. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function listAction() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$all = $this->users->findAll(); |
43
|
|
|
|
44
|
|
|
$this->theme->setTitle("List all users"); |
|
|
|
|
45
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
46
|
|
|
'users' => $all, |
47
|
|
|
'title' => "View all users", |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
/** |
51
|
|
|
* List user with acronym. |
52
|
|
|
* |
53
|
|
|
* @param int $id of user to display |
|
|
|
|
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function acronymAction($acronym = null) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$user = $this->users->query() |
60
|
|
|
->where('acronym = ' . "'$acronym'") |
61
|
|
|
->execute()[0]; |
62
|
|
|
$this->theme->setTitle("View user with acronym"); |
|
|
|
|
63
|
|
|
$this->views->add('users/view', [ |
|
|
|
|
64
|
|
|
'user' => $user, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
/** |
68
|
|
|
* List user with id. |
69
|
|
|
* |
70
|
|
|
* @param int $id of user to display |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function idAction($id = null) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$user = $this->users->find($id); |
77
|
|
|
|
78
|
|
|
$this->theme->setTitle("View user with id"); |
|
|
|
|
79
|
|
|
$this->views->add('users/view', [ |
|
|
|
|
80
|
|
|
'user' => $user, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* Add new user. |
85
|
|
|
* |
86
|
|
|
* @param string $acronym of user to add. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function add1Action($acronym = null) |
91
|
|
|
{ |
92
|
|
|
if (!isset($acronym)) { |
93
|
|
|
die("Missing acronym"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
97
|
|
|
|
98
|
|
|
$this->users->save([ |
99
|
|
|
'acronym' => $acronym, |
100
|
|
|
'email' => $acronym . '@mail.se', |
101
|
|
|
'name' => 'Mr/Mrs ' . $acronym, |
102
|
|
|
'password' => password_hash($acronym, PASSWORD_DEFAULT), |
103
|
|
|
'created' => $now, |
104
|
|
|
'active' => $now, |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
// $url = $this->url->create('users/id/' . $this->users->id); |
108
|
|
|
// $this->response->redirect($url); |
109
|
|
|
$this->redirectTo('users/id/' . $this->users->id); |
110
|
|
|
} |
111
|
|
|
/** |
112
|
|
|
* Add new user. |
113
|
|
|
* |
114
|
|
|
* @param string $acronym of user to add. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function addAction($acronym = null) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
121
|
|
|
$form = $this->createAddUserForm(); |
122
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
123
|
|
|
$this->di->theme->setTitle("Add user"); |
124
|
|
|
$this->di->views->add('default/page', [ |
125
|
|
|
'title' => "Add user", |
126
|
|
|
'content' => $form->getHTML() |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
private function createAddUserForm() |
130
|
|
|
{ |
131
|
|
|
return $this->di->form->create([], [ |
132
|
|
|
'name' => [ |
133
|
|
|
'type' => 'text', |
134
|
|
|
'label' => 'Name of person:', |
135
|
|
|
'required' => true, |
136
|
|
|
'validation' => ['not_empty'], |
137
|
|
|
], |
138
|
|
|
'acronym' => [ |
139
|
|
|
'type' => 'text', |
140
|
|
|
'label' => 'Acronym of person:', |
141
|
|
|
'required' => true, |
142
|
|
|
'validation' => ['not_empty'], |
143
|
|
|
], |
144
|
|
|
'email' => [ |
145
|
|
|
'type' => 'text', |
146
|
|
|
'required' => true, |
147
|
|
|
'validation' => ['not_empty', 'email_adress'], |
148
|
|
|
], |
149
|
|
|
'submit' => [ |
150
|
|
|
'type' => 'submit', |
151
|
|
|
'callback' => [$this, 'callbackSubmitAddUser'], |
152
|
|
|
], |
153
|
|
|
'submit-fail' => [ |
154
|
|
|
'type' => 'submit', |
155
|
|
|
'callback' => [$this, 'callbackSubmitFailAddUser'], |
156
|
|
|
], |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* Callback for submit-button. |
161
|
|
|
* |
162
|
|
|
*/ |
163
|
|
|
public function callbackSubmitAddUser($form) |
164
|
|
|
{ |
165
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
166
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
167
|
|
|
$acronym = $form->Value('acronym'); |
168
|
|
|
// Check for duplicate acronym. Die if exists. |
169
|
|
|
$all = $this->users->query() |
170
|
|
|
->where("acronym = '$acronym'") |
171
|
|
|
->execute(); |
172
|
|
|
if (count($all)!=0) { |
173
|
|
|
die("User with acronym $acronym already in model."); |
174
|
|
|
} |
175
|
|
|
// Save user data to database |
176
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
177
|
|
|
$this->users->save([ |
178
|
|
|
'acronym' => $form->Value('acronym'), |
179
|
|
|
'email' => $form->Value('email'), |
180
|
|
|
'name' => 'Mr/Mrs ' . $form->Value('name'), |
181
|
|
|
'password' => password_hash($acronym, PASSWORD_DEFAULT), |
182
|
|
|
'created' => $now, |
183
|
|
|
'active' => $now, |
184
|
|
|
]); |
185
|
|
|
|
186
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
187
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('email') . "</b></p>"); |
188
|
|
|
// $form->AddOutput("<p><b>Acronym: " . $form->Value('acronym') . "</b></p>"); |
189
|
|
|
$form->saveInSession = false; |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
/** |
193
|
|
|
* Callback for submit-button. |
194
|
|
|
* |
195
|
|
|
*/ |
196
|
|
|
public function callbackSubmitFailAddUser($form) |
197
|
|
|
{ |
198
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
/** |
202
|
|
|
* Callback What to do if the form was submitted? |
203
|
|
|
* |
204
|
|
|
*/ |
205
|
|
|
public function callbackSuccess($form) |
206
|
|
|
{ |
207
|
|
|
$form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>"); |
208
|
|
|
$this->redirectTo('users/id/' . $this->users->id); |
209
|
|
|
} |
210
|
|
|
/** |
211
|
|
|
* Callback What to do when form could not be processed? |
212
|
|
|
* |
213
|
|
|
*/ |
214
|
|
|
public function callbackFail($form) |
215
|
|
|
{ |
216
|
|
|
$form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>"); |
217
|
|
|
$this->redirectTo(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Update user. |
223
|
|
|
* |
224
|
|
|
* @param string $acronym of user to update. |
|
|
|
|
225
|
|
|
* |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
public function updateAction($id = null) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$this->di->session(); // Will load the session service which also starts the session |
231
|
|
|
// Check if valid entry exists. |
232
|
|
|
$all = $this->users->query() |
233
|
|
|
->where("id = '$id'") |
234
|
|
|
->execute(); |
235
|
|
|
if (count($all)!=1) { |
236
|
|
|
die("User with id $id not found."); |
237
|
|
|
} |
238
|
|
|
$user = $this->users->find($id); |
239
|
|
|
$form = $this->createUpdateUserForm($user); |
240
|
|
|
$form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
241
|
|
|
$this->di->theme->setTitle("Update user"); |
242
|
|
|
$this->di->views->add('default/page', [ |
243
|
|
|
'title' => "Update user", |
244
|
|
|
'content' => $form->getHTML() |
245
|
|
|
]); |
246
|
|
|
} |
247
|
|
|
private function createUpdateUserForm($user = null) |
248
|
|
|
{ |
249
|
|
|
return $this->di->form->create([], [ |
250
|
|
|
'name' => [ |
251
|
|
|
'type' => 'text', |
252
|
|
|
'value' => $user->name, |
253
|
|
|
'label' => 'Name of person:', |
254
|
|
|
'required' => true, |
255
|
|
|
'validation' => ['not_empty'], |
256
|
|
|
], |
257
|
|
|
'acronym' => [ |
258
|
|
|
'type' => 'text', |
259
|
|
|
'value' => $user->acronym, |
260
|
|
|
'label' => 'Acronym of person:', |
261
|
|
|
'required' => true, |
262
|
|
|
'validation' => ['not_empty'], |
263
|
|
|
], |
264
|
|
|
'email' => [ |
265
|
|
|
'type' => 'text', |
266
|
|
|
'value' => $user->email, |
267
|
|
|
'required' => true, |
268
|
|
|
'validation' => ['not_empty', 'email_adress'], |
269
|
|
|
], |
270
|
|
|
'id' => [ |
271
|
|
|
'type' => 'hidden', |
272
|
|
|
'value' => $user->id, |
273
|
|
|
], |
274
|
|
|
'submit' => [ |
275
|
|
|
'type' => 'submit', |
276
|
|
|
'callback' => [$this, 'callbackSubmitUpdateUser'], |
277
|
|
|
], |
278
|
|
|
'submit-fail' => [ |
279
|
|
|
'type' => 'submit', |
280
|
|
|
'callback' => [$this, 'callbackSubmitFailUpdateUser'], |
281
|
|
|
], |
282
|
|
|
]); |
283
|
|
|
} |
284
|
|
|
public function callbackSubmitUpdateUser($form) |
285
|
|
|
{ |
286
|
|
|
// $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>"); |
287
|
|
|
// $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>"); |
288
|
|
|
// Handle update to duplicate acronym. Die if other user already has acronym. |
289
|
|
|
$id = $form->Value('id'); |
290
|
|
|
$acronym = $form->Value('acronym'); |
291
|
|
|
$all = $this->users->query() |
292
|
|
|
->where("id != '$id'") |
293
|
|
|
->andwhere("acronym = '$acronym'") |
294
|
|
|
->execute(); |
295
|
|
|
if (count($all)==1) { |
296
|
|
|
die("User with acronym $acronym alredy defined for other user. "); |
297
|
|
|
} |
298
|
|
|
// die(); |
299
|
|
|
// Save user data to database |
300
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
|
|
|
|
301
|
|
|
$this->users->save([ |
302
|
|
|
'acronym' => $form->Value('acronym'), |
303
|
|
|
'email' => $form->Value('email'), |
304
|
|
|
'name' => 'Mr/Mrs ' . $form->Value('name'), |
305
|
|
|
// 'password' => password_hash($acronym, PASSWORD_DEFAULT), |
306
|
|
|
// 'created' => $now, |
307
|
|
|
// 'active' => $now, |
308
|
|
|
]); |
309
|
|
|
|
310
|
|
|
// $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>"); |
311
|
|
|
// $form->AddOutput("<p><b>Email: " . $form->Value('email') . "</b></p>"); |
312
|
|
|
// $form->AddOutput("<p><b>Phone: " . $form->Value('acronym') . "</b></p>"); |
313
|
|
|
$form->saveInSession = false; |
314
|
|
|
return true; |
315
|
|
|
} |
316
|
|
|
/** |
317
|
|
|
* Callback for submit-button. |
318
|
|
|
* |
319
|
|
|
*/ |
320
|
|
|
public function callbackSubmitFailUpdateUser($form) |
321
|
|
|
{ |
322
|
|
|
$form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>"); |
323
|
|
|
return false; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Delete user. |
330
|
|
|
* |
331
|
|
|
* @param integer $id of user to delete. |
332
|
|
|
* |
333
|
|
|
* @return void |
334
|
|
|
*/ |
335
|
|
|
public function deleteAction($id = null) |
336
|
|
|
{ |
337
|
|
|
if (!isset($id)) { |
338
|
|
|
die("Missing id"); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$res = $this->users->delete($id); |
|
|
|
|
342
|
|
|
|
343
|
|
|
$this->redirectTo('users/list/'); |
344
|
|
|
} |
345
|
|
|
/** |
346
|
|
|
* Delete (soft) user. |
347
|
|
|
* |
348
|
|
|
* @param integer $id of user to delete. |
349
|
|
|
* |
350
|
|
|
* @return void |
351
|
|
|
*/ |
352
|
|
View Code Duplication |
public function softDeleteAction($id = null) |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
if (!isset($id)) { |
355
|
|
|
die("Missing id"); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
$now = gmdate('Y-m-d H:i:s'); |
359
|
|
|
|
360
|
|
|
$user = $this->users->find($id); |
361
|
|
|
|
362
|
|
|
$user->deleted = $now; |
363
|
|
|
$user->save(); |
364
|
|
|
|
365
|
|
|
$this->redirectTo('users/list/'); |
366
|
|
|
} |
367
|
|
View Code Duplication |
public function undoDeleteAction($id = null) |
|
|
|
|
368
|
|
|
{ |
369
|
|
|
if (!isset($id)) { |
370
|
|
|
die("Missing id"); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
$user = $this->users->find($id); |
374
|
|
|
|
375
|
|
|
$user->deleted = null; |
376
|
|
|
$user->save(); |
377
|
|
|
|
378
|
|
|
$this->redirectTo('users/id/' . $id); |
379
|
|
|
} |
380
|
|
View Code Duplication |
public function activateAction($id = null) |
|
|
|
|
381
|
|
|
{ |
382
|
|
|
if (!isset($id)) { |
383
|
|
|
die("Missing id"); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$user = $this->users->find($id); |
387
|
|
|
|
388
|
|
|
$user->active = gmdate('Y-m-d H:i:s'); |
389
|
|
|
$user->save(); |
390
|
|
|
|
391
|
|
|
$this->redirectTo('users/id/' . $id); |
392
|
|
|
} |
393
|
|
View Code Duplication |
public function deactivateAction($id = null) |
|
|
|
|
394
|
|
|
{ |
395
|
|
|
if (!isset($id)) { |
396
|
|
|
die("Missing id"); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$user = $this->users->find($id); |
400
|
|
|
|
401
|
|
|
$user->active = null; |
402
|
|
|
$user->save(); |
403
|
|
|
|
404
|
|
|
$this->redirectTo('users/id/' . $id); |
405
|
|
|
} |
406
|
|
|
/** |
407
|
|
|
* List all active and not deleted users. |
408
|
|
|
* |
409
|
|
|
* @return void |
410
|
|
|
*/ |
411
|
|
View Code Duplication |
public function activeAction() |
|
|
|
|
412
|
|
|
{ |
413
|
|
|
$all = $this->users->query() |
414
|
|
|
->where('active IS NOT NULL') |
415
|
|
|
->andWhere('deleted is NULL') |
416
|
|
|
->execute(); |
417
|
|
|
|
418
|
|
|
$this->theme->setTitle("Users that are active"); |
|
|
|
|
419
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
420
|
|
|
'users' => $all, |
421
|
|
|
'title' => "Users that are active", |
422
|
|
|
]); |
423
|
|
|
} |
424
|
|
|
/** |
425
|
|
|
* List all inactive and not deleted users. |
426
|
|
|
* |
427
|
|
|
* @return void |
428
|
|
|
*/ |
429
|
|
View Code Duplication |
public function inactiveAction() |
|
|
|
|
430
|
|
|
{ |
431
|
|
|
$all = $this->users->query() |
432
|
|
|
->where('active IS NULL') |
433
|
|
|
->andWhere('deleted is NULL') |
434
|
|
|
->execute(); |
435
|
|
|
|
436
|
|
|
$this->theme->setTitle("Users that are inactive"); |
|
|
|
|
437
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
438
|
|
|
'users' => $all, |
439
|
|
|
'title' => "Users that are inactive", |
440
|
|
|
]); |
441
|
|
|
} |
442
|
|
|
/** |
443
|
|
|
* List all active and not deleted users. |
444
|
|
|
* |
445
|
|
|
* @return void |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function wastebasketAction() |
|
|
|
|
448
|
|
|
{ |
449
|
|
|
$all = $this->users->query() |
450
|
|
|
->where('deleted is NOT NULL') |
451
|
|
|
->execute(); |
452
|
|
|
|
453
|
|
|
$this->theme->setTitle("Users that are in wastebasket"); |
|
|
|
|
454
|
|
|
$this->views->add('users/list-all', [ |
|
|
|
|
455
|
|
|
'users' => $all, |
456
|
|
|
'title' => "Users that are in wastebasket", |
457
|
|
|
]); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
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: