1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
subscription.php
См. документацию.
1<?php
2
4use Bitrix\Socialnetwork\Integration;
7
9{
10 public static function CheckFields($ACTION, &$arFields, $ID = 0): bool
11 {
12 global $APPLICATION;
13
14 if (
15 $ACTION != "ADD"
16 && intval($ID) <= 0
17 )
18 {
19 $APPLICATION->ThrowException("System error 870164", "ERROR");
20 return false;
21 }
22
23 if (
24 (is_set($arFields, "USER_ID") || $ACTION == "ADD")
25 && intval($arFields["USER_ID"]) <= 0
26 )
27 {
28 $APPLICATION->ThrowException(Loc::getMessage("SONET_SS_EMPTY_USER_ID"), "EMPTY_USER_ID");
29 return false;
30 }
31 elseif (is_set($arFields, "USER_ID"))
32 {
33 $dbResult = CUser::GetByID($arFields["USER_ID"]);
34 if (!$dbResult->Fetch())
35 {
36 $APPLICATION->ThrowException(Loc::getMessage("SONET_SS_ERROR_NO_USER_ID"), "ERROR_NO_USER_ID");
37 return false;
38 }
39 }
40
41 if (
42 (is_set($arFields, "CODE") || $ACTION == "ADD")
43 && trim($arFields["CODE"]) == ''
44 )
45 {
46 $APPLICATION->ThrowException(Loc::getMessage("SONET_SS_EMPTY_CODE"), "EMPTY_CODE");
47 return false;
48 }
49
50 return True;
51 }
52
53 public static function Delete($ID)
54 {
55 global $DB;
56
57 if (!CSocNetGroup::__ValidateID($ID))
58 return false;
59
60 $ID = intval($ID);
61
62 return $DB->Query("DELETE FROM b_sonet_subscription WHERE ID = ".$ID."", true);
63 }
64
65 public static function DeleteEx($userID = false, $code = false): bool
66 {
67 global $DB, $CACHE_MANAGER;
68
69 $userID = intval($userID);
70 $code = trim($code);
71
72 if (
73 $userID <= 0
74 && $code == ''
75 )
76 return false;
77
78 $DB->Query("DELETE FROM b_sonet_subscription WHERE 1=1 ".
79 (intval($userID) > 0 ? "AND USER_ID = ".$userID." " : "").
80 ($code <> '' ? "AND CODE = '".$code."' " : "")
81 , true);
82
83 if(defined("BX_COMP_MANAGED_CACHE"))
84 {
85 $CACHE_MANAGER->ClearByTag("sonet_subscription".($code ? '_'.$code : ''));
86 }
87
88 return true;
89 }
90
91 public static function Set($userID, $code, $value = false): bool
92 {
93 global $CACHE_MANAGER;
94
95 if (!CSocNetGroup::__ValidateID($userID))
96 {
97 return false;
98 }
99
100 $userID = intval($userID);
101 $code = trim($code);
102
103 if (
104 $userID <= 0
105 || $code == ''
106 )
107 {
108 return false;
109 }
110
111 $value = ($value == "Y" ? "Y" : "N");
112
113 $rsSubscription = CSocNetSubscription::GetList(
114 array(),
115 array(
116 "USER_ID" => $userID,
117 "CODE" => $code
118 )
119 );
120
121 $result = false;
122
123 if ($arSubscription = $rsSubscription->Fetch())
124 {
125 if ($value != "Y")
126 {
127 $result = CSocNetSubscription::delete($arSubscription["ID"]);
128 }
129 }
130 else
131 {
132 if ($value == "Y")
133 {
134 $result = CSocNetSubscription::add(array(
135 "USER_ID" => $userID,
136 "CODE" => $code
137 ));
138 }
139 }
140
141 if (
142 $result
143 && preg_match('/^SG(\d+)$/i', $code, $matches)
144 )
145 {
146 $chatId = false;
147 $groupId = $matches[1];
149 'group_id' => $groupId
150 ));
151 if (!empty($chatData[$groupId]) && intval($chatData[$groupId]) > 0)
152 {
153 $chatId = $chatData[$groupId];
154 }
155
156 if ($chatId)
157 {
158 $CIMChat = new CIMChat($userID);
159 $CIMChat->muteNotify($chatId, ($value != "Y"));
160 }
161 }
162
163 if(defined("BX_COMP_MANAGED_CACHE"))
164 {
165 $CACHE_MANAGER->ClearByTag("sonet_subscription_".$code);
166 }
167
168 return true;
169 }
170
171 public static function NotifyGroup($arFields): array
172 {
173 $arUserIDSent = array();
174
175 if (!CModule::IncludeModule("im"))
176 {
177 return $arUserIDSent;
178 }
179
180 if (!is_array($arFields["GROUP_ID"] ?? null))
181 {
182 $arFields["GROUP_ID"] = array($arFields["GROUP_ID"] ?? null);
183 }
184
185 if (empty($arFields["GROUP_ID"]))
186 {
187 return $arUserIDSent;
188 }
189
190 if (empty($arFields["EXCLUDE_USERS"]))
191 {
192 $arFields["EXCLUDE_USERS"] = array();
193 }
194
195 if (intval($arFields["LOG_ID"] ?? null) > 0)
196 {
197 $rsUnFollower = CSocNetLogFollow::GetList(
198 array(
199 "CODE" => "L".intval($arFields["LOG_ID"]),
200 "TYPE" => "N"
201 ),
202 array("USER_ID")
203 );
204
205 while ($arUnFollower = $rsUnFollower->Fetch())
206 {
207 $arFields["EXCLUDE_USERS"][] = $arUnFollower["USER_ID"];
208 }
209
210 $arFields["EXCLUDE_USERS"] = array_unique($arFields["EXCLUDE_USERS"], SORT_REGULAR);
211 }
212
213 $roleList = array();
214
215 if (
216 !empty($arFields['PERMISSION'])
217 && !empty($arFields['PERMISSION']['FEATURE'])
218 && !empty($arFields['PERMISSION']['OPERATION'])
219 )
220 {
221 $roleList = CSocNetFeaturesPerms::getOperationPerm(SONET_ENTITY_GROUP, $arFields["GROUP_ID"], $arFields['PERMISSION']['FEATURE'], $arFields['PERMISSION']['OPERATION']);
222 }
223
224 $chatData = [];
225 if (!empty($arFields["MESSAGE_CHAT"]))
226 {
228 'group_id' => $arFields["GROUP_ID"]
229 ));
230 }
231
232 if (!empty($chatData))
233 {
234 $arFields["GROUP_ID"] = array_diff($arFields["GROUP_ID"], array_unique(array_keys($chatData)));
235
236 $tmp = CSocNetLogTools::processPath(
237 array(
238 "URL" => $arFields["URL"],
239 ),
240 (intval($arFields["FROM_USER_ID"]) > 0 ? $arFields["FROM_USER_ID"] : 1),
241 SITE_ID
242 );
243 $chatUrl = $tmp["URLS"]["URL"];
244
245 $chatMessageFields = array(
246 "MESSAGE" => str_replace(
247 array("#URL#", "#url#"),
248 $chatUrl,
249 $arFields["MESSAGE_CHAT"]
250 ),
251 );
252
253 if (intval($arFields["FROM_USER_ID"]) > 0)
254 {
255 $chatMessageFields["FROM_USER_ID"] = intval($arFields["FROM_USER_ID"]);
256 }
257
258 foreach($chatData as $groupId => $chatId)
259 {
260 // don't send message to chat if it's unavailable for all members
261 if (
262 isset($roleList[$groupId])
263 && $roleList[$groupId] < UserToGroupTable::ROLE_USER
264 )
265 {
266 continue;
267 }
268
269 CIMChat::addMessage(array_merge(
270 $chatMessageFields, array(
271 "TO_CHAT_ID" => $chatId
272 )
273 ));
274 }
275 }
276
277 // if all groups processed by chats
278 if (empty($arFields["GROUP_ID"]))
279 {
280 return $arUserIDSent;
281 }
282
283 $arMessageFields = array(
284 "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
285 "NOTIFY_TYPE" => IM_NOTIFY_FROM,
286 "NOTIFY_MODULE" => "socialnetwork",
287 "NOTIFY_EVENT" => "sonet_group_event",
288 "NOTIFY_TAG" => ""
289 );
290
291 if (intval($arFields["FROM_USER_ID"] ?? null) > 0)
292 {
293 $arMessageFields["FROM_USER_ID"] = $arFields["FROM_USER_ID"];
294 }
295
296 if (!empty($arFields["NOTIFY_TAG"]))
297 {
298 $arMessageFields["NOTIFY_TAG"] = $arFields["NOTIFY_TAG"];
299 CIMNotify::DeleteByTag(
300 $arFields["NOTIFY_TAG"],
301 (intval($arFields["FROM_USER_ID"]) > 0 ? intval($arFields["FROM_USER_ID"]) : false)
302 );
303 }
304 elseif (intval($arFields["LOG_ID"] ?? null) > 0)
305 {
306 $arMessageFields["NOTIFY_TAG"] = "SONET|EVENT|".intval($arFields["LOG_ID"]);
307 }
308
309 $arUserToSend = array();
310 $arUserIDToSend = array();
311 $arGroupID = array();
312 $arCodes = array();
313
314 foreach ($arFields["GROUP_ID"] as $group_id)
315 {
316 $arCodes[] = "SG".$group_id;
317 }
318
319 $rsSubscriber = CSocNetSubscription::GetList(
320 array(),
321 array(
322 "CODE" => $arCodes
323 ),
324 false,
325 false,
326 array("USER_ID", "CODE")
327 );
328
329 while($arSubscriber = $rsSubscriber->Fetch())
330 {
331 if (
332 !in_array($arSubscriber["USER_ID"], $arFields["EXCLUDE_USERS"])
333 && !in_array($arSubscriber["USER_ID"], $arUserIDToSend)
334 )
335 {
336 if (preg_match('/^SG(\d+)$/', $arSubscriber["CODE"], $matches))
337 {
338 $arUserToSend[] = array(
339 "USER_ID" => $arSubscriber["USER_ID"],
340 "GROUP_ID" => $matches[1]
341 );
342 $arUserIDToSend[] = $arSubscriber["USER_ID"];
343 $arGroupID[] = $matches[1];
344 }
345 }
346 }
347
348 $arGroups = array();
349
350 $rsGroup = CSocNetGroup::GetList(
351 array(),
352 array("ID" => $arGroupID),
353 false,
354 false,
355 array("ID", "NAME", "OWNER_ID")
356 );
357
358 while($arGroup = $rsGroup->GetNext())
359 {
360 if (!empty($arGroup['NAME']))
361 {
362 $arGroup['NAME'] = Emoji::decode($arGroup['NAME']);
363 }
364 $arGroups[$arGroup["ID"]] = $arGroup;
365 }
366
367 $workgroupsPage = COption::GetOptionString("socialnetwork", "workgroups_page", "/workgroups/", SITE_ID);
368 $groupUrlTemplate = \Bitrix\Socialnetwork\Helper\Path::get('group_path_template');
369 $groupUrlTemplate = "#GROUPS_PATH#".mb_substr($groupUrlTemplate, mb_strlen($workgroupsPage), mb_strlen($groupUrlTemplate) - mb_strlen($workgroupsPage));
370
371 $canViewUserIdList = array();
372
373 foreach($arUserToSend as $arUser)
374 {
375 $groupId = $arUser['GROUP_ID'];
376
377 if (isset($roleList[$groupId]))
378 {
379 if (!isset($canViewUserIdList[$groupId]))
380 {
381 $canViewUserIdList[$groupId] = array();
382 $res = UserToGroupTable::getList(array(
383 'filter' => array(
384 '=GROUP_ID' => $groupId,
385 '<=ROLE' => $roleList[$groupId]
386 ),
387 'select' => array('USER_ID')
388 ));
389 while($relation = $res->fetch())
390 {
391 $canViewUserIdList[$groupId][] = $relation['USER_ID'];
392 }
393 }
394
395 if (!in_array($arUser["USER_ID"], $canViewUserIdList[$groupId]))
396 {
397 continue;
398 }
399 }
400
401 $arMessageFields["TO_USER_ID"] = $arUser["USER_ID"];
402 if (intval($arFields["LOG_ID"] ?? null) > 0)
403 {
404 $arMessageFields["NOTIFY_SUB_TAG"] = "SONET|EVENT|".intval($arFields["LOG_ID"])."|".intval($arUser["USER_ID"]);
405 }
406
408 array(
409 "URL" => $arFields["URL"] ?? null,
410 "GROUP_URL" => str_replace(array("#group_id#", "#GROUP_ID#"), $arUser["GROUP_ID"], $groupUrlTemplate)
411 ),
412 $arUser["USER_ID"],
413 SITE_ID
414 );
415 $url = $arTmp["URLS"]["URL"];
416
417 $serverName = (
418 mb_strpos($url, "http://") === 0
419 || mb_strpos($url, "https://") === 0
420 ? ""
421 : $arTmp["SERVER_NAME"]
422 );
423
424 $groupUrl = $serverName.$arTmp["URLS"]["GROUP_URL"];
425
426 $group_name = (array_key_exists($arUser["GROUP_ID"], $arGroups) ? $arGroups[$arUser["GROUP_ID"]]["NAME"] : "");
427
428 $message = $arFields["MESSAGE"] ?? '';
429 $messageOut = $arFields["MESSAGE_OUT"] ?? '';
430
431 $arMessageFields["NOTIFY_MESSAGE"] = fn (?string $languageId = null) => str_replace(
432 array("#URL#", "#url#", "#group_name#", "#GROUP_ID#", "#group_id#"),
433 array($url, $url, "<a href=\"".$groupUrl."\" class=\"bx-notifier-item-action\">".$group_name."</a>", $arUser["GROUP_ID"], $arUser["GROUP_ID"]),
434 is_callable($message) ? $message($languageId) : $message
435 );
436 $arMessageFields["NOTIFY_MESSAGE_OUT"] = fn (?string $languageId = null) => str_replace(
437 array("#URL#", "#url#", "#group_name#"),
438 array($serverName.$url, $serverName.$url, $group_name),
439 is_callable($messageOut) ? $messageOut($languageId) : $messageOut
440 );
441
442 $arMessageFields["PUSH_PARAMS"] = array(
443 "ACTION" => "sonet_group_event",
444 "TAG" => $arMessageFields["NOTIFY_TAG"]
445 );
446
447 if ((int)($arFields["FROM_USER_ID"] ?? null) > 0)
448 {
449 $dbAuthor = CUser::getByID($arFields["FROM_USER_ID"]);
450 if($arAuthor = $dbAuthor->fetch())
451 {
452 if (!empty($arAuthor["PERSONAL_PHOTO"]))
453 {
454 $imageResized = CFile::resizeImageGet(
455 $arAuthor["PERSONAL_PHOTO"],
456 array(
457 "width" => 100,
458 "height" => 100
459 ),
461 );
462 if ($imageResized)
463 {
464 $authorAvatarUrl = \Bitrix\Im\Common::getPublicDomain().$imageResized["src"];
465 }
466 }
467
468 $authorName = CUser::formatName(CSite::getNameFormat(), $arAuthor, true);
469 }
470 }
471
472 if (empty($authorName))
473 {
474 $authorName = Loc::getMessage("SONET_SS_PUSH_USER");
475 }
476
477 $arMessageFields["PUSH_PARAMS"]["ADVANCED_PARAMS"] = array(
478 'senderName' => $authorName
479 );
480
481 if (!empty($authorAvatarUrl))
482 {
483 $arMessageFields["PUSH_PARAMS"]["ADVANCED_PARAMS"]["avatarUrl"] = $authorAvatarUrl;
484 }
485
486 $arMessageFields["PUSH_MESSAGE"] = fn (?string $languageId = null) => str_replace(
487 array("[URL=#URL#]", "[URL=#url#]", "[/URL]", "#group_name#", "#GROUP_ID#", "#group_id#"),
488 array('', '', '', $group_name, $arUser["GROUP_ID"], $arUser["GROUP_ID"]),
489 is_callable($message) ? $message($languageId) : $message
490 );
491
492 $arMessageFields2Send = $arMessageFields;
493 if (
494 !is_set($arMessageFields2Send["FROM_USER_ID"])
495 || (int)$arMessageFields2Send["FROM_USER_ID"] <= 0
496 )
497 {
498 $arMessageFields2Send["NOTIFY_TYPE"] = IM_NOTIFY_SYSTEM;
499 $arMessageFields2Send["FROM_USER_ID"] = 0;
500 }
501
502 CIMNotify::Add($arMessageFields2Send);
503
504 $arUserIDSent[] = $arUser["USER_ID"];
505 }
506
507 return $arUserIDSent;
508 }
509
510 public static function IsUserSubscribed($userID, $code): bool
511 {
512 global $CACHE_MANAGER;
513
514 $userID = intval($userID);
515 if ($userID <= 0)
516 {
517 return false;
518 }
519
520 $code = trim($code);
521 if ($code == '')
522 {
523 return false;
524 }
525
526 $cache = new CPHPCache;
527 $cache_time = 31536000;
528 $cache_id = "entity_".$code;
529 $cache_path = "/sonet/subscription/";
530
531 if ($cache->InitCache($cache_time, $cache_id, $cache_path))
532 {
533 $arCacheVars = $cache->GetVars();
534 $arSubscriberID = $arCacheVars["arSubscriberID"];
535 }
536 else
537 {
538 $cache->StartDataCache($cache_time, $cache_id, $cache_path);
539 $arSubscriberID = array();
540
541 $rsSubscription = CSocNetSubscription::GetList(
542 array(),
543 array("CODE" => $code)
544 );
545
546 while ($arSubscription = $rsSubscription->Fetch())
547 $arSubscriberID[] = $arSubscription["USER_ID"];
548
549 if (defined("BX_COMP_MANAGED_CACHE"))
550 {
551 $CACHE_MANAGER->StartTagCache($cache_path);
552 $CACHE_MANAGER->RegisterTag("sonet_subscription_".$code);
553 $CACHE_MANAGER->RegisterTag("sonet_group");
554 }
555
556 $arCacheData = Array(
557 "arSubscriberID" => $arSubscriberID
558 );
559 $cache->EndDataCache($arCacheData);
560
561 if(defined("BX_COMP_MANAGED_CACHE"))
562 {
563 $CACHE_MANAGER->EndTagCache();
564 }
565 }
566
567 return (in_array($userID, $arSubscriberID));
568 }
569
570 public static function OnAfterChatMuteNotify($fields): bool
571 {
572 $result = false;
573
574 if (
575 !is_array($fields)
576 || empty($fields['USER_ID'])
577 || !isset($fields['MUTE'])
578 || empty($fields['CHAT'])
579 || !isset($fields['CHAT']['ENTITY_TYPE'])
580 || $fields['CHAT']['ENTITY_TYPE'] != Integration\Im\Chat\Workgroup::CHAT_ENTITY_TYPE
581 || empty($fields['CHAT']['ENTITY_ID'])
582 )
583 {
584 return $result;
585 }
586
587 $groupId = intval($fields['CHAT']['ENTITY_ID']);
588 $userId = intval($fields['USER_ID']);
589
590 return self::set($userId, "SG".$groupId, ($fields['MUTE'] ? "N" : "Y"));
591 }
592}
global $APPLICATION
Определения include.php:80
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPublicDomain()
Определения common.php:8
Определения emoji.php:10
static get(string $key='', $siteId=SITE_ID)
Определения path.php:17
static NotifyGroup($arFields)
Определения subscription.php:171
static Delete($ID)
Определения subscription.php:53
static DeleteEx($userID=false, $code=false)
Определения subscription.php:65
static CheckFields($ACTION, &$arFields, $ID=0)
Определения subscription.php:10
static OnAfterChatMuteNotify($fields)
Определения subscription.php:570
static IsUserSubscribed($userID, $code)
Определения subscription.php:510
static Set($userID, $code, $value=false)
Определения subscription.php:91
Определения im_chat.php:15
Определения cache.php:11
static ProcessPath($arUrl, $user_id, $explicit_site_id=false)
Определения log_tools.php:5283
global $CACHE_MANAGER
Определения clear_component_cache.php:7
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$arGroups
Определения options.php:1766
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
if($ajaxMode) $ID
Определения get_user.php:27
const IM_MESSAGE_SYSTEM
Определения include.php:21
const IM_NOTIFY_SYSTEM
Определения include.php:38
const IM_NOTIFY_FROM
Определения include.php:37
global $DB
Определения cron_frame.php:29
$ACTION
Определения csv_new_setup.php:27
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
const BX_RESIZE_IMAGE_EXACT
Определения constants.php:12
$arCodes
Определения options.php:154
is_set($a, $k=false)
Определения tools.php:2133
$message
Определения payment.php:8
if(intval($iTestTransaction) > 0) $arTmp
Определения payment.php:22
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$matches
Определения index.php:22
const SONET_ENTITY_GROUP
Определения include.php:117
const SITE_ID
Определения sonet_set_content_view.php:12
$dbResult
Определения updtr957.php:3
$url
Определения iframe.php:7
$fields
Определения yandex_run.php:501