1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pamo\Question\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Pamo\Question\Question; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to delete an item. |
11
|
|
|
*/ |
12
|
|
|
class DeleteQuestionForm 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, $id) |
20
|
|
|
{ |
21
|
1 |
|
parent::__construct($di); |
22
|
1 |
|
$question = $this->getItemDetails($id); |
23
|
1 |
|
$this->questionid = $question->id; |
|
|
|
|
24
|
|
|
|
25
|
1 |
|
$this->form->create( |
26
|
|
|
[ |
27
|
1 |
|
"id" => __CLASS__, |
28
|
|
|
"legend" => "Delete an item", |
29
|
|
|
], |
30
|
|
|
[ |
31
|
1 |
|
"id" => [ |
32
|
1 |
|
"type" => "hidden", |
33
|
1 |
|
"value" => $question->id, |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
"title" => [ |
37
|
1 |
|
"type" => "text", |
38
|
|
|
"readonly" => true, |
39
|
|
|
"label" => false, |
40
|
1 |
|
"value" => "$question->title" |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
"text" => [ |
44
|
1 |
|
"type" => "textarea", |
45
|
|
|
"readonly" => true, |
46
|
|
|
"label" => false, |
47
|
1 |
|
"value" => "$question->text" |
48
|
|
|
], |
49
|
|
|
|
50
|
|
|
"submit" => [ |
51
|
1 |
|
"type" => "submit", |
52
|
1 |
|
"value" => "Delete question", |
53
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
"cancel" => [ |
57
|
1 |
|
"type" => "button", |
58
|
1 |
|
"value" => "Cancel", |
59
|
1 |
|
"onclick" => "location.href='$this->questionid';" |
60
|
|
|
], |
61
|
|
|
] |
62
|
|
|
); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all items. |
69
|
|
|
* |
70
|
|
|
* @return object with key value of all items. |
71
|
|
|
*/ |
72
|
1 |
|
protected function getItemDetails($id) : object |
73
|
|
|
{ |
74
|
1 |
|
$question = new Question(); |
75
|
1 |
|
$question->setDb($this->di->get("dbqb")); |
76
|
1 |
|
$question->find("id", $id); |
77
|
1 |
|
return $question; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
* @return null |
85
|
|
|
*/ |
86
|
1 |
|
public function deleteAnswers($id) |
87
|
|
|
{ |
88
|
1 |
|
$game = $this->di->get("game"); |
89
|
1 |
|
$answer = $game->answer; |
90
|
1 |
|
$answersToDelete = $answer->findAllWhere("questionid = ?", $id); |
91
|
|
|
|
92
|
1 |
|
foreach ($answersToDelete as $ans) { |
93
|
1 |
|
$answer->find("id", $ans->id); |
94
|
1 |
|
$answer->delete(); |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @return null |
103
|
|
|
*/ |
104
|
1 |
|
public function deleteAnswerComments($id) |
105
|
|
|
{ |
106
|
1 |
|
$game = $this->di->get("game"); |
107
|
1 |
|
$commentAnswer = $game->commentAnswer; |
108
|
1 |
|
$commentsToDelete = $commentAnswer->findAllWhere("questionid = ?", $id); |
109
|
|
|
|
110
|
1 |
|
foreach ($commentsToDelete as $comment) { |
111
|
1 |
|
$commentAnswer->find("id", $comment->id); |
112
|
1 |
|
$commentAnswer->delete(); |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @return null |
121
|
|
|
*/ |
122
|
1 |
|
public function deleteQuestionComments($id) |
123
|
|
|
{ |
124
|
1 |
|
$game = $this->di->get("game"); |
125
|
1 |
|
$commentQuestion = $game->commentQuestion; |
126
|
1 |
|
$commentsToDelete = $commentQuestion->findAllWhere("questionid = ?", $id); |
127
|
|
|
|
128
|
1 |
|
foreach ($commentsToDelete as $comment) { |
129
|
1 |
|
$commentQuestion->find("id", $comment->id); |
130
|
1 |
|
$commentQuestion->delete(); |
131
|
|
|
} |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @return null |
139
|
|
|
*/ |
140
|
1 |
|
public function deleteQuestionTags($id) |
141
|
|
|
{ |
142
|
1 |
|
$game = $this->di->get("game"); |
143
|
1 |
|
$tagQuestion = $game->tagQuestion; |
144
|
1 |
|
$tagsToDelete = $tagQuestion->findAllWhere("questionid = ?", $id); |
145
|
|
|
|
146
|
1 |
|
foreach ($tagsToDelete as $tag) { |
147
|
1 |
|
$tagQuestion->find("id", $tag->id); |
148
|
1 |
|
$tagQuestion->delete(); |
149
|
|
|
} |
150
|
1 |
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Callback for submit-button which should return true if it could |
156
|
|
|
* carry out its work and false if something failed. |
157
|
|
|
* |
158
|
|
|
* @return bool true if okey, false if something went wrong. |
159
|
|
|
*/ |
160
|
1 |
|
public function callbackSubmit() : bool |
161
|
|
|
{ |
162
|
1 |
|
$id = $this->form->value("id"); |
163
|
|
|
|
164
|
|
|
// First, delete all comments connected to question |
165
|
1 |
|
$this->deleteQuestionComments($id); |
166
|
1 |
|
$this->deleteAnswerComments($id); |
167
|
|
|
|
168
|
|
|
// Second, delete all answers connected to question |
169
|
1 |
|
$this->deleteAnswers($id); |
170
|
|
|
|
171
|
|
|
// Third, keep all tags but delete all links between tags and the question question |
172
|
1 |
|
$this->deleteQuestionTags($id); |
173
|
|
|
|
174
|
|
|
// Last, delete the actual question |
175
|
1 |
|
$question = new Question(); |
176
|
1 |
|
$question->setDb($this->di->get("dbqb")); |
177
|
1 |
|
$question->find("id", $id); |
178
|
1 |
|
$question->delete(); |
179
|
1 |
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Callback what to do if the form was successfully submitted, this |
186
|
|
|
* happen when the submit callback method returns true. This method |
187
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
188
|
|
|
*/ |
189
|
1 |
|
public function callbackSuccess() |
190
|
|
|
{ |
191
|
1 |
|
$this->di->get("response")->redirect("question")->send(); |
192
|
1 |
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
// /** |
197
|
|
|
// * Callback what to do if the form was unsuccessfully submitted, this |
198
|
|
|
// * happen when the submit callback method returns false or if validation |
199
|
|
|
// * fails. This method can/should be implemented by the subclass for a |
200
|
|
|
// * different behaviour. |
201
|
|
|
// */ |
202
|
|
|
// public function callbackFail() |
203
|
|
|
// { |
204
|
|
|
// $this->di->get("response")->redirect("game/question/$this->questionid")->send(); |
205
|
|
|
// } |
206
|
|
|
} |
207
|
|
|
|