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
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class CreateUserForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
public function __construct(ContainerInterface $di) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($di); |
22
|
|
|
$this->form->create( |
23
|
|
|
[ |
24
|
|
|
"id" => __CLASS__, |
25
|
|
|
"legend" => "Create user", |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
"username" => [ |
29
|
|
|
"type" => "text", |
30
|
|
|
], |
31
|
|
|
|
32
|
|
|
"password" => [ |
33
|
|
|
"type" => "password", |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
"password-again" => [ |
37
|
|
|
"type" => "password", |
38
|
|
|
"validation" => [ |
39
|
|
|
"match" => "password" |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
"email" => [ |
44
|
|
|
"type" => "email", |
45
|
|
|
], |
46
|
|
|
|
47
|
|
|
"submit" => [ |
48
|
|
|
"type" => "submit", |
49
|
|
|
"value" => "Create user", |
50
|
|
|
"callback" => [$this, "callbackSubmit"] |
51
|
|
|
], |
52
|
|
|
] |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Callback for submit-button which should return true if it could |
60
|
|
|
* carry out its work and false if something failed. |
61
|
|
|
* |
62
|
|
|
* @return boolean true if okey, false if something went wrong. |
63
|
|
|
*/ |
64
|
|
|
public function callbackSubmit() |
65
|
|
|
{ |
66
|
|
|
// Get values from the submitted form |
67
|
|
|
$username = $this->form->value("username"); |
68
|
|
|
$password = $this->form->value("password"); |
69
|
|
|
$passwordAgain = $this->form->value("password-again"); |
70
|
|
|
$email = $this->form->value("email"); |
71
|
|
|
|
72
|
|
|
// Check password matches |
73
|
|
|
if ($password !== $passwordAgain) { |
74
|
|
|
$this->form->rememberValues(); |
75
|
|
|
$this->form->addOutput("Password did not match."); |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
$user = new User(); |
79
|
|
|
$user->setDb($this->di->get("dbqb")); |
80
|
|
|
$user->username = $username; |
81
|
|
|
$user->email = $email; |
|
|
|
|
82
|
|
|
$user->setPassword($password); |
83
|
|
|
$user->save(); |
84
|
|
|
|
85
|
|
|
$this->form->addOutput("User was created."); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|