1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \Anax\Comment\Comment; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A controller for the Comment module |
11
|
|
|
* |
12
|
|
|
* @SuppressWarnings(PHPMD.ExitExpression) |
13
|
|
|
*/ |
14
|
|
|
class CommentController implements InjectionAwareInterface |
15
|
|
|
{ |
16
|
|
|
use InjectionAwareTrait; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
//private $started = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Start the session. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
/* |
27
|
|
|
public function start() |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
if (session_status() == PHP_SESSION_NONE) { |
31
|
|
|
$this->di->get("session")->start(); |
32
|
|
|
} |
33
|
|
|
if (!$this->di->get("comment")->hasComments()) { |
34
|
|
|
$this->di->get("comment")->init(); |
35
|
|
|
} |
36
|
|
|
$this->started = true; |
37
|
|
|
} |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Destroy the session. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
/* |
47
|
|
|
public function destroy() |
48
|
|
|
{ |
49
|
|
|
$this->di->get("session")->destroy(); |
50
|
|
|
exit; |
51
|
|
|
}*/ |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new item by getting the entry from the request body and add |
57
|
|
|
* to the dataset. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function postComment() |
62
|
|
|
{ |
63
|
|
|
$session = $this->di->get("session"); |
64
|
|
|
$comment = new Comment(); |
65
|
|
|
$comment->setDb($this->di->get("db")); |
66
|
|
|
$username = $session->get("username"); |
67
|
|
|
$content = getPost("comment"); |
68
|
|
|
$gravatar = "https://www.gravatar.com/avatar/" . $comment->convertEmail($session->get("email")); |
69
|
|
|
$comment->addComment($username, $content, $gravatar); |
70
|
|
|
$this->di->get("response")->redirect("comment"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get comments |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
View Code Duplication |
public function showComments() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
//$this->start(); |
82
|
|
|
$comment = new Comment(); |
83
|
|
|
$comment->setDb($this->di->get("db")); |
84
|
|
|
$this->di->get("view")->add("comment/comment", [ |
85
|
|
|
"comments" => $comment->findAll() |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
// Render a standard page using layout |
89
|
|
|
$this->di->get("pageRender")->renderPage([ |
90
|
|
|
"title" => "Comment", |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Edit a post |
96
|
|
|
* |
97
|
|
|
* @param string $postId for the post to edit |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
|
102
|
|
View Code Duplication |
public function showEdit($postId) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$comment = new Comment(); |
105
|
|
|
$comment->setDb($this->di->get("db")); |
106
|
|
|
$this->di->get("view")->add("comment/edit", [ |
107
|
|
|
"values" => $comment->find("id", $postId) |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
// Render a standard page using layout |
111
|
|
|
$this->di->get("pageRender")->renderPage([ |
112
|
|
|
"title" => "Edit", |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Save the edited post |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
|
122
|
|
|
public function saveEdit() |
123
|
|
|
{ |
124
|
|
|
$session = $this->di->get("session"); |
125
|
|
|
$comment = new Comment(); |
126
|
|
|
$comment->setDb($this->di->get("db")); |
127
|
|
|
$id = getPost("id"); |
128
|
|
|
$user = getPost("user"); |
129
|
|
|
$content = getPost("comment"); |
130
|
|
|
$gravatar = "https://www.gravatar.com/avatar/" . $comment->convertEmail($session->get("email")); |
131
|
|
|
$comment->updateComment($id, $user, $content, $gravatar); |
132
|
|
|
$this->di->get("response")->redirect("comment"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Delete a post |
137
|
|
|
* |
138
|
|
|
* @param string $postId for the post to delete |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
|
143
|
|
|
public function deletePost($postId) |
144
|
|
|
{ |
145
|
|
|
$comment = new Comment(); |
146
|
|
|
$comment->setDb($this->di->get("db")); |
147
|
|
|
$comment->deletePost($postId); |
148
|
|
|
$this->di->get("response")->redirect("comment"); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.