1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm\Admin; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Nicklas\Comment\Modules\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class CreateUserForm2 extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
18
|
|
|
*/ |
19
|
1 |
|
public function __construct(DIInterface $di) |
20
|
|
|
{ |
21
|
1 |
|
parent::__construct($di); |
22
|
1 |
|
$this->form->create( |
23
|
|
|
[ |
24
|
1 |
|
"id" => __CLASS__, |
25
|
1 |
|
"legend" => "Create user", |
26
|
|
|
"fieldset" => true |
27
|
1 |
|
], |
28
|
|
|
[ |
29
|
1 |
|
"name" => [ |
30
|
1 |
|
"type" => "text", |
31
|
1 |
|
"validation" => ["not_empty"] |
32
|
1 |
|
], |
33
|
|
|
|
34
|
|
|
"email" => [ |
35
|
1 |
|
"type" => "text", |
36
|
1 |
|
], |
37
|
|
|
|
38
|
|
|
"password" => [ |
39
|
1 |
|
"type" => "password", |
40
|
1 |
|
"validation" => ["not_empty"] |
41
|
1 |
|
], |
42
|
|
|
|
43
|
|
|
"password-again" => [ |
44
|
1 |
|
"type" => "password", |
45
|
|
|
"validation" => [ |
46
|
|
|
"match" => "password" |
47
|
1 |
|
], |
48
|
1 |
|
], |
49
|
|
|
|
50
|
|
|
"select" => [ |
51
|
1 |
|
"type" => "select", |
52
|
1 |
|
"label" => "Select authority", |
53
|
1 |
|
"options" => ["admin" => "admin", "user" => "user"], |
54
|
1 |
|
], |
55
|
|
|
|
56
|
|
|
"submit" => [ |
57
|
1 |
|
"type" => "submit", |
58
|
1 |
|
"value" => "Skapa user", |
59
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
60
|
1 |
|
], |
61
|
|
|
] |
62
|
1 |
|
); |
63
|
1 |
|
} |
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 = $this->form->value("name"); |
77
|
|
|
$email = $this->form->value("email"); |
78
|
|
|
$password = $this->form->value("password"); |
79
|
|
|
$passwordAgain = $this->form->value("password-again"); |
80
|
|
|
|
81
|
|
|
// Check password matches |
82
|
|
|
if ($password !== $passwordAgain) { |
83
|
|
|
$this->form->rememberValues(); |
84
|
|
|
$this->form->addOutput("Password did not match."); |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (strpos($name, '%') !== false) { |
89
|
|
|
$this->form->addOutput("% is not allowed"); |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$user = new User(); |
94
|
|
|
$user->setDb($this->di->get("db")); |
95
|
|
|
|
96
|
|
|
if ($user->userExists($name)) { |
97
|
|
|
$this->form->addOutput("User already exists"); |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$user->name = $name; |
102
|
|
|
$user->email = $email; |
103
|
|
|
$user->authority = $this->form->value("select"); |
104
|
|
|
$user->setPassword($password); |
105
|
|
|
$user->save(); |
106
|
|
|
|
107
|
|
|
$this->form->addOutput("You added the user"); |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|