1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Forum\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Forum\User\User; |
6
|
|
|
use Anax\HTMLForm\FormModel; |
7
|
|
|
use Psr\Container\ContainerInterface; |
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" => "Skapa användare", |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
"acronym" => [ |
29
|
|
|
"type" => "text", |
30
|
|
|
], |
31
|
|
|
|
32
|
|
|
"password" => [ |
33
|
|
|
"type" => "password", |
34
|
|
|
"description" => "Välj ett password", |
35
|
|
|
"placeholder" => "Your password", |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"hidden" => [ |
39
|
|
|
"type" => "hidden", |
40
|
|
|
"value" => "secret value", |
41
|
|
|
], |
42
|
|
|
// |
43
|
|
|
// "select" => [ |
44
|
|
|
// "type" => "select", |
45
|
|
|
// "label" => "Select your rating:", |
46
|
|
|
// "description" => "Here you can place a description.", |
47
|
|
|
// "options" => [ |
48
|
|
|
// "+1" => 1, |
49
|
|
|
// "-1" => -1, |
50
|
|
|
// ], |
51
|
|
|
// "value" => "potato", |
52
|
|
|
// ], |
53
|
|
|
|
54
|
|
|
"password-again" => [ |
55
|
|
|
"type" => "password", |
56
|
|
|
"validation" => [ |
57
|
|
|
"match" => "password" |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
|
61
|
|
|
"email" => [ |
62
|
|
|
"type" => "email", |
63
|
|
|
"description" => "Skriv in din email", |
64
|
|
|
"placeholder" => "Your email", |
65
|
|
|
], |
66
|
|
|
|
67
|
|
|
"submit" => [ |
68
|
|
|
"type" => "submit", |
69
|
|
|
"value" => "Submit", |
70
|
|
|
"callback" => [$this, "callbackSubmit"] |
71
|
|
|
], |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Callback for submit-button which should return true if it could |
80
|
|
|
* carry out its work and false if something failed. |
81
|
|
|
* |
82
|
|
|
* @return boolean true if okey, false if something went wrong. |
83
|
|
|
*/ |
84
|
|
|
public function callbackSubmit() |
85
|
|
|
{ |
86
|
|
|
// Get values from the submitted form |
87
|
|
|
$acronym = $this->form->value("acronym"); |
88
|
|
|
$password = $this->form->value("password"); |
89
|
|
|
$passwordAgain = $this->form->value("password-again"); |
90
|
|
|
$email = $this->form->value("email"); |
91
|
|
|
|
92
|
|
|
// Check password matches |
93
|
|
|
if ($password !== $passwordAgain ) { |
94
|
|
|
$this->form->rememberValues(); |
95
|
|
|
$this->form->addOutput("Password did not match."); |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Save to database |
100
|
|
|
// $db = $this->di->get("dbqb"); |
101
|
|
|
// $password = password_hash($password, PASSWORD_DEFAULT); |
102
|
|
|
// $db->connect() |
103
|
|
|
// ->insert("User", ["acronym", "password"]) |
104
|
|
|
// ->execute([$acronym]) |
105
|
|
|
// ->fetch(); |
106
|
|
|
$user = new User(); |
107
|
|
|
$user->setDb($this->di->get("dbqb")); |
108
|
|
|
$user->acronym = $acronym; |
109
|
|
|
$user->email = $email; |
110
|
|
|
$user->setPassword($password); |
111
|
|
|
$user->save(); |
112
|
|
|
|
113
|
|
|
$this->form->addOutput("User was created."); |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|