1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alfs18\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Alfs18\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" => "Create user", |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
"acronym" => [ |
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
|
|
|
"points" => [ |
44
|
|
|
"type" => "hidden", |
45
|
|
|
"value" => 0, |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
"created" => [ |
49
|
|
|
"type" => "hidden", |
50
|
|
|
"value" => date("d M Y, H:i"), |
51
|
|
|
], |
52
|
|
|
|
53
|
|
|
"submit" => [ |
54
|
|
|
"type" => "submit", |
55
|
|
|
"value" => "Create user", |
56
|
|
|
"callback" => [$this, "callbackSubmit"] |
57
|
|
|
], |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Callback for submit-button which should return true if it could |
66
|
|
|
* carry out its work and false if something failed. |
67
|
|
|
* |
68
|
|
|
* @return boolean true if okey, false if something went wrong. |
69
|
|
|
*/ |
70
|
|
|
public function callbackSubmit() |
71
|
|
|
{ |
72
|
|
|
$user = new User(); |
73
|
|
|
// Get values from the submitted form |
74
|
|
|
$acronym = $this->form->value("acronym"); |
75
|
|
|
$password = $user->changeCharacter($this->form->value("password")); |
76
|
|
|
$passwordAgain = $user->changeCharacter($this->form->value("password-again")); |
77
|
|
|
$points = $this->form->value("points"); |
78
|
|
|
$created = $this->form->value("created"); |
79
|
|
|
|
80
|
|
|
// Check password matches |
81
|
|
|
if ($password !== $passwordAgain) { |
82
|
|
|
$this->form->rememberValues(); |
83
|
|
|
$this->form->addOutput("Password dig not match."); |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Save to database |
88
|
|
|
// $db = $this->di->get("dbqb"); |
89
|
|
|
// $password = password_hash($password, PASSWORD_DEFAULT); |
90
|
|
|
// $db->connect() |
91
|
|
|
// ->insert("User", ["acronym", "password"]) |
92
|
|
|
// ->execute([$acronym, $password]); |
93
|
|
|
$user = new User(); |
94
|
|
|
$user->setDb($this->di->get("dbqb")); |
95
|
|
|
$user->acronym = $user->changeCharacter($acronym); |
96
|
|
|
$password = $user->changeCharacter($password); |
|
|
|
|
97
|
|
|
$user->setPassword($password); |
|
|
|
|
98
|
|
|
$user->setPicture($this->di); |
99
|
|
|
$user->points = intval($points); |
100
|
|
|
$user->created = $created; |
101
|
|
|
$user->saveUser($this->di); |
102
|
|
|
|
103
|
|
|
$this->form->addOutput("User was created."); |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|