Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rating.php
1<?php
2
11
12use Bitrix\Main;
15
17{
18 private const LOCK_KEY_PREFIX = 'rating.lock.';
19
20 public function configureActions(): array
21 {
22 $configureActions = parent::configureActions();
23
24 $configureActions['list'] = [
25 '-prefilters' => [
26 Main\Engine\ActionFilter\Authentication::class,
27 ]
28 ];
29
30 return $configureActions;
31 }
32
33 public function voteAction(array $params = []): ?array
34 {
35 $signedKey = (string) ($params['RATING_VOTE_KEY_SIGNED'] ?? '');
36 $entityId = (int) ($params['RATING_VOTE_ENTITY_ID'] ?? 0);
37 $entityTypeId = (string) ($params['RATING_VOTE_TYPE_ID'] ?? '');
38
39 $payloadValue = $entityTypeId . '-' . $entityId;
40
41 $signer = new \Bitrix\Main\Security\Sign\TimeSigner();
42 if (
43 $signedKey === ''
44 || $signer->unsign($signedKey, 'main.rating.vote') !== $payloadValue
45 )
46 {
47 $this->addError(new Main\Error('Access denied'));
48
49 return null;
50 }
51
52 $key = self::LOCK_KEY_PREFIX.$this->getCurrentUser()->getId();
53
54 if (!Application::getConnection()->lock($key))
55 {
56 $this->addError(new Main\Error('Request already exists', 'ERR_PARAMS'));
57 return null;
58 }
59
60 $action = (string)($params['RATING_VOTE_ACTION'] ?? '');
61 $reaction = (string)($params['RATING_VOTE_REACTION'] ?? '');
62
63 if (
64 $entityTypeId === ''
65 || $entityId <= 0
66 )
67 {
68 $this->addError(new Main\Error('Incorrect data', 'ERR_PARAMS'));
69 return null;
70 }
71
72 $ratingParams = [
73 'ENTITY_TYPE_ID' => $entityTypeId,
74 'ENTITY_ID' => $entityId,
75 'ACTION' => (in_array($action, [ 'plus', 'minus', 'change', 'cancel' ]) ? $action : 'list'),
76 'REACTION' => $reaction,
77 'RATING_RESULT' => 'N',
78 'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'],
79 'CURRENT_USER_ID' => $this->getCurrentUser()->getId(),
80 'CHECK_RIGHTS' => 'Y',
81 ];
82
83 $ratingVoteResult = \CRatings::getRatingVoteResult($ratingParams['ENTITY_TYPE_ID'], $ratingParams['ENTITY_ID']);
84 if (!empty($ratingVoteResult))
85 {
86 $ratingParams['TOTAL_VALUE'] = $ratingVoteResult['TOTAL_VALUE'];
87 $ratingParams['TOTAL_VOTES'] = $ratingVoteResult['TOTAL_VOTES'];
88 $ratingParams['TOTAL_POSITIVE_VOTES'] = $ratingVoteResult['TOTAL_POSITIVE_VOTES'];
89 $ratingParams['TOTAL_NEGATIVE_VOTES'] = $ratingVoteResult['TOTAL_NEGATIVE_VOTES'];
90 $ratingParams['USER_HAS_VOTED'] = $ratingVoteResult['USER_HAS_VOTED'];
91 $ratingParams['USER_VOTE'] = $ratingVoteResult['USER_VOTE'];
92 }
93 else
94 {
95 $ratingParams['TOTAL_VALUE'] = 0;
96 $ratingParams['TOTAL_VOTES'] = 0;
97 $ratingParams['TOTAL_POSITIVE_VOTES'] = 0;
98 $ratingParams['TOTAL_NEGATIVE_VOTES'] = 0;
99 $ratingParams['USER_HAS_VOTED'] = 'N';
100 $ratingParams['USER_VOTE'] = '0';
101 }
102
103 $voteList = Action::vote($ratingParams);
104 if (empty($voteList))
105 {
106 $this->addError(new Main\Error('Cannot do vote', 'CANNOT_VOTE'));
107 }
108
109 Application::getConnection()->unlock($key);
110
111 return $voteList;
112 }
113
114 public function listAction(array $params = []): ?array
115 {
116 $signedKey = (string) ($params['RATING_VOTE_KEY_SIGNED'] ?? '');
117 $entityId = (int) ($params['RATING_VOTE_ENTITY_ID'] ?? 0);
118 $entityTypeId = (string) ($params['RATING_VOTE_TYPE_ID'] ?? '');
119
120 $payloadValue = $entityTypeId . '-' . $entityId;
121
122 $signer = new \Bitrix\Main\Security\Sign\TimeSigner();
123 if (
124 $signedKey === ''
125 || $signer->unsign($signedKey, 'main.rating.vote') !== $payloadValue
126 )
127 {
128 $this->addError(new Main\Error('Access denied'));
129
130 return null;
131 }
132
133 $page = (int)($params['RATING_VOTE_LIST_PAGE'] ?? 1);
134 $listType = (
135 isset($params['RATING_VOTE_LIST_TYPE'])
136 && $params['RATING_VOTE_LIST_TYPE'] === 'minus'
137 ? 'minus'
138 : 'plus'
139 );
140 $reaction = (string)($params['RATING_VOTE_REACTION'] ?? '');
141 $pathToUserProfile = (string)($params['PATH_TO_USER_PROFILE'] ?? '/people/user/#USER_ID#/');
142
143 if (
144 $entityTypeId === ''
145 || $entityId <= 0
146 )
147 {
148 $this->addError(new Main\Error('Incorrect data', 'ERR_PARAMS'));
149 return null;
150 }
151
152 return Action::list([
153 'ENTITY_TYPE_ID' => $entityTypeId,
154 'ENTITY_ID' => $entityId,
155 'LIST_PAGE' => $page,
156 'LIST_LIMIT' => 20,
157 'REACTION' => $reaction,
158 'LIST_TYPE' => $listType,
159 'PATH_TO_USER_PROFILE' => $pathToUserProfile,
160 'CURRENT_USER_ID' => $this->getCurrentUser()->getId(),
161 'CHECK_RIGHTS' => 'Y',
162 ]);
163 }
164}
static getConnection($name="")
voteAction(array $params=[])
Definition rating.php:33
listAction(array $params=[])
Definition rating.php:114