Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usertag.php
1<?php
9
12
13
39class UserTagTable extends Entity\DataManager
40{
41 public static function getTableName()
42 {
43 return 'b_sonet_user_tag';
44 }
45
46 public static function getMap()
47 {
48 $fieldsMap = array(
49 'USER_ID' => array(
50 'data_type' => 'integer',
51 'primary' => true
52 ),
53 'USER' => array(
54 'data_type' => 'Bitrix\Main\UserTable',
55 'reference' => array('=this.USER_ID' => 'ref.ID'),
56 ),
57 'NAME' => array(
58 'data_type' => 'string',
59 'primary' => true,
60 'save_data_modification' => function() {
61 return array(
62 function ($text)
63 {
64 return ToLower($text);
65 }
66 );
67 },
68 'fetch_data_modification' => function() {
69 return array(
70 function ($text)
71 {
72 return ToLower($text);
73 }
74 );
75 }
76 )
77 );
78
79 return $fieldsMap;
80 }
81
82 public static function add(array $data)
83 {
84 try
85 {
86 $result = parent::add($data);
87 }
88 catch(\Exception $e)
89 {
90 $result = false;
91 }
92
93 return $result;
94 }
95
96 public static function getUserTagCountData($params = array())
97 {
98 global $DB;
99
100 $connection = \Bitrix\Main\HttpApplication::getConnection();
101
102 $result = array();
103
104 $userId = (
105 !empty($params['userId'])
106 ? intval($params['userId'])
107 : false
108 );
109
110 $params['tagName'] = $params['tagName'] ?? null;
111
112 $tagName = (
113 (is_array($params['tagName']) && !empty($params['tagName']))
114 || (!is_array($params['tagName']) && $params['tagName'] <> '')
115 ? $params['tagName']
116 : false
117 );
118
119 $nameFilter = (
120 $tagName !== false
121 ? (is_array($tagName)) ? " IN (".implode(',', array_map(function($val) use ($DB) { return "'".$DB->forSql($val)."'"; }, $tagName)).")" : " = '".$DB->forSql($tagName)."'"
122 : ($userId ? ' IN (SELECT NAME FROM '.self::getTableName().' WHERE USER_ID = '.$userId.')' : '')
123 );
124
125 $whereClause = "WHERE U.ACTIVE='Y' ".(!empty($nameFilter) ? 'AND UT.NAME '.$nameFilter : '');
126
127 $sql = '
128 SELECT CASE WHEN (MIN(USER_ID) = 0) THEN COUNT(USER_ID)-1 ELSE COUNT(USER_ID) END AS CNT, UT.NAME AS NAME
129 FROM '.self::getTableName().' UT
130 INNER JOIN b_user U ON U.ID=UT.USER_ID '.
131 $whereClause.'
132 GROUP BY UT.NAME
133 ';
134
135 $res = $connection->query($sql);
136 while ($tagData = $res->fetch())
137 {
138 $result[$tagData['NAME']] = $tagData['CNT'];
139 }
140
141 return $result;
142 }
143
144 public static function getUserTagTopData($params = array())
145 {
146 global $USER, $DB;
147
148 $result = array();
149
150 $userId = (
151 !empty($params['userId'])
152 ? intval($params['userId'])
153 : false
154 );
155
156 $topCount = (
157 isset($params['topCount'])
158 ? intval($params['topCount'])
159 : 0
160 );
161
162 if ($topCount <= 0)
163 {
164 $topCount = 2;
165 }
166
167 if ($topCount > 5)
168 {
169 $topCount = 5;
170 }
171
172 $avatarSize = (
173 isset($params['avatarSize'])
174 ? intval($params['avatarSize'])
175 : 100
176 );
177
178 $connection = \Bitrix\Main\Application::getConnection();
179 $connection->queryExecute('SET @user_rank = 0');
180 $connection->queryExecute('SET @current_entity_id = 0');
181
182 $params['tagName'] = $params['tagName'] ?? null;
183
184 $tagName = (
185 (is_array($params['tagName']) && !empty($params['tagName']))
186 || (!is_array($params['tagName']) && $params['tagName'] <> '')
187 ? $params['tagName']
188 : false
189 );
190
191 $nameFilter = (
192 $tagName !== false
193 ? (is_array($tagName)) ? " NAME IN (".implode(',', array_map(function($val) use ($DB) { return "'".$DB->forSql($val)."'"; }, $tagName)).")" : " NAME = '".$DB->forSql($tagName)."'"
194 : ($userId ? " USER_ID = ".$userId : "")
195 );
196
197 $whereClause = (!empty($nameFilter) ? 'WHERE '.$nameFilter : '');
198
199 $tagsSql = 'SELECT NAME FROM '.self::getTableName().' '.$whereClause;
200
201 if (\Bitrix\Main\ModuleManager::isModuleInstalled('intranet'))
202 {
203 $ratingId = \CRatings::getAuthorityRating();
204 if (intval($ratingId) <= 0)
205 {
206 return $result;
207 }
208
209 $res = $connection->query("SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN() */
210 @user_rank := IF(
211 @current_name = tmp.NAME,
212 @user_rank + 1,
213 1
214 ) as USER_RANK,
215 @current_name := tmp.NAME,
216 tmp.USER_ID as USER_ID,
217 tmp.NAME as NAME,
218 tmp.WEIGHT as WEIGHT
219 FROM (
220 SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN() */
221 @rownum := @rownum + 1 as ROWNUM,
222 RS1.ENTITY_ID as USER_ID,
223 UT1.NAME as NAME,
224 MAX(RS1.VOTES) as WEIGHT
225 FROM
226 b_rating_subordinate RS1,
227 ".self::getTableName()." UT1
228 INNER JOIN b_user U ON U.ID = UT1.USER_ID
229 WHERE
230 RS1.RATING_ID = ".intval($ratingId)."
231 AND RS1.ENTITY_ID = UT1.USER_ID
232 AND UT1.NAME IN (".$tagsSql.")
233 AND U.ACTIVE = 'Y'
234 GROUP BY
235 UT1.NAME, RS1.ENTITY_ID
236 ORDER BY
237 UT1.NAME,
238 WEIGHT DESC
239 ) tmp");
240 }
241 else
242 {
243 $res = $connection->query("SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN() */
244 @user_rank := IF(
245 @current_name = tmp.NAME,
246 @user_rank + 1,
247 1
248 ) as USER_RANK,
249 tmp.USER_ID as USER_ID,
250 tmp.NAME as NAME,
251 1 as WEIGHT
252 FROM (
253 SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN() */
254 @rownum := @rownum + 1 as ROWNUM,
255 UT1.USER_ID as USER_ID,
256 UT1.NAME as NAME
257 FROM
258 ".self::getTableName()." UT1
259 INNER JOIN b_user U ON U.ID = UT1.USER_ID
260 WHERE
261 UT1.NAME IN (".$tagsSql.")
262 AND U.ACTIVE = 'Y'
263 ORDER BY
264 UT1.NAME
265 ) tmp");
266 }
267
268 $userWeightData = $tagUserData = array();
269
270 $currentTagName = false;
271 $hasMine = false;
272
273 while ($resultFields = $res->fetch())
274 {
275 if (
276 !$hasMine
277 && $resultFields['USER_ID'] == $USER->getId()
278 )
279 {
280 $hasMine = true;
281 }
282
283 if ($resultFields['NAME'] != $currentTagName)
284 {
285 $cnt = 0;
286 $hasMine = false;
287 $tagUserData[$resultFields['NAME']] = array();
288 }
289
290 $currentTagName = $resultFields['NAME'];
291 $cnt++;
292
293 if ($cnt > ($hasMine ? $topCount+1 : $topCount))
294 {
295 continue;
296 }
297
298 $tagUserData[$resultFields['NAME']][] = $resultFields['USER_ID'];
299 if (!isset($userWeightData[$resultFields['USER_ID']]))
300 {
301 $userWeightData[$resultFields['USER_ID']] = floatval($resultFields['WEIGHT']);
302 }
303 }
304
305 $userData = \Bitrix\Socialnetwork\Item\UserTag::getUserData([
306 'userIdList' => array_keys($userWeightData),
307 'avatarSize' => $avatarSize
308 ]);
309
310 foreach($tagUserData as $tagName => $userIdList)
311 {
312 $result[$tagName] = array();
313
314 foreach($userIdList as $userId)
315 {
316 $result[$tagName][] = array(
317 'ID' => $userId,
318 'NAME_FORMATTED' => $userData[$userId]['NAME_FORMATTED'],
319 'PERSONAL_PHOTO' => $userData[$userId]['PERSONAL_PHOTO']['ID'],
320 'PERSONAL_PHOTO_SRC' => $userData[$userId]['PERSONAL_PHOTO']['SRC'],
321 'PERSONAL_GENDER' => $userData[$userId]['PERSONAL_GENDER'],
322 'WEIGHT' => $userWeightData[$userId]
323 );
324 }
325 }
326
327 foreach($result as $tagName => $data)
328 {
329 usort(
330 $data,
331 function($a, $b)
332 {
333 if ($a['WEIGHT'] == $b['WEIGHT'])
334 {
335 return 0;
336 }
337 return ($a['WEIGHT'] > $b['WEIGHT']) ? -1 : 1;
338 }
339 );
340 $result[$tagName] = $data;
341 }
342
343 return $result;
344 }
345
346 public static function update($primary, array $data)
347 {
348 throw new NotImplementedException("Use add() method of the class.");
349 }
350}
static getUserTagTopData($params=array())
Definition usertag.php:144
static add(array $data)
Definition usertag.php:82
static getUserTagCountData($params=array())
Definition usertag.php:96
static update($primary, array $data)
Definition usertag.php:346