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 CreateCommentsForm 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 $qId the questionId. |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function __construct(ContainerInterface $di, $acronym, $qId) |
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
|
|
|
"questionId" => [ |
37
|
|
|
"type" => "hidden", |
38
|
|
|
"value" => $qId, |
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", |
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
|
|
|
$qId = $this->form->value("questionId"); |
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->questionId = $qId; |
87
|
|
|
$res->created = $created; |
88
|
|
|
$res->comment = $res->changeCharacter($comment); |
89
|
|
|
$res->points = intval($points); |
90
|
|
|
// var_dump("Hello"); |
91
|
|
|
// var_dump($res); |
92
|
|
|
$res->saveComment($this->di); |
93
|
|
|
|
94
|
|
|
$this->form->addOutput("Comment was created."); |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|