1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Almrooth\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\Configure\ConfigureInterface; |
6
|
|
|
use \Anax\Configure\ConfigureTrait; |
7
|
|
|
use \Anax\DI\InjectionAwareInterface; |
8
|
|
|
use \Anax\DI\InjectionAwareTrait; |
9
|
|
|
|
10
|
|
|
use \Almrooth\Comment\HTMLForm\LoginForm; |
11
|
|
|
use \Almrooth\Comment\HTMLForm\RegisterForm; |
12
|
|
|
use \Almrooth\Comment\HTMLForm\AddCommentForm; |
13
|
|
|
use \Almrooth\Comment\HTMLForm\UpdateCommentForm; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A controller class. |
17
|
|
|
*/ |
18
|
|
|
class CommentController implements |
19
|
|
|
ConfigureInterface, |
20
|
|
|
InjectionAwareInterface |
21
|
|
|
{ |
22
|
|
|
use ConfigureTrait, |
23
|
|
|
InjectionAwareTrait; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var $data description |
28
|
|
|
*/ |
29
|
|
|
//private $data; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function checkLogin() |
33
|
|
|
{ |
34
|
|
|
if (!$this->di->get("session")->get("user_id")) { |
35
|
|
|
$this->di->get("response")->redirect("user/login"); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
View Code Duplication |
public function getPostIndex() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$title = "Alla kommentarer"; |
43
|
|
|
$view = $this->di->get("view"); |
44
|
|
|
$pageRender = $this->di->get("pageRender"); |
45
|
|
|
$comment = new Comment(); |
46
|
|
|
$comment->setDb($this->di->get("db")); |
47
|
|
|
|
48
|
|
|
$form = new AddCommentForm($this->di); |
49
|
|
|
|
50
|
|
|
$form->check(); |
51
|
|
|
|
52
|
|
|
$data = [ |
53
|
|
|
"comments" => $comment->getAll(), |
54
|
|
|
"form" => $form->getHTML(), |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$view->add("comment/index", $data); |
58
|
|
|
|
59
|
|
|
$pageRender->renderPage(["title" => $title]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function getComment($id) |
64
|
|
|
{ |
65
|
|
|
$title = "Redigera kommentar"; |
66
|
|
|
$view = $this->di->get("view"); |
67
|
|
|
$pageRender = $this->di->get("pageRender"); |
68
|
|
|
|
69
|
|
|
$comment = new Comment(); |
70
|
|
|
$comment->setDb($this->di->get("db")); |
71
|
|
|
$comment->get($id); |
72
|
|
|
|
73
|
|
|
$data = [ |
74
|
|
|
"comment" => $comment, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$view->add("comment/comment", $data); |
78
|
|
|
|
79
|
|
|
$pageRender->renderPage(["title" => $title]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function getPostUpdate($id) |
84
|
|
|
{ |
85
|
|
|
$title = "Redigera kommentar"; |
86
|
|
|
$view = $this->di->get("view"); |
87
|
|
|
$pageRender = $this->di->get("pageRender"); |
88
|
|
|
$form = new UpdateCommentForm($this->di, $id); |
89
|
|
|
|
90
|
|
|
$form->check(); |
91
|
|
|
|
92
|
|
|
$data = [ |
93
|
|
|
"form" => $form->getHTML(), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$view->add("comment/update", $data); |
97
|
|
|
|
98
|
|
|
$pageRender->renderPage(["title" => $title]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
public function getDelete($id) |
103
|
|
|
{ |
104
|
|
|
$comment = new Comment(); |
105
|
|
|
$comment->setDb($this->di->get("db")); |
106
|
|
|
$comment->find("id", $id); |
107
|
|
|
|
108
|
|
|
// If logged in user is owner of comment or admin then delete |
109
|
|
|
$currentId = $this->di->get("session")->get("user_id"); |
110
|
|
|
$currentRole = $this->di->get("session")->get("user_role"); |
111
|
|
|
if ($currentId === $comment->user_id || $currentRole === "admin") { |
112
|
|
|
$comment->delete(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->di->get("response")->redirect("comment"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.