|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nicklas\Comment\HTMLForm\User; |
|
3
|
|
|
|
|
4
|
|
|
use \Anax\HTMLForm\FormModel; |
|
5
|
|
|
use \Anax\DI\DIInterface; |
|
6
|
|
|
use \Nicklas\Comment\Modules\User; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Example of FormModel implementation. |
|
10
|
|
|
*/ |
|
11
|
|
|
class UserResetForm extends FormModel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Constructor injects with DI container. |
|
15
|
|
|
* |
|
16
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(DIInterface $di) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($di); |
|
21
|
|
|
$this->form->create( |
|
22
|
|
|
[ |
|
23
|
|
|
"id" => __CLASS__, |
|
24
|
|
|
"br-after-label" => false, |
|
25
|
|
|
"use_fieldset" => false, |
|
26
|
|
|
"wrapper-element" => "div", |
|
27
|
|
|
], |
|
28
|
|
|
[ |
|
29
|
|
|
"user" => [ |
|
30
|
|
|
"type" => "text", |
|
31
|
|
|
//"description" => "Here you can place a description.", |
|
32
|
|
|
"placeholder" => "Användarnamn", |
|
33
|
|
|
"validation" => ["not_empty"], |
|
34
|
|
|
"label" => false, |
|
35
|
|
|
], |
|
36
|
|
|
"answer" => [ |
|
37
|
|
|
"type" => "text", |
|
38
|
|
|
"description" => "Vad är din favorträtt?", |
|
39
|
|
|
"placeholder" => "Kontrollfråga", |
|
40
|
|
|
"validation" => ["not_empty"], |
|
41
|
|
|
"label" => false, |
|
42
|
|
|
], |
|
43
|
|
|
"submit" => [ |
|
44
|
|
|
"type" => "submit", |
|
45
|
|
|
"value" => "Svara", |
|
46
|
|
|
"callback" => [$this, "callbackSubmit"] |
|
47
|
|
|
], |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
/** |
|
52
|
|
|
* Callback for submit-button which should return true if it could |
|
53
|
|
|
* carry out its work and false if something failed. |
|
54
|
|
|
* |
|
55
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function callbackSubmit() |
|
58
|
|
|
{ |
|
59
|
|
|
// Get values from the submitted form |
|
60
|
|
|
$name = $this->form->value("user"); |
|
61
|
|
|
$answer = $this->form->value("answer"); |
|
62
|
|
|
$user = new User(); |
|
63
|
|
|
$user->setDb($this->di->get("db")); |
|
64
|
|
|
$res = $user->verifyQuestion($name, $answer); |
|
65
|
|
|
if ($res) { |
|
66
|
|
|
$this->form->rememberValues(); |
|
67
|
|
|
$random = substr(md5(mt_rand()), 0, 7); |
|
68
|
|
|
$this->form->addOutput("Rätt! Ditt lösenord har ändrats till <b>'{$random}'</b>. |
|
69
|
|
|
Ange det när du loggar in och byt sedan till valfritt i din profilsida."); |
|
70
|
|
|
$user->setPassword($random); |
|
71
|
|
|
$user->save(); |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
$this->form->rememberValues(); |
|
75
|
|
|
$this->form->addOutput("Du hade fel, kontrollera så du inte skriver med stora bokstäver"); |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|