1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lyco\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Lyco\Post\Post; |
8
|
|
|
use Lyco\User\User; |
9
|
|
|
use Lyco\Comment\Comment; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Form to create an item. |
13
|
|
|
*/ |
14
|
|
|
class CreateForm extends FormModel |
15
|
|
|
{ |
16
|
|
|
public $postId; |
17
|
|
|
public $id; |
18
|
|
|
/** |
19
|
|
|
* Constructor injects with DI container. |
20
|
|
|
* |
21
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function __construct(ContainerInterface $di, $postId) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($di); |
26
|
|
|
$this->postId = $postId; |
27
|
|
|
$this->replyId = $this->di->get("request")->getGet("replyId"); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->form->create( |
30
|
|
|
[ |
31
|
|
|
"commentId" => __CLASS__ |
32
|
|
|
], |
33
|
|
|
[ |
34
|
|
|
"comment" => [ |
35
|
|
|
"type" => "textarea", |
36
|
|
|
"validation" => ["not_empty"], |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
"submit" => [ |
40
|
|
|
"type" => "submit", |
41
|
|
|
"value" => "Send", |
42
|
|
|
"callback" => [$this, "callbackSubmit"] |
43
|
|
|
], |
44
|
|
|
] |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for submit-button which should return true if it could |
50
|
|
|
* carry out its work and false if something failed. |
51
|
|
|
* |
52
|
|
|
* @return bool true if okey, false if something went wrong. |
53
|
|
|
*/ |
54
|
|
|
public function callbackSubmit() : bool |
55
|
|
|
{ |
56
|
|
|
$comment = new Comment(); |
57
|
|
|
$comment->setDb($this->di->get("dbqb")); |
58
|
|
|
$comment->postId = $this->postId; |
59
|
|
|
$comment->replyId = $this->replyId; |
60
|
|
|
$comment->id = $this->di->get("session")->get("userId"); |
61
|
|
|
$comment->text = $this->form->value("comment"); |
62
|
|
|
$comment->save(); |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Callback what to do if the form was successfully submitted, this |
68
|
|
|
* happen when the submit callback method returns true. This method |
69
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
70
|
|
|
*/ |
71
|
|
|
public function callbackSuccess() |
72
|
|
|
{ |
73
|
|
|
$this->di->get("response")->redirect("post/view/" . $this->postId)->send(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|