1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm\User; |
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 CreateUserForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
18
|
|
|
*/ |
19
|
2 |
|
public function __construct(DIInterface $di) |
20
|
|
|
{ |
21
|
2 |
|
parent::__construct($di); |
22
|
2 |
|
$this->form->create( |
23
|
|
|
[ |
24
|
2 |
|
"id" => __CLASS__, |
25
|
2 |
|
"br-after-label" => false, |
26
|
2 |
|
"use_fieldset" => false, |
27
|
2 |
|
"class" => "login-widget", |
28
|
2 |
|
], |
29
|
|
|
[ |
30
|
|
|
"name" => [ |
31
|
2 |
|
"type" => "text", |
32
|
2 |
|
"validation" => ["not_empty"], |
33
|
2 |
|
"placeholder" => "Användarnamn", |
34
|
2 |
|
"label" => false, |
35
|
2 |
|
], |
36
|
|
|
|
37
|
|
|
"email" => [ |
38
|
2 |
|
"type" => "text", |
39
|
2 |
|
"placeholder" => "Mejladress", |
40
|
2 |
|
"label" => false, |
41
|
2 |
|
], |
42
|
|
|
"question" => [ |
43
|
2 |
|
"type" => "text", |
44
|
2 |
|
"placeholder" => "Din favoriträtt", |
45
|
2 |
|
"validation" => ["not_empty"], |
46
|
2 |
|
"label" => false, |
47
|
2 |
|
], |
48
|
|
|
|
49
|
|
|
"password" => [ |
50
|
2 |
|
"type" => "password", |
51
|
2 |
|
"validation" => ["not_empty"], |
52
|
2 |
|
"placeholder" => "Lösenord", |
53
|
|
|
"label" => false |
54
|
2 |
|
], |
55
|
|
|
|
56
|
|
|
"password-again" => [ |
57
|
2 |
|
"type" => "password", |
58
|
|
|
"validation" => [ |
59
|
|
|
"match" => "password" |
60
|
2 |
|
], |
61
|
2 |
|
"placeholder" => "Lösenord igen", |
62
|
|
|
"label" => false |
63
|
2 |
|
], |
64
|
|
|
|
65
|
|
|
"submit" => [ |
66
|
2 |
|
"label" => false, |
67
|
2 |
|
"type" => "submit", |
68
|
2 |
|
"value" => "Skapa konto", |
69
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
70
|
2 |
|
], |
71
|
|
|
] |
72
|
2 |
|
); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Callback for submit-button which should return true if it could |
79
|
|
|
* carry out its work and false if something failed. |
80
|
|
|
* |
81
|
|
|
* @return boolean true if okey, false if something went wrong. |
82
|
|
|
*/ |
83
|
|
|
public function callbackSubmit() |
84
|
|
|
{ |
85
|
|
|
// Get values from the submitted form |
86
|
|
|
$name = $this->form->value("name"); |
87
|
|
|
$email = $this->form->value("email"); |
88
|
|
|
$question = $this->form->value("question"); |
89
|
|
|
$password = $this->form->value("password"); |
90
|
|
|
$passwordAgain = $this->form->value("password-again"); |
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
|
|
|
if (preg_match("/[^-a-z0-9_]/i", $name)) { |
100
|
|
|
$this->form->addOutput("Ogiltigt användarnamn, vänligen försök igen."); |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$user = new User(); |
105
|
|
|
$user->setDb($this->di->get("db")); |
106
|
|
|
|
107
|
|
|
if ($user->userExists($name)) { |
108
|
|
|
$this->form->addOutput("User already exists"); |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$user->name = $name; |
113
|
|
|
$user->email = $email; |
114
|
|
|
$user->question = $question; |
115
|
|
|
$user->setPassword($password); |
116
|
|
|
$user->save(); |
117
|
|
|
|
118
|
|
|
$this->di->get('session')->set("user", $name); # set user in session |
119
|
|
|
$this->di->get("response")->redirect("user/profile"); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|