Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
extranet.php
1<?php
3
5
7{
8 const CACHE_TOKEN_TTL = 2592000; // 1 month
9
10 public static function checkModules()
11 {
12 return \Bitrix\Main\Loader::includeModule('extranet') && \Bitrix\Main\Loader::includeModule("socialnetwork");
13 }
14
15 public static function getGroup($params, $userId = null)
16 {
17 if (!self::checkModules())
18 return false;
19
20 $params = is_array($params)? $params: [];
21
22 $userId = \Bitrix\Im\Common::getUserId($userId);
23 if ($userId <= 0)
24 {
25 return false;
26 }
27
28 $cacheId = 'im_sonet_extranet_v3_'.$userId;
29 $cachePath = '/bx/imc/sonet/extranet'.\Bitrix\Im\Common::getCacheUserPostfix($userId);
30
31 $cache = \Bitrix\Main\Application::getInstance()->getCache();
32 $taggedCache = \Bitrix\Main\Application::getInstance()->getTaggedCache();
33
34 if($cache->initCache(self::CACHE_TOKEN_TTL, $cacheId, $cachePath))
35 {
36 return $cache->getVars();
37 }
38
39 $cache->startDataCache();
40
41 $taggedCache->startTagCache($cachePath);
42
43 $db = \CSocNetUserToGroup::GetList(
44 array(),
45 array(
46 "USER_ID" => $userId,
47 "<=ROLE" => SONET_ROLES_USER,
48 "GROUP_SITE_ID" => \CExtranet::GetExtranetSiteID(),
49 "GROUP_ACTIVE" => "Y",
50 "GROUP_CLOSED" => "N"
51 ),
52 false,
53 false,
54 array("ID", "GROUP_ID", "GROUP_NAME")
55 );
56
57 $groups = [];
58 $groupIds = [];
59 while ($row = $db->GetNext(true, false))
60 {
61 $groupIds[] = $row["GROUP_ID"];
62 $groups['SG'.$row['GROUP_ID']] = array(
63 'ID' => 'SG'.$row['GROUP_ID'],
64 'NAME' => Loc::getMessage('IM_INT_SN_GROUP_EXTRANET', Array('#GROUP_NAME#' => $row['GROUP_NAME'])),
65 'USERS' => []
66 );
67
68 $taggedCache->registerTag('sonet_group_'.$row['GROUP_ID']);
69 $taggedCache->registerTag('sonet_user2group_G'.$row['GROUP_ID']);
70 }
71
72 if (count($groups) <= 0)
73 {
74 return false;
75 }
76
77 $taggedCache->registerTag('sonet_user2group');
78
79 $taggedCache->endTagCache();
80
81 $db = \CSocNetUserToGroup::GetList(
82 array(),
83 array(
84 "@GROUP_ID" => $groupIds,
85 "<=ROLE" => SONET_ROLES_USER,
86 "USER_ACTIVE" => "Y",
87 "USER_CONFIRM_CODE" => false
88 ),
89 false,
90 false,
91 array("ID", "USER_ID", "GROUP_ID")
92 );
93 while ($row = $db->GetNext(true, false))
94 {
95 if($row["USER_ID"] == $userId || !isset($groups['SG'.$row['GROUP_ID']]))
96 continue;
97
98 $groups['SG'.$row['GROUP_ID']]['USERS'][] = $row["USER_ID"];
99 }
100
101 $cache->endDataCache($groups);
102
103 return $groups;
104 }
105
106 public static function isUserInGroup($userId, $currentUserId = null)
107 {
108 $currentUserId = \Bitrix\Im\Common::getUserId($currentUserId);
109 if ($currentUserId <= 0)
110 {
111 return false;
112 }
113
114 if ($userId == $currentUserId)
115 {
116 return true;
117 }
118
119 $extranetUsers = [];
120 $groups = self::getGroup([], $currentUserId);
121 if (is_array($groups))
122 {
123 foreach ($groups as $group)
124 {
125 foreach ($group['USERS'] as $uid)
126 {
127 $extranetUsers[$uid] = $uid;
128 }
129 }
130 }
131
132 return isset($extranetUsers[$userId]);
133 }
134
135 public static function filterUserList(array $userList, $currentUserId = null)
136 {
137 $currentUserId = \Bitrix\Im\Common::getUserId($currentUserId);
138 if ($currentUserId <= 0)
139 {
140 return false;
141 }
142
143 if (empty($userList))
144 {
145 return [];
146 }
147
148 $extranetUsers = [];
149 $groups = self::getGroup([], $currentUserId);
150 if (is_array($groups))
151 {
152 foreach ($groups as $group)
153 {
154 foreach ($group['USERS'] as $uid)
155 {
156 $extranetUsers[$uid] = $uid;
157 }
158 }
159 }
160
161 return array_filter($userList, function($userId) use ($extranetUsers) {
162 return isset($extranetUsers[$userId]);
163 });
164 }
165}
166
167
168
static isUserInGroup($userId, $currentUserId=null)
Definition extranet.php:106
static filterUserList(array $userList, $currentUserId=null)
Definition extranet.php:135
static getGroup($params, $userId=null)
Definition extranet.php:15
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29