1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
event.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Mail;
11
12use Bitrix\Main;
13use Bitrix\Main\Config;
14use Bitrix\Main\Application;
15use Bitrix\Main\ORM\Data\AddResult;
16
17class Event
18{
19 const SEND_RESULT_NONE = 'N';
21 const SEND_RESULT_ERROR = 'F';
22 const SEND_RESULT_PARTLY = 'P';
24
30 public static function sendImmediate(array $data)
31 {
32 if (!static::onBeforeEventAdd($data))
33 {
34 return static::SEND_RESULT_NONE;
35 }
36
37 $data["ID"] = 0;
38
39 return static::handleEvent($data);
40 }
41
48 public static function send(array $data)
49 {
50 if (!static::onBeforeEventAdd($data))
51 {
52 return (new AddResult())->addError(new Main\Error('OnBeforeEventAdd'));
53 }
54
55 $manageCache = Application::getInstance()->getManagedCache();
56 if (CACHED_b_event !== false && $manageCache->read(CACHED_b_event, "events"))
57 {
58 $manageCache->clean('events');
59 }
60
61 $fileList = [];
62 if (isset($data['FILE']))
63 {
64 if (is_array($data['FILE']))
65 {
66 $fileList = $data['FILE'];
67 }
68
69 unset($data['FILE']);
70 }
71
72 $attachments = [];
73 foreach ($fileList as $file)
74 {
75 $attachment = [];
76 if (is_numeric($file) && \CFile::GetFileArray($file))
77 {
78 $attachment['FILE_ID'] = $file;
79 $attachment['IS_FILE_COPIED'] = 'N';
80 }
81 else
82 {
83 $fileArray = \CFile::MakeFileArray($file);
84 $fileArray['MODULE_ID'] = 'main';
85
86 $attachment['FILE'] = $fileArray;
87 $attachment['IS_FILE_COPIED'] = 'Y';
88 }
89 $attachments[] = $attachment;
90 }
91
93
94 $connection->startTransaction();
95
97
98 if ($result->isSuccess())
99 {
100 $id = $result->getId();
101
102 foreach ($attachments as $file)
103 {
104 $attachment = [
105 'EVENT_ID' => $id,
106 'IS_FILE_COPIED' => $file['IS_FILE_COPIED'],
107 'FILE_ID' => $file['FILE_ID'] ?? null,
108 ];
109
110 if ($attachment['IS_FILE_COPIED'] == 'Y')
111 {
112 $attachment['FILE_ID'] = \CFile::SaveFile($file['FILE'], 'main');
113 }
114
116 }
117 }
118
119 $connection->commitTransaction();
120
121 return $result;
122 }
123
124 protected static function onBeforeEventAdd(array &$data): bool
125 {
126 foreach (GetModuleEvents("main", "OnBeforeEventAdd", true) as $arEvent)
127 {
128 if (ExecuteModuleEventEx($arEvent, [&$data["EVENT_NAME"], &$data["LID"], &$data["C_FIELDS"], &$data["MESSAGE_ID"], &$data["FILE"], &$data["LANGUAGE_ID"]]) === false)
129 {
130 return false;
131 }
132 }
133
134 if (isset($data['LID']) && is_array($data['LID']))
135 {
136 $data['LID'] = implode(",", $data['LID']);
137 }
138
139 if (!is_array($data["C_FIELDS"] ?? null))
140 {
141 $data["C_FIELDS"] = [];
142 }
143
144 if (isset($data["MESSAGE_ID"]) && (int)$data["MESSAGE_ID"] > 0)
145 {
146 $data["MESSAGE_ID"] = (int)$data["MESSAGE_ID"];
147 }
148 else
149 {
150 unset($data["MESSAGE_ID"]);
151 }
152
153 return true;
154 }
155
161 public static function handleEvent(array $arEvent)
162 {
163 if (!isset($arEvent['FIELDS']) && isset($arEvent['C_FIELDS']))
164 {
165 $arEvent['FIELDS'] = $arEvent['C_FIELDS'];
166 }
167
168 if (!is_array($arEvent['FIELDS']))
169 {
170 throw new Main\ArgumentTypeException("FIELDS");
171 }
172
173 $flag = static::SEND_RESULT_TEMPLATE_NOT_FOUND; // no templates
174 $arResult = [
175 "Success" => false,
176 "Fail" => false,
177 "Was" => false,
178 "Skip" => false,
179 ];
180
181 $trackRead = null;
182 $trackClick = null;
183 if (array_key_exists('TRACK_READ', $arEvent))
184 {
185 $trackRead = $arEvent['TRACK_READ'];
186 }
187 if (array_key_exists('TRACK_CLICK', $arEvent))
188 {
189 $trackClick = $arEvent['TRACK_CLICK'];
190 }
191
192 $arSites = explode(",", $arEvent["LID"]);
193 if (empty($arSites))
194 {
195 return $flag;
196 }
197
198 // get charset and server name for languages of event
199 // actually it's one of the sites (let it be the first one)
200 $charset = false;
201 $serverName = null;
202
203 static $sites = [];
204 $infoSite = reset($arSites);
205
206 if (!isset($sites[$infoSite]))
207 {
208 $siteDb = Main\SiteTable::getList([
209 'select' => ['SERVER_NAME', 'CULTURE_CHARSET' => 'CULTURE.CHARSET'],
210 'filter' => ['=LID' => $infoSite],
211 ]);
212 $sites[$infoSite] = $siteDb->fetch();
213 }
214
215 if (is_array($sites[$infoSite]))
216 {
217 $charset = $sites[$infoSite]['CULTURE_CHARSET'];
218 $serverName = $sites[$infoSite]['SERVER_NAME'];
219 }
220
221 if (!$charset)
222 {
223 return $flag;
224 }
225
226 // get filter for list of message templates
227 $arEventMessageFilter = [];
228 $MESSAGE_ID = intval($arEvent["MESSAGE_ID"] ?? 0);
229 if ($MESSAGE_ID > 0)
230 {
231 $eventMessageDb = Internal\EventMessageTable::getById($MESSAGE_ID);
232 if ($eventMessageDb->Fetch())
233 {
234 $arEventMessageFilter['=ID'] = $MESSAGE_ID;
235 $arEventMessageFilter['=ACTIVE'] = 'Y';
236 }
237 }
238 if (empty($arEventMessageFilter))
239 {
240 $arEventMessageFilter = [
241 '=ACTIVE' => 'Y',
242 '=EVENT_NAME' => $arEvent["EVENT_NAME"],
243 '=EVENT_MESSAGE_SITE.SITE_ID' => $arSites,
244 ];
245
246 if ($arEvent["LANGUAGE_ID"] <> '')
247 {
248 $arEventMessageFilter[] = [
249 "LOGIC" => "OR",
250 ["=LANGUAGE_ID" => $arEvent["LANGUAGE_ID"]],
251 ["=LANGUAGE_ID" => null],
252 ];
253 }
254 }
255
256 // get list of message templates of event
258 'select' => ['ID'],
259 'filter' => $arEventMessageFilter,
260 'group' => ['ID'],
261 ]);
262
263 while ($arMessage = $messageDb->fetch())
264 {
265 $eventMessage = Internal\EventMessageTable::getRowById($arMessage['ID']);
266
267 $eventMessage['FILE'] = [];
269 'select' => ['FILE_ID'],
270 'filter' => ['=EVENT_MESSAGE_ID' => $arMessage['ID']],
271 ]);
272 while ($arAttachmentDb = $attachmentDb->fetch())
273 {
274 $eventMessage['FILE'][] = $arAttachmentDb['FILE_ID'];
275 }
276
277 $context = new Context();
278 $arFields = $arEvent['FIELDS'];
279
280 foreach (GetModuleEvents("main", "OnBeforeEventSend", true) as $event)
281 {
282 if (ExecuteModuleEventEx($event, [&$arFields, &$eventMessage, $context, &$arResult]) === false)
283 {
284 continue 2;
285 }
286 }
287
288 // get message object for send mail
289 $arMessageParams = [
290 'EVENT' => $arEvent,
291 'FIELDS' => $arFields,
292 'MESSAGE' => $eventMessage,
293 'SITE' => $arSites,
294 'CHARSET' => $charset,
295 ];
297 try
298 {
299 $message->compile();
300 }
301 catch (StopException)
302 {
303 $arResult["Was"] = true;
304 $arResult["Fail"] = true;
305 continue;
306 }
307
308 // send mail
310 'TO' => $message->getMailTo(),
311 'SUBJECT' => $message->getMailSubject(),
312 'BODY' => $message->getMailBody(),
313 'HEADER' => $message->getMailHeaders(),
314 'CHARSET' => $message->getMailCharset(),
315 'CONTENT_TYPE' => $message->getMailContentType(),
316 'MESSAGE_ID' => $message->getMailId(),
317 'ATTACHMENT' => $message->getMailAttachment(),
318 'TRACK_READ' => $trackRead,
319 'TRACK_CLICK' => $trackClick,
320 'LINK_PROTOCOL' => Config\Option::get("main", "mail_link_protocol"),
321 'LINK_DOMAIN' => $serverName,
322 'CONTEXT' => $context,
323 ]);
324 if ($result)
325 {
326 $arResult["Success"] = true;
327 }
328 else
329 {
330 $arResult["Fail"] = true;
331 }
332
333 $arResult["Was"] = true;
334 }
335
336 if ($arResult["Was"])
337 {
338 if ($arResult["Success"])
339 {
340 if ($arResult["Fail"])
341 {
342 // partly sent
343 $flag = static::SEND_RESULT_PARTLY;
344 }
345 else
346 {
347 // all sent
348 $flag = static::SEND_RESULT_SUCCESS;
349 }
350 }
351 else
352 {
353 if ($arResult["Fail"])
354 {
355 // all templates failed
356 $flag = static::SEND_RESULT_ERROR;
357 }
358 }
359 }
360 elseif ($arResult["Skip"])
361 {
362 // skip this event
363 $flag = static::SEND_RESULT_NONE;
364 }
365
366 return $flag;
367 }
368}
$connection
Определения actionsdefinitions.php:38
$arSites
Определения options.php:15
$arResult
Определения generate_coupon.php:16
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
Определения error.php:15
Определения event.php:5
static handleEvent(array $arEvent)
Определения event.php:161
static send(array $data)
Определения event.php:48
static sendImmediate(array $data)
Определения event.php:30
const SEND_RESULT_TEMPLATE_NOT_FOUND
Определения event.php:23
const SEND_RESULT_SUCCESS
Определения event.php:20
const SEND_RESULT_NONE
Определения event.php:19
const SEND_RESULT_PARTLY
Определения event.php:22
static onBeforeEventAdd(array &$data)
Определения event.php:124
const SEND_RESULT_ERROR
Определения event.php:21
static createInstance(array $arMessageParams)
Определения eventmessagecompiler.php:102
static send($mailParams)
Определения mail.php:162
static getRowById($id, array $parameters=[])
Определения datamanager.php:380
static getById($id)
Определения datamanager.php:364
static getList(array $parameters=array())
Определения datamanager.php:431
static add(array $data)
Определения datamanager.php:877
$sites
Определения clear_component_cache.php:15
$arFields
Определения dblapprove.php:5
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
Определения culture.php:9
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393