Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
8namespace Bitrix\Main\Mail;
9
10use Bitrix\Main;
14
15class Event
16{
17 const SEND_RESULT_NONE = 'N';
19 const SEND_RESULT_ERROR = 'F';
20 const SEND_RESULT_PARTLY = 'P';
22
28 public static function sendImmediate(array $data)
29 {
30 $data["ID"] = 0;
31
32 return static::handleEvent($data);
33 }
34
41 public static function send(array $data)
42 {
43 $manageCache = Application::getInstance()->getManagedCache();
44 if(CACHED_b_event !== false && $manageCache->read(CACHED_b_event, "events"))
45 {
46 $manageCache->clean('events');
47 }
48
49 $fileList = [];
50 if(isset($data['FILE']))
51 {
52 if(is_array($data['FILE']))
53 $fileList = $data['FILE'];
54
55 unset($data['FILE']);
56 }
57
58 $attachments = [];
59 foreach ($fileList as $file)
60 {
61 $attachment = [];
62 if (is_numeric($file) && \CFile::GetFileArray($file))
63 {
64 $attachment['FILE_ID'] = $file;
65 $attachment['IS_FILE_COPIED'] = 'N';
66 }
67 else
68 {
69 $fileArray = \CFile::MakeFileArray($file);
70 $fileArray['MODULE_ID'] = 'main';
71
72 $attachment['FILE'] = $fileArray;
73 $attachment['IS_FILE_COPIED'] = 'Y';
74 }
75 $attachments[] = $attachment;
76 }
77
78 $connection = Application::getConnection();
79
80 $connection->startTransaction();
81
82 $result = MailInternal\EventTable::add($data);
83
84 if ($result->isSuccess())
85 {
86 $id = $result->getId();
87
88 foreach ($attachments as $file)
89 {
90 $attachment = [
91 'EVENT_ID' => $id,
92 'IS_FILE_COPIED' => $file['IS_FILE_COPIED'],
93 'FILE_ID' => $file['FILE_ID'] ?? null,
94 ];
95
96 if ($attachment['IS_FILE_COPIED'] == 'Y')
97 {
98 $attachment['FILE_ID'] = \CFile::SaveFile($file['FILE'], 'main');
99 }
100
101 MailInternal\EventAttachmentTable::add($attachment);
102 }
103 }
104
105 $connection->commitTransaction();
106
107 return $result;
108 }
109
117 public static function handleEvent(array $arEvent)
118 {
119 if(!isset($arEvent['FIELDS']) && isset($arEvent['C_FIELDS']))
120 $arEvent['FIELDS'] = $arEvent['C_FIELDS'];
121
122 if(!is_array($arEvent['FIELDS']))
123 throw new Main\ArgumentTypeException("FIELDS" );
124
125 $flag = static::SEND_RESULT_TEMPLATE_NOT_FOUND; // no templates
126 $arResult = array(
127 "Success" => false,
128 "Fail" => false,
129 "Was" => false,
130 "Skip" => false,
131 );
132
133 $trackRead = null;
134 $trackClick = null;
135 if(array_key_exists('TRACK_READ', $arEvent))
136 $trackRead = $arEvent['TRACK_READ'];
137 if(array_key_exists('TRACK_CLICK', $arEvent))
138 $trackClick = $arEvent['TRACK_CLICK'];
139
140 $arSites = explode(",", $arEvent["LID"]);
141 if(empty($arSites))
142 {
143 return $flag;
144 }
145
146 // get charset and server name for languages of event
147 // actually it's one of the sites (let it be the first one)
148 $charset = false;
149 $serverName = null;
150
151 static $sites = array();
152 $infoSite = reset($arSites);
153
154 if(!isset($sites[$infoSite]))
155 {
156 $siteDb = Main\SiteTable::getList(array(
157 'select' => array('SERVER_NAME', 'CULTURE_CHARSET'=>'CULTURE.CHARSET'),
158 'filter' => array('=LID' => $infoSite)
159 ));
160 $sites[$infoSite] = $siteDb->fetch();
161 }
162
163 if(is_array($sites[$infoSite]))
164 {
165 $charset = $sites[$infoSite]['CULTURE_CHARSET'];
166 $serverName = $sites[$infoSite]['SERVER_NAME'];
167 }
168
169 if(!$charset)
170 {
171 return $flag;
172 }
173
174 // get filter for list of message templates
175 $arEventMessageFilter = array();
176 $MESSAGE_ID = intval($arEvent["MESSAGE_ID"]);
177 if($MESSAGE_ID > 0)
178 {
179 $eventMessageDb = MailInternal\EventMessageTable::getById($MESSAGE_ID);
180 if($eventMessageDb->Fetch())
181 {
182 $arEventMessageFilter['=ID'] = $MESSAGE_ID;
183 $arEventMessageFilter['=ACTIVE'] = 'Y';
184 }
185 }
186 if(empty($arEventMessageFilter))
187 {
188 $arEventMessageFilter = array(
189 '=ACTIVE' => 'Y',
190 '=EVENT_NAME' => $arEvent["EVENT_NAME"],
191 '=EVENT_MESSAGE_SITE.SITE_ID' => $arSites,
192 );
193
194 if($arEvent["LANGUAGE_ID"] <> '')
195 {
196 $arEventMessageFilter[] = array(
197 "LOGIC" => "OR",
198 array("=LANGUAGE_ID" => $arEvent["LANGUAGE_ID"]),
199 array("=LANGUAGE_ID" => null),
200 );
201 }
202 }
203
204 // get list of message templates of event
205 $messageDb = MailInternal\EventMessageTable::getList(array(
206 'select' => array('ID'),
207 'filter' => $arEventMessageFilter,
208 'group' => array('ID')
209 ));
210
211 while($arMessage = $messageDb->fetch())
212 {
213 $eventMessage = MailInternal\EventMessageTable::getRowById($arMessage['ID']);
214
215 $eventMessage['FILE'] = array();
216 $attachmentDb = MailInternal\EventMessageAttachmentTable::getList(array(
217 'select' => array('FILE_ID'),
218 'filter' => array('=EVENT_MESSAGE_ID' => $arMessage['ID']),
219 ));
220 while($arAttachmentDb = $attachmentDb->fetch())
221 {
222 $eventMessage['FILE'][] = $arAttachmentDb['FILE_ID'];
223 }
224
225 $context = new Context();
226 $arFields = $arEvent['FIELDS'];
227
228 foreach (GetModuleEvents("main", "OnBeforeEventSend", true) as $event)
229 {
230 if(ExecuteModuleEventEx($event, array(&$arFields, &$eventMessage, $context, &$arResult)) === false)
231 {
232 continue 2;
233 }
234 }
235
236 // get message object for send mail
237 $arMessageParams = array(
238 'EVENT' => $arEvent,
239 'FIELDS' => $arFields,
240 'MESSAGE' => $eventMessage,
241 'SITE' => $arSites,
242 'CHARSET' => $charset,
243 );
244 $message = EventMessageCompiler::createInstance($arMessageParams);
245 try
246 {
247 $message->compile();
248 }
249 catch(StopException $e)
250 {
251 $arResult["Was"] = true;
252 $arResult["Fail"] = true;
253 continue;
254 }
255
256 // send mail
257 $result = Main\Mail\Mail::send(array(
258 'TO' => $message->getMailTo(),
259 'SUBJECT' => $message->getMailSubject(),
260 'BODY' => $message->getMailBody(),
261 'HEADER' => $message->getMailHeaders(),
262 'CHARSET' => $message->getMailCharset(),
263 'CONTENT_TYPE' => $message->getMailContentType(),
264 'MESSAGE_ID' => $message->getMailId(),
265 'ATTACHMENT' => $message->getMailAttachment(),
266 'TRACK_READ' => $trackRead,
267 'TRACK_CLICK' => $trackClick,
268 'LINK_PROTOCOL' => Config\Option::get("main", "mail_link_protocol", ''),
269 'LINK_DOMAIN' => $serverName,
270 'CONTEXT' => $context,
271 ));
272 if($result)
273 $arResult["Success"] = true;
274 else
275 $arResult["Fail"] = true;
276
277 $arResult["Was"] = true;
278 }
279
280 if($arResult["Was"])
281 {
282 if($arResult["Success"])
283 {
284 if($arResult["Fail"])
285 $flag = static::SEND_RESULT_PARTLY; // partly sent
286 else
287 $flag = static::SEND_RESULT_SUCCESS; // all sent
288 }
289 else
290 {
291 if($arResult["Fail"])
292 $flag = static::SEND_RESULT_ERROR; // all templates failed
293 }
294 }
295 elseif($arResult["Skip"])
296 {
297 $flag = static::SEND_RESULT_NONE; // skip this event
298 }
299
300 return $flag;
301 }
302}
static getConnection($name="")
static handleEvent(array $arEvent)
Definition event.php:117
static send(array $data)
Definition event.php:41
static sendImmediate(array $data)
Definition event.php:28
const SEND_RESULT_TEMPLATE_NOT_FOUND
Definition event.php:21
const SEND_RESULT_SUCCESS
Definition event.php:18