1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pamo\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Pamo\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class DeleteUserForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
18
|
|
|
*/ |
19
|
1 |
|
public function __construct(ContainerInterface $di, $username) |
20
|
|
|
{ |
21
|
1 |
|
parent::__construct($di); |
22
|
1 |
|
$user = $this->getItemDetails($username); |
23
|
1 |
|
$this->form->create( |
24
|
|
|
[ |
25
|
1 |
|
"id" => __CLASS__, |
26
|
|
|
"legend" => "Create user", |
27
|
|
|
], |
28
|
|
|
[ |
29
|
1 |
|
"id" => [ |
30
|
1 |
|
"type" => "hidden", |
31
|
1 |
|
"value" => $user->id, |
32
|
|
|
], |
33
|
|
|
|
34
|
|
|
"password" => [ |
35
|
|
|
"type" => "password", |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"submit" => [ |
39
|
1 |
|
"type" => "submit", |
40
|
1 |
|
"value" => "Delete Account", |
41
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
42
|
|
|
], |
43
|
|
|
] |
44
|
|
|
); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get details on item to load form with. |
51
|
|
|
* |
52
|
|
|
* @param integer $id get details on item with id. |
53
|
|
|
* |
54
|
|
|
* @return User |
55
|
|
|
*/ |
56
|
1 |
|
public function getItemDetails($username) : object |
57
|
|
|
{ |
58
|
1 |
|
$user = new User(); |
59
|
1 |
|
$user->setDb($this->di->get("dbqb")); |
60
|
1 |
|
$user->find("username", $username); |
61
|
1 |
|
return $user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Callback for submit-button which should return true if it could |
68
|
|
|
* carry out its work and false if something failed. |
69
|
|
|
* |
70
|
|
|
* @return boolean true if okey, false if something went wrong. |
71
|
|
|
*/ |
72
|
1 |
|
public function callbackSubmit() |
73
|
|
|
{ |
74
|
1 |
|
$password = $this->form->value("password"); |
75
|
|
|
|
76
|
1 |
|
$user = new User(); |
77
|
1 |
|
$user->setDb($this->di->get("dbqb")); |
78
|
1 |
|
$user->find("id", $this->form->value("id")); |
79
|
|
|
|
80
|
1 |
|
$res = $user->verifyPassword($user->username, $password); |
81
|
|
|
|
82
|
1 |
|
if (!$res) { |
83
|
1 |
|
$this->form->rememberValues(); |
84
|
1 |
|
$this->form->addOutput("Password did not match."); |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$user->delete(); |
89
|
1 |
|
$this->di->session->set("user", null); |
90
|
|
|
|
91
|
1 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback what to do if the form was successfully submitted, this |
98
|
|
|
* happen when the submit callback method returns true. This method |
99
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
100
|
|
|
*/ |
101
|
1 |
|
public function callbackSuccess() |
102
|
|
|
{ |
103
|
1 |
|
$this->di->get("response")->redirect("user/login")->send(); |
104
|
1 |
|
} |
105
|
|
|
} |
106
|
|
|
|