Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
socialnetwork.php
1<?php
3
12
13Loc::loadMessages(__FILE__);
14
16{
20 const SETTINGS_CODE_SHORT = 'knowledge';
21
25 const SETTINGS_CODE = 'landing_knowledge';
26
31 const PATH_GROUP_BINDING = 'kb/binding/group/create.php?groupId=#groupId#';
32
39 public static function getBindingRow(int $groupId, bool $checkAccess = true)
40 {
41 \Bitrix\Landing\Site\Type::setScope(
42 \Bitrix\Landing\Site\Type::SCOPE_CODE_GROUP
43 );
44
45 $groupId = intval($groupId);
46 $bindings = Binding\Group::getList($groupId);
47
48 if ($bindings)
49 {
50 $bindings = array_pop($bindings);
51
52 if ($bindings['ENTITY_TYPE'] == Binding\Entity::ENTITY_TYPE_SITE)
53 {
54 $hasAccess = !$checkAccess || Rights::hasAccessForSite(
55 $bindings['ENTITY_ID'],
57 );
58 if ($hasAccess)
59 {
60 return $bindings;
61 }
62 }
63 }
64
65 return [];
66 }
67
74 public static function getSocNetMenuUrl($groupId, $returnCreateLink = true)
75 {
76 if (Option::get(Group::MODULE_ID, Group::CHECKER_OPTION . $groupId, '') == 'Y')
77 {
78 return '';
79 }
80
81 // tariff limits
82 if (!Restriction\Manager::isAllowed('limit_crm_free_knowledge_base_project'))
83 {
84 $asset = \Bitrix\Main\Page\Asset::getInstance();
85 $asset->addString(
86 $asset->insertJs(
87 'var KnowledgeCreate = function()
88 {
89 ' . Restriction\Manager::getActionCode('limit_crm_free_knowledge_base_project') . '
90 };',
91 '',
92 true
93 )
94 );
95 return 'javascript:void(KnowledgeCreate());';
96 }
97
98 $link = '';
99 $groupId = intval($groupId);
100 $bindings = self::getBindingRow($groupId, false);
101
102 // binding exist
103 if ($bindings)
104 {
105 if (self::canPerformOperation($groupId, Rights::ACCESS_TYPES['read']))
106 {
107 $link = $bindings['PUBLIC_URL'];
108 self::processTabHit($link);
109 }
110 }
111 // binding don't exist, allow to create new one
112 else if ($returnCreateLink && self::canCreateNewBinding($groupId))
113 {
114 \CJSCore::init('sidepanel');
115 $link = SITE_DIR . str_replace('#groupId#', $groupId, self::PATH_GROUP_BINDING);
116 }
117
118 return $link;
119 }
120
126 public static function getSocNetMenuTitle($groupId)
127 {
128 $title = '';
129 $groupId = intval($groupId);
130 $bindings = self::getBindingRow($groupId, false);
131 if ($bindings['TITLE'])
132 {
133 $title = $bindings['TITLE'];
134 }
135 return $title;
136 }
137
143 public static function onFillSocNetFeaturesList(&$socNetFeaturesSettings)
144 {
146 if (
147 \Bitrix\Landing\Site\Type::isEnabled($scopeCode) &&
148 \Bitrix\Main\ModuleManager::isModuleInstalled('intranet')
149 )
150 {
151 $restrictedAccess = [SONET_ENTITY_GROUP => [SONET_ROLES_ALL, SONET_ROLES_AUTHORIZED]];
152
153 $socNetFeaturesSettings[self::SETTINGS_CODE] = [
154 'allowed' => [SONET_ENTITY_GROUP],
155 'title' => Loc::getMessage('LANDING_CONNECTOR_SN_TITLE'),
156 'operation_titles' => [
157 Rights::ACCESS_TYPES['read'] => Loc::getMessage('LANDING_CONNECTOR_SN_PERMS_READ'),
158 Rights::ACCESS_TYPES['edit'] => Loc::getMessage('LANDING_CONNECTOR_SN_PERMS_EDIT'),
159 Rights::ACCESS_TYPES['sett'] => Loc::getMessage('LANDING_CONNECTOR_SN_PERMS_SETT'),
160 Rights::ACCESS_TYPES['delete'] => Loc::getMessage('LANDING_CONNECTOR_SN_PERMS_DELETE'),
161 ],
162 'operations' => [
163 Rights::ACCESS_TYPES['read'] => [SONET_ENTITY_GROUP => SONET_ROLES_USER],
164 Rights::ACCESS_TYPES['edit'] => [SONET_ENTITY_GROUP => SONET_ROLES_USER, 'restricted' => $restrictedAccess],
165 Rights::ACCESS_TYPES['sett'] => [SONET_ENTITY_GROUP => SONET_ROLES_USER, 'restricted' => $restrictedAccess],
166 Rights::ACCESS_TYPES['delete'] => [SONET_ENTITY_GROUP => SONET_ROLES_USER, 'restricted' => $restrictedAccess],
167 ],
168 'minoperation' => ['read'],
169 ];
170 }
171 }
172
180 public static function onSocNetFeaturesUpdate(int $id, array $fields): void
181 {
182 $groupId = self::getGroupIdByFeatureId($id);
183
184 if ($groupId)
185 {
186 AddEventHandler('main', 'onEpilog', function() use($groupId)
187 {
188 $siteId = Binding\Group::getSiteIdByGroupId($groupId);
189 if ($siteId)
190 {
191 $binding = new \Bitrix\Landing\Binding\Group($groupId);
192 $binding->rebindSite($siteId);
193 }
194 });
195 }
196 }
197
203 public static function onFillSocNetMenu(&$result)
204 {
205 // allowed only for groups
206 if (!isset($result['Group']['ID']))
207 {
208 return;
209 }
210 if (!isset($result['Urls']['View']))
211 {
212 return;
213 }
214
215 // is enabled in features or not
216 if (!empty($result['ActiveFeatures']))
217 {
218 $enable = array_key_exists(
219 self::SETTINGS_CODE,
220 $result['ActiveFeatures']
221 );
222 }
223 else
224 {
225 $enable = false;
226 }
227
228 if ($enable)
229 {
230 $url = self::getSocNetMenuUrl($result['Group']['ID']);
231 if (!$url)
232 {
233 $enable = false;
234 }
235 $title = self::getSocNetMenuTitle($result['Group']['ID']);
236 if ($title !== '')
237 {
238 $title = ' - ' . $title;
239 }
240 }
241 else
242 {
243 $url = '';
244 $title = '';
245 }
246
247 // build menu params
248 $result['CanView'][self::SETTINGS_CODE] = $enable;
249 $result['Title'][self::SETTINGS_CODE] = Loc::getMessage('LANDING_CONNECTOR_SN_TITLE') . $title;
250 $result['Urls'][self::SETTINGS_CODE] = $url;
251 }
252
257 protected static function isExtranet()
258 {
259 if (\Bitrix\Main\Loader::includeModule('extranet'))
260 {
261 return \CExtranet::isExtranetSite();
262 }
263
264 return false;
265 }
266
272 protected static function processTabHit($url)
273 {
274 $request = \Bitrix\Main\Application::getInstance()->getContext()->getRequest();
275 if ($request->get('tab') == self::SETTINGS_CODE_SHORT)
276 {
277 if ($request->get('page'))
278 {
279 $url = $request->get('page');
280 }
281 $asset = \Bitrix\Main\Page\Asset::getInstance();
282 $asset->addString(
283 $asset->insertJs(
284 'BX.ready(function(){BX.SidePanel.Instance.open(\'' . \CUtil::jsEscape($url) . '\');});',
285 '',
286 true
287 )
288 );
289 }
290 }
291
299 public static function getTabUrl(int $groupId, ?string $pagePath = null, bool $generalPath = false): ?string
300 {
301 static $groupPath = null;
302
303 if ($groupPath === null)
304 {
305 $groupPath = Option::get('socialnetwork', 'group_path_template', '', SITE_ID);
306 if (mb_substr($groupPath, -1) == '/')
307 {
308 $groupPath .= 'general/';
309 }
310 }
311
312 if ($groupId && $groupPath)
313 {
314 $groupPath = str_replace('#group_id#', $groupId, $groupPath);
315 }
316
317 if ($generalPath)
318 {
319 return $groupPath;
320 }
321
322 if ($groupId && $groupPath)
323 {
324 $uri = new \Bitrix\Main\Web\Uri($groupPath);
325 $uri->addParams([
326 'tab' => self::SETTINGS_CODE_SHORT
327 ]);
328 if ($pagePath)
329 {
330 $uri->addParams([
331 'page' => $pagePath
332 ]);
333 }
334 return $uri->getUri();
335 }
336
337 return null;
338 }
339
346 public static function userInGroup(int $groupId): bool
347 {
348 if (\Bitrix\Main\Loader::includeModule('socialnetwork'))
349 {
350 return \CSocNetUserToGroup::getUserRole(
351 Manager::getUserId(),
352 $groupId
353 ) <= SONET_ROLES_USER;
354 }
355
356 return false;
357 }
358
367 public static function canPerformOperation(int $groupId, string $operation): bool
368 {
369 if ($groupId && \Bitrix\Main\Loader::includeModule('socialnetwork'))
370 {
371 return \CSocNetFeaturesPerms::canPerformOperation(
372 Manager::getUserId(),
373 SONET_ENTITY_GROUP,
374 $groupId,
375 self::SETTINGS_CODE,
376 $operation
377 );
378 }
379
380 return false;
381 }
382
389 public static function canCreateNewBinding(int $groupId): bool
390 {
391 $operation = Rights::ACCESS_TYPES['edit'];
392 return self::userInGroup($groupId) && self::canPerformOperation($groupId, $operation);
393 }
394
400 public static function onSocNetGroupDelete($groupId)
401 {
402 \Bitrix\Landing\Site\Type::setScope(
403 \Bitrix\Landing\Site\Type::SCOPE_CODE_GROUP
404 );
405 $bindings = Binding\Group::getList($groupId);
406 foreach ($bindings as $binding)
407 {
408 if ($binding['ENTITY_TYPE'] == Binding\Group::ENTITY_TYPE_SITE)
409 {
410 Site::delete($binding['ENTITY_ID'], true)->isSuccess();
411 }
412 }
413 }
414
421 private static function getGroupIdByFeatureId(int $featureId): ?int
422 {
423 static $featureToGroup = null;
424
425 if ($featureToGroup === null)
426 {
427 $res = \CSocNetFeatures::getList(
428 [],
429 [
430 'ENTITY_TYPE' => SONET_ENTITY_GROUP,
431 'FEATURE' => self::SETTINGS_CODE,
432 ],
433 false, false,
434 ['ID', 'ENTITY_ID']
435 );
436 while ($row = $res->fetch())
437 {
438 $featureToGroup[$row['ID']] = $row['ENTITY_ID'];
439 }
440 }
441
442 return $featureToGroup[$featureId] ?? null;
443 }
444}
static onSocNetFeaturesUpdate(int $id, array $fields)
static onFillSocNetFeaturesList(&$socNetFeaturesSettings)
static getBindingRow(int $groupId, bool $checkAccess=true)
static getSocNetMenuUrl($groupId, $returnCreateLink=true)
static hasAccessForSite($siteId, $accessType, $deleted=false)
Definition rights.php:544
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29