|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pamo\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Pamo\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 Psr\Container\ContainerInterface $di a service container |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function __construct(ContainerInterface $di, $username) |
|
20
|
|
|
{ |
|
21
|
1 |
|
parent::__construct($di); |
|
22
|
1 |
|
$user = $this->getItemDetails($username); |
|
23
|
1 |
|
$this->form->create( |
|
24
|
|
|
[ |
|
25
|
1 |
|
"id" => __CLASS__, |
|
26
|
|
|
"legend" => "Create user", |
|
27
|
|
|
], |
|
28
|
|
|
[ |
|
29
|
1 |
|
"id" => [ |
|
30
|
1 |
|
"type" => "hidden", |
|
31
|
1 |
|
"value" => $user->id, |
|
32
|
|
|
], |
|
33
|
|
|
|
|
34
|
|
|
"username" => [ |
|
35
|
1 |
|
"type" => "text", |
|
36
|
1 |
|
"value" => $user->username, |
|
37
|
|
|
], |
|
38
|
|
|
|
|
39
|
|
|
"email" => [ |
|
40
|
1 |
|
"type" => "email", |
|
41
|
1 |
|
"value" => $user->email |
|
42
|
|
|
], |
|
43
|
|
|
|
|
44
|
|
|
"old-password" => [ |
|
45
|
|
|
"type" => "password", |
|
46
|
|
|
"placeholder" => "To update password", |
|
47
|
|
|
], |
|
48
|
|
|
|
|
49
|
|
|
"new-password" => [ |
|
50
|
|
|
"type" => "password", |
|
51
|
|
|
"placeholder" => "To update password", |
|
52
|
|
|
], |
|
53
|
|
|
|
|
54
|
|
|
"repeat-new-password" => [ |
|
55
|
|
|
"type" => "password", |
|
56
|
|
|
"placeholder" => "To update password", |
|
57
|
|
|
], |
|
58
|
|
|
|
|
59
|
|
|
"submit" => [ |
|
60
|
1 |
|
"type" => "submit", |
|
61
|
1 |
|
"value" => "Save", |
|
62
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
|
63
|
|
|
], |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get details on item to load form with. |
|
72
|
|
|
* |
|
73
|
|
|
* @param integer $id get details on item with id. |
|
74
|
|
|
* |
|
75
|
|
|
* @return User |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getItemDetails($username) : object |
|
78
|
|
|
{ |
|
79
|
1 |
|
$user = new User(); |
|
80
|
1 |
|
$user->setDb($this->di->get("dbqb")); |
|
81
|
1 |
|
$user->find("username", $username); |
|
82
|
1 |
|
return $user; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Callback for submit-button which should return true if it could |
|
89
|
|
|
* carry out its work and false if something failed. |
|
90
|
|
|
* |
|
91
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function callbackSubmit() |
|
94
|
|
|
{ |
|
95
|
|
|
// Get values from the submitted form |
|
96
|
1 |
|
$username = $this->form->value("username"); |
|
97
|
1 |
|
$email = $this->form->value("email"); |
|
98
|
1 |
|
$oldPassword = $this->form->value("old-password"); |
|
99
|
1 |
|
$newPassword = $this->form->value("new-password"); |
|
100
|
1 |
|
$newPasswordAgain = $this->form->value("repeat-new-password"); |
|
101
|
|
|
|
|
102
|
1 |
|
$user = new User(); |
|
103
|
1 |
|
$user->setDb($this->di->get("dbqb")); |
|
104
|
1 |
|
$user->find("id", $this->form->value("id")); |
|
105
|
|
|
|
|
106
|
1 |
|
if ($newPassword) { |
|
107
|
1 |
|
$res = $user->verifyPassword($user->username, $oldPassword); |
|
108
|
|
|
|
|
109
|
1 |
|
if (!$res) { |
|
110
|
1 |
|
$this->form->rememberValues(); |
|
111
|
1 |
|
$this->form->addOutput("Old password did not match."); |
|
112
|
1 |
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
// Check password matches |
|
115
|
1 |
|
if ($newPassword !== $newPasswordAgain) { |
|
116
|
1 |
|
$this->form->rememberValues(); |
|
117
|
1 |
|
$this->form->addOutput("New password did not match."); |
|
118
|
1 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$user->setPassword($newPassword); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
$user->username = $username; |
|
125
|
1 |
|
$user->email = $email; |
|
126
|
1 |
|
$user->save(); |
|
127
|
|
|
|
|
128
|
1 |
|
$this->di->session->set("user", [ |
|
129
|
1 |
|
"id" => $user->id, |
|
130
|
1 |
|
"username" => $user->username, |
|
131
|
1 |
|
"email" => $user->email |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
1 |
|
$this->form->addOutput("Profile updated!"); |
|
135
|
1 |
|
return true; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Callback what to do if the form was successfully submitted, this |
|
142
|
|
|
* happen when the submit callback method returns true. This method |
|
143
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function callbackSuccess() |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->di->get("response")->redirect("user/update")->send(); |
|
148
|
1 |
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|