|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Peto16\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
|
6
|
|
|
use \Anax\DI\DIInterface; |
|
7
|
|
|
use \Peto16\User\User; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example of FormModel implementation. |
|
11
|
|
|
*/ |
|
12
|
|
|
class UpdateUserForm extends FormModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor injects with DI container. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
18
|
|
|
* @param integer $id to update |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function __construct(DIInterface $di, $id) |
|
21
|
|
|
{ |
|
22
|
2 |
|
parent::__construct($di); |
|
23
|
2 |
|
$user = $di->get("userService")->getUserByField("id", $id); |
|
24
|
2 |
|
$userLoggedIn = $di->get("userService")->getCurrentLoggedInUser(); |
|
25
|
|
|
$formItems = [ |
|
26
|
|
|
"id" => [ |
|
27
|
2 |
|
"type" => "text", |
|
28
|
2 |
|
"validation" => ["not_empty"], |
|
29
|
2 |
|
"readonly" => true, |
|
30
|
2 |
|
"value" => $user->id |
|
31
|
2 |
|
], |
|
32
|
|
|
|
|
33
|
|
|
"username" => [ |
|
34
|
2 |
|
"label" => "Användarnamn", |
|
35
|
2 |
|
"type" => "text", |
|
36
|
2 |
|
"value" => htmlentities($user->username) |
|
37
|
2 |
|
], |
|
38
|
|
|
|
|
39
|
|
|
"firstname" => [ |
|
40
|
2 |
|
"label" => "Förnamn", |
|
41
|
2 |
|
"type" => "text", |
|
42
|
2 |
|
"value" => htmlentities($user->firstname) |
|
43
|
2 |
|
], |
|
44
|
|
|
|
|
45
|
|
|
"lastname" => [ |
|
46
|
2 |
|
"label" => "Efternamn", |
|
47
|
2 |
|
"type" => "text", |
|
48
|
2 |
|
"value" => htmlentities($user->lastname) |
|
49
|
2 |
|
], |
|
50
|
|
|
|
|
51
|
|
|
"email" => [ |
|
52
|
2 |
|
"label" => "Epost", |
|
53
|
2 |
|
"type" => "email", |
|
54
|
2 |
|
"value" => htmlentities($user->email) |
|
55
|
2 |
|
], |
|
56
|
|
|
|
|
57
|
|
|
"enabled" => [ |
|
58
|
2 |
|
"label" => "Aktiverad", |
|
59
|
2 |
|
"type" => "checkbox", |
|
60
|
2 |
|
"checked" => $user->enabled === 1 ? 1 : 0 |
|
61
|
2 |
|
], |
|
62
|
|
|
"administrator" => [ |
|
63
|
2 |
|
"label" => "Administrator", |
|
64
|
2 |
|
"type" => "checkbox", |
|
65
|
2 |
|
"checked" => $user->administrator === 1 ? 1: 0 |
|
66
|
2 |
|
], |
|
67
|
|
|
|
|
68
|
|
|
"password" => [ |
|
69
|
2 |
|
"label" => "Lösenord", |
|
70
|
2 |
|
"type" => "password", |
|
71
|
2 |
|
], |
|
72
|
|
|
|
|
73
|
|
|
"password-again" => [ |
|
74
|
2 |
|
"label" => "Bekräfta lösenord", |
|
75
|
2 |
|
"type" => "password", |
|
76
|
|
|
"validation" => [ |
|
77
|
|
|
"match" => "password" |
|
78
|
2 |
|
], |
|
79
|
2 |
|
], |
|
80
|
|
|
|
|
81
|
|
|
"submit" => [ |
|
82
|
2 |
|
"type" => "submit", |
|
83
|
2 |
|
"value" => "Uppdatera", |
|
84
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
|
85
|
2 |
|
], |
|
86
|
2 |
|
]; |
|
87
|
|
|
|
|
88
|
2 |
|
if (!$userLoggedIn->administrator) { |
|
89
|
2 |
|
unset($formItems["enabled"]); |
|
90
|
2 |
|
unset($formItems["administrator"]); |
|
91
|
2 |
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$this->form->create( |
|
94
|
|
|
[ |
|
95
|
2 |
|
"id" => __CLASS__, |
|
96
|
2 |
|
"legend" => "Uppdatera användare", |
|
97
|
2 |
|
], |
|
98
|
|
|
$formItems |
|
99
|
2 |
|
); |
|
100
|
2 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Callback for submit-button which should return true if it could |
|
106
|
|
|
* carry out its work and false if something failed. |
|
107
|
|
|
* |
|
108
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function callbackSubmit() |
|
111
|
|
|
{ |
|
112
|
|
|
|
|
113
|
1 |
|
$userService = $this->di->get("userService"); |
|
114
|
|
|
|
|
115
|
|
|
// Get values from the submitted form |
|
116
|
1 |
|
$password = $this->form->value("password"); |
|
117
|
1 |
|
$passwordAgain = $this->form->value("password-again"); |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// Check password matches |
|
121
|
1 |
|
if ($password !== $passwordAgain) { |
|
122
|
|
|
$this->form->rememberValues(); |
|
123
|
|
|
$this->form->addOutput("Password did not match."); |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
1 |
|
$userStored = $userService->getUserByField("id", $this->form->value("id")); |
|
129
|
1 |
|
$userLoggedIn = $userService->getCurrentLoggedInUser(); |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
// Create user. |
|
133
|
1 |
|
$user = new User(); |
|
134
|
|
|
|
|
135
|
1 |
|
$user->id = $this->form->value("id"); |
|
136
|
1 |
|
$user->username = $this->form->value("username"); |
|
137
|
1 |
|
$user->firstname = $this->form->value("firstname"); |
|
138
|
1 |
|
$user->lastname = $this->form->value("lastname"); |
|
139
|
1 |
|
$user->email = $this->form->value("email"); |
|
140
|
1 |
|
$user->enabled = $userStored->enabled; |
|
141
|
1 |
|
$user->administrator = $userStored->administrator; |
|
142
|
|
|
|
|
143
|
1 |
|
if ($userLoggedIn->administrator) { |
|
144
|
1 |
|
$user->enabled = $this->form->value("enabled"); |
|
145
|
1 |
|
$user->administrator = $this->form->value("administrator"); |
|
146
|
1 |
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
$user->password = $userStored->password; |
|
149
|
1 |
|
if ($password !== null) { |
|
150
|
|
|
$user->password = $user->hashPassword($password); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Save user to database |
|
154
|
1 |
|
$userService->updateUser($user); |
|
155
|
|
|
|
|
156
|
1 |
|
$this->form->addOutput("Din profil är uppdaterad."); |
|
157
|
1 |
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|