|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Comments; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\App as AppRecord; |
|
6
|
|
|
use Apps\ActiveRecord\CommentPost; |
|
7
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
9
|
|
|
use Ffcms\Core\Network\Request; |
|
10
|
|
|
use Ffcms\Core\Network\Response; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait ActionCount |
|
14
|
|
|
* @package Apps\Controller\Api\Comments |
|
15
|
|
|
* @property Request $request |
|
16
|
|
|
* @property Response $response |
|
17
|
|
|
* @method void setJsonHeader() |
|
18
|
|
|
*/ |
|
19
|
|
|
trait ActionCount |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Get comment count by $appName and array of ids |
|
23
|
|
|
* @param $appName |
|
24
|
|
|
* @return string |
|
25
|
|
|
* @throws NativeException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function cnt(string $appName): ?string |
|
28
|
|
|
{ |
|
29
|
|
|
// set headers |
|
30
|
|
|
$this->setJsonHeader(); |
|
31
|
|
|
// get configs |
|
32
|
|
|
$configs = AppRecord::getConfigs('widget', 'Comments'); |
|
33
|
|
|
// get path array from request |
|
34
|
|
|
$ids = $this->request->query->get('id'); |
|
35
|
|
|
if (!Any::isArray($ids) || count($ids) < 1) { |
|
36
|
|
|
throw new NativeException('Wrong query params'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$count = []; |
|
40
|
|
|
// for each item in path array calculate comments count |
|
41
|
|
|
foreach ($ids as $id) { |
|
42
|
|
|
$query = CommentPost::where('app_name', $appName) |
|
43
|
|
|
->where('app_relation_id', (int)$id) |
|
44
|
|
|
->where('moderate', '=', 0); |
|
45
|
|
|
// check if comments is depend of language locale |
|
46
|
|
|
if ((bool)$configs['onlyLocale']) { |
|
47
|
|
|
$query = $query->where('lang', '=', $this->request->getLanguage()); |
|
48
|
|
|
} |
|
49
|
|
|
// set itemId => count |
|
50
|
|
|
$count[(int)$id] = $query->count(); |
|
51
|
|
|
} |
|
52
|
|
|
// render json response |
|
53
|
|
|
return json_encode(['status' => 1, 'count' => $count]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|