Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventhandler.php
1<?php
10
11use Bitrix\Main;
12use Bitrix\Main\Entity as MainEntity;
26
27Loc::loadMessages(__FILE__);
28
34{
40 public static function onSenderPresetList()
41 {
42 $list = array();
43
44 if (ModuleManager::isModuleInstalled('crm'))
45 {
46 $list[] = 'Bitrix\Sender\Integration\Crm\Preset\Segment';
47 }
48
49 return $list;
50 }
51
57 public static function onSenderCompanyCard()
58 {
59 $list = array();
60
61 if (Loader::includeModule('crm'))
62 {
63 $list[] = Crm\CompanyCard::getArray();
64 }
65
66 return $list;
67 }
68
76 public static function onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
77 {
78 if (ModuleManager::isModuleInstalled('crm'))
79 {
80 Crm\EventHandler::onAfterPostingSendRecipient($eventData, $letter);
81 }
82
83 if (Bitrix24\Service::isCloud() && $eventData['SEND_RESULT'] && $letter->getMessage()->getCode() === Message\iBase::CODE_MAIL)
84 {
85 Bitrix24\Limitation\DailyLimit::increment();
86 }
87 }
88
96 public static function onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)
97 {
98 if (ModuleManager::isModuleInstalled('crm'))
99 {
100 Crm\EventHandler::onAfterPostingSendRecipientMultiple($eventDataArray, $letter);
101 }
102 }
103
110 public static function onAfterPostingRecipientUnsubscribe(array $eventData)
111 {
112 if (Bitrix24\Service::isCloud() && is_array($eventData))
113 {
114 Bitrix24\Limitation\Rating::regulate();
115 }
116 }
117
124 public static function onConstantList($className)
125 {
126 // sale
127 if (Loader::includeModule('sale') && !Bitrix24\Service::isCloud())
128 {
129 if (Templates\Category::class === $className)
130 {
131 return Sale\Preset\TriggerCampaign::getTemplateCategories();
132 }
133 }
134
135 return [];
136 }
137
144 public static function onTemplateList($messageType)
145 {
146 $list = [
147 Message\iBase::CODE_MAIL => []
148 ];
149
150 // sale
151 if (Loader::includeModule('sale') && !Bitrix24\Service::isCloud())
152 {
153 foreach (Sale\Preset\TriggerCampaign::getAll() as $item)
154 {
155 foreach ($item['CHAIN'] as $letter)
156 {
157 $list[Message\iBase::CODE_MAIL][] = array(
158 'ID' => $letter['TEMPLATE_ID'],
159 'TYPE' => $letter['TEMPLATE_TYPE'],
160 'CATEGORY' => mb_strtoupper($item['CODE']),
161 'MESSAGE_CODE' => Message\iBase::CODE_MAIL,
162 'VERSION' => 2,
163 'IS_TRIGGER' => true,
164 'HOT' => false,
165 'ICON' => false,
166
167 'NAME' => $letter['SUBJECT'],
168 'DESC' => '',
169 'FIELDS' => array(
170 'SUBJECT' => array(
171 'CODE' => 'SUBJECT',
172 'VALUE' => $letter['SUBJECT'],
173 ),
174 'MESSAGE' => array(
175 'CODE' => 'MESSAGE',
176 'VALUE' => $letter['MESSAGE'],
177 'ON_DEMAND' => true
178 ),
179 ),
180 );
181 }
182 }
183 }
184
185 return isset($list[$messageType]) ? $list[$messageType] : [];
186 }
187
194 public static function onConnectorList(array $data = array())
195 {
196 // sender
197 $data['CONNECTOR'][] = 'Bitrix\Sender\Integration\Sender\Connectors\Recipient';
198 $data['CONNECTOR'][] = 'Bitrix\Sender\Integration\Sender\Connectors\Contact';
199 $data['CONNECTOR'][] = 'Bitrix\Sender\Integration\Sender\Connectors\UnSubscribers';
200
201 // crm
202 if (Loader::includeModule('crm'))
203 {
204 $data['CONNECTOR'][] = 'Bitrix\Sender\Integration\Crm\Connectors\Lead';
205 $data['CONNECTOR'][] = 'Bitrix\Sender\Integration\Crm\Connectors\Client';
206 }
207
208 return $data;
209 }
210
217 public static function onTriggerList(array $data = [])
218 {
219 $data['TRIGGER'] = [
220 'Bitrix\Sender\Integration\Main\Triggers\UserAuth',
221 'Bitrix\Sender\Integration\Main\Triggers\UserDontAuth',
222 ];
223
224 return $data;
225 }
226
232 public static function onSenderMessageList()
233 {
234 $list = array();
235
236 // mail
237 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\MessageMail';
238
239 // sms
240 if (MessageService\Sms\Service::canUse())
241 {
242 $list[] = 'Bitrix\Sender\Integration\MessageService\Sms\MessageSms';
243 }
244
245 // im
246 if (Im\Service::canUse())
247 {
248 $list[] = 'Bitrix\Sender\Integration\Im\MessageIm';
249 }
250
251 // call
252 if (VoxImplant\Service::canUse())
253 {
254 $list[] = 'Bitrix\Sender\Integration\VoxImplant\MessageCall';
255 $list[] = 'Bitrix\Sender\Integration\VoxImplant\MessageAudioCall';
256 }
257
258 // web_hook
259 //$list[] = 'Bitrix\Sender\Integration\Sender\WebHook\MessageWebHook';
260
261 // Ads
262 if (Seo\Ads\Service::canUse())
263 {
264 $adsList = array(
265 'Bitrix\Sender\Integration\Seo\Ads\MessageYa',
266 \Bitrix\Sender\Integration\Seo\Ads\MessageLookalikeYandex::class,
267 'Bitrix\Sender\Integration\Seo\Ads\MessageGa',
268 \Bitrix\Sender\Integration\Seo\Ads\MessageVk::class,
269 'Bitrix\Sender\Integration\Seo\Ads\MessageFb',
270 'Bitrix\Sender\Integration\Seo\Ads\MessageMarketingFb',
271 'Bitrix\Sender\Integration\Seo\Ads\MessageMarketingInstagram',
272 'Bitrix\Sender\Integration\Seo\Ads\MessageLookalikeFb',
273 );
274 foreach ($adsList as $adsClass)
275 {
276 if (!Bitrix24\Service::isAdVisibleInRegion($adsClass::CODE))
277 {
278 continue;
279 }
280
281 $list[] = $adsClass;
282 }
283 }
284
285 // Return Customer
286 if (Crm\ReturnCustomer\Service::canUse())
287 {
288 $list[] = 'Bitrix\Sender\Integration\Crm\ReturnCustomer\MessageLead';
289 $list[] = 'Bitrix\Sender\Integration\Crm\ReturnCustomer\MessageDeal';
290 }
291
292 if (Bitrix24\Service::isMasterYandexVisibleInRegion())
293 {
294 $list[] = \Bitrix\Sender\Integration\Yandex\Master\MessageMasterYandex::class;
295 }
296 if(Bitrix24\Service::isTolokaVisibleInRegion())
297 {
298 $list[] = 'Bitrix\Sender\Integration\Yandex\Toloka\MessageToloka';
299 }
300
301 return $list;
302 }
303
309 public static function onSenderTriggerCampaignPreset()
310 {
311 $list = [];
312
313 // sale
314 if (Loader::includeModule('sale') && !Bitrix24\Service::isCloud())
315 {
316 $list = array_merge($list, Sale\Preset\TriggerCampaign::getAll());
317 }
318
319
320 return $list;
321 }
322 public static function onSenderConsentMessageBuildersList(): array
323 {
324 $list = [];
325 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\ConsentBuilderMail';
326 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\TestConsentBuilderMail';
327 return $list;
328 }
329 public static function onSenderConsentResponseList(): array
330 {
331 $list = [];
332 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\ConsentResponseMail';
333 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\TestConsentResponseMail';
334 return $list;
335 }
341 public static function onSenderTransportList()
342 {
343 $list = array();
344
345 // mail
346 $list[] = 'Bitrix\Sender\Integration\Sender\Mail\TransportMail';
347
348 // sms
349 if (MessageService\Sms\Service::canUse())
350 {
351 $list[] = 'Bitrix\Sender\Integration\MessageService\Sms\TransportSms';
352 }
353
354 // im
355 if (Im\Service::canUse())
356 {
357 $list[] = 'Bitrix\Sender\Integration\Im\TransportIm';
358 }
359
360 // call
361 if (VoxImplant\Service::canUse())
362 {
363 $list[] = 'Bitrix\Sender\Integration\VoxImplant\TransportCall';
364 $list[] = 'Bitrix\Sender\Integration\VoxImplant\TransportAudioCall';
365 }
366
367 // web_hook
368 //$list[] = 'Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook';
369
370 // Ads
371 if (Seo\Ads\Service::canUse())
372 {
373 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportYa';
374 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportGa';
375 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportVk';
376 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportFb';
377 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportMarketingFb';
378 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportMarketingInstagram';
379 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportLookalikeVk';
380 $list[] = 'Bitrix\Sender\Integration\Seo\Ads\TransportLookalikeFb';
381 $list[] = \Bitrix\Sender\Integration\Seo\Ads\TransportLookalikeYandex::class;
382 }
383
384 // Return Customer
385 if (Crm\ReturnCustomer\Service::canUse())
386 {
387 $list[] = 'Bitrix\Sender\Integration\Crm\ReturnCustomer\TransportLead';
388 $list[] = 'Bitrix\Sender\Integration\Crm\ReturnCustomer\TransportDeal';
389 }
390
391 $list[] = 'Bitrix\Sender\Integration\Yandex\Toloka\TransportToloka';
392
393 return $list;
394 }
395
403 public static function onBeforeUpdateLetterTable(MainEntity\Event $event, MainEntity\EventResult $result)
404 {
405 $data = $event->getParameters();
406
407 if (Bitrix24\Service::isCloud() && isset($data['fields']['STATUS']))
408 {
409 $oldRow = LetterTable::getRowById($data['primary']['ID']);
410 $updatedBy = $data['fields']['UPDATED_BY'] ?? $oldRow['UPDATED_BY'];
411
412 if (in_array($data['fields']['STATUS'], Dispatch\Semantics::getWorkStates()))
413 {
414 $user = new User($updatedBy);
415 if (!$user->isAgreementAccepted())
416 {
417 $result->addError(
418 new MainEntity\EntityError(Agreement::getErrorText(), 'NEED_ACCEPT_AGREEMENT')
419 );
420 return;
421 }
422
423 $letter = Entity\Letter::createInstanceById($data['primary']['ID']);
424
425 if (is_null($letter))
426 {
427 $result->addError(
428 new MainEntity\EntityError(
429 Loc::getMessage("SENDER_LETTER_ONBEFOREUPDATE_ERROR_LETTER_NOT_AVAILABLE"), 'FEATURE_NOT_AVAILABLE'
430 )
431 );
432 return;
433 }
434
435 if (!$letter->getMessage()->isAvailable())
436 {
437 if ($letter->getState()->isWaiting() || $letter->getState()->isSending())
438 {
439 $letter->stop();
440 }
441
442 $result->addError(
443 new MainEntity\EntityError(
444 Loc::getMessage("SENDER_LETTER_ONBEFOREUPDATE_ERROR_FEATURE_NOT_AVAILABLE"), 'FEATURE_NOT_AVAILABLE'
445 )
446 );
447 return;
448 }
449
450 $isEmail = ($oldRow['MESSAGE_CODE'] === Message\iBase::CODE_MAIL);
451 $isEmailBlocked = Bitrix24\Limitation\Rating::isBlocked();
452 if ($isEmail && $isEmailBlocked)
453 {
454 $result->addError(
455 new MainEntity\EntityError(
456 Bitrix24\Limitation\Rating::getNotifyText('blocked')
457 )
458 );
459 }
460
461 if ($isEmail)
462 {
463 // check sender email:
464 $emailFrom = $letter->getMessage()->getConfiguration()->getOption('EMAIL_FROM')->getValue();
465 if (!Sender\AllowedSender::isAllowed($emailFrom, $updatedBy))
466 {
467 $result->addError(
468 new MainEntity\EntityError(
469 Loc::getMessage("SENDER_LETTER_ONBEFOREUPDATE_ERROR_INVALID_FROM_EMAIL"), 'WRONG_EMAIL_FROM'
470 )
471 );
472 }
473 }
474 }
475 }
476 }
477
484 public static function onMailEventMailChangeStatus($result)
485 {
486 if (!$result->isBelongTo('sender', 'rcpt'))
487 {
488 return;
489 }
490
491 // return if status already updated
492 $row = PostingRecipientTable::getRow([
493 'select' => [
494 'STATUS', 'POSTING_ID', 'CONTACT_ID',
495 'CONTACT_IS_SEND_SUCCESS' => 'CONTACT.IS_SEND_SUCCESS'
496 ],
497 'filter' => ['=ID' => $result->getEntityId()]
498 ]);
499 if (!$row)
500 {
501 return;
502 }
503
504 if (!$result->isError())
505 {
506 // update contact send_success flag
507 if ($row['CONTACT_IS_SEND_SUCCESS'] !== 'Y')
508 {
509 ContactTable::update($row['CONTACT_ID'], ['IS_SEND_SUCCESS' => 'Y']);
510 }
511 }
512 elseif ($result->isPermanentError())
513 {
514 // return if status already updated
515 if ($row['STATUS'] === PostingRecipientTable::SEND_RESULT_ERROR)
516 {
517 return;
518 }
519
520 // update recipient status
521 Model\Posting\RecipientTable::update(
522 $result->getEntityId(),
524 );
525
526 // update posting counters
527 Model\PostingTable::update(
528 $row['POSTING_ID'],
529 [
530 'COUNT_SEND_ERROR' => new Main\DB\SqlExpression('?# + 1', 'COUNT_SEND_ERROR'),
531 'COUNT_SEND_SUCCESS' => new Main\DB\SqlExpression('?# - 1', 'COUNT_SEND_SUCCESS')
532 ]
533 );
534
535 // update daily limit counters
536 if (Bitrix24\Service::isCloud())
537 {
538 Bitrix24\Limitation\DailyLimit::incrementError();
539 }
540 }
541 }
542}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onConnectorList(array $data=array())
static onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
static onBeforeUpdateLetterTable(MainEntity\Event $event, MainEntity\EventResult $result)
static onAfterPostingRecipientUnsubscribe(array $eventData)
static onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)