1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\User\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Anax\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to delete an item. |
11
|
|
|
*/ |
12
|
|
|
class DeleteUserForm 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 |
|
"legend" => "Radera användare", |
26
|
2 |
|
], |
27
|
|
|
[ |
28
|
|
|
"select" => [ |
29
|
2 |
|
"type" => "select", |
30
|
2 |
|
"label" => "Välj användare att radera:", |
31
|
2 |
|
"options" => $this->getAllItems(), |
32
|
2 |
|
], |
33
|
|
|
|
34
|
|
|
"submit" => [ |
35
|
2 |
|
"type" => "submit", |
36
|
2 |
|
"value" => "Ta bort användare", |
37
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
38
|
2 |
|
], |
39
|
|
|
"submit2" => [ |
40
|
2 |
|
"type" => "submit", |
41
|
2 |
|
"value" => "Redigera användare", |
42
|
2 |
|
"callback" => [$this, "callbackSubmit2"] |
43
|
2 |
|
], |
44
|
|
|
] |
45
|
2 |
|
); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get all items as array suitable for display in select option dropdown. |
52
|
|
|
* |
53
|
|
|
* @return array with key value of all items. |
54
|
|
|
*/ |
55
|
|
|
// protected function getAllItems() |
56
|
2 |
|
public function getAllItems() |
57
|
|
|
{ |
58
|
2 |
|
$user = new User(); |
59
|
2 |
|
$user->setDb($this->di->get("db")); |
60
|
2 |
|
$allUsers = $user->findAll(); |
61
|
|
|
|
62
|
2 |
|
$users = ["-1" => "Select an item..."]; |
63
|
2 |
|
foreach ($allUsers as $obj) { |
64
|
2 |
|
$users[$obj->id] = "{$obj->acronym} ({$obj->id})"; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
return $users; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Callback for submit-button which should return true if it could |
74
|
|
|
* carry out its work and false if something failed. |
75
|
|
|
* |
76
|
|
|
* @return boolean true if okey, false if something went wrong. |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function callbackSubmit() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$user = new User(); |
81
|
|
|
$user->setDb($this->di->get("db")); |
82
|
|
|
$user->find("id", $this->form->value("select")); |
83
|
|
|
$user->delete(); |
84
|
|
|
|
85
|
|
|
$this->form->addOutput("Användaren raderades."); |
86
|
|
|
$this->di->get("response")->redirect("user/edit-all"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Callback for submit-button which should return true if it could |
91
|
|
|
* carry out its work and false if something failed. |
92
|
|
|
* |
93
|
|
|
* @return boolean true if okey, false if something went wrong. |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function callbackSubmit2() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$user = new User(); |
98
|
|
|
$user->setDb($this->di->get("db")); |
99
|
|
|
$user->find("id", $this->form->value("select")); |
100
|
|
|
|
101
|
|
|
$id = $this->form->value("select"); |
102
|
|
|
|
103
|
|
|
$this->di->get("response")->redirect("user/edit/$id"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.