1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alvo\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Alvo\Comment\Comment; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class EditCommentForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
|
|
|
18
|
|
|
*/ |
19
|
2 |
|
public function __construct(DIInterface $di, $id) |
20
|
|
|
{ |
21
|
2 |
|
parent::__construct($di); |
22
|
|
|
|
23
|
2 |
|
$comment = new Comment(); |
24
|
2 |
|
$comment->setDb($di->get("db")); |
25
|
2 |
|
$this->comment = $comment->getComment(null, $id); |
|
|
|
|
26
|
|
|
// debug($id); |
27
|
|
|
|
28
|
2 |
|
$this->form->create( |
29
|
|
|
[ |
30
|
2 |
|
"id" => __CLASS__, |
31
|
|
|
"class" => "mb-2", |
32
|
|
|
"wrapper-element" => "div", |
33
|
|
|
"use_fieldset" => false, |
34
|
|
|
], |
35
|
|
|
[ |
36
|
2 |
|
"heading" => [ |
37
|
2 |
|
"type" => "text", |
38
|
2 |
|
"class" => "form-control", |
39
|
2 |
|
"value" => $comment->heading, |
40
|
|
|
"validation" => [ |
41
|
|
|
"not_empty" |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
|
45
|
|
|
"text" => [ |
46
|
2 |
|
"type" => "textarea", |
47
|
2 |
|
"class" => "form-control", |
48
|
2 |
|
"value" => $comment->text, |
49
|
|
|
"validation" => [ |
50
|
|
|
"not_empty" |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
|
54
|
|
|
"tags" => [ |
55
|
2 |
|
"label" => "Space separated tags", |
56
|
2 |
|
"type" => "text", |
57
|
2 |
|
"class" => "form-control", |
58
|
2 |
|
"value" => $comment->tags, |
59
|
|
|
"validation" => [ |
60
|
|
|
"not_empty" |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
|
64
|
|
|
"submit" => [ |
65
|
2 |
|
"type" => "submit", |
66
|
2 |
|
"value" => "Update comment", |
67
|
2 |
|
"callback" => [$this, "callbackSubmit"], |
68
|
2 |
|
"class" => "btn btn-success" |
69
|
|
|
], |
70
|
|
|
] |
71
|
|
|
); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Callback for submit-button which should return true if it could |
78
|
|
|
* carry out its work and false if something failed. |
79
|
|
|
* |
80
|
|
|
* @return boolean true if okey, false if something went wrong. |
81
|
|
|
*/ |
82
|
1 |
|
public function callbackSubmit() |
83
|
|
|
{ |
84
|
|
|
// Get values from the submitted form |
85
|
1 |
|
$heading = $this->form->value("heading"); |
86
|
1 |
|
$text = $this->form->value("text"); |
87
|
1 |
|
$tags = $this->form->value("tags"); |
88
|
|
|
|
89
|
1 |
|
$this->comment->setDb($this->di->get("db")); |
90
|
1 |
|
$this->comment->heading = $heading; |
91
|
1 |
|
$this->comment->text = $text; |
92
|
1 |
|
$this->comment->tags = $tags; |
93
|
1 |
|
$this->comment->updated = date("Y-m-d H:i:s"); |
|
|
|
|
94
|
1 |
|
$this->comment->save(); |
95
|
|
|
|
96
|
1 |
|
$this->form->addOutput("Comment was updated"); |
97
|
|
|
|
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|