1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radchasay\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Radchasay\User\User; |
7
|
|
|
use \Anax\DI\DIInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class AdminCreateUserForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
18
|
|
|
*/ |
19
|
|
|
public function __construct(DIInterface $di) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($di); |
22
|
|
|
$this->form->create( |
23
|
|
|
[ |
24
|
|
|
"id" => __CLASS__, |
25
|
|
|
"legend" => "Create user", |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
"name" => [ |
29
|
|
|
"type" => "text", |
30
|
|
|
], |
31
|
|
|
|
32
|
|
|
"email" => [ |
33
|
|
|
"type" => "email", |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
"age" => [ |
37
|
|
|
"type" => "number", |
38
|
|
|
], |
39
|
|
|
|
40
|
|
|
"password" => [ |
41
|
|
|
"type" => "password", |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"password-again" => [ |
45
|
|
|
"type" => "password", |
46
|
|
|
"validation" => [ |
47
|
|
|
"match" => "password", |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
"submit" => [ |
52
|
|
|
"type" => "submit", |
53
|
|
|
"value" => "Create user", |
54
|
|
|
"callback" => [$this, "callbackSubmit"], |
55
|
|
|
], |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Callback for submit-button which should return true if it could |
63
|
|
|
* carry out its work and false if something failed. |
64
|
|
|
* |
65
|
|
|
* @return boolean true if okey, false if something went wrong. |
66
|
|
|
*/ |
67
|
|
|
public function callbackSubmit() |
68
|
|
|
{ |
69
|
|
|
// Get values from the submitted form |
70
|
|
|
$name = htmlentities($this->form->value("name")); |
71
|
|
|
$email = htmlentities($this->form->value("email")); |
72
|
|
|
$age = htmlentities($this->form->value("age")); |
73
|
|
|
$password = htmlentities($this->form->value("password")); |
74
|
|
|
$passwordAgain = htmlentities($this->form->value("password-again")); |
75
|
|
|
|
76
|
|
|
// Check password matches |
77
|
|
|
if ($password !== $passwordAgain) { |
78
|
|
|
$this->form->rememberValues(); |
79
|
|
|
$this->form->addOutput("Password did not match."); |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Save to database |
84
|
|
|
/*$db = $this->di->get("db"); |
|
|
|
|
85
|
|
|
$password = password_hash($password, PASSWORD_DEFAULT); |
86
|
|
|
$db->connect() |
87
|
|
|
->insert("User", ["acronym", "password"]) |
88
|
|
|
->execute([$acronym, $password]);*/ |
89
|
|
|
|
90
|
|
|
$user = new User(); |
91
|
|
|
$user->setDb($this->di->get("db")); |
92
|
|
|
$user->name = $name; |
93
|
|
|
$user->email = $email; |
94
|
|
|
$user->age = $age; |
95
|
|
|
$user->setPassword($password); |
96
|
|
|
$user->save(); |
97
|
|
|
|
98
|
|
|
//$this->form->addOutput("User was created."); |
|
|
|
|
99
|
|
|
$url = $this->di->get("url")->create("admin/viewUsers"); |
100
|
|
|
$this->di->get("response")->redirect($url); |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.