|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Forum\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Anax\Forum\Answer; |
|
8
|
|
|
use Anax\Forum\Forum; |
|
9
|
|
|
use Anax\Forum\Comment; |
|
10
|
|
|
use Anax\Forum\AnswerComment; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Example of FormModel implementation. |
|
14
|
|
|
*/ |
|
15
|
|
|
class CommentAnswerForm extends FormModel |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor injects with DI container and the id to update. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
|
|
21
|
|
|
* @param integer $id to update |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(ContainerInterface $di, $id) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($di); |
|
26
|
|
|
$answer = $this->getItemDetails($id); |
|
27
|
|
|
$session = $this->di->get("session"); |
|
28
|
|
|
$user = $session->get("login"); |
|
29
|
|
|
$this->form->create( |
|
30
|
|
|
[ |
|
31
|
|
|
"id" => __CLASS__, |
|
32
|
|
|
"legend" => "Post a comment", |
|
33
|
|
|
], |
|
34
|
|
|
[ |
|
35
|
|
|
"comment" => [ |
|
36
|
|
|
"type" => "textarea", |
|
37
|
|
|
"placeholder" => "Write the comment in markdown to change how it looks.", |
|
38
|
|
|
], |
|
39
|
|
|
|
|
40
|
|
|
"answer" => [ |
|
41
|
|
|
"type" => "text", |
|
42
|
|
|
"value" => $answer->id, |
|
43
|
|
|
"readonly" => "readonly", |
|
44
|
|
|
], |
|
45
|
|
|
|
|
46
|
|
|
"user" => [ |
|
47
|
|
|
"type" => "text", |
|
48
|
|
|
"value" => $user, |
|
49
|
|
|
"readonly" => "readonly", |
|
50
|
|
|
], |
|
51
|
|
|
|
|
52
|
|
|
"submit" => [ |
|
53
|
|
|
"type" => "submit", |
|
54
|
|
|
"value" => "Answer", |
|
55
|
|
|
"callback" => [$this, "callbackSubmit"] |
|
56
|
|
|
], |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get details on item to load form with. |
|
65
|
|
|
* |
|
66
|
|
|
* @param integer $id get details on item with id. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Forum |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getItemDetails($id) : object |
|
71
|
|
|
{ |
|
72
|
|
|
$answer = new Answer(); |
|
73
|
|
|
$answer->setDb($this->di->get("dbqb")); |
|
74
|
|
|
$answer->find("id", $id); |
|
75
|
|
|
return $answer; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Callback for submit-button which should return true if it could |
|
82
|
|
|
* carry out its work and false if something failed. |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool true if okey, false if something went wrong. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function callbackSubmit() |
|
87
|
|
|
{ |
|
88
|
|
|
// Get values from the submitted form |
|
89
|
|
|
$content = $this->form->value("comment"); |
|
90
|
|
|
$answer = $this->form->value("answer"); |
|
91
|
|
|
$user = $this->form->value("user"); |
|
92
|
|
|
$comment = new AnswerComment(); |
|
93
|
|
|
$comment->setDb($this->di->get("dbqb")); |
|
94
|
|
|
$comment->content = $content; |
|
95
|
|
|
$comment->answer_id = $answer; |
|
96
|
|
|
$comment->user = $user; |
|
|
|
|
|
|
97
|
|
|
$comment->save(); |
|
98
|
|
|
$this->form->addOutput("The comment has been published."); |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function callbackSuccess() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->di->get("response")->redirect("forum")->send(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|