1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Forum\Forum\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Forum\Forum\Question; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to delete an item. |
11
|
|
|
*/ |
12
|
|
|
class DeleteForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
18
|
|
|
*/ |
19
|
|
|
public function __construct(ContainerInterface $di) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($di); |
22
|
|
|
$this->form->create( |
23
|
|
|
[ |
24
|
|
|
"id" => __CLASS__, |
25
|
|
|
"legend" => "Delete an item", |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
"select" => [ |
29
|
|
|
"type" => "select", |
30
|
|
|
"label" => "Select item to delete:", |
31
|
|
|
"options" => $this->getAllItems(), |
32
|
|
|
], |
33
|
|
|
|
34
|
|
|
"submit" => [ |
35
|
|
|
"type" => "submit", |
36
|
|
|
"value" => "Delete item", |
37
|
|
|
"callback" => [$this, "callbackSubmit"] |
38
|
|
|
], |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all items as array suitable for display in select option dropdown. |
47
|
|
|
* |
48
|
|
|
* @return array with key value of all items. |
49
|
|
|
*/ |
50
|
|
|
protected function getAllItems() : array |
51
|
|
|
{ |
52
|
|
|
$book = new Book(); |
53
|
|
|
$book->setDb($this->di->get("dbqb")); |
54
|
|
|
|
55
|
|
|
$books = ["-1" => "Select an item..."]; |
56
|
|
|
foreach ($book->findAll() as $obj) { |
57
|
|
|
$books[$obj->id] = "{$obj->column1} ({$obj->id})"; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $books; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Callback for submit-button which should return true if it could |
67
|
|
|
* carry out its work and false if something failed. |
68
|
|
|
* |
69
|
|
|
* @return bool true if okey, false if something went wrong. |
70
|
|
|
*/ |
71
|
|
|
public function callbackSubmit() : bool |
72
|
|
|
{ |
73
|
|
|
$book = new Book(); |
74
|
|
|
$book->setDb($this->di->get("dbqb")); |
75
|
|
|
$book->find("id", $this->form->value("select")); |
76
|
|
|
$book->delete(); |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Callback what to do if the form was successfully submitted, this |
84
|
|
|
* happen when the submit callback method returns true. This method |
85
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
86
|
|
|
*/ |
87
|
|
|
public function callbackSuccess() |
88
|
|
|
{ |
89
|
|
|
$this->di->get("response")->redirect("book")->send(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
// /** |
95
|
|
|
// * Callback what to do if the form was unsuccessfully submitted, this |
96
|
|
|
// * happen when the submit callback method returns false or if validation |
97
|
|
|
// * fails. This method can/should be implemented by the subclass for a |
98
|
|
|
// * different behaviour. |
99
|
|
|
// */ |
100
|
|
|
// public function callbackFail() |
101
|
|
|
// { |
102
|
|
|
// $this->di->get("response")->redirectSelf()->send(); |
103
|
|
|
// } |
104
|
|
|
} |
105
|
|
|
|