Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
secretary.php
1<?php
2
4
16
17class Secretary extends Controller
18{
19 protected function processBeforeAction(Action $action)
20 {
21 if (! Loader::includeModule('intranet'))
22 {
23 return false;
24 }
25 return parent::processBeforeAction($action);
26 }
27
33 public function configureActions()
34 {
35 $config = [
36 'createChatFromMessage' => [
37 '+prefilters' => [
38 new \Bitrix\Main\Engine\ActionFilter\HttpMethod(['POST']),
39 new \Bitrix\Main\Engine\ActionFilter\Csrf(),
40 ]
41 ],
42 'onCalendarSave' => [
43 '+prefilters' => [
44 new \Bitrix\Main\Engine\ActionFilter\HttpMethod(['POST']),
45 new \Bitrix\Main\Engine\ActionFilter\Csrf(),
46 ]
47 ],
48 'getCalendarEventDataFromMessage' => [
49 '+prefilters' => [
50 new \Bitrix\Main\Engine\ActionFilter\HttpMethod(['POST']),
51 new \Bitrix\Main\Engine\ActionFilter\Csrf(),
52 ]
53 ],
54 ];
55
56 if (Loader::includeModule('intranet'))
57 {
58 $config['createChatFromMessage']['+prefilters'][] = new \Bitrix\Intranet\ActionFilter\IntranetUser();
59 $config['onCalendarSave']['+prefilters'][] = new \Bitrix\Intranet\ActionFilter\IntranetUser();
60 $config['getCalendarEventDataFromMessage']['+prefilters'][] = new \Bitrix\Intranet\ActionFilter\IntranetUser();
61 }
62
63 return $config;
64 }
65
73 public function createChatFromMessageAction(int $messageId): ?int
74 {
75 if (!Loader::includeModule('im'))
76 {
77 return null;
78 }
79
80 global $USER;
81 $userId = $USER->GetID();
82
83 if (!$this->canBindEntities($messageId, (int)$userId))
84 {
85 $this->addError(new Error(Loc::getMessage('MAIL_SECRETARY_ACCESS_DENIED')));
86 return null;
87 }
88
89 $message = \Bitrix\Mail\Integration\Intranet\Secretary::getMessage($messageId);
90 $messageData = $message->toArray();
91 $messageData['USER_IDS'] = [$USER->GetID()];
92
93 if ($chatId = \Bitrix\Intranet\Secretary::getChatIdIfExists($messageId, 'MAIL'))
94 {
95 // get back to chat if user left in past
96 if (! \Bitrix\Intranet\Secretary::isUserInChat($chatId, $userId))
97 {
98 \Bitrix\Intranet\Secretary::addUserToChat($chatId, $userId, false);
99 // // post welcome message again because it was hidden
100 // \Bitrix\Intranet\Secretary::postMailChatWelcomeMessage($message, $chatId, $userId);
101 }
102 }
103 else
104 {
105 $lockName = "chat_create_mail_{$messageId}";
106 if (!Application::getConnection()->lock($lockName))
107 {
108 $this->addError(new Error(
109 Loc::getMessage('MAIL_SECRETARY_CREATE_CHAT_LOCK_ERROR'), 'lock_error')
110 );
111 return null;
112 }
113
114 $chatId = \Bitrix\Intranet\Secretary::createMailChat($messageData, $userId);
115
116 Application::getConnection()->unlock($lockName);
117 }
118
119 if (Loader::includeModule('pull'))
120 {
121 $mailboxId = \Bitrix\Mail\Integration\Intranet\Secretary::getMailboxIdForMessage($messageId);
122
123 if($mailboxId)
124 {
125 \CPullWatch::addToStack(
126 'mail_mailbox_' . $mailboxId,
127 [
128 'module_id' => 'mail',
129 'command' => 'messageBindingCreated',
130 'params' => [
131 'messageId' => $messageId,
132 'mailboxId' => $mailboxId,
133 'entityType' => Message::ENTITY_TYPE_IM_CHAT,
134 'entityId' => $chatId,
135 'bindingEntityLink' =>
136 \CComponentEngine::makePathFromTemplate(
137 '/online/?IM_DIALOG=chat#chat_id#',
138 [
139 'chat_id' => $chatId,
140 ]
141 ),
142 ],
143 ]
144 );
145 }
146 }
147
148 return $chatId;
149 }
150
151 public function onCalendarSaveAction(int $messageId, int $calendarEventId)
152 {
153 if ($this->provideAccessToMessage($messageId, $calendarEventId))
154 {
155 if ($this->postCalendarBackLinkComment($messageId, $calendarEventId))
156 {
157 $this->assignCreatedCalendarLabelToMessage($messageId, $calendarEventId);
158 }
159 else
160 {
161 $this->addError(new Error('secretary: comment post error'));
162 }
163 }
164 else
165 {
166 $this->addError(new Error('secretary: grant access to message failed'));
167 }
168 }
169
178 private function assignCreatedCalendarLabelToMessage(int $messageId, int $calendarEventId): bool
179 {
180 if (Loader::includeModule('pull'))
181 {
182 $mailboxId = \Bitrix\Mail\Integration\Intranet\Secretary::getMailboxIdForMessage($messageId);
183
184 if($mailboxId)
185 {
186 global $USER;
187
188 $userPage = \Bitrix\Main\Config\Option::get('socialnetwork', 'user_page', '/company/personal/', SITE_ID);
189
190 \CPullWatch::addToStack(
191 'mail_mailbox_' . $mailboxId,
192 [
193 'module_id' => 'mail',
194 'command' => 'messageBindingCreated',
195 'params' => [
196 'messageId' => $messageId,
197 'mailboxId' => $mailboxId,
199 'entityId' => $calendarEventId,
200 'bindingEntityLink' =>
201 \CComponentEngine::makePathFromTemplate(
202 $userPage . 'user/#user_id#/calendar/?EVENT_ID=#event_id#',
203 [
204 'user_id' => $USER->getId(),
205 'event_id' => $calendarEventId,
206 ]
207 ),
208 ],
209 ]
210 );
211 }
212
213 return true;
214 }
215
216 return false;
217 }
218
226 private function provideAccessToMessage(int $messageId, int $calendarEventId): bool
227 {
228 global $USER;
229 $userId = $USER->GetID();
230
231 return \Bitrix\Mail\Integration\Intranet\Secretary::provideAccessToMessage(
232 $messageId,
234 $calendarEventId,
235 $userId
236 );
237 }
238
247 private function postCalendarBackLinkComment(int $messageId, int $calendarEventId): bool
248 {
249 if (! Loader::includeModule('calendar'))
250 {
251 $this->addError(new Error('module calendar unloaded'));
252 return false;
253 }
254
255 if (! Loader::includeModule('forum'))
256 {
257 $this->addError(new Error('module forum unloaded'));
258 return false;
259 }
260
261 global $USER;
262 $userId = (int)$USER->GetID();
263
264 $xmlId = 'EVENT_' . $calendarEventId;
265
266 $calendarEntry = \CCalendarEvent::getEventForViewInterface($calendarEventId, [
267 'eventDate' => '',
268 'userId' => $userId,
269 ]);
270
271 if (!$this->canBindEntities($messageId, $userId))
272 {
273 $this->addError(new Error(Loc::getMessage('MAIL_SECRETARY_ACCESS_DENIED')));
274 return false;
275 }
276
277 if (! isset($calendarEntry['CREATED_BY']) || (int)$calendarEntry['CREATED_BY'] !== $userId)
278 {
279 $this->addError(new Error(Loc::getMessage('MAIL_SECRETARY_ACCESS_DENIED_CALENDAR')));
280 return false;
281 }
282
283 if ($calendarEntry)
284 {
285 $xmlId = \CCalendarEvent::getEventCommentXmlId($calendarEntry);
286 }
287
288 $feedParams = [
289 'type' => 'EV', // \Bitrix\Socialnetwork\Livefeed\ForumPost::getForumTypeMap()
290 'id' => $calendarEventId,
291 'xml_id' => $xmlId,
292 ];
293
294 $forumId = self::getForumId(array_merge($feedParams, [
295 'SITE_ID' => SITE_ID,
296 ]));
297
298 if (!$forumId)
299 {
300 $this->addError(new Error('forum id error'));
301 return false;
302 }
303
304 $feed = new \Bitrix\Forum\Comments\Feed(
305 $forumId,
306 $feedParams,
307 $userId
308 );
309
310 $link = \Bitrix\Mail\Integration\Intranet\Secretary::getMessageUrlForCalendarEvent($messageId, $calendarEventId);
311 $commentMessage = Loc::getMessage('MAIL_SECRETARY_POST_MESSAGE_CALENDAR_EVENT', [
312 '#LINK#' => $link,
313 ]);
314
315 $forumMessageFields = [
316 'POST_MESSAGE' => $commentMessage,
317 ];
318 $forumComment = $feed->add($forumMessageFields);
319
320 if (!$forumComment)
321 {
322 $this->addError(new Error('forum comment error'));
323 return false;
324 }
325
326 // TODO post to social network feed
327 // if (! Loader::includeModule('socialnetwork'))
328 // {
329 // $this->addError(new Error('module socialnetwork unloaded'));
330 // return null;
331 // }
332
333 return true;
334 }
335
344 public function getCalendarEventDataFromMessageAction(int $messageId)
345 {
346 if (! Loader::includeModule('intranet'))
347 {
348 $this->addError(new Error('module intranet unloaded')); // FIXME translate
349 return null;
350 }
351
352 global $USER;
353 if (!$this->canBindEntities($messageId, (int)$USER->getId()))
354 {
355 $this->addError(new Error(Loc::getMessage('MAIL_SECRETARY_ACCESS_DENIED')));
356 return null;
357 }
358
359 $message = \Bitrix\Mail\Integration\Intranet\Secretary::getMessage($messageId);
360 $address = new \Bitrix\Main\Mail\Address($message->getFrom());
361 $desc = Loc::getMessage('MAIL_SECRETARY_CALENDAR_EVENT_DESC', [
362 '#SUBJECT#' => htmlspecialcharsbx($message->getSubject()),
363 '#FROM#' => htmlspecialcharsbx($message->getFrom()),
364 '#DATE#' => $message->getDate()->toString(),
365 '#LINK_FROM#' => 'mailto:' . htmlspecialcharsbx($address->getEmail()),
366 '#LINK#' => \Bitrix\Mail\Integration\Intranet\Secretary::getDirectMessageUrl($message->getId()),
367 ]);
368
369 $isIcal = Message::isIcalMessage($message);
370
371 return [
372 'name' => htmlspecialcharsbx($message->getSubject()),
373 'desc' => $desc,
374 'isIcal' => $isIcal,
375 'isNewEvent' => !$isIcal,
376 // 'userIds' => $data['USER_IDS'],
377 ];
378 }
379
380 // FIXME copypaste from forum module, must be simplified
381 private static function getForumId($params = [])
382 {
383 $result = 0;
384
385 $siteId = (
386 isset($params['SITE_ID'])
387 && $params['SITE_ID'] <> ''
388 ? $params['SITE_ID']
389 : SITE_ID
390 );
391
392 if (isset($params['type']))
393 {
394 if ($params['type'] === 'TK')
395 {
396 $result = Option::get('tasks', 'task_forum_id', 0, $siteId);
397
398 if (
399 (int)$result <= 0
400 && Loader::includeModule('forum')
401 )
402 {
403 $res = ForumTable::getList([
404 'filter' => [
405 '=XML_ID' => 'intranet_tasks',
406 ],
407 'select' => [ 'ID' ],
408 ]);
409 if ($forumFields = $res->fetch())
410 {
411 $result = (int)$forumFields['ID'];
412 }
413 }
414 }
415 elseif ($params['type'] === 'WF')
416 {
417 $result = Option::get('bizproc', 'forum_id', 0, $siteId);
418
419 if ((int)$result <= 0)
420 {
421 $res = ForumTable::getList([
422 'filter' => [
423 '=XML_ID' => 'bizproc_workflow',
424 ],
425 'select' => [ 'ID' ],
426 ]);
427 if ($forumFields = $res->fetch())
428 {
429 $result = (int)$forumFields['ID'];
430 }
431 }
432 }
433 elseif (in_array($params['type'], [ 'TM', 'TR' ]))
434 {
435 $result = Option::get('timeman', 'report_forum_id', 0, $siteId);
436 }
437 elseif (
438 $params['type'] === 'EV'
439 && Loader::includeModule('calendar')
440 )
441 {
442 $calendarSettings = \CCalendar::getSettings();
443 $result = $calendarSettings["forum_id"];
444 }
445 elseif (
446 $params['type'] === 'PH'
447 && Loader::includeModule('forum')
448 )
449 {
450 $res = ForumTable::getList(array(
451 'filter' => array(
452 '=XML_ID' => 'PHOTOGALLERY_COMMENTS'
453 ),
454 'select' => array('ID')
455 ));
456 if ($forumFields = $res->fetch())
457 {
458 $result = (int)$forumFields['ID'];
459 }
460 }
461 elseif ($params['type'] === 'IBLOCK')
462 {
463 $result = Option::get('wiki', 'socnet_forum_id', 0, $siteId);
464 }
465 else
466 {
467 $res = ForumTable::getList(array(
468 'filter' => array(
469 '=XML_ID' => 'USERS_AND_GROUPS'
470 ),
471 'select' => array('ID')
472 ));
473 if ($forumFields = $res->fetch())
474 {
475 $result = (int)$forumFields['ID'];
476 }
477 }
478 }
479
480 return $result;
481 }
482
490 private function canBindEntities(int $messageId, int $userId): bool
491 {
492 return \Bitrix\Mail\MessageAccess::createByMessageId($messageId, $userId)->canModifyMessage();
493 }
494}
getCalendarEventDataFromMessageAction(int $messageId)
processBeforeAction(Action $action)
Definition secretary.php:19
onCalendarSaveAction(int $messageId, int $calendarEventId)
createChatFromMessageAction(int $messageId)
Definition secretary.php:73
static isIcalMessage(\Bitrix\Mail\Item\Message $message)
Definition message.php:819
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29