|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Marcusgsta\Comment; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\Configure\ConfigureInterface; |
|
6
|
|
|
use \Anax\Configure\ConfigureTrait; |
|
7
|
|
|
use \Anax\DI\InjectionAwareInterface; |
|
8
|
|
|
use \Anax\DI\InjectionAwareTrait; |
|
9
|
|
|
use \Marcusgsta\Comment\HTMLForm\EditCommentForm; |
|
10
|
|
|
use \Marcusgsta\Comment\HTMLForm\CreateCommentForm; |
|
11
|
|
|
use \Marcusgsta\Comment\HTMLForm\DeleteCommentForm; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A controller class. |
|
15
|
|
|
*/ |
|
16
|
|
|
class CommentController implements |
|
17
|
|
|
ConfigureInterface, |
|
18
|
|
|
InjectionAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use ConfigureTrait, |
|
21
|
|
|
InjectionAwareTrait; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var $data description |
|
27
|
|
|
*/ |
|
28
|
|
|
//private $data; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Description. |
|
34
|
|
|
* |
|
35
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exception |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function getComments($test = "not_test") |
|
42
|
|
|
{ |
|
43
|
|
|
//$route = $this->di->request->getRoute(); |
|
44
|
|
|
|
|
45
|
1 |
|
$comment = new Comment(); |
|
46
|
1 |
|
$comment->setDb($this->di->get("db")); |
|
47
|
|
|
|
|
48
|
1 |
|
$allComments = $comment->findAll(); |
|
49
|
|
|
|
|
50
|
|
|
// filter array of comments to current page |
|
51
|
1 |
|
$newArray = array_filter($allComments, function ($obj) use ($test) { |
|
52
|
|
|
|
|
53
|
1 |
|
$route = $this->di->get("request")->getRoute(); |
|
54
|
1 |
|
$route = empty($route) ? "index" : $route; |
|
55
|
|
|
|
|
56
|
|
|
// for testing purposes |
|
57
|
1 |
|
if ($test == "test") { |
|
58
|
1 |
|
$route = "index"; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
if ($obj->page != $route) { |
|
62
|
1 |
|
return false; |
|
63
|
|
|
} |
|
64
|
1 |
|
return true; |
|
65
|
1 |
|
}); |
|
66
|
|
|
|
|
67
|
|
|
$data = [ |
|
68
|
1 |
|
"items" => $newArray, |
|
69
|
1 |
|
]; |
|
70
|
1 |
|
return $data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Description. |
|
75
|
|
|
* |
|
76
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @throws Exception |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
public function getPostCreateComment() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$title = "A create comment page"; |
|
85
|
|
|
$view = $this->di->get("view"); |
|
86
|
|
|
$pageRender = $this->di->get("pageRender"); |
|
87
|
|
|
$form = new CreateCommentForm($this->di); |
|
88
|
|
|
|
|
89
|
|
|
$form->check(); |
|
90
|
|
|
|
|
91
|
|
|
$data = [ |
|
92
|
|
|
"content" => $form->getHTML(), |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$view->add("default2/article", $data); |
|
96
|
|
|
|
|
97
|
|
|
$pageRender->renderPage(["title" => $title]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Description. |
|
102
|
|
|
* |
|
103
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
104
|
|
|
* |
|
105
|
|
|
* @throws Exception |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getPostEditComment($key, $itemId) |
|
110
|
|
|
{ |
|
111
|
|
|
$data = ["page" => $key, "commentid" => $itemId]; |
|
112
|
|
|
|
|
113
|
|
|
$commentId = $data['commentid']; |
|
114
|
|
|
$loggedInUser = $this->di->session->get("user"); |
|
115
|
|
|
$role = $this->getRole($loggedInUser); |
|
116
|
|
|
|
|
117
|
|
|
if ($role != 10) { |
|
118
|
|
|
if (!$this->isEditable($commentId, $loggedInUser)) { |
|
|
|
|
|
|
119
|
|
|
echo "Du har inte tillgång till att redigera denna kommentar."; |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$title = "An edit comment page"; |
|
125
|
|
|
$view = $this->di->get("view"); |
|
126
|
|
|
$pageRender = $this->di->get("pageRender"); |
|
127
|
|
|
$form = new EditCommentForm($this->di, $data); |
|
128
|
|
|
|
|
129
|
|
|
$form->check(); |
|
130
|
|
|
|
|
131
|
|
|
$data = [ |
|
132
|
|
|
"content" => $form->getHTML(), |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
$view->add("default2/article", $data); |
|
136
|
|
|
|
|
137
|
|
|
$pageRender->renderPage(["title" => $title]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Description. |
|
142
|
|
|
* |
|
143
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
144
|
|
|
* |
|
145
|
|
|
* @throws Exception |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
|
View Code Duplication |
public function deleteComment() |
|
|
|
|
|
|
150
|
|
|
{ |
|
151
|
|
|
$title = "A delete comments page"; |
|
152
|
|
|
$view = $this->di->get("view"); |
|
153
|
|
|
$pageRender = $this->di->get("pageRender"); |
|
154
|
|
|
$form = new DeleteCommentForm($this->di); |
|
155
|
|
|
|
|
156
|
|
|
$form->check(); |
|
157
|
|
|
|
|
158
|
|
|
$data = [ |
|
159
|
|
|
"content" => $form->getHTML(), |
|
160
|
|
|
]; |
|
161
|
|
|
|
|
162
|
|
|
$view->add("default2/article", $data); |
|
163
|
|
|
|
|
164
|
|
|
$pageRender->renderPage(["title" => $title]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* get a user's role |
|
169
|
|
|
* @param string user's acronym |
|
170
|
|
|
* |
|
171
|
|
|
* @return integer user's role |
|
172
|
|
|
**/ |
|
173
|
2 |
|
public function getRole($acronym) |
|
174
|
|
|
{ |
|
175
|
2 |
|
$user = new \Anax\User\User(); |
|
176
|
2 |
|
$user->setDb($this->di->get("db")); |
|
177
|
2 |
|
$user->find("acronym", $acronym); |
|
178
|
2 |
|
$role = isset($user->role) ? $user->role : null; |
|
|
|
|
|
|
179
|
2 |
|
return $role; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* check if logged in user can edit the comment |
|
184
|
|
|
* @param string commentid |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool |
|
187
|
|
|
**/ |
|
188
|
|
|
public function isEditable($commentId, $user) |
|
189
|
|
|
{ |
|
190
|
|
|
$comment = new Comment; |
|
191
|
|
|
$comment->setDb($this->di->get("db")); |
|
192
|
|
|
$comment = $comment->find("id", $commentId); |
|
193
|
|
|
|
|
194
|
|
|
if ($comment) { |
|
|
|
|
|
|
195
|
|
|
if ($comment->acronym != $user) { |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
return true; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.