Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
action.php
1<?php
2
4
7
8Loc::loadMessages(__FILE__);
9
10class Action
11{
12 public static function vote(array $params = []): array
13 {
14 global $APPLICATION;
15
16 $voteList = [];
17
18 $allowVoteData = \CRatings::checkAllowVote($params);
19 if (!$allowVoteData['RESULT'])
20 {
21 return $voteList;
22 }
23
24 $APPLICATION->RestartBuffer();
25 $userData = [];
26 $remoteAddr = $params['REMOTE_ADDR'];
27
28 if (in_array($params['ACTION'], [ 'plus', 'minus' ]))
29 {
30 $userData = \CRatings::addRatingVote([
31 'ENTITY_TYPE_ID' => $params['ENTITY_TYPE_ID'],
32 'ENTITY_ID' => $params['ENTITY_ID'],
33 'VALUE' => $params['ACTION'] === 'plus' ? 1 : -1,
34 'USER_IP' => $remoteAddr,
35 'USER_ID' => $params['CURRENT_USER_ID'],
36 'REACTION' => (
37 $params['ACTION'] === 'plus'
38 && !empty($params['REACTION'])
39 ? $params['REACTION']
40 : \CAllRatings::REACTION_DEFAULT
41 ),
42 ]);
43
44 }
45 elseif ($params['ACTION'] === 'change')
46 {
47 $userData = \CRatings::changeRatingVote([
48 'ENTITY_TYPE_ID' => $params['ENTITY_TYPE_ID'],
49 'ENTITY_ID' => $params['ENTITY_ID'],
50 'USER_IP' => $remoteAddr,
51 'USER_ID' => $params['CURRENT_USER_ID'],
52 'REACTION' => (
53 !empty($params['REACTION'])
54 ? $params['REACTION']
55 : \CAllRatings::REACTION_DEFAULT
56 )
57 ]);
58 }
59 else if ($params['ACTION'] === 'cancel')
60 {
61 $userData = \CRatings::cancelRatingVote([
62 'ENTITY_TYPE_ID' => $params['ENTITY_TYPE_ID'],
63 'ENTITY_ID' => $params['ENTITY_ID'],
64 'USER_ID' => $params['CURRENT_USER_ID'],
65 ]);
66 }
67
68 $voteList = \CRatings::getRatingVoteList([
69 'ENTITY_TYPE_ID' => $params['ENTITY_TYPE_ID'],
70 'ENTITY_ID' => $params['ENTITY_ID'],
71 'LIST_LIMIT' => 0,
72 'LIST_TYPE' => ($params['ACTION'] === 'minus' ? 'minus' : 'plus'),
73 ]);
74
75 if ($params['RATING_RESULT'] === 'Y')
76 {
77 $voteList = array_merge(
78 $voteList,
79 self::getVoteResult($params['ENTITY_TYPE_ID'], $params['ENTITY_ID'])
80 );
81 }
82
83 $voteList['action'] = $params['ACTION'];
84 $voteList['user_data'] = $userData;
85
86 return $voteList;
87 }
88
89 public static function getVoteResult($entityTypeId, $entityId): array
90 {
91 global $USER;
92
93 $entityId = (int)$entityId;
94 $userId = (int)$USER->getId();
95
96 $ratingResult = \CRatings::getRatingVoteResult($entityTypeId, $entityId, $userId);
97 if (empty($ratingResult))
98 {
99 $ratingResult['USER_HAS_VOTED'] = $USER->isAuthorized() ? 'N' : 'Y';
100 $ratingResult['USER_VOTE'] = 0;
101 $ratingResult['TOTAL_VALUE'] = 0;
102 $ratingResult['TOTAL_VOTES'] = 0;
103 $ratingResult['TOTAL_POSITIVE_VOTES'] = 0;
104 $ratingResult['TOTAL_NEGATIVE_VOTES'] = 0;
105 }
106
107 $resultStatus = $ratingResult['TOTAL_VALUE'] < 0 ? 'minus' : 'plus';
108 $resultTitle = sprintf(
109 Loc::getMessage('RATING_COMPONENT_DESC'),
110 $ratingResult['TOTAL_VOTES'],
111 $ratingResult['TOTAL_POSITIVE_VOTES'],
112 $ratingResult['TOTAL_NEGATIVE_VOTES']
113 );
114
115 return [
116 'resultValue' => $ratingResult['TOTAL_VALUE'],
117 'resultVotes' => $ratingResult['TOTAL_VOTES'],
118 'resultPositiveVotes' => $ratingResult['TOTAL_POSITIVE_VOTES'],
119 'resultNegativeVotes' => $ratingResult['TOTAL_NEGATIVE_VOTES'],
120 'resultStatus' => $resultStatus,
121 'resultTitle' => $resultTitle,
122 ];
123 }
124
125 public static function list(array $params = []): array
126 {
127 $mailInstalled = ModuleManager::isModuleInstalled('mail');
128 $extranetInstalled = ModuleManager::isModuleInstalled('extranet');
129
130 if ($extranetInstalled)
131 {
132 $params['USER_SELECT'] = [ 'UF_DEPARTMENT' ];
133 }
134
135 if ($mailInstalled)
136 {
137 $params['USER_FIELDS'] = [
138 'ID', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'LOGIN',
139 'PERSONAL_PHOTO', 'EXTERNAL_AUTH_ID',
140 ];
141 }
142
143 $result = \CRatings::getRatingVoteList($params);
144
145 $voteList = [
146 'items' => [],
147 'items_all' => $result['items_all'],
148 'items_page' => $result['items_page'],
149 'reactions' => (
150 isset($result['reactions'])
151 && is_array($result['reactions'])
152 ? $result['reactions']
153 : []
154 ),
155 'list_page' => $result['list_page'],
156 ];
157
158 foreach ($result['items'] as $key => $value)
159 {
160 $userVote = [
161 'USER_ID' => $value['ID'],
162 'VOTE_VALUE' => $value['VOTE_VALUE'],
163 'PHOTO' => $value['PHOTO'],
164 'PHOTO_SRC' => $value['PHOTO_SRC'],
165 'FULL_NAME' => $value['FULL_NAME'],
166 'URL' => \CComponentEngine::makePathFromTemplate(
167 $params['PATH_TO_USER_PROFILE'],
168 [
169 'UID' => $value['USER_ID'],
170 'user_id' => $value['USER_ID'],
171 'USER_ID' => $value['USER_ID'],
172 ],
173 ),
174 ];
175
176 if (
177 $mailInstalled
178 && $value['EXTERNAL_AUTH_ID'] === 'email'
179 )
180 {
181 $userVote['USER_TYPE'] = 'mail';
182 }
183 elseif (
184 $extranetInstalled
185 && (
186 empty($value['UF_DEPARTMENT'])
187 || (int)$value['UF_DEPARTMENT'][0] <= 0
188 )
189 )
190 {
191 $userVote['USER_TYPE'] = 'extranet';
192 }
193
194 $voteList['items'][] = $userVote;
195 }
196
197 if ($params['CURRENT_USER_ID'] > 0)
198 {
199 $event = new \Bitrix\Main\Event(
200 'main',
201 'onRatingListViewed',
202 [
203 'entityTypeId' => $params['ENTITY_TYPE_ID'],
204 'entityId' => $params['ENTITY_ID'],
205 'userId' => $params['CURRENT_USER_ID'],
206 ]
207 );
208 $event->send();
209 }
210
211 return $voteList;
212 }
213}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)
static vote(array $params=[])
Definition action.php:12
static getVoteResult($entityTypeId, $entityId)
Definition action.php:89
static list(array $params=[])
Definition action.php:125