1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aiur18\Question\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Aiur18\Question\Question; |
8
|
|
|
use Aiur18\getset\getset; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Form to delete an item. |
12
|
|
|
*/ |
13
|
|
|
class DeleteForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public function __construct(ContainerInterface $di) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($di); |
23
|
|
|
$this->form->create( |
24
|
|
|
[ |
25
|
|
|
"id" => __CLASS__, |
26
|
|
|
"legend" => "Delete an item", |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
"select" => [ |
30
|
|
|
"type" => "select", |
31
|
|
|
"label" => "Select item to delete:", |
32
|
|
|
"options" => $this->getAllItems(), |
33
|
|
|
], |
34
|
|
|
|
35
|
|
|
"submit" => [ |
36
|
|
|
"type" => "submit", |
37
|
|
|
"value" => "Delete item", |
38
|
|
|
"callback" => [$this, "callbackSubmit"] |
39
|
|
|
], |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** Method |
47
|
|
|
*/ |
48
|
|
|
protected function getAllItems() : array |
49
|
|
|
{ |
50
|
|
|
$question = new Question(); |
51
|
|
|
$question->setDb($this->di->get("dbqb")); |
52
|
|
|
|
53
|
|
|
$questions = ["-1" => "Select an item..."]; |
54
|
|
|
foreach ($question->findAll() as $obj) { |
55
|
|
|
$questions[$obj->id] = "{$obj->column1} ({$obj->id})"; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $questions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** Method |
64
|
|
|
*/ |
65
|
|
|
public function callbackSubmit() : bool |
66
|
|
|
{ |
67
|
|
|
$question = new Question(); |
68
|
|
|
$question->setDb($this->di->get("dbqb")); |
69
|
|
|
$question->find("id", $this->form->value("select")); |
70
|
|
|
$question->delete(); |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** Method |
77
|
|
|
*/ |
78
|
|
|
public function callbackSuccess() |
79
|
|
|
{ |
80
|
|
|
$this->di->get("response")->redirect("question")->send(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|