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 update an item. |
12
|
|
|
*/ |
13
|
|
|
class UpdateCommentForm 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
|
2 |
|
public function __construct(ContainerInterface $di, $type, $commentId) |
22
|
|
|
{ |
23
|
2 |
|
parent::__construct($di); |
24
|
2 |
|
$comment = $this->getItemDetails($type, $commentId); |
25
|
|
|
|
26
|
2 |
|
$this->questionid = $comment->questionid; |
|
|
|
|
27
|
|
|
|
28
|
2 |
|
$this->form->create( |
29
|
|
|
[ |
30
|
2 |
|
"id" => __CLASS__, |
31
|
|
|
"legend" => "Update a comment", |
32
|
|
|
], |
33
|
|
|
[ |
34
|
2 |
|
"id" => [ |
35
|
2 |
|
"type" => "hidden", |
36
|
2 |
|
"value" => $comment->id, |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
"type" => [ |
40
|
2 |
|
"type" => "hidden", |
41
|
2 |
|
"value" => $type, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
"text" => [ |
45
|
2 |
|
"type" => "text", |
46
|
|
|
"label" => false, |
47
|
2 |
|
"value" => $comment->text |
48
|
|
|
], |
49
|
|
|
|
50
|
|
|
"submit" => [ |
51
|
2 |
|
"type" => "submit", |
52
|
2 |
|
"value" => "Save", |
53
|
2 |
|
"callback" => [$this, "callbackSubmit"] |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
"cancel" => [ |
57
|
2 |
|
"type" => "button", |
58
|
2 |
|
"value" => "Cancel", |
59
|
2 |
|
"onclick" => "location.href='$this->questionid';" |
60
|
|
|
], |
61
|
|
|
] |
62
|
|
|
); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get details on item to load form with. |
69
|
|
|
* |
70
|
|
|
* @param integer $id get details on item with id. |
71
|
|
|
* |
72
|
|
|
* @return $comment |
|
|
|
|
73
|
|
|
*/ |
74
|
2 |
|
public function getItemDetails($type, $commentId) : object |
75
|
|
|
{ |
76
|
2 |
|
$comment = $type === "question" ? new CommentQ() : new CommentA(); |
77
|
2 |
|
$comment->setDb($this->di->get("dbqb")); |
78
|
2 |
|
$comment->find("id", $commentId); |
79
|
2 |
|
return $comment; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Callback for submit-button which should return true if it could |
86
|
|
|
* carry out its work and false if something failed. |
87
|
|
|
* |
88
|
|
|
* @return bool true if okey, false if something went wrong. |
89
|
|
|
*/ |
90
|
2 |
|
public function callbackSubmit() : bool |
91
|
|
|
{ |
92
|
2 |
|
$type = $this->form->value("type"); |
93
|
2 |
|
$comment = $type === "question" ? new CommentQ() : new CommentA(); |
94
|
2 |
|
$comment->setDb($this->di->get("dbqb")); |
95
|
2 |
|
$comment->find("id", $this->form->value("id")); |
96
|
2 |
|
$comment->text = $this->form->value("text"); |
97
|
2 |
|
$comment->save(); |
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
|
|
|
|