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