|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alfs18\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use Alfs18\User\Comments; |
|
6
|
|
|
use Anax\HTMLForm\FormModel; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example of FormModel implementation. |
|
11
|
|
|
*/ |
|
12
|
|
|
class CreateAnswerCommentsForm extends FormModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor injects with DI container. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
|
|
18
|
|
|
* @param $acronym the name of the one who posted the comment. |
|
|
|
|
|
|
19
|
|
|
* @param $aId the answerId. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(ContainerInterface $di, $acronym, $aId) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($di); |
|
24
|
|
|
$this->form->create( |
|
25
|
|
|
[ |
|
26
|
|
|
"id" => __CLASS__, |
|
27
|
|
|
// "legend" => "Kommentera", |
|
28
|
|
|
"class" => "comments", |
|
29
|
|
|
], |
|
30
|
|
|
[ |
|
31
|
|
|
"acronym" => [ |
|
32
|
|
|
"type" => "hidden", |
|
33
|
|
|
"value" => $acronym, |
|
34
|
|
|
], |
|
35
|
|
|
|
|
36
|
|
|
"answerId" => [ |
|
37
|
|
|
"type" => "hidden", |
|
38
|
|
|
"value" => $aId, |
|
39
|
|
|
], |
|
40
|
|
|
|
|
41
|
|
|
"created" => [ |
|
42
|
|
|
"type" => "hidden", |
|
43
|
|
|
"value" => date("d M Y, H:i"), |
|
44
|
|
|
], |
|
45
|
|
|
|
|
46
|
|
|
"comment" => [ |
|
47
|
|
|
"type" => "text", |
|
48
|
|
|
"placeholder" => "Kommentera svar", |
|
49
|
|
|
], |
|
50
|
|
|
|
|
51
|
|
|
"points" => [ |
|
52
|
|
|
"type" => "hidden", |
|
53
|
|
|
"value" => 0, |
|
54
|
|
|
], |
|
55
|
|
|
|
|
56
|
|
|
"submit" => [ |
|
57
|
|
|
"type" => "submit", |
|
58
|
|
|
"value" => "Skicka", |
|
59
|
|
|
"callback" => [$this, "callbackSubmit"] |
|
60
|
|
|
], |
|
61
|
|
|
] |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Callback for submit-button which should return true if it could |
|
69
|
|
|
* carry out its work and false if something failed. |
|
70
|
|
|
* |
|
71
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function callbackSubmit() |
|
74
|
|
|
{ |
|
75
|
|
|
// Get values from the submitted form |
|
76
|
|
|
$acronym = $this->form->value("acronym"); |
|
77
|
|
|
$aId = $this->form->value("answerId"); |
|
78
|
|
|
$created = $this->form->value("created"); |
|
79
|
|
|
$comment = $this->form->value("comment"); |
|
80
|
|
|
$points = $this->form->value("points"); |
|
81
|
|
|
|
|
82
|
|
|
// Save to database |
|
83
|
|
|
$res = new Comments(); |
|
84
|
|
|
$res->setDb($this->di->get("dbqb")); |
|
85
|
|
|
$res->acronym = $acronym; |
|
86
|
|
|
$res->answerId = $aId; |
|
87
|
|
|
$res->created = $created; |
|
88
|
|
|
$res->comment = $res->changeCharacter($comment); |
|
89
|
|
|
$res->points = intval($points); |
|
90
|
|
|
$res->saveAnswerComment($this->di); |
|
91
|
|
|
|
|
92
|
|
|
$this->form->addOutput("Comment was created."); |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|