Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
provider.php
1<?php
2
4
5use Bitrix\Disk\AttachedObject;
12use Bitrix\Socialnetwork\Internals\LiveFeed\Counter\CounterService;
18
19Loc::loadMessages(__FILE__);
20
21abstract class Provider
22{
23 public const DATA_RESULT_TYPE_SOURCE = 'SOURCE';
24
25 public const TYPE_POST = 'POST';
26 public const TYPE_COMMENT = 'COMMENT';
27
28 public const DATA_ENTITY_TYPE_BLOG_POST = 'BLOG_POST';
29 public const DATA_ENTITY_TYPE_BLOG_COMMENT = 'BLOG_COMMENT';
30 public const DATA_ENTITY_TYPE_TASKS_TASK = 'TASK';
31 public const DATA_ENTITY_TYPE_FORUM_TOPIC = 'FORUM_TOPIC';
32 public const DATA_ENTITY_TYPE_FORUM_POST = 'FORUM_POST';
33 public const DATA_ENTITY_TYPE_CALENDAR_EVENT = 'CALENDAR_EVENT';
34 public const DATA_ENTITY_TYPE_LOG_ENTRY = 'LOG_ENTRY';
35 public const DATA_ENTITY_TYPE_LOG_COMMENT = 'LOG_COMMENT';
36 public const DATA_ENTITY_TYPE_RATING_LIST = 'RATING_LIST';
37 public const DATA_ENTITY_TYPE_PHOTOGALLERY_ALBUM = 'PHOTO_ALBUM';
38 public const DATA_ENTITY_TYPE_PHOTOGALLERY_PHOTO = 'PHOTO_PHOTO';
39 public const DATA_ENTITY_TYPE_LISTS_ITEM = 'LISTS_NEW_ELEMENT';
40 public const DATA_ENTITY_TYPE_WIKI = 'WIKI';
41 public const DATA_ENTITY_TYPE_TIMEMAN_ENTRY = 'TIMEMAN_ENTRY';
42 public const DATA_ENTITY_TYPE_TIMEMAN_REPORT = 'TIMEMAN_REPORT';
43 public const DATA_ENTITY_TYPE_INTRANET_NEW_USER = 'INTRANET_NEW_USER';
44 public const DATA_ENTITY_TYPE_BITRIX24_NEW_USER = 'BITRIX24_NEW_USER';
45
46 public const PERMISSION_DENY = 'D';
47 public const PERMISSION_READ = 'I';
48 public const PERMISSION_FULL = 'W';
49
50 public const CONTENT_TYPE_ID = '';
51
52 protected $entityId = 0;
53 protected $additionalParams = [];
54 protected $logId = 0;
55 protected $sourceFields = [];
56 protected $siteId = false;
57 protected $options = [];
58 protected string $ratingTypeId = '';
59 protected int|null $ratingEntityId = null;
60 protected $parentProvider = false;
61
62 protected $cloneDiskObjects = false;
63 protected $sourceDescription = '';
64 protected $sourceTitle = '';
65 protected $pinnedTitle = '';
66 protected $sourceOriginalText = '';
67 protected $sourceAuxData = [];
69 protected $sourceDiskObjects = [];
70 protected $diskObjectsCloned = [];
72 protected $sourceDateTime = null;
73 protected $sourceAuthorId = 0;
74
75 protected $logEventId = null;
76 protected $logEntityType = null;
77 protected $logEntityId = null;
78
79 protected static $logTable = LogTable::class;
80
84 public static function className(): string
85 {
86 return static::class;
87 }
88
89 public function setSiteId($siteId): void
90 {
91 $this->siteId = $siteId;
92 }
93
94 public function getSiteId()
95 {
96 return $this->siteId;
97 }
98
105 public function setOption(string $key, $value): void
106 {
107 $this->options[$key] = $value;
108 }
109
115 public function getOption(string $key)
116 {
117 return ($this->options[$key] ?? null);
118 }
119
120 public static function getId()
121 {
122 return 'BASE';
123 }
124
125 public function getEventId()
126 {
127 return false;
128 }
129
130 public function getType()
131 {
132 return '';
133 }
134
135 public function getRatingTypeId(): string
136 {
137 return $this->ratingTypeId;
138 }
139
140 public function setRatingTypeId(string $value): void
141 {
142 $this->ratingTypeId = $value;
143 }
144
145 public function getRatingEntityId(): int|null
146 {
148 }
149
150 public function setRatingEntityId(int $value): void
151 {
152 $this->ratingEntityId = $value;
153 }
154
155 public function getUserTypeEntityId(): string
156 {
157 return '';
158 }
159
160 public function getCommentProvider()
161 {
162 return false;
163 }
164
165 public function setParentProvider($value): void
166 {
167 $this->parentProvider = $value;
168 }
169
170 public function getParentProvider()
171 {
173 }
174
175 private static function getTypes(): array
176 {
177 return [
180 ];
181 }
182
183 final public static function getProvider($entityType)
184 {
185 $provider = false;
186
187 $moduleEvent = new Main\Event(
188 'socialnetwork',
189 'onLogProviderGetProvider',
190 [
191 'entityType' => $entityType
192 ]
193 );
194 $moduleEvent->send();
195
196 foreach ($moduleEvent->getResults() as $moduleEventResult)
197 {
198 if ($moduleEventResult->getType() === EventResult::SUCCESS)
199 {
200 $moduleEventParams = $moduleEventResult->getParameters();
201
202 if (
203 is_array($moduleEventParams)
204 && !empty($moduleEventParams['provider'])
205 )
206 {
207 $provider = $moduleEventParams['provider'];
208 }
209 break;
210 }
211 }
212
213 if (!$provider)
214 {
215 switch($entityType)
216 {
218 $provider = new BlogPost();
219 break;
221 $provider = new BlogComment();
222 break;
224 $provider = new TasksTask();
225 break;
227 $provider = new ForumTopic();
228 break;
230 $provider = new ForumPost();
231 break;
233 $provider = new CalendarEvent();
234 break;
236 $provider = new LogEvent();
237 break;
239 $provider = new LogComment();
240 break;
242 $provider = new RatingVoteList();
243 break;
245 $provider = new PhotogalleryAlbum();
246 break;
248 $provider = new PhotogalleryPhoto();
249 break;
251 $provider = new ListsItem();
252 break;
254 $provider = new Wiki();
255 break;
257 $provider = new TimemanEntry();
258 break;
260 $provider = new TimemanReport();
261 break;
263 $provider = new IntranetNewUser();
264 break;
266 $provider = new Bitrix24NewUser();
267 break;
268 default:
269 $provider = false;
270 }
271 }
272
273 return $provider;
274 }
275
276 public static function init(array $params)
277 {
278 $provider = self::getProvider($params['ENTITY_TYPE']);
279
280 if ($provider)
281 {
282 $provider->setEntityId($params['ENTITY_ID']);
283 $provider->setSiteId($params['SITE_ID'] ?? SITE_ID);
284
285 if (
286 isset($params['CLONE_DISK_OBJECTS'])
287 && $params['CLONE_DISK_OBJECTS'] === true
288 )
289 {
290 $provider->cloneDiskObjects = true;
291 }
292
293 if (
294 isset($params['LOG_ID'])
295 && (int)$params['LOG_ID'] > 0
296 )
297 {
298 $provider->setLogId((int)$params['LOG_ID']);
299 }
300
301 if (isset($params['RATING_TYPE_ID']))
302 {
303 $provider->setRatingTypeId($params['RATING_TYPE_ID']);
304 }
305
306 if (isset($params['RATING_ENTITY_ID']))
307 {
308 $provider->setRatingEntityId($params['RATING_ENTITY_ID']);
309 }
310
311 if (
312 isset($params['ADDITIONAL_PARAMS'])
313 && is_array($params['ADDITIONAL_PARAMS'])
314 )
315 {
316 $provider->setAdditionalParams($params['ADDITIONAL_PARAMS']);
317 }
318 }
319
320 return $provider;
321 }
322
323 public static function canRead($params)
324 {
325 return false;
326 }
327
328 protected function getPermissions(array $entity)
329 {
331 }
332
333 public function getLogId($params = [])
334 {
335 $result = false;
336
337 if ($this->logId > 0)
338 {
339 $result = $this->logId;
340 }
341 else
342 {
343 $eventId = $this->getEventId();
344
345 if (
346 empty($eventId)
347 || $this->entityId <= 0
348 )
349 {
350 return $result;
351 }
352
353 if ($this->getType() === Provider::TYPE_POST)
354 {
355 $filter = [
356 'EVENT_ID' => $eventId
357 ];
358
359 if (static::getId() === LogEvent::PROVIDER_ID)
360 {
361 $filter['=ID'] = $this->entityId;
362 }
363 else
364 {
365 $filter['=SOURCE_ID'] = $this->entityId;
366 }
367
368 if (
369 is_array($params)
370 && isset($params['inactive'])
371 && $params['inactive']
372 )
373 {
374 $filter['=INACTIVE'] = 'Y';
375 }
376
377 $res = \CSocNetLog::getList(
378 [],
379 $filter,
380 false,
381 [ 'nTopCount' => 1 ],
382 [ 'ID' ]
383 );
384
385 $logEntry = $res->fetch();
386 if (
387 !$logEntry
388 && static::getId() === TasksTask::PROVIDER_ID
389 && Loader::includeModule('crm')
390 )
391 {
392 $res = \CCrmActivity::getList(
393 [],
394 [
395 'ASSOCIATED_ENTITY_ID' => $this->entityId,
396 'TYPE_ID' => \CCrmActivityType::Task,
397 'CHECK_PERMISSIONS' => 'N'
398 ],
399 false,
400 false,
401 [ 'ID' ]
402 );
403 if ($activityFields = $res->fetch())
404 {
405 $res = \CSocNetLog::getList(
406 [],
407 [
408 'EVENT_ID' => $eventId,
409 '=ENTITY_TYPE' => 'CRMACTIVITY',
410 '=ENTITY_ID' => $activityFields['ID'],
411 ],
412 false,
413 [ 'nTopCount' => 1 ],
414 [ 'ID' ]
415 );
416 $logEntry = $res->fetch();
417 }
418 }
419
420 if (
421 $logEntry
422 && ((int)$logEntry['ID'] > 0)
423 )
424 {
425 $result = $this->logId = (int)$logEntry['ID'];
426 }
427 }
428 elseif ($this->getType() === Provider::TYPE_COMMENT)
429 {
430 $filter = [
431 'EVENT_ID' => $eventId
432 ];
433
434 if (static::getId() === LogComment::PROVIDER_ID)
435 {
436 $filter['ID'] = $this->entityId;
437 }
438 else
439 {
440 $filter['SOURCE_ID'] = $this->entityId;
441 }
442
443 $res = \CSocNetLogComments::getList(
444 [],
445 $filter,
446 false,
447 [ 'nTopCount' => 1 ],
448 [ 'ID', 'LOG_ID' ]
449 );
450
451 if (
452 ($logCommentEntry = $res->fetch())
453 && ((int)$logCommentEntry['LOG_ID'] > 0)
454 )
455 {
456 $result = $this->logId = (int)$logCommentEntry['LOG_ID'];
457 }
458 }
459 }
460
461 return $result;
462 }
463
464 public function getLogCommentId()
465 {
466 $result = false;
467
468 $eventId = $this->getEventId();
469 if (
470 empty($eventId)
471 || $this->getType() !== self::TYPE_COMMENT
472 )
473 {
474 return $result;
475 }
476
477 $filter = [
478 'EVENT_ID' => $eventId
479 ];
480
481 if (static::getId() === LogComment::PROVIDER_ID)
482 {
483 $filter['ID'] = $this->entityId;
484 }
485 else
486 {
487 $filter['SOURCE_ID'] = $this->entityId;
488 }
489
490 $res = \CSocNetLogComments::getList(
491 [],
492 $filter,
493 false,
494 [ 'nTopCount' => 1 ],
495 [ 'ID', 'LOG_ID' ]
496 );
497
498 if ($logCommentEntry = $res->fetch())
499 {
500 $result = (int)$logCommentEntry['ID'];
501 if ((int)$logCommentEntry['LOG_ID'] > 0)
502 {
503 $this->logId = (int)$logCommentEntry['LOG_ID'];
504 }
505 }
506
507 return $result;
508 }
509
510 public function getSonetGroupsAvailable($feature = false, $operation = false): array
511 {
512 global $USER;
513
514 $result = [];
515
516 $logRights = $this->getLogRights();
517 if (
518 !empty($logRights)
519 && is_array($logRights)
520 )
521 {
522 foreach ($logRights as $groupCode)
523 {
524 if (preg_match('/^SG(\d+)/', $groupCode, $matches))
525 {
526 $result[] = (int)$matches[1];
527 }
528 }
529 }
530
531 if (
532 !empty($result)
533 && !!$feature
534 && !!$operation
535 )
536 {
537 $activity = \CSocNetFeatures::isActiveFeature(
538 SONET_ENTITY_GROUP,
539 $result,
540 $feature
541 );
542 $availability = \CSocNetFeaturesPerms::canPerformOperation(
543 $USER->getId(),
544 SONET_ENTITY_GROUP,
545 $result,
546 $feature,
547 $operation
548 );
549 foreach ($result as $key => $groupId)
550 {
551 if (
552 !isset($activity[$groupId])
553 || !$activity[$groupId]
554 || !isset($availability[$groupId])
555 || !$availability[$groupId]
556 )
557 {
558 unset($result[$key]);
559 }
560 }
561 }
562 $result = array_unique($result);
563
564 return $result;
565 }
566
567 public function getLogRights(): array
568 {
569 $result = [];
570 $logId = $this->getLogId();
571
572 if ($logId > 0)
573 {
574 $result = $this->getLogRightsEntry();
575 }
576
577 return $result;
578 }
579
580 protected function getLogRightsEntry(): array
581 {
582 $result = [];
583
584 if ($this->logId > 0)
585 {
586 $res = \CSocNetLogRights::getList(
587 [],
588 [
589 'LOG_ID' => $this->logId
590 ]
591 );
592
593 while ($right = $res->fetch())
594 {
595 $result[] = $right['GROUP_CODE'];
596 }
597 }
598
599 return $result;
600 }
601
602 public function setEntityId($entityId)
603 {
604 $this->entityId = $entityId;
605 }
606
607 final public function getEntityId()
608 {
609 return $this->entityId;
610 }
611
612 final public function setLogId($logId): void
613 {
614 $this->logId = $logId;
615 }
616
617 final public function setAdditionalParams(array $additionalParams): void
618 {
619 $this->additionalParams = $additionalParams;
620 }
621
622 final public function getAdditionalParams(): array
623 {
625 }
626
627 final protected function setSourceFields(array $fields): void
628 {
629 $this->sourceFields = $fields;
630 }
631
632 public function initSourceFields()
633 {
634 return $this->sourceFields;
635 }
636
637 final public function getSourceFields(): array
638 {
639 return $this->sourceFields;
640 }
641
642 final protected function setSourceDescription($description): void
643 {
644 $this->sourceDescription = $description;
645 }
646
647 public function getSourceDescription()
648 {
649 if (empty($this->sourceFields))
650 {
651 $this->initSourceFields();
652 }
653
654 $result = $this->sourceDescription;
655
656 if ($this->cloneDiskObjects === true)
657 {
658 $this->getAttachedDiskObjects(true);
659 $result = $this->processDescription($result);
660 }
661
662 return $result;
663 }
664
665 final protected function setSourceTitle($title): void
666 {
667 $this->sourceTitle = $title;
668 }
669
670 public function getSourceTitle(): string
671 {
672 if (empty($this->sourceFields))
673 {
674 $this->initSourceFields();
675 }
676
677 return $this->sourceTitle;
678 }
679
680 public function getPinnedTitle()
681 {
682 if (empty($this->sourceFields))
683 {
684 $this->initSourceFields();
685 }
686
687 $result = $this->pinnedTitle;
688 if ($result === null)
689 {
690 $result = $this->getSourceTitle();
691 }
692
693 return $result;
694 }
695
696 public function getPinnedDescription()
697 {
698 if (empty($this->sourceFields))
699 {
700 $this->initSourceFields();
701 }
702
703 $result = $this->getSourceDescription();
704 $result = truncateText(\CTextParser::clearAllTags($result), 100);
705
706 return $result;
707 }
708
709 final protected function setSourceOriginalText($text): void
710 {
711 $this->sourceOriginalText = $text;
712 }
713
714 public function getSourceOriginalText(): string
715 {
716 if (empty($this->sourceFields))
717 {
718 $this->initSourceFields();
719 }
720
722 }
723
724 final protected function setSourceAuxData($auxData): void
725 {
726 $this->sourceAuxData = $auxData;
727 }
728
729 public function getSourceAuxData(): array
730 {
731 if (empty($this->sourceFields))
732 {
733 $this->initSourceFields();
734 }
735
737 }
738
739 final protected function setSourceAttachedDiskObjects(array $diskAttachedObjects): void
740 {
741 $this->sourceAttachedDiskObjects = $diskAttachedObjects;
742 }
743
744 final protected function setSourceDiskObjects(array $files): void
745 {
746 $this->sourceDiskObjects = $files;
747 }
748
749 final public function setDiskObjectsCloned(array $values): void
750 {
751 $this->diskObjectsCloned = $values;
752 }
753
754 final public function getDiskObjectsCloned(): array
755 {
757 }
758
759 final public function getAttachedDiskObjectsCloned(): array
760 {
762 }
763
764 public function getSourceAttachedDiskObjects(): array
765 {
766 if (empty($this->sourceFields))
767 {
768 $this->initSourceFields();
769 }
770
772 }
773
774 public function getSourceDiskObjects(): array
775 {
776 if (empty($this->sourceFields))
777 {
778 $this->initSourceFields();
779 }
780
782 }
783
784 protected function getAttachedDiskObjects($clone = false)
785 {
786 return [];
787 }
788
789 final protected function setSourceDateTime(DateTime $datetime): void
790 {
791 $this->sourceDateTime = $datetime;
792 }
793
794 final public function getSourceDateTime(): ?DateTime
795 {
797 }
798
799 final protected function setSourceAuthorId($authorId = 0): void
800 {
801 $this->sourceAuthorId = (int)$authorId;
802 }
803
804 final public function getSourceAuthorId(): int
805 {
807 }
808
809 protected static function cloneUfValues(array $values)
810 {
811 global $USER;
812
813 $result = [];
814 if (Loader::includeModule('disk'))
815 {
816 $result = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager()->cloneUfValuesFromAttachedObject($values, $USER->getId());
817 }
818
819 return $result;
820 }
821
822 public function getDiskObjects($entityId, $clone = false): array
823 {
824 $result = [];
825
826 if ($clone)
827 {
828 $result = $this->getAttachedDiskObjects(true);
829
830 if (
831 empty($this->diskObjectsCloned)
832 && Loader::includeModule('disk')
833 )
834 {
835 foreach ($result as $clonedDiskObjectId)
836 {
837 if (
838 in_array($clonedDiskObjectId, $this->attachedDiskObjectsCloned)
839 && ($attachedDiskObjectId = array_search($clonedDiskObjectId, $this->attachedDiskObjectsCloned))
840 )
841 {
842 $attachedObject = AttachedObject::loadById($attachedDiskObjectId);
843 if ($attachedObject)
844 {
845 $this->diskObjectsCloned[\Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX.$attachedObject->getObjectId()] = $this->attachedDiskObjectsCloned[$attachedDiskObjectId];
846 }
847 }
848 }
849 }
850
851 return $result;
852 }
853
854 $diskObjects = $this->getAttachedDiskObjects(false);
855
856 if (
857 !empty($diskObjects)
858 && Loader::includeModule('disk')
859 )
860 {
861 foreach ($diskObjects as $attachedObjectId)
862 {
863 $attachedObject = AttachedObject::loadById($attachedObjectId);
864 if ($attachedObject)
865 {
866 $result[] = \Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX . $attachedObject->getObjectId();
867 }
868 }
869 }
870
871 return $result;
872 }
873
874 private function processDescription($text)
875 {
876 $result = $text;
877
880
881 if (
882 !empty($diskObjectsCloned)
883 && is_array($diskObjectsCloned)
884 )
885 {
886 $result = preg_replace_callback(
887 "#\\[disk file id=(n\\d+)\\]#is" . BX_UTF_PCRE_MODIFIER,
888 [ $this, 'parseDiskObjectsCloned' ],
889 $result
890 );
891 }
892
893 if (
895 && is_array($attachedDiskObjectsCloned)
896 )
897 {
898 $result = preg_replace_callback(
899 "#\\[disk file id=(\\d+)\\]#is" . BX_UTF_PCRE_MODIFIER,
900 [ $this, 'parseAttachedDiskObjectsCloned' ],
901 $result
902 );
903 }
904
905 return $result;
906 }
907
908 private function parseDiskObjectsCloned($matches)
909 {
910 $text = $matches[0];
911
913
914 if (array_key_exists($matches[1], $diskObjectsCloned))
915 {
916 $text = str_replace($matches[1], $diskObjectsCloned[$matches[1]], $text);
917 }
918
919 return $text;
920 }
921
922 private function parseAttachedDiskObjectsCloned($matches)
923 {
924 $text = $matches[0];
925
927
928 if (array_key_exists($matches[1], $attachedDiskObjectsCloned))
929 {
930 $text = str_replace($matches[1], $attachedDiskObjectsCloned[$matches[1]], $text);
931 }
932
933 return $text;
934 }
935
936 public function getLiveFeedUrl()
937 {
938 return '';
939 }
940
941 final public function getContentTypeId(): string
942 {
943 return static::CONTENT_TYPE_ID;
944 }
945
946 public static function getContentId($event = [])
947 {
948 $result = false;
949
950 if (!is_array($event))
951 {
952 return $result;
953 }
954
955 $contentEntityType = false;
956 $contentEntityId = false;
957
958 $moduleEvent = new Main\Event(
959 'socialnetwork',
960 'onLogProviderGetContentId',
961 [
962 'eventFields' => $event,
963 ]
964 );
965 $moduleEvent->send();
966
967 foreach ($moduleEvent->getResults() as $moduleEventResult)
968 {
969 if ($moduleEventResult->getType() === EventResult::SUCCESS)
970 {
971 $moduleEventParams = $moduleEventResult->getParameters();
972
973 if (
974 is_array($moduleEventParams)
975 && !empty($moduleEventParams['contentEntityType'])
976 && !empty($moduleEventParams['contentEntityId'])
977 )
978 {
979 $contentEntityType = $moduleEventParams['contentEntityType'];
980 $contentEntityId = $moduleEventParams['contentEntityId'];
981 }
982 break;
983 }
984 }
985
986 if (
987 $contentEntityType
988 && $contentEntityId > 0
989 )
990 {
991 return [
992 'ENTITY_TYPE' => $contentEntityType,
993 'ENTITY_ID' => $contentEntityId
994 ];
995 }
996
997 // getContent
998
999 if (
1000 !empty($event['EVENT_ID'])
1001 && $event['EVENT_ID'] === 'photo'
1002 )
1003 {
1004 $contentEntityType = self::DATA_ENTITY_TYPE_PHOTOGALLERY_ALBUM;
1005 $contentEntityId = (int)$event['SOURCE_ID'];
1006 }
1007 elseif (
1008 !empty($event['EVENT_ID'])
1009 && $event['EVENT_ID'] === 'photo_photo'
1010 )
1011 {
1012 $contentEntityType = self::DATA_ENTITY_TYPE_PHOTOGALLERY_PHOTO;
1013 $contentEntityId = (int)$event['SOURCE_ID'];
1014 }
1015 elseif (
1016 !empty($event['EVENT_ID'])
1017 && $event['EVENT_ID'] === 'data'
1018 )
1019 {
1020 $contentEntityType = self::DATA_ENTITY_TYPE_LOG_ENTRY;
1021 $contentEntityId = (int)$event['ID'];
1022 }
1023 elseif (
1024 !empty($event['RATING_TYPE_ID'])
1025 && !empty($event['RATING_ENTITY_ID'])
1026 && (int)$event['RATING_ENTITY_ID'] > 0
1027 )
1028 {
1029 $contentEntityType = $event['RATING_TYPE_ID'];
1030 $contentEntityId = (int)$event['RATING_ENTITY_ID'];
1031
1032 if (in_array($event['RATING_TYPE_ID'], [ 'IBLOCK_ELEMENT', 'IBLOCK_SECTION' ]))
1033 {
1034 $res = self::$logTable::getList([
1035 'filter' => [
1036 '=RATING_TYPE_ID' => $event['RATING_TYPE_ID'],
1037 '=RATING_ENTITY_ID' => $event['RATING_ENTITY_ID'],
1038 ],
1039 'select' => [ 'EVENT_ID' ]
1040 ]);
1041 if ($logEntryFields = $res->fetch())
1042 {
1043 if ($event['RATING_TYPE_ID'] === 'IBLOCK_ELEMENT')
1044 {
1045 $found = false;
1046 $photogalleryPhotoProvider = new \Bitrix\Socialnetwork\Livefeed\PhotogalleryPhoto;
1047 if (in_array($logEntryFields['EVENT_ID'], $photogalleryPhotoProvider->getEventId(), true))
1048 {
1049 $contentEntityType = self::DATA_ENTITY_TYPE_PHOTOGALLERY_PHOTO;
1050 $contentEntityId = (int)$event['RATING_ENTITY_ID'];
1051 $found = true;
1052 }
1053
1054 if (!$found)
1055 {
1056 $wikiProvider = new \Bitrix\Socialnetwork\Livefeed\Wiki;
1057 if (in_array($logEntryFields['EVENT_ID'], $wikiProvider->getEventId()))
1058 {
1059 $contentEntityType = self::DATA_ENTITY_TYPE_WIKI;
1060 $contentEntityId = (int)$event['RATING_ENTITY_ID'];
1061 $found = true;
1062 }
1063 }
1064 }
1065 elseif ($event['RATING_TYPE_ID'] === 'IBLOCK_SECTION')
1066 {
1067 $photogalleryalbumProvider = new \Bitrix\Socialnetwork\Livefeed\PhotogalleryAlbum;
1068 if (in_array($logEntryFields['EVENT_ID'], $photogalleryalbumProvider->getEventId(), true))
1069 {
1070 $contentEntityType = self::DATA_ENTITY_TYPE_PHOTOGALLERY_ALBUM;
1071 $contentEntityId = (int)$event['RATING_ENTITY_ID'];
1072 }
1073 }
1074 }
1075 }
1076 elseif (preg_match('/^wiki_[\d]+_page$/i', $event['RATING_TYPE_ID'], $matches))
1077 {
1078 $contentEntityType = self::DATA_ENTITY_TYPE_WIKI;
1079 $contentEntityId = (int)$event['SOURCE_ID'];
1080 $found = true;
1081 }
1082 }
1083 elseif (
1084 !empty($event['EVENT_ID'])
1085 && !empty($event['SOURCE_ID'])
1086 && (int)$event['SOURCE_ID'] > 0
1087 )
1088 {
1089 switch ($event['EVENT_ID'])
1090 {
1091 case 'tasks':
1092 $contentEntityType = self::DATA_ENTITY_TYPE_TASKS_TASK;
1093 $contentEntityId = (int)$event['SOURCE_ID'];
1094 break;
1095 case 'calendar':
1096 $contentEntityType = self::DATA_ENTITY_TYPE_CALENDAR_EVENT;
1097 $contentEntityId = (int)$event['SOURCE_ID'];
1098 break;
1099 case 'timeman_entry':
1100 $contentEntityType = self::DATA_ENTITY_TYPE_TIMEMAN_ENTRY;
1101 $contentEntityId = (int)$event['SOURCE_ID'];
1102 break;
1103 case 'report':
1104 $contentEntityType = self::DATA_ENTITY_TYPE_TIMEMAN_REPORT;
1105 $contentEntityId = (int)$event['SOURCE_ID'];
1106 break;
1107 case 'lists_new_element':
1108 $contentEntityType = self::DATA_ENTITY_TYPE_LISTS_ITEM;
1109 $contentEntityId = (int)$event['SOURCE_ID'];
1110 break;
1111 default:
1112 }
1113 }
1114
1115 if (
1116 $contentEntityType
1117 && $contentEntityId > 0
1118 )
1119 {
1120 $result = [
1121 'ENTITY_TYPE' => $contentEntityType,
1122 'ENTITY_ID' => $contentEntityId
1123 ];
1124 }
1125
1126 return $result;
1127 }
1128
1129 public function setContentView($params = [])
1130 {
1131 global $USER;
1132
1133 if (!is_array($params))
1134 {
1135 $params = [];
1136 }
1137
1138 if (
1139 !isset($params['user_id'])
1140 && is_object($USER)
1141 && \CSocNetUser::isCurrentUserModuleAdmin()
1142 ) // don't track users on God Mode
1143 {
1144 return false;
1145 }
1146
1147 $userId = (
1148 isset($params['user_id'])
1149 && (int)$params['user_id'] > 0
1150 ? (int)$params['user_id']
1151 : 0
1152 );
1153 if ($userId <= 0 && is_object($USER))
1154 {
1155 $userId = $USER->getId();
1156 }
1157
1158 $contentTypeId = $this->getContentTypeId();
1159 $contentEntityId = $this->getEntityId();
1160 $logId = $this->getLogId();
1161 $save = (!isset($params['save']) || (bool)$params['save']);
1162
1163 if (
1164 (int)$userId <= 0
1165 || !$contentTypeId
1166 || !$contentEntityId
1167 )
1168 {
1169 return false;
1170 }
1171
1172 $viewParams = [
1173 'userId' => $userId,
1174 'typeId' => $contentTypeId,
1175 'entityId' => $contentEntityId,
1176 'logId' => $logId,
1177 'save' => $save
1178 ];
1179
1180 $pool = Application::getInstance()->getConnectionPool();
1181 $pool->useMasterOnly(true);
1182
1183 $result = UserContentViewTable::set($viewParams);
1184
1185 // we need to update the last DATE_VIEW for the parent post if it is a comment
1186 if ($this->isComment($this->getContentTypeId()))
1187 {
1188 $logItem = Log::getById($logId);
1189 if ($logItem)
1190 {
1191 $fields = $logItem->getFields();
1192 $contentTypeId = $fields['RATING_TYPE_ID'] ?? null;
1193 $contentEntityId = $fields['RATING_ENTITY_ID'] ?? null;
1194 if ($contentTypeId && $contentEntityId)
1195 {
1196 $result = UserContentViewTable::set([
1197 'userId' => $userId,
1198 'typeId' => $contentTypeId,
1199 'entityId' => $contentEntityId,
1200 'logId' => $logId,
1201 'save' => true
1202 ]);
1203 }
1204 }
1205 }
1206
1207 $pool->useMasterOnly(false);
1208
1209 if (
1210 $result
1211 && isset($result['success'])
1212 && $result['success']
1213 )
1214 {
1215 /*
1216 TODO: markAsRead sonet module notifications
1217 ContentViewHandler::onContentViewed($viewParams);
1218 */
1219 if (UserContentView::getAvailability())
1220 {
1221 if (
1222 isset($result['savedInDB'])
1223 && $result['savedInDB']
1224 )
1225 {
1226 if (Loader::includeModule('pull') && !$this->isComment($this->getContentTypeId()))
1227 {
1228 \CPullWatch::addToStack('CONTENTVIEW' . $viewParams['typeId'] . '-' . $viewParams['entityId'],
1229 [
1230 'module_id' => 'contentview',
1231 'command' => 'add',
1232 'expiry' => 0,
1233 'params' => [
1234 'USER_ID' => $userId,
1235 'TYPE_ID' => $viewParams['typeId'],
1236 'ENTITY_ID' => $viewParams['entityId'],
1237 'CONTENT_ID' => $viewParams['typeId'] . '-' . $viewParams['entityId']
1238 ]
1239 ]
1240 );
1241 }
1242 }
1243
1244 if ($logId > 0)
1245 {
1246 Subscription::onContentViewed([
1247 'userId' => $userId,
1248 'logId' => $logId
1249 ]);
1250
1251 \Bitrix\Socialnetwork\Internals\EventService\Service::addEvent(
1252 \Bitrix\Socialnetwork\Internals\EventService\EventDictionary::EVENT_SPACE_LIVEFEED_POST_VIEW,
1253 [
1254 'SONET_LOG_ID' => (int)$logId,
1255 'USER_ID' => (int)$userId,
1256 'TYPE_ID' => $contentTypeId,
1257 'ENTITY_ID' => $contentEntityId,
1258 ]
1259 );
1260 }
1261
1262 $event = new Main\Event(
1263 'socialnetwork', 'onContentViewed',
1264 $viewParams
1265 );
1266 $event->send();
1267 }
1268 }
1269
1270 return $result;
1271 }
1272
1273 final public static function getEntityData(array $params)
1274 {
1275 $entityType = false;
1276 $entityId = false;
1277
1278 $type = (
1279 isset($params['TYPE'])
1280 && in_array($params['TYPE'], self::getTypes())
1281 ? $params['TYPE']
1283 );
1284
1285 if (!empty($params['EVENT_ID']))
1286 {
1287 $blogPostLivefeedProvider = new BlogPost;
1288 if (
1289 $type === self::TYPE_POST
1290 && in_array($params['EVENT_ID'], $blogPostLivefeedProvider->getEventId(), true)
1291 )
1292 {
1294 $entityId = (isset($params['SOURCE_ID']) ? (int)$params['SOURCE_ID'] : false);
1295 }
1296 }
1297
1298 return (
1299 $entityType
1300 && $entityId
1301 ? [
1302 'ENTITY_TYPE' => $entityType,
1303 'ENTITY_ID' => $entityId
1304 ]
1305 : false
1306 );
1307 }
1308
1309 public function getSuffix()
1310 {
1311 return '';
1312 }
1313
1314 public function add()
1315 {
1316 return false;
1317 }
1318
1319 final public function setLogEventId($eventId = ''): bool
1320 {
1321 if ($eventId == '')
1322 {
1323 return false;
1324 }
1325
1326 $this->logEventId = $eventId;
1327
1328 return true;
1329 }
1330
1331 private function setLogEntityType($entityType = ''): bool
1332 {
1333 if ($entityType == '')
1334 {
1335 return false;
1336 }
1337
1338 $this->logEntityType = $entityType;
1339
1340 return true;
1341 }
1342
1343 private function setLogEntityId($entityId = 0): bool
1344 {
1345 if ((int)$entityId <= 0)
1346 {
1347 return false;
1348 }
1349
1350 $this->logEntityId = $entityId;
1351
1352 return true;
1353 }
1354
1355 final protected function getLogFields(): array
1356 {
1357 $return = [];
1358
1359 $logId = $this->getLogId();
1360 if ((int)$logId <= 0)
1361 {
1362 return $return;
1363 }
1364
1365 $res = self::$logTable::getList([
1366 'filter' => [
1367 'ID' => $logId,
1368 ],
1369 'select' => [ 'EVENT_ID', 'ENTITY_TYPE', 'ENTITY_ID' ]
1370 ]);
1371 if ($logFields = $res->fetch())
1372 {
1373 $return = $logFields;
1374
1375 $this->setLogEventId($logFields['EVENT_ID']);
1376 $this->setLogEntityType($logFields['ENTITY_TYPE']);
1377 $this->setLogEntityId($logFields['ENTITY_ID']);
1378 }
1379
1380 return $return;
1381 }
1382
1383 protected function getLogEventId()
1384 {
1385 $result = false;
1386
1387 if ($this->logEventId !== null)
1388 {
1389 $result = $this->logEventId;
1390 }
1391 else
1392 {
1393 $logFields = $this->getLogFields();
1394 if (!empty($logFields['EVENT_ID']))
1395 {
1396 $result = $logFields['EVENT_ID'];
1397 }
1398 }
1399
1400 return $result;
1401 }
1402
1403 protected function getLogEntityType()
1404 {
1405 $result = false;
1406
1407 if ($this->logEntityType !== null)
1408 {
1409 $result = $this->logEntityType;
1410 }
1411 else
1412 {
1413 $logFields = $this->getLogFields();
1414 if (!empty($logFields['ENTITY_TYPE']))
1415 {
1416 $result = $logFields['ENTITY_TYPE'];
1417 }
1418 }
1419
1420 return $result;
1421 }
1422
1423 protected function getLogEntityId()
1424 {
1425 $result = false;
1426
1427 if ($this->logEntityId !== null)
1428 {
1429 $result = $this->logEntityId;
1430 }
1431 else
1432 {
1433 $logFields = $this->getLogFields();
1434 if (!empty($logFields['ENTITY_ID']))
1435 {
1436 $result = $logFields['ENTITY_ID'];
1437 }
1438 }
1439
1440 return $result;
1441 }
1442
1443 public function getAdditionalData($params = [])
1444 {
1445 return [];
1446 }
1447
1448 protected function checkAdditionalDataParams(&$params): bool
1449 {
1450 if (
1451 empty($params)
1452 || !is_array($params)
1453 || empty($params['id'])
1454 )
1455 {
1456 return false;
1457 }
1458
1459 if (!is_array($params['id']))
1460 {
1461 $params['id'] = [ $params['id'] ];
1462 }
1463
1464 return true;
1465 }
1466
1467 public function warmUpAuxCommentsStaticCache(array $params = []): void
1468 {
1469
1470 }
1471
1472 protected function getUnavailableTitle()
1473 {
1474 return Loc::getMessage('SONET_LIVEFEED_BASE_TITLE_UNAVAILABLE');
1475 }
1476
1477 protected function getEntityAttachedDiskObjects(array $params = [])
1478 {
1479 global $USER_FIELD_MANAGER;
1480
1481 $result = [];
1482
1483 $userFieldEntity = (string)($params['userFieldEntity'] ?? '');
1484 $userFieldEntityId = $this->entityId;
1485 $userFieldCode = (string)($params['userFieldCode'] ?? '');
1486 $clone = (boolean)($params['clone'] ?? false);
1487
1488 if (
1489 $userFieldEntity === ''
1490 || $userFieldCode === ''
1491 || $userFieldEntityId <= 0
1492 )
1493 {
1494 return $result;
1495 }
1496
1497 static $cache = [];
1498
1499 $cacheKey = $userFieldEntity . $userFieldEntityId . $clone;
1500
1501 if (isset($cache[$cacheKey]))
1502 {
1503 $result = $cache[$cacheKey];
1504 }
1505 else
1506 {
1507 $entityUF = $USER_FIELD_MANAGER->getUserFields($userFieldEntity, $userFieldEntityId, LANGUAGE_ID);
1508 if (
1509 !empty($entityUF[$userFieldCode])
1510 && !empty($entityUF[$userFieldCode]['VALUE'])
1511 && is_array($entityUF[$userFieldCode]['VALUE'])
1512 )
1513 {
1514 if ($clone)
1515 {
1516 $this->attachedDiskObjectsCloned = self::cloneUfValues($entityUF[$userFieldCode]['VALUE']);
1517 $result = $cache[$cacheKey] = array_values($this->attachedDiskObjectsCloned);
1518 }
1519 else
1520 {
1521 $result = $cache[$cacheKey] = $entityUF[$userFieldCode]['VALUE'];
1522 }
1523 }
1524 }
1525
1526 if (!is_array($result))
1527 {
1528 $result = [];
1529 }
1530
1531 return $result;
1532 }
1533
1534 public function getParentEntityId(): int
1535 {
1536 return 0;
1537 }
1538
1539 private function isComment(string $contentTypeId): bool
1540 {
1541 return $contentTypeId === LogComment::CONTENT_TYPE_ID
1542 || $contentTypeId === BlogComment::CONTENT_TYPE_ID
1543 || $contentTypeId === ForumPost::CONTENT_TYPE_ID;
1544 }
1545}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
Definition blogpost.php:13
Definition forumpost.php:22
const CONTENT_TYPE_ID
Definition forumpost.php:24
warmUpAuxCommentsStaticCache(array $params=[])
static cloneUfValues(array $values)
Definition provider.php:809
setSourceDateTime(DateTime $datetime)
Definition provider.php:789
setAdditionalParams(array $additionalParams)
Definition provider.php:617
setSourceAttachedDiskObjects(array $diskAttachedObjects)
Definition provider.php:739
getEntityAttachedDiskObjects(array $params=[])
static getEntityData(array $params)
getSonetGroupsAvailable($feature=false, $operation=false)
Definition provider.php:510
getDiskObjects($entityId, $clone=false)
Definition provider.php:822