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