1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Anax\User\User; |
8
|
|
|
|
9
|
|
|
class EditUserForm extends FormModel |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Constructor injects with DI container. |
13
|
|
|
* |
14
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
15
|
|
|
* @param integer $id to update |
16
|
|
|
*/ |
17
|
|
|
public function __construct(ContainerInterface $di, $id) |
18
|
|
|
{ |
19
|
|
|
parent::__construct($di); |
20
|
|
|
$user = $this->getItemDetails($id); |
21
|
|
|
$this->form->create( |
22
|
|
|
[ |
23
|
|
|
"id" => __CLASS__, |
24
|
|
|
"legend" => "Edit exsiting user", |
25
|
|
|
], |
26
|
|
|
[ |
27
|
|
|
"id" => [ |
28
|
|
|
"type" => "text", |
29
|
|
|
"validation" => ["not_empty"], |
30
|
|
|
"readonly" => "readonly", |
31
|
|
|
"value" => $user->id, |
32
|
|
|
], |
33
|
|
|
|
34
|
|
|
"username" => [ |
35
|
|
|
"type" => "text", |
36
|
|
|
"validation" => ["not_empty"], |
37
|
|
|
"value" => $user->username, |
38
|
|
|
], |
39
|
|
|
|
40
|
|
|
"password" => [ |
41
|
|
|
"type" => "password", |
42
|
|
|
"validation" => ["not_empty"], |
43
|
|
|
], |
44
|
|
|
|
45
|
|
|
"password-again" => [ |
46
|
|
|
"type" => "password", |
47
|
|
|
"validation" => [ |
48
|
|
|
"match" => "password" |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
|
52
|
|
|
"email" => [ |
53
|
|
|
"type" => "email", |
54
|
|
|
"validation" => ["not_empty"], |
55
|
|
|
"value" => $user->email, |
56
|
|
|
], |
57
|
|
|
|
58
|
|
|
"submit" => [ |
59
|
|
|
"type" => "submit", |
60
|
|
|
"value" => "Create user", |
61
|
|
|
"callback" => [$this, "callbackSubmit"] |
62
|
|
|
], |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get details on item to load form with. |
69
|
|
|
* |
70
|
|
|
* @param integer $id get details on item with id. |
71
|
|
|
* |
72
|
|
|
* @return Book |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
public function getItemDetails($id) : object |
75
|
|
|
{ |
76
|
|
|
$user = new User(); |
77
|
|
|
$user->setDb($this->di->get("dbqb")); |
78
|
|
|
$user->find("id", $id); |
79
|
|
|
return $user; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Callback for submit-button which should return true if it could |
85
|
|
|
* carry out its work and false if something failed. |
86
|
|
|
* |
87
|
|
|
* @return boolean true if okey, false if something went wrong. |
88
|
|
|
*/ |
89
|
|
|
public function callbackSubmit() |
90
|
|
|
{ |
91
|
|
|
// Get values from the submitted form |
92
|
|
|
$user = new User(); |
93
|
|
|
$user->setDb($this->di->get("dbqb")); |
94
|
|
|
$username = $this->form->value("username"); |
95
|
|
|
$password = $this->form->value("password"); |
96
|
|
|
$passwordAgain = $this->form->value("password-again"); |
97
|
|
|
$email = $this->form->value("email"); |
98
|
|
|
|
99
|
|
|
// Check password matches |
100
|
|
|
if ($password !== $passwordAgain) { |
101
|
|
|
$this->form->rememberValues(); |
102
|
|
|
$this->form->addOutput("Password did not match."); |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
$user->find("id", $this->form->value("id")); |
106
|
|
|
$user->username = $username; |
107
|
|
|
$user->password = $this->form->value("password"); |
108
|
|
|
$user->email = $email; |
|
|
|
|
109
|
|
|
$user->save(); |
110
|
|
|
$this->form->addOutput("User was updated."); |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|