Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgroup.php
1<?php
2
10
11use Bitrix\Main;
22
23Loc::loadMessages(__FILE__);
24
26{
27 protected const UF_ENTITY_ID = 'SONET_GROUP';
28
29 private $fields;
30 protected static $groupsIdToCheckList = [];
31
32 public function __construct()
33 {
34 $this->fields = array();
35 }
36
42 public static function getById($groupId = 0, $useCache = true)
43 {
44 global $USER_FIELD_MANAGER;
45
46 static $cachedFields = [];
47
48 $groupItem = false;
49 $groupId = (int)$groupId;
50
51 if ($groupId > 0)
52 {
53 $groupItem = new Workgroup;
54 $groupFields = [];
55
56 if ($useCache && isset($cachedFields[$groupId]))
57 {
58 $groupFields = $cachedFields[$groupId];
59 }
60 else
61 {
62 $res = WorkgroupTable::getList(array(
63 'filter' => array('=ID' => $groupId)
64 ));
65 if ($fields = $res->fetch())
66 {
67 $groupFields = $fields;
68
69 if ($groupFields['DATE_CREATE'] instanceof \Bitrix\Main\Type\DateTime)
70 {
71 $groupFields['DATE_CREATE'] = $groupFields['DATE_CREATE']->toString();
72 }
73 if ($groupFields['DATE_UPDATE'] instanceof \Bitrix\Main\Type\DateTime)
74 {
75 $groupFields['DATE_UPDATE'] = $groupFields['DATE_UPDATE']->toString();
76 }
77 if ($groupFields['DATE_ACTIVITY'] instanceof \Bitrix\Main\Type\DateTime)
78 {
79 $groupFields['DATE_ACTIVITY'] = $groupFields['DATE_ACTIVITY']->toString();
80 }
81
82 $uf = $USER_FIELD_MANAGER->getUserFields(self::UF_ENTITY_ID, $groupId, false, 0);
83 if (is_array($uf))
84 {
85 $groupFields = array_merge($groupFields, $uf);
86 }
87 }
88
89 $cachedFields[$groupId] = $groupFields;
90 }
91
92 $groupItem->setFields($groupFields);
93 }
94
95 return $groupItem;
96 }
97
98 public function setFields($fields = array()): void
99 {
100 $this->fields = $fields;
101 }
102
103 public function getFields(): array
104 {
105 return $this->fields;
106 }
107
108 public function isProject(): bool
109 {
110 return (
111 isset($this->fields['PROJECT'])
112 && $this->fields['PROJECT'] === 'Y'
113 );
114 }
115
116 public function isScrumProject(): bool
117 {
118 return (!empty($this->fields['SCRUM_MASTER_ID']));
119 }
120
121 public function getDefaultSprintDuration(): int
122 {
123 return ($this->fields['SCRUM_SPRINT_DURATION'] ? : 0);
124 }
125
126 public function getScrumMaster(): int
127 {
128 return ($this->fields['SCRUM_MASTER_ID'] ? : 0);
129 }
130
131 public function getScrumTaskResponsible(): string
132 {
133 if ($this->fields['SCRUM_TASK_RESPONSIBLE'])
134 {
135 $scrumTaskResponsible = $this->fields['SCRUM_TASK_RESPONSIBLE'];
136 $availableResponsibleTypes = ['A', 'M'];
137 return (
138 in_array($scrumTaskResponsible, $availableResponsibleTypes, true) ? $scrumTaskResponsible : 'A'
139 );
140 }
141
142 return 'A';
143 }
144
145 public function syncDeptConnection($exclude = false): void
146 {
147 global $USER;
148
149 if (!ModuleManager::isModuleInstalled('intranet'))
150 {
151 return;
152 }
153
154 $groupFields = $this->getFields();
155
156 if (
157 empty($groupFields)
158 || empty($groupFields["ID"])
159 )
160 {
161 return;
162 }
163
164 if (
165 isset($groupFields['UF_SG_DEPT']['VALUE'])
166 && Loader::includeModule('intranet')
167 )
168 {
169 $workgroupsToSync = Option::get('socialnetwork', 'workgroupsToSync', "");
170 $workgroupsToSync = ($workgroupsToSync !== "" ? @unserialize($workgroupsToSync, [ 'allowed_classes' => false ]) : []);
171 if (!is_array($workgroupsToSync))
172 {
173 $workgroupsToSync = [];
174 }
175 $workgroupsToSync[] = array(
176 'groupId' => $groupFields["ID"],
177 'initiatorId' => (is_object($USER) ? $USER->getId() : $groupFields['OWNER_ID']),
178 'exclude' => $exclude
179 );
180 $workgroupsToSync = $this->reduceSyncList($workgroupsToSync);
181 Option::set('socialnetwork', 'workgroupsToSync', serialize($workgroupsToSync));
182 \Bitrix\Socialnetwork\Update\WorkgroupDeptSync::bind(1);
183 }
184 }
185
186 public function getGroupUrlData($params = array())
187 {
188 static $cache = array();
189
190 $groupFields = $this->getFields();
191 $userId = (int)($params['USER_ID'] ?? 0);
192
193 if (
194 !empty($cache)
195 && !empty($cache[$groupFields["ID"]])
196 )
197 {
198 $groupUrlTemplate = $cache[$groupFields['ID']]['URL_TEMPLATE'];
199 $groupSiteId = $cache[$groupFields['ID']]['SITE_ID'];
200 }
201 else
202 {
203 $groupSiteId = \CSocNetGroup::getDefaultSiteId($groupFields["ID"], $groupFields["SITE_ID"]);
204 $workgroupsPage = Option::get("socialnetwork", "workgroups_page", "/workgroups/", SITE_ID);
205 $groupUrlTemplate = Helper\Path::get('group_path_template');
206 $groupUrlTemplate = "#GROUPS_PATH#".mb_substr($groupUrlTemplate, mb_strlen($workgroupsPage), mb_strlen($groupUrlTemplate) - mb_strlen($workgroupsPage));
207
208 $cache[$groupFields["ID"]] = array(
209 'URL_TEMPLATE' => $groupUrlTemplate ,
210 'SITE_ID' => $groupSiteId
211 );
212 }
213
214 $groupUrl = str_replace(array("#group_id#", "#GROUP_ID#"), $groupFields["ID"], $groupUrlTemplate);
215 $serverName = $domainName = '';
216
217 if ($userId > 0)
218 {
219 $tmp = \CSocNetLogTools::processPath(
220 [
221 'GROUP_URL' => $groupUrl,
222 ],
223 $userId,
224 $groupSiteId
225 );
226
227 $groupUrl = $tmp["URLS"]["GROUP_URL"];
228 $serverName = (mb_strpos($groupUrl, "http://") === 0 || mb_strpos($groupUrl, "https://") === 0 ? "" : $tmp["SERVER_NAME"]);
229 $domainName = (mb_strpos($groupUrl, "http://") === 0 || mb_strpos($groupUrl, "https://") === 0 ? "" : (isset($tmp["DOMAIN"]) && !empty($tmp["DOMAIN"]) ? "//".$tmp["DOMAIN"] : ""));
230 }
231
232 return [
233 'URL' => $groupUrl,
234 'SERVER_NAME' => $serverName,
235 'DOMAIN' => $domainName
236 ];
237 }
238
239 public static function onBeforeIBlockSectionUpdate($section)
240 {
241 if (
242 !isset($section['ID'], $section['IBLOCK_ID'])
243 || (int)$section['ID'] <= 0
244 || (int)$section['IBLOCK_ID'] <= 0
245 || (
246 isset($section['ACTIVE'])
247 && $section['ACTIVE'] === 'N'
248 )
249 || (int)$section['IBLOCK_ID'] !== (int)Option::get('intranet', 'iblock_structure', 0)
250 )
251 {
252 return true;
253 }
254
255 $rootSectionIdList = [];
256 $res = \CIBlockSection::getNavChain($section['IBLOCK_ID'], $section['ID'], array('ID'));
257 while ($rootSection = $res->fetch())
258 {
259 if ((int)$rootSection['ID'] !== (int)$section['ID'])
260 {
261 $rootSectionIdList[] = $rootSection['ID'];
262 }
263 }
264
265 if (!empty($rootSectionIdList))
266 {
267 $groupList = UserToGroup::getConnectedGroups($rootSectionIdList);
268 self::$groupsIdToCheckList = array_merge(self::$groupsIdToCheckList, $groupList);
269 }
270
271 return true;
272 }
273
274 public static function onAfterIBlockSectionUpdate($section)
275 {
276 if(
277 !isset($section['ID'], $section['IBLOCK_ID'])
278 || (int)$section['ID'] <= 0
279 || (int)$section['IBLOCK_ID'] <= 0
280 || (int)$section['IBLOCK_ID'] !== (int)Option::get('intranet', 'iblock_structure', 0)
281 )
282 {
283 return true;
284 }
285
286 $oldGroupsIdToCheckList = self::$groupsIdToCheckList;
287 $newGroupsIdToCheckList = [];
288
289 if (
290 isset($section['ACTIVE'])
291 && $section['ACTIVE'] === 'N'
292 )
293 {
294 self::disconnectSection($section['ID']);
295 }
296 else
297 {
298 $res = \CIBlockSection::getNavChain($section['IBLOCK_ID'], $section['ID'], array('ID'));
299 while ($rootSection = $res->fetch())
300 {
301 if ((int)$rootSection['ID'] !== (int)$section['ID'])
302 {
303 $rootSectionIdList[] = $rootSection['ID'];
304 }
305 }
306
307 if (!empty($rootSectionIdList))
308 {
309 $newGroupsIdToCheckList = UserToGroup::getConnectedGroups($rootSectionIdList);
310 }
311 }
312
313 if (!empty($oldGroupsIdToCheckList))
314 {
315 $oldGroupsIdToCheckList = array_unique($oldGroupsIdToCheckList);
316 foreach($oldGroupsIdToCheckList as $groupId)
317 {
318 $groupItem = self::getById($groupId, false);
319 $groupItem->syncDeptConnection(true);
320 }
321 }
322
323 if (!empty($newGroupsIdToCheckList))
324 {
325 $newGroupsIdToCheckList = array_unique($newGroupsIdToCheckList);
326 foreach($newGroupsIdToCheckList as $groupId)
327 {
328 $groupItem = self::getById($groupId, false);
329 $groupItem->syncDeptConnection();
330 }
331 }
332
333 return true;
334 }
335
336 public static function onBeforeIBlockSectionDelete($sectionId)
337 {
338 if ((int)$sectionId <= 0)
339 {
340 return true;
341 }
342
343 $res = \CIBlockSection::getList(
344 [],
345 [ 'ID' => $sectionId ],
346 false,
347 [ 'ID', 'IBLOCK_ID' ]
348 );
349 if (
350 !($section = $res->fetch())
351 || !isset($section['IBLOCK_ID'])
352 || (int)$section['IBLOCK_ID'] <= 0
353 || (
354 isset($section['ACTIVE'])
355 && $section['ACTIVE'] === 'N'
356 )
357 || (int)$section['IBLOCK_ID'] !== (int)Option::get('intranet', 'iblock_structure', 0)
358 )
359 {
360 return true;
361 }
362
363 $rootSectionIdList = [];
364 $res = \CIBlockSection::getNavChain($section['IBLOCK_ID'], $section['ID'], array('ID'));
365 while ($rootSection = $res->fetch())
366 {
367 if ((int)$rootSection['ID'] !== (int)$section['ID'])
368 {
369 $rootSectionIdList[] = $rootSection['ID'];
370 }
371 }
372
373 if (!empty($rootSectionIdList))
374 {
375 $groupList = UserToGroup::getConnectedGroups($rootSectionIdList);
376 self::$groupsIdToCheckList = array_merge(self::$groupsIdToCheckList, $groupList);
377 }
378
379 return true;
380 }
381
382 public static function onAfterIBlockSectionDelete($section): bool
383 {
384 if(
385 !isset($section['ID'], $section['IBLOCK_ID'])
386 || (int)$section['ID'] <= 0
387 || (int)$section['IBLOCK_ID'] <= 0
388 || (int)$section['IBLOCK_ID'] !== (int)Option::get('intranet', 'iblock_structure', 0)
389 )
390 {
391 return true;
392 }
393
394 self::disconnectSection($section['ID']);
395
396 if (!empty(self::$groupsIdToCheckList))
397 {
398 $groupsToCheck = array_unique(self::$groupsIdToCheckList);
399 foreach($groupsToCheck as $groupId)
400 {
401 $groupItem = self::getById($groupId, false);
402 $groupItem->syncDeptConnection();
403 }
404 }
405
406 return true;
407 }
408
409 private static function disconnectSection($sectionId): void
410 {
411 $groupList = array();
412 $res = WorkgroupTable::getList(array(
413 'filter' => array(
414 '=UF_SG_DEPT' => $sectionId
415 ),
416 'select' => array('ID', 'UF_SG_DEPT')
417 ));
418 while($group = $res->fetch())
419 {
420 $groupList[] = $group;
421 }
422
423 foreach($groupList as $group)
424 {
425 $departmentListOld = array_map('intval', $group['UF_SG_DEPT']);
426 $departmentListNew = array_diff($departmentListOld, array($sectionId));
427
428 \CSocNetGroup::update($group['ID'], array(
429 'UF_SG_DEPT' => $departmentListNew
430 ));
431
432 $groupItem = self::getById($group['ID'], false);
433 $groupItem->syncDeptConnection(true);
434 }
435 }
436
437 public static function getTypes($params = []): array
438 {
439 return Helper\Workgroup::getTypes($params);
440 }
441
442 public static function getPresets($params = array()): array
443 {
444 return Helper\Workgroup::getPresets($params);
445 }
446
447 private static function getGroupContent($params = array()): string
448 {
449 static $fieldsList = null;;
450
451 $content = '';
452
453 $groupId = (int)($params['id'] ?? 0);
454
455 if ($groupId <= 0)
456 {
457 return $content;
458 }
459
460 if ($fieldsList === null)
461 {
462 $fieldsList = self::getContentFieldsList();
463 }
464
465 if (
466 isset($params['fields'])
467 && is_array($params['fields'])
468 && ($diff = array_diff($fieldsList, array_keys($params['fields'])))
469 && empty($diff)
470 )
471 {
472 $groupFieldsList = $params['fields'];
473 }
474 else
475 {
476 $res = WorkgroupTable::getList(array(
477 'filter' => array(
478 'ID' => $groupId
479 ),
480 'select' => $fieldsList
481 ));
482 $groupFieldsList = $res->fetch();
483 }
484
485 if (!empty($groupFieldsList))
486 {
487 $content .= $groupFieldsList['NAME'];
488 if (!empty($groupFieldsList['DESCRIPTION']))
489 {
490 $content .= ' '.$groupFieldsList['DESCRIPTION'];
491 }
492
493 if (!empty($groupFieldsList['KEYWORDS']))
494 {
495 $keywordList = explode(",", $groupFieldsList["KEYWORDS"]);
496 $tagList = array();
497 foreach($keywordList as $keyword)
498 {
499 $tagList[] = trim($keyword);
500 $tagList[] = '#'.trim($keyword);
501 }
502 if (!empty($tagList))
503 {
504 $content .= ' '.implode(' ', $tagList);
505 }
506 }
507
508 if (
509 !empty($groupFieldsList['OWNER_ID'])
510 && (int)$groupFieldsList['OWNER_ID'] > 0
511 )
512 {
513 $res = Main\UserTable::getList(array(
514 'filter' => array(
515 'ID' => (int)$groupFieldsList['OWNER_ID']
516 ),
517 'select' => array('ID', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'LOGIN', 'EMAIL')
518 ));
519 if ($userFields = $res->fetch())
520 {
521 $content .= ' '.\CUser::formatName(\CSite::getNameFormat(null, $groupFieldsList['SITE_ID']), $userFields, true);
522 }
523 }
524 }
525
526 return $content;
527 }
528
529 public static function setIndex($params = array()): void
530 {
531 global $DB;
532
533 static $connection = null;
534
535 if (!is_array($params))
536 {
537 return;
538 }
539
540 $fields = (isset($params['fields']) ? $params['fields'] : array());
541
542 if (
543 !is_array($fields)
544 || empty($fields)
545 )
546 {
547 return;
548 }
549
550 $groupId = (isset($fields['ID']) ? intval($fields['ID']) : 0);
551
552 if ($groupId <= 0)
553 {
554 return;
555 }
556
557 $content = self::getGroupContent(array(
558 'id' => $groupId,
559 'fields' => $fields
560 ));
561
562 $content = self::prepareToken($content);
563
564 $event = new Main\Event(
565 'socialnetwork',
566 'onWorkgroupIndexGetContent',
567 array(
568 'groupId' => $groupId,
569 )
570 );
571 $event->send();
572
573 foreach($event->getResults() as $eventResult)
574 {
575 if ($eventResult->getType() == \Bitrix\Main\EventResult::SUCCESS)
576 {
577 $eventParams = $eventResult->getParameters();
578
579 if (
580 is_array($eventParams)
581 && isset($eventParams['content'])
582 )
583 {
584 $eventContent = $eventParams['content'];
585 if (Main\Loader::includeModule('search'))
586 {
587 $eventContent = \CSearch::killTags($eventContent);
588 }
589 $eventContent = trim(str_replace(
590 array("\r", "\n", "\t"),
591 " ",
592 $eventContent
593 ));
594
595 $eventContent = self::prepareToken($eventContent);
596 if (!empty($eventContent))
597 {
598 $content .= ' '.$eventContent;
599 }
600 }
601 }
602 }
603
604 if (!empty($content))
605 {
606 if ($connection === null)
607 {
608 $connection = \Bitrix\Main\Application::getConnection();
609 }
610
611 $value = $DB->forSql($content);
612 $encryptedValue = sha1($content);
613
614 $connection->query("UPDATE ".WorkgroupTable::getTableName()." SET SEARCH_INDEX = IF(SHA1(SEARCH_INDEX) = '{$encryptedValue}', SEARCH_INDEX, '{$value}') WHERE ID = {$groupId}");
615 }
616 }
617
618 public static function getContentFieldsList(): array
619 {
620 return [ 'NAME', 'DESCRIPTION', 'OWNER_ID', 'KEYWORDS', 'SITE_ID' ];
621 }
622
623 public static function prepareToken($str)
624 {
625 return str_rot13($str);
626 }
627
628 public static function getInitiatePermOptionsList(array $params = []): array
629 {
630 $ownerValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_OWNER');
631 $moderatorsValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_MOD');
632 $userValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_USER');
633
634 if (
635 isset($params['scrum'])
636 && $params['scrum']
637 )
638 {
639 $ownerValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_OWNER_SCRUM2');
640 $moderatorsValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_MOD_SCRUM2');
641 $userValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_USER_SCRUM');
642 }
643 elseif (
644 isset($params['project'])
645 && $params['project']
646 )
647 {
648 $ownerValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_OWNER_PROJECT');
649 $moderatorsValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_MOD_PROJECT');
650 $userValue = Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_USER_PROJECT');
651 }
652
653 return [
654 UserToGroupTable::ROLE_OWNER => $ownerValue,
655 UserToGroupTable::ROLE_MODERATOR => $moderatorsValue,
656 UserToGroupTable::ROLE_USER => $userValue,
657 ];
658 }
659
660 public static function getSpamPermOptionsList(): array
661 {
662 return [
663 UserToGroupTable::ROLE_OWNER => Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_OWNER'),
664 UserToGroupTable::ROLE_MODERATOR => Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_MOD'),
665 UserToGroupTable::ROLE_USER => Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_USER'),
666 SONET_ROLES_ALL => Loc::getMessage('SOCIALNETWORK_ITEM_WORKGROUP_IP_ALL'),
667 ];
668 }
669
675 public static function getByFeatureOperation(array $params = []): array
676 {
677 return Helper\Workgroup::getByFeatureOperation($params);
678 }
679
680 public static function getListSprintDuration(): array
681 {
682 return Helper\Workgroup::getListSprintDuration();
683 }
684
685 public static function getScrumTaskResponsibleList(): array
686 {
687 return Helper\Workgroup::getScrumTaskResponsibleList();
688 }
689
690 public static function getTypeCodeByParams($params)
691 {
692 return Helper\Workgroup::getTypeCodeByParams($params);
693 }
694
695 public static function getTypeByCode($params = [])
696 {
697 return Helper\Workgroup::getTypeByCode($params);
698 }
699
700 public static function getEditFeaturesAvailability()
701 {
702 return Helper\Workgroup::getEditFeaturesAvailability();
703 }
704
705 public static function canWorkWithClosedWorkgroups(): bool
706 {
707 static $optionValue = null;
708 if ($optionValue === null)
709 {
710 $optionValue = Option::get('socialnetwork', 'work_with_closed_groups', 'N');
711 }
712
713 return ($optionValue === 'Y');
714 }
715
716 private function reduceSyncList(array $workgroupsToSync = []): array
717 {
718 $result = [];
719
720 foreach ($workgroupsToSync as $workgroupData)
721 {
722 $workgroupId = (int) $workgroupData['groupId'];
723 $result[$workgroupId] = $workgroupData;
724 }
725
726 return array_values($result);
727 }
728}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onBeforeIBlockSectionDelete($sectionId)
static onAfterIBlockSectionDelete($section)
static onBeforeIBlockSectionUpdate($section)
static getById($groupId=0, $useCache=true)
Definition workgroup.php:42
static onAfterIBlockSectionUpdate($section)
static getByFeatureOperation(array $params=[])
static getInitiatePermOptionsList(array $params=[])
static getPresets($params=array())
static setIndex($params=array())