1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CJ\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \CJ\Comment\Comment; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Form to create an item. |
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 |
View Code Duplication |
public function __construct(DIInterface $di, $comment) |
|
|
|
|
20
|
|
|
{ |
21
|
2 |
|
parent::__construct($di); |
22
|
2 |
|
$this->form->create( |
23
|
|
|
[ |
24
|
2 |
|
"id" => __CLASS__, |
25
|
2 |
|
], |
26
|
|
|
[ |
27
|
|
|
"title" => [ |
28
|
2 |
|
"type" => "text", |
29
|
2 |
|
"validation" => ["not_empty"], |
30
|
2 |
|
"class" => "form-control", |
31
|
2 |
|
"value" => "$comment->heading" |
32
|
2 |
|
], |
33
|
|
|
|
34
|
|
|
"message" => [ |
35
|
2 |
|
"type" => "textarea", |
36
|
2 |
|
"validation" => ["not_empty"], |
37
|
2 |
|
"class" => "form-control", |
38
|
2 |
|
"value" => "$comment->msg" |
39
|
2 |
|
], |
40
|
|
|
|
41
|
|
|
"id" => [ |
42
|
2 |
|
"type" => "hidden", |
43
|
2 |
|
"class" => "form-control", |
44
|
2 |
|
"value" => "$comment->id" |
45
|
2 |
|
], |
46
|
|
|
|
47
|
|
|
"submit" => [ |
48
|
2 |
|
"type" => "submit", |
49
|
2 |
|
"value" => "Comment", |
50
|
2 |
|
"callback" => [$this, "callbackSubmit"], |
51
|
|
|
"class" => "btn btn-default" |
52
|
2 |
|
], |
53
|
|
|
] |
54
|
2 |
|
); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return boolean true if okey, false if something went wrong. |
61
|
|
|
*/ |
62
|
1 |
|
public function callbackSubmit() |
63
|
|
|
{ |
64
|
1 |
|
$title = $this->form->value("title"); |
65
|
1 |
|
$message = $this->form->value("message"); |
66
|
1 |
|
$id = $this->form->value("id"); |
67
|
|
|
|
68
|
1 |
|
$comment = new Comment(); |
69
|
1 |
|
$comment->setDb($this->di->get("db")); |
70
|
|
|
|
71
|
1 |
|
$comment->find("id", $id); |
72
|
1 |
|
$comment->msg = $message; |
73
|
1 |
|
$comment->heading = $title; |
74
|
|
|
|
75
|
1 |
|
$comment->save(); |
76
|
|
|
|
77
|
1 |
|
$this->form->addOutput("Kommentar uppdaterad"); |
78
|
1 |
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.