1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\CommentPost; |
6
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
7
|
|
|
use Extend\Core\Arch\ApiController; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Exception\JsonException; |
10
|
|
|
use Ffcms\Core\Helper\Date; |
11
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
13
|
|
|
|
14
|
|
|
class Comments extends ApiController |
15
|
|
|
{ |
16
|
|
|
const ITEM_PER_PAGE = 10; |
17
|
|
|
|
18
|
|
|
public function actionList($offset) |
19
|
|
|
{ |
20
|
|
|
// set header |
21
|
|
|
$this->setJsonHeader(); |
22
|
|
|
// offset can be only integer |
23
|
|
|
$offset = (int)$offset; |
24
|
|
|
// get comment target path and check |
25
|
|
|
$path = (string)App::$Request->query->get('path'); |
26
|
|
|
if (Str::likeEmpty($path)) { |
27
|
|
|
throw new JsonException('Wrong path'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// select comments from db and check it |
31
|
|
|
$records = CommentPost::where('pathway', '=', $path) |
32
|
|
|
->skip($offset * self::ITEM_PER_PAGE) |
33
|
|
|
->take(self::ITEM_PER_PAGE); |
34
|
|
|
|
35
|
|
|
if ($records->count() < 1) { |
36
|
|
|
throw new JsonException('No comments is found'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// build output json data as array |
40
|
|
|
$data = []; |
41
|
|
View Code Duplication |
foreach ($records->get() as $comment) { |
|
|
|
|
42
|
|
|
// comment can be passed from registered user (with unique ID) or from guest (without ID) |
43
|
|
|
$userName = __('Unknown'); |
44
|
|
|
$userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg'; |
45
|
|
|
$userObject = $comment->getUser(); |
46
|
|
|
if ($userObject !== null) { |
47
|
|
|
$userName = $userObject->getProfile()->nick; |
48
|
|
|
$userAvatar = $userObject->getProfile()->getAvatarUrl('small'); |
49
|
|
|
} else { |
50
|
|
|
if (!Str::likeEmpty($comment->guest_name)) { |
51
|
|
|
$userName = App::$Security->strip_tags($comment->guest_name); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// build output json data |
56
|
|
|
$data[] = [ |
57
|
|
|
'id' => $comment->id, |
58
|
|
|
'text' => $comment->message, |
59
|
|
|
'date' => Date::convertToDatetime($comment->created_at, Date::FORMAT_TO_HOUR), |
60
|
|
|
'user' => [ |
61
|
|
|
'id' => $comment->user_id, |
62
|
|
|
'name' => $userName, |
63
|
|
|
'avatar' => $userAvatar |
64
|
|
|
], |
65
|
|
|
'answers' => $comment->getAnswerCount() |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->response = json_encode([ |
70
|
|
|
'status' => 1, |
71
|
|
|
'data' => $data |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function actionShowanswers($commentId) |
76
|
|
|
{ |
77
|
|
|
// check input data |
78
|
|
|
if (!Obj::isLikeInt($commentId) || (int)$commentId < 1) { |
79
|
|
|
throw new JsonException('Input data is incorrect'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// get data from db by comment id |
83
|
|
|
$records = CommentAnswer::where('comment_id', '=', $commentId); |
84
|
|
|
if ($records->count() < 1) { |
85
|
|
|
throw new JsonException('No answers for comment is founded'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// prepare output |
89
|
|
|
$response = []; |
90
|
|
View Code Duplication |
foreach ($records->get() as $row) { |
|
|
|
|
91
|
|
|
$userInstance = $row->getUser(); |
92
|
|
|
$userName = __('Unknown'); |
93
|
|
|
$userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg'; |
94
|
|
|
if ($userInstance !== null) { |
95
|
|
|
if (!Str::likeEmpty($userInstance->getProfile()->nick)) { |
96
|
|
|
$userName = $userInstance->getProfile()->nick; |
97
|
|
|
} |
98
|
|
|
$userAvatar = $userInstance->getProfile()->getAvatarUrl('small'); |
99
|
|
|
} else { |
100
|
|
|
if (!Str::likeEmpty($row->guest_name)) { |
101
|
|
|
$userName = App::$Security->strip_tags($row->guest_name); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$response[] = [ |
106
|
|
|
'id' => $row->id, |
107
|
|
|
'text' => $row->message, |
108
|
|
|
'date' => Date::convertToDatetime($row->created_at, Date::FORMAT_TO_HOUR), |
109
|
|
|
'user' => [ |
110
|
|
|
'id' => $row->user_id, |
111
|
|
|
'name' => $userName, |
112
|
|
|
'avatar' => $userAvatar |
113
|
|
|
] |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->response = json_encode([ |
118
|
|
|
'status' => 1, |
119
|
|
|
'data' => $response |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
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.