1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Almrooth\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Almrooth\Comment\User; |
8
|
|
|
use \Almrooth\Comment\Comment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Example of FormModel implementation. |
12
|
|
|
*/ |
13
|
|
|
class AddCommentForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Anax\DI\DIInterface $di a service container |
19
|
|
|
*/ |
20
|
1 |
|
public function __construct(DIInterface $di) |
21
|
|
|
{ |
22
|
1 |
|
parent::__construct($di); |
23
|
|
|
|
24
|
1 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
1 |
|
"id" => __CLASS__, |
27
|
1 |
|
"use_fieldset" => false, |
28
|
1 |
|
], |
29
|
1 |
|
[ |
30
|
|
|
"content" => [ |
31
|
1 |
|
"type" => "textarea", |
32
|
1 |
|
"label" => "Kommentar", |
33
|
1 |
|
"validation" => ["not_empty"], |
34
|
1 |
|
], |
35
|
|
|
|
36
|
|
|
"submit" => [ |
37
|
1 |
|
"type" => "submit", |
38
|
1 |
|
"value" => "Spara kommentar", |
39
|
1 |
|
"callback" => [$this, "callbackSubmit"], |
40
|
|
|
"class" => "btn" |
41
|
1 |
|
], |
42
|
|
|
] |
43
|
1 |
|
); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Callback for submit-button which should return true if it could |
49
|
|
|
* carry out its work and false if something failed. |
50
|
|
|
* |
51
|
|
|
* @return boolean true if okey, false if something went wrong. |
52
|
|
|
*/ |
53
|
|
|
public function callbackSubmit() |
54
|
|
|
{ |
55
|
|
|
// Get current user |
56
|
|
|
$user = new User(); |
57
|
|
|
$user->setDb($this->di->get("db")); |
58
|
|
|
$user->find("id", $this->di->get("session")->get("user_id")); |
59
|
|
|
|
60
|
|
|
// Create new comment |
61
|
|
|
$comment = new Comment(); |
62
|
|
|
$comment->setDb($this->di->get("db")); |
63
|
|
|
$comment->user_id = $user->id; |
64
|
|
|
$comment->content = $this->form->value("content"); |
65
|
|
|
$comment->save(); |
66
|
|
|
|
67
|
|
|
// Redirect to profile page |
68
|
|
|
$this->di->get("response")->redirectSelf(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|