Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usercontroller.php
1<?php
3
6
8{
9 private static
10 $MAIL_TYPE_EXTERNAL = "CALENDAR_INVITATION_EXTERNAL";
11
12
13 public static function inviteUser($userId, $params)
14 {
15 $eventFields = $params['arFields'];
16 $invitedUser = $params['userIndex'][$userId];
17 $email = $invitedUser['EMAIL'];
18
19 $nameFormatted = str_replace(['<', '>', '"'], '', \CCalendar::GetUserName($invitedUser));
20 $mailMessageId = "<CALENDAR_INVITE_".$eventFields["PARENT_ID"]."@".$GLOBALS["SERVER_NAME"].">";
21 $mailMessageInReplyTo = "<CALENDAR_INVITE_".$eventFields["PARENT_ID"]."@".$GLOBALS["SERVER_NAME"].">";
22 $siteId = SITE_ID;
23 $fromName = Loc::getMessage('CALENDAR_SERVICE_NAME');
24 $responseEmailAddress = "CALENDAR_INVITE_".$eventFields["PARENT_ID"]."@".$GLOBALS["SERVER_NAME"];
25
26 $res = \Bitrix\Mail\User::getReplyTo(
27 $siteId,
28 $userId,
29 'CALENDAR_EVENT',
30 $eventFields["PARENT_ID"],
31 self::getPublicUrl()
32 );
33
34 if (is_array($res))
35 {
36 list($replyTo, $backUrl) = $res;
37
38 if (
39 $replyTo
40 && $backUrl
41 )
42 {
43 $icsAttachment = self::getIcsFileAttachment($eventFields,
44 [
45 'attendees' => self::prepareAttendeesData(
46 $eventFields['ATTENDEES'],
47 $params['currentAttendees'],
48 $params['userIndex'],
49 $params['userId']
50 ),
51 'responseEmailAddress' => $responseEmailAddress
52 ]);
53 $attachments = [$icsAttachment];
54 // 1. create ical-content
55 // 2. save file ($APPLICATION->SaveFileContent($abs_path, $filesrc_for_save))
56 // 3. add b_file entry
57 // 4. clear file after sending
58
59 $fromName = str_replace(['<', '>', '"'], '', $fromName);
60 $id = \CEvent::Send(
61 self::$MAIL_TYPE_EXTERNAL,
62 $siteId,
63 [
64 "=Reply-To" => $fromName.' <'.$replyTo.'>',
65 "=Message-Id" => $mailMessageId,
66 "=In-Reply-To" => $mailMessageInReplyTo == $mailMessageId ? '' : $mailMessageInReplyTo,
67 "EMAIL_FROM" => $fromName.' <'.\Bitrix\Mail\User::getDefaultEmailFrom().'>',
68 "EMAIL_TO" => (!empty($nameFormatted) ? ''.$nameFormatted.' <'.$email.'>' : $email),
69 "RECIPIENT_ID" => $userId,
70 "COMMENT_ID" => '',
71 "POST_ID" => intval($eventFields["PARENT_ID"]),
72 "POST_TITLE" => 'calendar invitation',
73 "URL" => self::getPublicUrl()
74 ],
75 'Y',
76 '',
77 $attachments
78 );
79 }
80 }
81 }
82
83 private static function prepareAttendeesData($attendeeIdList, $currentAttendees, $userIndex, $currentUserId)
84 {
85 $attendeesList = [];
86 $attendeeIndex = [];
87 if (is_array($currentAttendees))
88 {
89 foreach($currentAttendees as $user)
90 {
91 $attendeeIndex[$user['USER_ID']] = $user;
92 }
93 }
94
95 if (is_array($attendeeIdList))
96 {
97 foreach($attendeeIdList as $userId)
98 {
99 if (isset($userIndex[$userId]))
100 {
101 if ($attendeeIndex[$userId])
102 {
103 $attendeesList[] = [
104 'id' => $userIndex[$userId]['ID'],
105 'email' => $userIndex[$userId]['EMAIL'],
106 'external_auth_id' => $userIndex[$userId]['EXTERNAL_AUTH_ID'],
107 'name' => $attendeeIndex[$userId]['DISPLAY_NAME'],
108 'status' => $attendeeIndex[$userId]['STATUS']
109 ];
110 }
111 else
112 {
113 $attendeesList[] = [
114 'id' => $userIndex[$userId]['ID'],
115 'email' => $userIndex[$userId]['EMAIL'],
116 'external_auth_id' => $userIndex[$userId]['EXTERNAL_AUTH_ID'],
117 'name' => \CCalendar::getUserName($userIndex[$userId]),
118 'status' => $currentUserId === $userId ? 'H' : 'Q'
119 ];
120 }
121 }
122 }
123 }
124 //eventFields['ATTENDEES'], $params['currentAttendees'], $params['userIndex']
125
126 return $attendeesList;
127 }
128
129 private static function getPublicUrl()
130 {
131 return '/pub/event.php';
132 }
133
139 private static function getIcsFileAttachment($eventFields, $params = [])
140 {
141 $fileName = 'invite.ics';
142 $fileData = array(
143 'name' => $fileName,
144 'type' => 'text/calendar',
145 'content' => self::getIcsFileContent($eventFields, $params),
146 'MODULE_ID' => 'calendar'
147 );
148 $fileId = \CFile::SaveFile($fileData, 'calendar');
149 $fileArray = \CFile::GetFileArray($fileId);
150 if (!is_array($fileArray))
151 {
152 return false;
153 }
154
155 //$storageTypeId = StorageType::getDefaultTypeID();
156 //return StorageManager::saveEmailAttachment($fileArray, $storageTypeId, $siteId);
157 return $fileId;
158 }
159
160 public static function getIcsFileContent($eventFields, $params = [])
161 {
162 $ics = new \Bitrix\Calendar\ICal\IcsBuilder(
163 [
164 'summary' => $eventFields['NAME'],
165 'description' => '',
166 'dtstart' => Util::getTimestamp($eventFields['DATE_FROM']),
167 'dtend' => Util::getTimestamp($eventFields['DATE_TO']),
168 'location' => \CCalendar::getTextLocation($eventFields['LOCATION']['NEW']),
169 'uid' => isset($eventFields['DAV_XML_ID']) ? $eventFields['DAV_XML_ID'] : uniqid()
170 ]);
171
172 if ($eventFields['SKIP_TIME'] !== 'Y')
173 {
174 $ics->setFullDayMode($eventFields['SKIP_TIME'] == 'Y');
175 $ics->setConfig(
176 [
177 'timezoneFrom' => $eventFields['TZ_FROM'],
178 'timezoneTo' => !empty($eventFields['TZ_TO']) ? $eventFields['TZ_TO'] : $eventFields['TZ_FROM']
179 ]
180 );
181 }
182
183 $ics->setOrganizer($eventFields['MEETING']['HOST_NAME'], $params['responseEmailAddress']);
184 $ics->setAttendees($params['attendees']);
185 return $ics->render();
186 }
187}
static inviteUser($userId, $params)
static getIcsFileContent($eventFields, $params=[])
static getTimestamp($date, $round=true, $getTime=true)
Definition util.php:55
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1