1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CJ\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \CJ\Comment\HTMLForm\CreateCommentForm; |
8
|
|
|
use \CJ\Comment\HTMLForm\EditCommentForm; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A controller for the comment section |
12
|
|
|
*/ |
13
|
|
|
class CommentController implements InjectionAwareInterface |
14
|
|
|
{ |
15
|
|
|
use InjectionAwareTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* process incomming POST |
19
|
|
|
*/ |
20
|
|
|
public function newComment() |
21
|
|
|
{ |
22
|
|
|
$umodel = $this->di->get("umodel"); |
23
|
|
|
|
24
|
|
|
if (!$umodel->isLoggedIn()) { |
25
|
|
|
$this->di->get("response")->redirect($this->di->get("url")->create("user/login")); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$form = new CreateCommentForm($this->di, $umodel->getLoggedInUser()); |
29
|
|
|
$form->check(); |
30
|
|
|
|
31
|
|
|
$data = ["form" => $form->getHTML()]; |
32
|
|
|
|
33
|
|
|
$this->di->get("view")->add("user/createUser", $data); |
34
|
|
|
|
35
|
|
|
$this->di->get("pageRender")->renderPage(["title" => "CommentForm"]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* remove one comment |
40
|
|
|
*/ |
41
|
|
|
public function removeComment($index) |
42
|
|
|
{ |
43
|
|
|
$user = $this->di->get("umodel"); |
44
|
|
|
$comment = $this->di->get("comment"); |
45
|
|
|
$comment->getComment($index); |
46
|
|
|
|
47
|
|
View Code Duplication |
if ($comment->userId !== $this->di->get("session")->get("user")) { |
|
|
|
|
48
|
|
|
if ($user->isUserAdmin()) { |
49
|
|
|
// Do nothing |
50
|
|
|
} else { |
51
|
|
|
$this->di->get("response")->redirect($this->di->get("url")->create("comment")); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$comment->deleteComment($index); |
56
|
|
|
$this->di->get("response")->redirect($this->di->get("url")->create("comment")); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* load edit page |
61
|
|
|
*/ |
62
|
|
|
public function editComment($index) |
63
|
|
|
{ |
64
|
|
|
$comment = $this->di->get("comment")->getComment($index); |
65
|
|
|
$user = $this->di->get("umodel"); |
66
|
|
|
|
67
|
|
|
|
68
|
|
View Code Duplication |
if ($comment->userId !== $this->di->get("session")->get("user")) { |
|
|
|
|
69
|
|
|
if ($user->isUserAdmin()) { |
70
|
|
|
// Do nothing |
71
|
|
|
} else { |
72
|
|
|
$this->di->get("response")->redirect($this->di->get("url")->create("comment")); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$form = new EditCommentForm($this->di, $comment); |
77
|
|
|
$form->check(); |
78
|
|
|
|
79
|
|
|
$data = ["form" => $form->getHTML()]; |
80
|
|
|
|
81
|
|
|
$this->di->get("view")->add("user/update", $data, "main"); |
82
|
|
|
$this->di->get("pageRender")->renderPage(["title" => "guestbook - edit"]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* render comment view |
88
|
|
|
*/ |
89
|
|
|
public function renderComments() |
90
|
|
|
{ |
91
|
|
|
$data = ["title" => "guestbook"]; |
92
|
|
|
$comments = $this->di->get("comment")->getComments(); |
93
|
|
|
$user = $this->di->get("umodel")->getLoggedInUser(); |
94
|
|
|
|
95
|
|
|
$comments = array_reverse($comments); |
96
|
|
|
|
97
|
|
|
$this->di->get("view")->add("components/commentholder", [ |
98
|
|
|
"comments" => $comments, |
99
|
|
|
"user" => $user |
100
|
|
|
], "main"); |
101
|
|
|
|
102
|
|
|
$this->di->get("pageRender")->renderPage($data); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.