Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
callrecord.php
1<?php
2
4
5use Bitrix\Disk\AttachedObject;
6use Bitrix\Disk\File;
7use Bitrix\Disk\Uf\FileUserType;
16
17Loc::loadMessages(__FILE__);
18
25final class CallRecord
26{
27 private const ENTITY_TYPE_TASK = 'TASKS';
28 private const ENTITY_TYPE_CALENDAR_EVENT = 'CALENDAR';
29
30 public static function getEntityTypeList(): array
31 {
32 return [
33 self::ENTITY_TYPE_TASK,
34 self::ENTITY_TYPE_CALENDAR_EVENT,
35 ];
36 }
37
44 public static function onDiskRecordShare(Event $event): bool
45 {
46 global $USER;
47
48 $diskObjectId = (int)$event->getParameter('DISK_ID');
49 $chat = $event->getParameter('CHAT');
50 $userId = $event->getParameter('USER_ID');
51
52 $userId = ($userId === null ? $USER->getId() : (int)$userId);
53
54 if (
55 $userId <= 0
56 || $diskObjectId <= 0
57 || !is_array($chat)
58 )
59 {
60 return false;
61 }
62
63 $entityType = (string)$chat['ENTITY_TYPE'];
64 $entityId = (int)$chat['ENTITY_ID'];
65
66 if (
67 $entityId <= 0
68 || !in_array($entityType, self::getEntityTypeList())
69 )
70 {
71 return false;
72 }
73
74 $normalizedEntityType = self::getNormalizedEntityType($entityType);
75 if ($normalizedEntityType === '')
76 {
77 return false;
78 }
79
80 $postProvider = Livefeed\Provider::init([
81 'ENTITY_TYPE' => $normalizedEntityType,
82 'ENTITY_ID' => $entityId,
83 ]);
84 if (!$postProvider)
85 {
86 return false;
87 }
88
89 if (!Loader::includeModule('disk'))
90 {
91 return false;
92 }
93
94 $postProvider->initSourceFields();
95 $logId = $postProvider->getLogId();
96
97 $commentProvider = $postProvider->getCommentProvider();
98 $commentProvider->setParentProvider($postProvider);
99 $commentProvider->setLogId($logId);
100
101 $fileName = Loc::getMessage('SOCIALNETWORK_CHAT_CALLRECORD_FILE_NOT_FOUND');
102 if ($diskObject = File::loadById($diskObjectId))
103 {
104 $fileName = $diskObject->getName();
105 }
106
107 $sonetCommentData = $commentProvider->add([
108 'AUTHOR_ID' => $userId,
109 'MESSAGE' => Loc::getMessage('SOCIALNETWORK_CHAT_CALLRECORD_TEXT', [
110 '#FILE_NAME#' => $fileName,
111 '#DISK_FILE_ID#' => $diskObjectId,
112 ]),
113 'MODULE' => false,
114 ]);
115
116 $sonetCommentId = (int)($sonetCommentData['sonetCommentId'] ?? 0);
117 $sourceCommentId = (int)($sonetCommentData['sourceCommentId'] ?? 0);
118
119 if (
120 $sourceCommentId > 0
121 && $commentProvider->getId() === Livefeed\ForumPost::PROVIDER_ID
122 && Loader::includeModule('forum')
123 )
124 {
125 $updateFields = [
126 'UF_FORUM_MESSAGE_DOC' => [ FileUserType::NEW_FILE_PREFIX . $diskObjectId ],
127 ];
128 if (\Bitrix\Forum\Message::update($sourceCommentId, $updateFields)->isSuccess())
129 {
130 $res = MessageTable::getList([
131 'filter' => [
132 '=ID' => $sourceCommentId,
133 ],
134 'select' => [ 'UF_FORUM_MESSAGE_DOC' ],
135 ]);
136 if (
137 ($messageFields = $res->fetch())
138 && !empty($messageFields['UF_FORUM_MESSAGE_DOC'])
139 )
140 {
141 $attachedDiskId = (int)$messageFields['UF_FORUM_MESSAGE_DOC'][0];
142 $renderedMessage = '';
143
144 if (
145 Loader::includeModule('disk')
146 && ($attachedObject = AttachedObject::loadById($attachedDiskId, [ 'OBJECT' ]))
147 )
148 {
149 $url = \Bitrix\Disk\Driver::getInstance()->getUrlManager()->getUrlUfController('download', [ 'attachedId' => $attachedDiskId ]);
150 $renderedMessage = Loc::getMessage('SOCIALNETWORK_CHAT_CALLRECORD_TEXT', [
151 '#FILE_NAME#' => (!empty($url) ? '[URL=' . $url . ']' . $fileName . '[/URL]' : $fileName),
152 '#DISK_FILE_ID#' => $diskObjectId,
153 ]);
154
155 $updateFields = [
156 'POST_MESSAGE' => $renderedMessage,
157 ];
158 MessageTable::update($sourceCommentId, $updateFields);
159 }
160
161 if ($sonetCommentId > 0)
162 {
163 $updateFields = [
164 'UF_SONET_COM_DOC' => $messageFields['UF_FORUM_MESSAGE_DOC']
165 ];
166 if (!empty($renderedMessage))
167 {
168 $updateFields['MESSAGE'] = $renderedMessage;
169 }
170 LogCommentTable::update($sonetCommentId, $updateFields);
171 }
172
173 $forumId = self::getForumId($entityType);
174 if ($forumId > 0)
175 {
176 $feed = new Comments\Feed(
177 $forumId,
178 [
179 'type' => self::getForumFeedType($entityType),
180 'id' => $entityId,
181 'xml_id' => self::getForumFeedXmlIdPrefix($entityType) . $entityId,
182 ],
183 $userId
184 );
185
186 $feed->send(
187 $sourceCommentId,
188 [
189 'URL_TEMPLATES_PROFILE_VIEW' => Option::get('socialnetwork', 'user_page', '/company/personal/') . 'user/#user_id#/',
190 'SKIP_USER_READ' => 'N',
191 ]
192 );
193 }
194 }
195 }
196 }
197
198 return true;
199 }
200
201 private static function getNormalizedEntityType(string $entityType): string
202 {
203 switch ($entityType)
204 {
205 case self::ENTITY_TYPE_TASK:
206 $result = Livefeed\Provider::DATA_ENTITY_TYPE_TASKS_TASK;
207 break;
208 case self::ENTITY_TYPE_CALENDAR_EVENT:
209 $result = Livefeed\Provider::DATA_ENTITY_TYPE_CALENDAR_EVENT;
210 break;
211 default:
212 $result = '';
213 }
214
215 return $result;
216 }
217
218 private static function getForumFeedType(string $entityType): string
219 {
220 if (!Loader::includeModule('forum'))
221 {
222 return '';
223 }
224
225 switch ($entityType)
226 {
227 case self::ENTITY_TYPE_TASK:
228 $result = mb_strtoupper(Comments\TaskEntity::ENTITY_TYPE);
229 break;
230 case self::ENTITY_TYPE_CALENDAR_EVENT:
231 $result = mb_strtoupper(Comments\CalendarEntity::ENTITY_TYPE);
232 break;
233 default:
234 $result = '';
235 }
236
237 return $result;
238 }
239
240 private static function getForumFeedXmlIdPrefix(string $entityType): string
241 {
242 if (!Loader::includeModule('forum'))
243 {
244 return '';
245 }
246
247 switch ($entityType)
248 {
249 case self::ENTITY_TYPE_TASK:
250 $result = mb_strtoupper(Comments\TaskEntity::XML_ID_PREFIX);
251 break;
252 case self::ENTITY_TYPE_CALENDAR_EVENT:
253 $result = mb_strtoupper(Comments\CalendarEntity::XML_ID_PREFIX);
254 break;
255 default:
256 $result = '';
257 }
258
259 return $result;
260 }
261
262 public static function getForumId($entityType): int
263 {
264 switch ($entityType)
265 {
266 case self::ENTITY_TYPE_TASK:
267 $result = (Loader::includeModule('tasks') ? \CTasksTools::getForumIdForIntranet() : 0);
268 break;
269 case self::ENTITY_TYPE_CALENDAR_EVENT:
270 $settings = \CCalendar::getSettings();
271 $result = (int)$settings['forum_id'];
272 break;
273 default:
274 $result = 0;
275 }
276
277 return $result;
278 }
279}
static update($id, array &$fields)
Definition message.php:709
getParameter($key)
Definition event.php:80
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static update($primary, array $data)