1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radchasay\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Radchasay\Comment\Comment; |
8
|
|
|
use \Radchasay\User\User; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Example of FormModel implementation. |
12
|
|
|
*/ |
13
|
|
|
class CreateCommentForm extends FormModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor injects with DI container. |
17
|
|
|
* |
18
|
|
|
* @param Anax\DI\DIInterface $di a service container |
19
|
|
|
*/ |
20
|
2 |
View Code Duplication |
public function __construct(DIInterface $di, $id) |
|
|
|
|
21
|
|
|
{ |
22
|
2 |
|
parent::__construct($di); |
23
|
|
|
|
24
|
2 |
|
$this->form->create( |
25
|
|
|
[ |
26
|
2 |
|
"id" => __CLASS__, |
27
|
2 |
|
"legend" => "Create new comment", |
28
|
2 |
|
], |
29
|
1 |
|
[ |
30
|
|
|
"text" => [ |
31
|
|
|
"type" => "textarea" |
32
|
2 |
|
], |
33
|
|
|
|
34
|
|
|
"hidden" => [ |
35
|
2 |
|
"type" => "hidden", |
36
|
2 |
|
"value" => $id, |
37
|
2 |
|
], |
38
|
|
|
|
39
|
|
|
"submit" => [ |
40
|
2 |
|
"type" => "submit", |
41
|
2 |
|
"value" => "Create comment", |
42
|
2 |
|
"callback" => [$this, "callbackSubmit"], |
43
|
2 |
|
], |
44
|
|
|
] |
45
|
2 |
|
); |
46
|
2 |
|
} |
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 boolean true if okey, false if something went wrong. |
53
|
|
|
*/ |
54
|
|
|
public function callbackSubmit() |
55
|
|
|
{ |
56
|
|
|
// Get values from the submitted form |
57
|
|
|
|
58
|
|
|
$comment = new Comment(); |
59
|
|
|
$comment->setDB($this->di->get("db")); |
60
|
|
|
$data = $this->form->value("text"); |
61
|
|
|
$text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]); |
62
|
|
|
$comment->commenttext = $text; |
63
|
|
|
$comment->idpost = $this->form->value("hidden"); |
64
|
|
|
$comment->postuser = $this->di->get("session")->get("email"); |
65
|
|
|
|
66
|
|
|
$user = new User(); |
67
|
|
|
$user->setDb($this->di->get("db")); |
68
|
|
|
$user->getInformation($comment->postuser); |
69
|
|
|
$user->points += 1; |
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
$comment->save(); |
73
|
|
|
$user->save(); |
74
|
|
|
|
75
|
|
|
$url = $this->di->get("url")->create("comment/retrieve/$comment->idpost"); |
76
|
|
|
$this->di->get("response")->redirect($url); |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.