|
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\CommentAdd; |
|
9
|
|
|
use Apps\Model\Api\Comments\CommentAnswerAdd; |
|
10
|
|
|
use Apps\Model\Api\Comments\CommentPostAdd; |
|
11
|
|
|
use Apps\Model\Api\Comments\EntityCommentData; |
|
12
|
|
|
use Extend\Core\Arch\ApiController; |
|
13
|
|
|
use Ffcms\Core\App; |
|
14
|
|
|
use Ffcms\Core\Exception\JsonException; |
|
15
|
|
|
use Ffcms\Core\Helper\Date; |
|
16
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
17
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
18
|
|
|
|
|
19
|
|
|
class Comments extends ApiController |
|
20
|
|
|
{ |
|
21
|
|
|
const ITEM_PER_PAGE = 10; |
|
22
|
|
|
|
|
23
|
|
|
public function actionAdd() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->setJsonHeader(); |
|
26
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
27
|
|
|
|
|
28
|
|
|
$replayTo = (int)App::$Request->request->get('replay-to'); |
|
29
|
|
|
$model = null; |
|
|
|
|
|
|
30
|
|
|
// check if its a answer (comment answer type) |
|
31
|
|
|
if ($replayTo > 0) { |
|
32
|
|
|
$model = new CommentAnswerAdd($configs); |
|
|
|
|
|
|
33
|
|
|
$model->replayTo = $replayTo; |
|
34
|
|
|
} else { // sounds like new comment row |
|
35
|
|
|
$model = new CommentPostAdd($configs); |
|
|
|
|
|
|
36
|
|
|
$model->pathway = App::$Security->strip_tags(App::$Request->request->get('pathway')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// pass general comment params to model |
|
40
|
|
|
$model->message = App::$Security->secureHtml((string)App::$Request->request->get('message')); |
|
41
|
|
|
$model->guestName = App::$Security->strip_tags(App::$Request->request->get('guest-name')); |
|
42
|
|
|
|
|
43
|
|
|
// check model conditions before add new row |
|
44
|
|
|
if ($model === null || !$model->check()) { |
|
45
|
|
|
throw new JsonException('Unknown error'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// add comment post or answer to database and get response active record row |
|
49
|
|
|
$record = $model->buildRecord(); |
|
50
|
|
|
// pass row to entity builder model |
|
51
|
|
|
$response = new EntityCommentData($record); |
|
52
|
|
|
|
|
53
|
|
|
return json_encode([ |
|
54
|
|
|
'status' => 1, |
|
55
|
|
|
'data' => $response->make() // build row to standard format |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function actionList($index) |
|
60
|
|
|
{ |
|
61
|
|
|
// set header |
|
62
|
|
|
$this->setJsonHeader(); |
|
63
|
|
|
// get config count per page |
|
64
|
|
|
$perPage = (int)AppRecord::getConfig('widget', 'Comments', 'perPage'); |
|
65
|
|
|
// offset can be only integer |
|
66
|
|
|
$index = (int)$index; |
|
67
|
|
|
$offset = $perPage * $index; |
|
68
|
|
|
// get comment target path and check |
|
69
|
|
|
$path = (string)App::$Request->query->get('path'); |
|
70
|
|
|
if (Str::likeEmpty($path)) { |
|
71
|
|
|
throw new JsonException('Wrong path'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// select comments from db and check it |
|
75
|
|
|
$records = CommentPost::where('pathway', '=', $path) |
|
76
|
|
|
->skip($offset) |
|
77
|
|
|
->take($perPage) |
|
78
|
|
|
->get(); |
|
79
|
|
|
|
|
80
|
|
|
if ($records->count() < 1) { |
|
81
|
|
|
throw new JsonException(__('There is no comments found yet. You can be the first!')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// build output json data as array |
|
85
|
|
|
$data = []; |
|
86
|
|
|
foreach ($records as $comment) { |
|
87
|
|
|
// prepare specified data to output response, based on entity model |
|
88
|
|
|
$commentResponse = new EntityCommentData($comment); |
|
89
|
|
|
|
|
90
|
|
|
// build output json data |
|
91
|
|
|
$data[] = $commentResponse->make(); |
|
92
|
|
|
$commentResponse = null; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// calculate comments left count |
|
96
|
|
|
$count = CommentPost::where('pathway', '=', $path)->count(); |
|
97
|
|
|
$count -= $offset + $perPage; |
|
98
|
|
|
if ($count < 0) { |
|
99
|
|
|
$count = 0; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return json_encode([ |
|
103
|
|
|
'status' => 1, |
|
104
|
|
|
'data' => $data, |
|
105
|
|
|
'leftCount' => $count |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function actionShowanswers($commentId) |
|
110
|
|
|
{ |
|
111
|
|
|
// check input data |
|
112
|
|
|
if (!Obj::isLikeInt($commentId) || (int)$commentId < 1) { |
|
113
|
|
|
throw new JsonException('Input data is incorrect'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// get data from db by comment id |
|
117
|
|
|
$records = CommentAnswer::where('comment_id', '=', $commentId); |
|
118
|
|
|
if ($records->count() < 1) { |
|
119
|
|
|
throw new JsonException(__('No answers for comment is founded')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// prepare output |
|
123
|
|
|
$response = []; |
|
124
|
|
|
foreach ($records->get() as $row) { |
|
125
|
|
|
$commentAnswer = new EntityCommentData($row); |
|
126
|
|
|
$response[] = $commentAnswer->make(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return json_encode([ |
|
130
|
|
|
'status' => 1, |
|
131
|
|
|
'data' => $response |
|
132
|
|
|
]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.