1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mafd16\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A controller for the Comment System. |
10
|
|
|
* |
11
|
|
|
* @SuppressWarnings(PHPMD.ExitExpression) |
12
|
|
|
*/ |
13
|
|
|
//class CommentController implements AppInjectableInterface |
14
|
|
|
class CommentController implements InjectionAwareInterface |
15
|
|
|
{ |
16
|
|
|
use InjectionAwareTrait; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get ALL comments from an article. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function getComments() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$key = "comPage"; |
27
|
|
|
// Get comments from model |
28
|
|
|
$comments = $this->di->get("com")->getComments($key); |
29
|
|
|
// Add views to a specific region, add comments |
30
|
|
|
$this->di->get("view")->add("comment/index", ["comments"=>$comments], "main"); |
31
|
|
|
// Render a standard page using layout |
32
|
|
|
$this->di->get("pageRender")->renderPage(["title" => "Kommentarssystem"]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get ONE comment from an article. |
38
|
|
|
* |
39
|
|
|
* @param string $key for the article |
|
|
|
|
40
|
|
|
* @param int $id for the comment id |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
2 |
|
public function getComment($id) |
45
|
|
|
{ |
46
|
2 |
|
$comment = $this->di->get("com")->getComment($id); |
47
|
2 |
|
return $comment; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get ONE comment for editing. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function getCommentToEdit() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$id = $this->di->get("request")->getGet("id"); |
59
|
|
|
// Get the comment from Model. |
60
|
|
|
$comment = $this->di->get("com")->getComment($id); |
61
|
|
|
// Add views to a specific region |
62
|
|
|
$this->di->get("view")->add("comment/edit", ["comment"=>$comment], "main"); |
63
|
|
|
// Render a standard page using layout |
64
|
|
|
$this->di->get("pageRender")->renderPage([ |
65
|
|
|
"title" => "Redigera kommentar", |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Edit a comment. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function editComment() |
76
|
|
|
{ |
77
|
|
|
// Get post-variables |
78
|
|
|
$post = $this->di->get("request")->getPost(); |
79
|
|
|
// Instruct Model to edit comment: |
80
|
|
|
// Edited comment: |
81
|
|
|
$comment = [ |
82
|
|
|
"user_id" => $post["user_id"], |
83
|
|
|
"name" => $post["name"], |
84
|
|
|
"email" => $post["email"], |
85
|
|
|
"comment" => $post["comment"], |
86
|
|
|
"id" => $post["id"] |
87
|
|
|
]; |
88
|
|
|
$this->di->get("com")->updateComment($post["id"], $comment); |
89
|
|
|
// Send user back to comment page. |
90
|
|
|
$url = $this->di->get("url")->create("comment"); |
91
|
|
|
$this->di->get("response")->redirect($url); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Post a comment, with name and email. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function postComment() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
// Catch post variables |
104
|
|
|
$post = $this->di->get("request")->getPost(); |
105
|
|
|
// Instruct Model to add comment: |
106
|
|
|
$this->di->get("com")->addComment($post); |
107
|
|
|
// Send user back to comment page. |
108
|
|
|
$url = $this->di->get("url")->create("comment"); |
109
|
|
|
$this->di->get("response")->redirect($url); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Update old comment with new comment |
115
|
|
|
* |
116
|
|
|
* @param int $id id for comment |
117
|
|
|
* @param array $comment the comment-array (name, email, comment, id) |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
1 |
|
public function updateComment($id, $comment) |
122
|
|
|
{ |
123
|
1 |
|
$this->di->get("com")->updateComment($id, $comment); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Delete comment with id |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function deleteComment() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
// Get id-variable from request. |
135
|
|
|
$id = $this->di->get("request")->getGet("id"); |
136
|
|
|
// Instruct Model to delete comment: |
137
|
|
|
$this->di->get("com")->deleteComment($id); |
138
|
|
|
// Send user back to comment page. |
139
|
|
|
$url = $this->di->get("url")->create("comment"); |
140
|
|
|
$this->di->get("response")->redirect($url); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.