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 CreateUserForm 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
|
|
|
"create" => [ |
58
|
|
|
"type" => "submit", |
59
|
|
|
"value" => "Back to login", |
60
|
|
|
"callback" => [$this, "backToLogin"], |
61
|
|
|
], |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Callback for submit-button which should return true if it could |
69
|
|
|
* carry out its work and false if something failed. |
70
|
|
|
* |
71
|
|
|
* @return boolean true if okey, false if something went wrong. |
72
|
|
|
*/ |
73
|
|
|
public function callbackSubmit() |
74
|
|
|
{ |
75
|
|
|
// Get values from the submitted form |
76
|
|
|
$name = htmlentities($this->form->value("name")); |
77
|
|
|
$email = htmlentities($this->form->value("email")); |
78
|
|
|
$age = htmlentities($this->form->value("age")); |
79
|
|
|
$password = htmlentities($this->form->value("password")); |
80
|
|
|
$passwordAgain = htmlentities($this->form->value("password-again")); |
81
|
|
|
|
82
|
|
|
// Check password matches |
83
|
|
|
if ($password !== $passwordAgain) { |
84
|
|
|
$this->form->rememberValues(); |
85
|
|
|
$this->form->addOutput("Password did not match."); |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($password == null || $email == null || $name == null || $passwordAgain == null) { |
90
|
|
|
$this->form->addOutput("Please fill all inputs!"); |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$user = new User(); |
95
|
|
|
$user->setDb($this->di->get("db")); |
96
|
|
|
|
97
|
|
|
if ($user->checkUserExists($email)) { |
98
|
|
|
$user->name = $name; |
99
|
|
|
$user->email = $email; |
100
|
|
|
$user->age = $age; |
101
|
|
|
$user->setPassword($password); |
102
|
|
|
$user->save(); |
103
|
|
|
} else { |
104
|
|
|
$this->form->addOutput("Email already in use"); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
//$this->form->addOutput("User was created."); |
|
|
|
|
110
|
|
|
$this->di->get("session")->delete("create"); |
111
|
|
|
$url = $this->di->get("url")->create("user/login"); |
112
|
|
|
$this->di->get("response")->redirect($url); |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function backToLogin() |
117
|
|
|
{ |
118
|
|
|
$this->di->get("session")->delete("create"); |
119
|
|
|
$url = $this->di->get("url")->create("user/login"); |
120
|
|
|
$this->di->get("response")->redirect($url); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.