1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pamo\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Pamo\Comment\CommentQ; |
8
|
|
|
use Pamo\Comment\CommentA; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Form to create an item. |
12
|
|
|
*/ |
13
|
|
|
class CreateCommentForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
19
|
|
|
*/ |
20
|
2 |
|
public function __construct(ContainerInterface $di, $type, $questionid, $answerid) |
21
|
|
|
{ |
22
|
2 |
|
parent::__construct($di); |
23
|
2 |
|
$this->user = $di->get("session")->get("user", null); |
|
|
|
|
24
|
2 |
|
$this->questionid = $questionid; |
|
|
|
|
25
|
2 |
|
$this->form->create( |
26
|
|
|
[ |
27
|
2 |
|
"id" => __CLASS__, |
28
|
|
|
"legend" => "Details of the item", |
29
|
|
|
], |
30
|
|
|
[ |
31
|
2 |
|
"type" => [ |
32
|
2 |
|
"type" => "hidden", |
33
|
2 |
|
"value" => $type, |
34
|
|
|
], |
35
|
|
|
|
36
|
|
|
"questionid" => [ |
37
|
2 |
|
"type" => "hidden", |
38
|
2 |
|
"value" => $questionid, |
39
|
|
|
], |
40
|
|
|
|
41
|
|
|
"answerid" => [ |
42
|
2 |
|
"type" => "hidden", |
43
|
2 |
|
"value" => $answerid, |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
"user" => [ |
47
|
2 |
|
"type" => "hidden", |
48
|
2 |
|
"value" => $this->user["username"], |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
"text" => [ |
52
|
|
|
"type" => "text", |
53
|
|
|
"label" => false |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
"submit" => [ |
57
|
2 |
|
"type" => "submit", |
58
|
2 |
|
"value" => "Post Comment", |
59
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
60
|
|
|
], |
61
|
|
|
|
62
|
|
|
"cancel" => [ |
63
|
2 |
|
"type" => "button", |
64
|
2 |
|
"value" => "Cancel", |
65
|
2 |
|
"onclick" => "location.href='$this->questionid';" |
66
|
|
|
], |
67
|
|
|
] |
68
|
|
|
); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Callback for submit-button which should return true if it could |
75
|
|
|
* carry out its work and false if something failed. |
76
|
|
|
* |
77
|
|
|
* @return bool true if okey, false if something went wrong. |
78
|
|
|
*/ |
79
|
2 |
|
public function callbackSubmit() : bool |
80
|
|
|
{ |
81
|
2 |
|
$type = $this->form->value("type"); |
82
|
2 |
|
$comment = $type === "question" ? new CommentQ() : new CommentA(); |
83
|
2 |
|
$comment->setDb($this->di->get("dbqb")); |
84
|
|
|
|
85
|
2 |
|
if ($type === "answer") { |
86
|
1 |
|
$comment->answerid = $this->form->value("answerid"); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$comment->questionid = $this->form->value("questionid"); |
90
|
2 |
|
$comment->user = $this->form->value("user"); |
91
|
2 |
|
$comment->text = $this->form->value("text"); |
92
|
2 |
|
$comment->vote = 0; |
93
|
2 |
|
$comment->save(); |
94
|
|
|
|
95
|
2 |
|
$game = $this->di->get("game"); |
96
|
2 |
|
$game->updateRank($comment->user, 5); |
97
|
|
|
|
98
|
2 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Callback what to do if the form was successfully submitted, this |
105
|
|
|
* happen when the submit callback method returns true. This method |
106
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
107
|
|
|
*/ |
108
|
2 |
|
public function callbackSuccess() |
109
|
|
|
{ |
110
|
2 |
|
$this->di->get("response")->redirect("game/question/$this->questionid")->send(); |
111
|
2 |
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
// /** |
116
|
|
|
// * Callback what to do if the form was unsuccessfully submitted, this |
117
|
|
|
// * happen when the submit callback method returns false or if validation |
118
|
|
|
// * fails. This method can/should be implemented by the subclass for a |
119
|
|
|
// * different behaviour. |
120
|
|
|
// */ |
121
|
|
|
// public function callbackFail() |
122
|
|
|
// { |
123
|
|
|
// $this->di->get("response")->redirectSelf()->send(); |
124
|
|
|
// } |
125
|
|
|
} |
126
|
|
|
|