1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seb\QuestionComments\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Seb\QuestionComments\QuestionComments; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to create an item. |
11
|
|
|
*/ |
12
|
|
|
class CreateQuestionCommentsForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
public $qid; |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
19
|
|
|
*/ |
20
|
2 |
|
public function __construct(ContainerInterface $di, int $id) |
21
|
|
|
{ |
22
|
2 |
|
$this->qid = $id; |
23
|
2 |
|
parent::__construct($di); |
24
|
2 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
2 |
|
"id" => __CLASS__, |
27
|
|
|
"legend" => "Create Comment", |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
|
31
|
|
|
"questionid" => [ |
32
|
2 |
|
"type" => "hidden", |
33
|
|
|
"validation" => ["not_empty"], |
34
|
|
|
"readonly" => true, |
35
|
2 |
|
"value" => $id, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"comment" => [ |
39
|
|
|
"type" => "textarea", |
40
|
|
|
"validation" => ["not_empty"], |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
"submit" => [ |
44
|
2 |
|
"type" => "submit", |
45
|
2 |
|
"value" => "Create item", |
46
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
47
|
|
|
], |
48
|
|
|
] |
49
|
|
|
); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Callback for submit-button which should return true if it could |
54
|
|
|
* carry out its work and false if something failed. |
55
|
|
|
* |
56
|
|
|
* @return bool true if okey, false if something went wrong. |
57
|
|
|
*/ |
58
|
|
|
public function callbackSubmit() : bool |
59
|
|
|
{ |
60
|
|
|
$session = $this->di->get("session"); |
61
|
|
|
$comments = new QuestionComments(); |
62
|
|
|
$comments->setDb($this->di->get("dbqb")); |
63
|
|
|
$comments->acronym = $session->get("acronym"); |
64
|
|
|
$comments->userid = $session->get("userid"); |
65
|
|
|
$comments->comment = $this->form->value("comment"); |
66
|
|
|
$comments->questionid = $this->form->value("questionid"); |
67
|
|
|
$comments->save(); |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Callback what to do if the form was successfully submitted, this |
73
|
|
|
* happen when the submit callback method returns true. This method |
74
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
75
|
|
|
*/ |
76
|
|
|
public function callbackSuccess() |
77
|
|
|
{ |
78
|
|
|
$this->di->get("response")->redirect("forum/topic/{$this->qid}")->send(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|