1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vihd14\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Vihd14\Comment\Comment; |
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 Anax\DI\DIInterface $di a service container |
18
|
|
|
*/ |
19
|
|
|
public function __construct(DIInterface $di, $id) |
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" => "", |
31
|
|
|
"options" => $this->getItem($id), |
32
|
|
|
], |
33
|
|
|
|
34
|
|
|
"submit" => [ |
35
|
|
|
"type" => "submit", |
36
|
|
|
"options" => $this->getItem($id), |
37
|
|
|
"value" => "Delete", |
38
|
|
|
"callback" => [$this, "callbackSubmit"] |
39
|
|
|
], |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all items as array suitable for display in select option dropdown. |
48
|
|
|
* |
49
|
|
|
* @return array with key value of all items. |
50
|
|
|
*/ |
51
|
|
|
protected function getItem($id) |
52
|
|
|
{ |
53
|
|
|
$comment = new Comment(); |
54
|
|
|
$comment->setDb($this->di->get("db")); |
55
|
|
|
|
56
|
|
|
$comments = ["-1" => "Choose an object"]; |
57
|
|
|
|
58
|
|
|
$obj = $comment->find("id", $id); |
59
|
|
|
$comments[$obj->id] = "{$obj->email} ({$obj->id})"; |
60
|
|
|
|
61
|
|
|
// foreach ($comment->find("id", $id) as $obj) { |
62
|
|
|
// $comments[$obj->id] = "{$obj->email} ({$obj->id})"; |
63
|
|
|
// } |
64
|
|
|
|
65
|
|
|
return $comments; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Callback for submit-button which should return true if it could |
72
|
|
|
* carry out its work and false if something failed. |
73
|
|
|
* |
74
|
|
|
* @return boolean true if okey, false if something went wrong. |
75
|
|
|
*/ |
76
|
|
|
public function callbackSubmit() |
77
|
|
|
{ |
78
|
|
|
$comment = new Comment(); |
79
|
|
|
$comment->setDb($this->di->get("db")); |
80
|
|
|
$comment->find("id", $this->form->value("select")); |
81
|
|
|
$comment->delete(); |
82
|
|
|
$this->di->get("response")->redirect("comments"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|