|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The comments controller |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
7
|
|
|
* @author Omar El Gabry <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
class CommentsController extends Controller{ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
public function beforeAction(){ |
|
13
|
|
|
|
|
14
|
|
|
parent::beforeAction(); |
|
15
|
|
|
|
|
16
|
|
|
$action = $this->request->param('action'); |
|
17
|
|
|
$actions = ['getAll', 'create', 'getUpdateForm', 'update', 'getById', 'delete']; |
|
18
|
|
|
$this->Security->requireAjax($actions); |
|
|
|
|
|
|
19
|
|
|
$this->Security->requirePost($actions); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
switch($action){ |
|
22
|
|
|
case "getAll": |
|
23
|
|
|
$this->Security->config("form", [ 'fields' => ['post_id', 'page', 'comments_created']]); |
|
|
|
|
|
|
24
|
|
|
break; |
|
25
|
|
|
case "create": |
|
26
|
|
|
$this->Security->config("form", [ 'fields' => ['post_id', 'content']]); |
|
|
|
|
|
|
27
|
|
|
break; |
|
28
|
|
|
case "getUpdateForm": |
|
29
|
|
|
$this->Security->config("form", [ 'fields' => ['comment_id']]); |
|
|
|
|
|
|
30
|
|
|
break; |
|
31
|
|
|
case "update": |
|
32
|
|
|
$this->Security->config("form", [ 'fields' => ['comment_id', 'content']]); |
|
|
|
|
|
|
33
|
|
|
break; |
|
34
|
|
|
case "getById": |
|
35
|
|
|
case "delete": |
|
36
|
|
|
$this->Security->config("form", [ 'fields' => ['comment_id']]); |
|
|
|
|
|
|
37
|
|
|
break; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* get all comments |
|
43
|
|
|
* |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getAll(){ |
|
46
|
|
|
|
|
47
|
|
|
$postId = Encryption::decryptId($this->request->data("post_id")); |
|
48
|
|
|
|
|
49
|
|
|
$pageNum = $this->request->data("page"); |
|
50
|
|
|
$commentsCreated = (int)$this->request->data("comments_created"); |
|
51
|
|
|
|
|
52
|
|
|
$commentsData = $this->comment->getAll($postId, $pageNum, $commentsCreated); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$commentsHTML = $this->view->render(Config::get('VIEWS_PATH') . 'posts/comments.php', array("comments" => $commentsData["comments"])); |
|
55
|
|
|
$paginationHTML = $this->view->render(Config::get('VIEWS_PATH') . 'pagination/comments.php', array("pagination" => $commentsData["pagination"])); |
|
56
|
|
|
|
|
57
|
|
|
$this->view->renderJson(array("data" => ["comments" => $commentsHTML, "pagination" => $paginationHTML])); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function create(){ |
|
61
|
|
|
|
|
62
|
|
|
$postId = Encryption::decryptId($this->request->data("post_id")); |
|
63
|
|
|
|
|
64
|
|
|
$content = $this->request->data("content"); |
|
65
|
|
|
|
|
66
|
|
|
$comment = $this->comment->create(Session::getUserId(), $postId, $content); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if(!$comment){ |
|
69
|
|
|
$this->view->renderErrors($this->comment->errors()); |
|
|
|
|
|
|
70
|
|
|
}else{ |
|
71
|
|
|
|
|
72
|
|
|
$html = $this->view->render(Config::get('VIEWS_PATH') . 'posts/comments.php', array("comments" => $comment)); |
|
73
|
|
|
$this->view->renderJson(array("data" => $html)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* whenever the user hits 'edit' button, |
|
79
|
|
|
* a request will be sent to get the update form of that comment, |
|
80
|
|
|
* so that the user can 'update' or even 'cancel' the edit request. |
|
81
|
|
|
* |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
public function getUpdateForm(){ |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$commentId = Encryption::decryptIdWithDash($this->request->data("comment_id")); |
|
86
|
|
|
|
|
87
|
|
|
if(!$this->comment->exists($commentId)){ |
|
|
|
|
|
|
88
|
|
|
return $this->error(404); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$comment = $this->comment->getById($commentId); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$commentsHTML = $this->view->render(Config::get('VIEWS_PATH') . 'posts/commentUpdateForm.php', array("comment" => $comment[0])); |
|
94
|
|
|
$this->view->renderJson(array("data" => $commentsHTML)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* update comment |
|
99
|
|
|
* |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function update(){ |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$commentId = Encryption::decryptIdWithDash($this->request->data("comment_id")); |
|
104
|
|
|
$content = $this->request->data("content"); |
|
105
|
|
|
|
|
106
|
|
|
if(!$this->comment->exists($commentId)){ |
|
|
|
|
|
|
107
|
|
|
return $this->error(404); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$comment = $this->comment->update($commentId, $content); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
if(!$comment){ |
|
113
|
|
|
$this->view->renderErrors($this->comment->errors()); |
|
|
|
|
|
|
114
|
|
|
}else{ |
|
115
|
|
|
|
|
116
|
|
|
$html = $this->view->render(Config::get('VIEWS_PATH') . 'posts/comments.php', array("comments" => $comment)); |
|
117
|
|
|
$this->view->renderJson(array("data" => $html)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* get comment by Id |
|
123
|
|
|
* |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function getById(){ |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$commentId = Encryption::decryptIdWithDash($this->request->data("comment_id")); |
|
128
|
|
|
|
|
129
|
|
|
if(!$this->comment->exists($commentId)){ |
|
|
|
|
|
|
130
|
|
|
return $this->error(404); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$comment = $this->comment->getById($commentId); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$commentsHTML = $this->view->render(Config::get('VIEWS_PATH') . 'posts/comments.php', array("comments" => $comment)); |
|
136
|
|
|
$this->view->renderJson(array("data" => $commentsHTML)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
View Code Duplication |
public function delete(){ |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$commentId = Encryption::decryptIdWithDash($this->request->data("comment_id")); |
|
142
|
|
|
|
|
143
|
|
|
if(!$this->comment->exists($commentId)){ |
|
|
|
|
|
|
144
|
|
|
return $this->error(404); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$this->comment->deleteById($commentId); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
$this->view->renderJson(array("success" => true)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function isAuthorized(){ |
|
153
|
|
|
|
|
154
|
|
|
$action = $this->request->param('action'); |
|
155
|
|
|
$role = Session::getUserRole(); |
|
156
|
|
|
$resource = "comments"; |
|
157
|
|
|
|
|
158
|
|
|
// only for admins |
|
159
|
|
|
Permission::allow('admin', $resource, ['*']); |
|
160
|
|
|
|
|
161
|
|
|
// only for normal users |
|
162
|
|
|
Permission::allow('user', $resource, ['getAll', 'getById', 'create']); |
|
163
|
|
|
Permission::allow('user', $resource, ['update', 'delete', 'getUpdateForm'], 'owner'); |
|
164
|
|
|
|
|
165
|
|
|
$commentId = $this->request->data("comment_id"); |
|
166
|
|
|
if(!empty($commentId)){ |
|
167
|
|
|
$commentId = Encryption::decryptIdWithDash($commentId); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$config = [ |
|
171
|
|
|
"user_id" => Session::getUserId(), |
|
172
|
|
|
"table" => "comments", |
|
173
|
|
|
"id" => $commentId]; |
|
174
|
|
|
|
|
175
|
|
|
return Permission::check($role, $resource, $action, $config); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.