|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\CommentPost; |
|
6
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
|
7
|
|
|
use Apps\ActiveRecord\App as AppRecord; |
|
8
|
|
|
use Apps\Model\Api\Comments\CommentAnswerAdd; |
|
9
|
|
|
use Apps\Model\Api\Comments\CommentPostAdd; |
|
10
|
|
|
use Apps\Model\Api\Comments\EntityCommentData; |
|
11
|
|
|
use Extend\Core\Arch\ApiController; |
|
12
|
|
|
use Ffcms\Core\App; |
|
13
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
14
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
15
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
|
16
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
17
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Comments. View and add comments and answers via json based ajax query's |
|
21
|
|
|
* @package Apps\Controller\Api |
|
22
|
|
|
*/ |
|
23
|
|
|
class Comments extends ApiController |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Add comment or answer via ajax. |
|
27
|
|
|
* @return string |
|
28
|
|
|
* @throws NativeException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function actionAdd() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setJsonHeader(); |
|
33
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
34
|
|
|
|
|
35
|
|
|
$replayTo = (int)$this->request->request->get('replay-to'); |
|
|
|
|
|
|
36
|
|
|
$model = null; |
|
|
|
|
|
|
37
|
|
|
// check if its a answer (comment answer type) |
|
38
|
|
|
if ($replayTo > 0) { |
|
39
|
|
|
$model = new CommentAnswerAdd($configs); |
|
|
|
|
|
|
40
|
|
|
$model->replayTo = $replayTo; |
|
41
|
|
|
} else { // sounds like new comment row |
|
42
|
|
|
$model = new CommentPostAdd($configs); |
|
|
|
|
|
|
43
|
|
|
$model->pathway = App::$Security->strip_tags($this->request->request->get('pathway')); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// pass general comment params to model |
|
47
|
|
|
$model->message = App::$Security->secureHtml((string)$this->request->request->get('message')); |
|
|
|
|
|
|
48
|
|
|
$model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name')); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// check model conditions before add new row |
|
51
|
|
|
if ($model === null || !$model->check()) { |
|
52
|
|
|
throw new NativeException('Unknown error'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// add comment post or answer to database and get response active record row |
|
56
|
|
|
$record = $model->buildRecord(); |
|
57
|
|
|
// pass row to entity builder model |
|
58
|
|
|
$response = new EntityCommentData($record); |
|
59
|
|
|
|
|
60
|
|
|
return json_encode([ |
|
61
|
|
|
'status' => 1, |
|
62
|
|
|
'data' => $response->make() // build row to standard format |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* List comments as json object with defined offset index |
|
68
|
|
|
* @param int $index |
|
69
|
|
|
* @return string |
|
70
|
|
|
* @throws NotFoundException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function actionList($index) |
|
73
|
|
|
{ |
|
74
|
|
|
// set header |
|
75
|
|
|
$this->setJsonHeader(); |
|
76
|
|
|
// get configs |
|
77
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
78
|
|
|
// items per page |
|
79
|
|
|
$perPage = (int)$configs['perPage']; |
|
80
|
|
|
// offset can be only integer |
|
81
|
|
|
$index = (int)$index; |
|
82
|
|
|
$offset = $perPage * $index; |
|
83
|
|
|
// get comment target path and check |
|
84
|
|
|
$path = (string)$this->request->query->get('path'); |
|
|
|
|
|
|
85
|
|
|
if (Str::likeEmpty($path)) { |
|
86
|
|
|
throw new NotFoundException('Wrong path'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// select comments from db and check it |
|
90
|
|
|
$query = CommentPost::where('pathway', '=', $path) |
|
91
|
|
|
->where('moderate', '=', 0); |
|
92
|
|
|
|
|
93
|
|
|
// check if comments is depend of language locale |
|
94
|
|
View Code Duplication |
if ((int)$configs['onlyLocale'] === 1) { |
|
|
|
|
|
|
95
|
|
|
$query = $query->where('lang', '=', $this->request->getLanguage()); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// get comments with offset and limit |
|
99
|
|
|
$records = $query->skip($offset) |
|
100
|
|
|
->take($perPage) |
|
101
|
|
|
->get(); |
|
102
|
|
|
|
|
103
|
|
|
// check if records is not empty |
|
104
|
|
|
if ($records->count() < 1) { |
|
105
|
|
|
throw new NotFoundException(__('There is no comments found yet. You can be the first!')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// build output json data as array |
|
109
|
|
|
$data = []; |
|
110
|
|
|
foreach ($records as $comment) { |
|
111
|
|
|
// prepare specified data to output response, based on entity model |
|
112
|
|
|
$commentResponse = new EntityCommentData($comment); |
|
113
|
|
|
|
|
114
|
|
|
// build output json data |
|
115
|
|
|
$data[] = $commentResponse->make(); |
|
116
|
|
|
$commentResponse = null; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// calculate comments left count |
|
120
|
|
|
$count = CommentPost::where('pathway', '=', $path)->where('moderate', '=', 0)->count(); |
|
121
|
|
|
$count -= $offset + $perPage; |
|
122
|
|
|
if ($count < 0) { |
|
123
|
|
|
$count = 0; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return json_encode([ |
|
127
|
|
|
'status' => 1, |
|
128
|
|
|
'data' => $data, |
|
129
|
|
|
'leftCount' => $count |
|
130
|
|
|
]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* List answers by comment id as json object |
|
135
|
|
|
* @param int $commentId |
|
136
|
|
|
* @return string |
|
137
|
|
|
* @throws ForbiddenException |
|
138
|
|
|
* @throws NotFoundException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function actionShowanswers($commentId) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->setJsonHeader(); |
|
143
|
|
|
// check input data |
|
144
|
|
|
if (!Obj::isLikeInt($commentId) || (int)$commentId < 1) { |
|
145
|
|
|
throw new ForbiddenException('Input data is incorrect'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// get configs |
|
149
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
150
|
|
|
|
|
151
|
|
|
// get data from db by comment id |
|
152
|
|
|
$records = CommentAnswer::where('comment_id', '=', $commentId) |
|
153
|
|
|
->where('moderate', '=', 0); |
|
154
|
|
View Code Duplication |
if ((int)$configs['onlyLocale'] === 1) { |
|
|
|
|
|
|
155
|
|
|
$records = $records->where('lang', '=', $this->request->getLanguage()); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// check objects count |
|
159
|
|
|
if ($records->count() < 1) { |
|
160
|
|
|
throw new NotFoundException(__('No answers for comment is founded')); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// prepare output |
|
164
|
|
|
$response = []; |
|
165
|
|
|
foreach ($records->get() as $row) { |
|
166
|
|
|
$commentAnswer = new EntityCommentData($row); |
|
167
|
|
|
$response[] = $commentAnswer->make(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return json_encode([ |
|
171
|
|
|
'status' => 1, |
|
172
|
|
|
'data' => $response |
|
173
|
|
|
]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get commentaries count for pathway. Pathway should be array [itemId => pathway] |
|
178
|
|
|
* @throws NativeException |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function actionCount() |
|
182
|
|
|
{ |
|
183
|
|
|
// set headers |
|
184
|
|
|
$this->setJsonHeader(); |
|
185
|
|
|
// get configs |
|
186
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
187
|
|
|
// get path array from request |
|
188
|
|
|
$path = $this->request->query->get('path'); |
|
|
|
|
|
|
189
|
|
|
if (!Obj::isArray($path) || count($path) < 1) { |
|
190
|
|
|
throw new NativeException('Wrong query params'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$count = []; |
|
194
|
|
|
// for each item in path array calculate comments count |
|
195
|
|
|
foreach ($path as $id => $uri) { |
|
196
|
|
|
$query = CommentPost::where('pathway', '=', $uri)->where('moderate', '=', 0); |
|
197
|
|
|
// check if comments is depend of language locale |
|
198
|
|
View Code Duplication |
if ((int)$configs['onlyLocale'] === 1) { |
|
|
|
|
|
|
199
|
|
|
$query = $query->where('lang', '=', $this->request->getLanguage()); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
// set itemId => count |
|
202
|
|
|
$count[(int)$id] = $query->count(); |
|
203
|
|
|
} |
|
204
|
|
|
// render json response |
|
205
|
|
|
return json_encode(['status' => 1, 'count' => $count]); |
|
206
|
|
|
} |
|
207
|
|
|
} |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.