1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
controller.php
См. документацию.
1<?php
2
4
5use Bitrix\AI\Context;
6use Bitrix\AI\Engine;
13
14final class Controller
15{
16 private static array $listMessages = [];
17 private static int $blogAuthorId = 0;
18 private const LIMIT = 20;
19
20 public static function onContextGetMessages(Event $event): array
21 {
22 $moduleId = $event->getParameter('module');
23 $contextId = $event->getParameter('id');
24 $contextParameters = $event->getParameter('params');
25 $nextStep = $event->getParameter('next_step');
26
27 $isCommentContext = (
28 $moduleId === 'socialnetwork'
29 && str_starts_with($contextId, 'sonet_comment_')
30 );
31 if ($isCommentContext)
32 {
33 $messages = [];
34
35 $xmlId = is_string($contextParameters['xmlId'] ?? null) ? $contextParameters['xmlId'] : null;
36 if (!$xmlId)
37 {
38 return ['messages' => []];
39 }
40
41 if (isset(self::$listMessages[$xmlId]))
42 {
43 return ['messages' => self::$listMessages[$xmlId]];
44 }
45
46 if (str_starts_with($xmlId, 'BLOG_') && Loader::includeModule('blog'))
47 {
48 $postId = (int) mb_substr($xmlId, 5);
49 $postMessages = self::getPostContext($postId);
50 foreach ($postMessages as $postMessage)
51 {
52 $messages[] = ['content' => $postMessage];
53 }
54 }
55 else if (
56 str_starts_with($xmlId, 'TASK_')
57 && Loader::includeModule('tasks')
58 && Loader::includeModule('forum')
59 )
60 {
61 $postMessages = self::getTaskContext($xmlId);
62 foreach ($postMessages as $postMessage)
63 {
64 $messages[] = ['content' => $postMessage];
65 }
66 }
67
68 $messages[0] = self::modifyOriginalMessage($messages[0] ?? []);
69
70 if ($messages)
71 {
72 self::$listMessages[$xmlId] = $messages;
73 }
74
75 return ['messages' => $messages];
76 }
77
78 return ['messages' => []];
79 }
80
81 private static function getPostContext(int $postId): array
82 {
83 $messages = [];
84
85 $textParser = new \CTextParser();
86
87 $post = \CBlogPost::getByID($postId);
88 if (!BlogPost::canRead(['POST' => $post]))
89 {
90 return [];
91 }
92
93 if ($post)
94 {
95 self::setBlogAuthorId((int)$post['AUTHOR_ID']);
96
97 $messages[] = $textParser->clearAllTags($post['DETAIL_TEXT']);
98
99 $comments = self::getLastComments($postId);
100
101 $messages = array_merge($messages, $comments);
102 }
103
104 return $messages;
105 }
106
107 public static function getTaskContext(string $xmlId): array
108 {
109 $taskId = (int) mb_substr($xmlId, 5);
110
111 $textParser = new \CTextParser();
112
113 $messages = [];
114
115 $task = \Bitrix\Tasks\Internals\Registry\TaskRegistry::getInstance()->getObject($taskId);
116 self::setBlogAuthorId((int)$task->getCreatedBy());
117 $messages[] = $textParser->clearAllTags($task->getDescription());
118
120 'ENTITY_TYPE' => \Bitrix\Socialnetwork\Livefeed\Provider::DATA_ENTITY_TYPE_TASKS_TASK,
121 'ENTITY_ID' => $taskId,
122 ]);
123 if ($liveFeedEntity)
124 {
125 $logId = (int) $liveFeedEntity->getLogId();
126 if ($logId)
127 {
128 $comments = self::getForumComments($xmlId);
129
130 $messages = array_merge($messages, array_reverse($comments));
131 }
132 }
133
134 return $messages;
135 }
136
137 private static function getForumComments(string $xmlId): array
138 {
139 $textParser = new \CTextParser();
140
141 $comments = [];
142
143 $query = MessageTable::query();
144 $query
145 ->setSelect(['ID', 'POST_MESSAGE'])
146 ->where('XML_ID', $xmlId)
147 ->whereNull('SERVICE_TYPE')
148 ->whereNull('PARAM1')
149 ->setOrder(['POST_DATE' => 'desc'])
150 ->setLimit(self::LIMIT);
151
152 $postMessages = $query->exec()->fetchCollection();
153 foreach ($postMessages as $postMessage)
154 {
155 $comments[] = $textParser->clearAllTags($postMessage->getPostMessage());
156 }
157
158 return $comments;
159 }
160
161 private static function getLastComments(int $postId): array
162 {
163 $textParser = new \CTextParser();
164
165 $comments = [];
166
167 $queryCommentObject = \CBlogComment::getList(
168 ['ID' => 'DESC'],
169 [
170 'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH,
171 'POST_ID' => $postId,
172 '!=POST_TEXT' => \Bitrix\Socialnetwork\CommentAux\TaskInfo::POST_TEXT,
173 ],
174 false,
175 [
176 'nTopCount' => self::LIMIT
177 ],
178 ['POST_TEXT']
179 );
180 while ($commentData = $queryCommentObject->fetch())
181 {
182 $comments[] = $textParser->clearAllTags($commentData['POST_TEXT']);
183 }
184
185 return $comments;
186 }
187
188 private static function modifyOriginalMessage(array $message): array
189 {
190 $message['is_original_message'] = true;
191
192 if (self::$blogAuthorId)
193 {
194 $author = new Author(self::$blogAuthorId);
195 $message['meta'] = $author->toMeta();
196 }
197
198 return $message;
199 }
200
201 private static function setBlogAuthorId(int $userId): void
202 {
203 self::$blogAuthorId = $userId;
204 }
205}
const BLOG_PUBLISH_STATUS_PUBLISH
Определения include.php:47
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения event.php:5
Определения loader.php:13
static onContextGetMessages(Event $event)
Определения controller.php:20
static getTaskContext(string $xmlId)
Определения controller.php:107
Определения blogpost.php:13
static init(array $params)
Определения provider.php:279
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$moduleId
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
$messages
Определения template.php:8
$post
Определения template.php:8