41 $args = func_get_args();
42 if (isset($args[0]) && $args[0] instanceof
Event)
46 $moduleId = $event->getModuleId();
47 $eventType = $event->getEventType();
48 $eventData = $event->getParameters();
52 global $BX_MODULE_EVENT_LAST;
53 $moduleId = $BX_MODULE_EVENT_LAST[
'FROM_MODULE_ID'];
54 $eventType = $BX_MODULE_EVENT_LAST[
'MESSAGE_ID'];
58 static::processEvent(array(
59 'MODULE_ID' => $moduleId,
60 'EVENT_TYPE' => $eventType,
61 'EVENT_DATA' => $eventData,
72 $moduleId = $params[
'MODULE_ID'];
73 $eventType = $params[
'EVENT_TYPE'];
74 $eventData = $params[
'EVENT_DATA'];
77 '=MAILING_CHAIN.MAILING.ACTIVE' =>
'Y',
78 '=MAILING_CHAIN.IS_TRIGGER' =>
'Y',
80 '=EVENT' => $moduleId.
'/'.$eventType
82 if(isset($params[
'FILTER']) && is_array($params[
'FILTER']))
84 $filter = $filter + $params[
'FILTER'];
87 $chainDb = MailingTriggerTable::getList(array(
90 'SITE_ID' =>
'MAILING_CHAIN.MAILING.SITE_ID',
91 'ID' =>
'MAILING_CHAIN.ID',
92 'MAILING_ID' =>
'MAILING_CHAIN.MAILING_ID',
93 'PARENT_ID' =>
'MAILING_CHAIN.PARENT_ID',
94 'POSTING_ID' =>
'MAILING_CHAIN.POSTING_ID',
95 'TIME_SHIFT' =>
'MAILING_CHAIN.TIME_SHIFT',
96 'STATUS' =>
'MAILING_CHAIN.STATUS',
97 'AUTO_SEND_TIME' =>
'MAILING_CHAIN.AUTO_SEND_TIME'
100 'order' => array(
'MAILING_CHAIN_ID' =>
'ASC',
'IS_TYPE_START' =>
'ASC')
102 while($chain = $chainDb->fetch())
104 $settings =
new Settings($chain[
'ENDPOINT']);
105 $trigger = static::getOnce($settings->getEndpoint());
106 if(!$trigger)
continue;
108 $trigger->setSiteId($chain[
'SITE_ID']);
109 $trigger->setFields($settings->getFields());
110 $trigger->setParams(array(
'CHAIN' => $chain,
'EVENT' => $eventData));
113 $runForOldData = ($trigger->canRunForOldData() && $settings->canRunForOldData() && !$settings->wasRunForOldData());
114 $trigger->setRunForOldData($runForOldData);
117 if(!$trigger->filter())
continue;
120 static::$postingId =
null;
121 $recipientDb = $trigger->getRecipientResult();
122 while($recipient = $recipientDb->fetch())
124 if($settings->isTypeStart())
126 static::addRecipient($chain, $settings, $recipient);
130 static::stop($chain, $recipient,
true);
140 if($settings->isTypeStart())
143 if($settings->isPreventEmail())
145 static::preventMailEvent($trigger->getMailEventToPrevent());
149 static::send($chain);
163 protected static function stop($chain, $data, $setGoal)
165 if(!$data || empty($data[
'EMAIL']))
170 $code = $data[
'EMAIL'];
171 $typeId = Recipient\Type::detect($data[
'EMAIL']);
172 $code = Recipient\Normalizer::normalize($code, $typeId);
175 $recipientDb = PostingRecipientTable::getList(array(
176 'select' => array(
'ID',
'ROOT_ID',
'POSTING_ID',
'STATUS',
'POSTING_STATUS' =>
'POSTING.STATUS'),
178 '=CONTACT.CODE' => $code,
179 '=CONTACT.TYPE_ID' => $typeId,
180 '=POSTING.MAILING_ID' => $chain[
'MAILING_ID'],
188 if($recipient = $recipientDb->fetch())
192 Model\Posting\RecipientTable::update($recipient[
'ID'], $updateFields);
197 $recipientCountDb = PostingRecipientTable::getList(array(
198 'select' => array(
'POSTING_ID'),
200 '=POSTING_ID' => $recipient[
'POSTING_ID'],
208 if(!$recipientCountDb->fetch())
221 $recipientDb = PostingRecipientTable::getList(array(
222 'select' => array(
'ID',
'DATE_DENY'),
224 '=CONTACT.CODE' => $code,
225 '=CONTACT.TYPE_ID' => $typeId,
226 '=POSTING.MAILING_ID' => $chain[
'MAILING_ID'],
231 'order' => array(
'DATE_SENT' =>
'DESC',
'ID' =>
'DESC'),
234 if($recipient = $recipientDb->fetch())
236 if(empty($recipient[
'DATE_DENY']))
238 Model\Posting\RecipientTable::update($recipient[
'ID'], [
'DATE_DENY' =>
new DateTime]);
306 if(!$data || empty($data[
'EMAIL']))
311 $code = $data[
'EMAIL'];
312 $typeId = Recipient\Type::detect($data[
'EMAIL']);
313 $code = Recipient\Normalizer::normalize($code, $typeId);
322 if(!empty($chain[
'PARENT_ID']))
324 $recipientDb = PostingRecipientTable::getList(array(
325 'select' => array(
'ID',
'STATUS'),
327 '=CONTACT.CODE' => $code,
328 '=CONTACT.TYPE_ID' => $typeId,
329 '=POSTING.MAILING_CHAIN_ID' => $chain[
'ID'],
334 while($recipient = $recipientDb->fetch())
347 if($statusNew !==
null)
349 Model\Posting\RecipientTable::update(
351 [
'STATUS' => $statusNew]
359 $recipientExistsDb = PostingRecipientTable::getList(array(
360 'select' => array(
'ID'),
362 '=CONTACT.CODE' => $code,
363 '=CONTACT.TYPE_ID' => $typeId,
364 '=POSTING.MAILING_ID' => $chain[
'MAILING_ID'],
372 if($recipientExistsDb->fetch())
377 if(static::$postingId)
383 $postingAddDb = PostingTable::add(array(
384 'MAILING_ID' => $chain[
'MAILING_ID'],
385 'MAILING_CHAIN_ID' => $chain[
'ID'],
387 if(!$postingAddDb->isSuccess())
return;
393 $contact = ContactTable::getRow([
'filter' => [
395 '=TYPE_ID' => $typeId,
401 'TYPE_ID' => $typeId,
402 'NAME' => !empty($data[
'NAME']) ? $data[
'NAME'] :
null,
403 'USER_ID' => !empty($data[
'USER_ID']) ? $data[
'USER_ID'] :
null,
405 $contact[
'ID'] = ContactTable::add($contact)->getId();
408 PostingRecipientTable::add([
410 'CONTACT_ID' => $contact[
'ID'],
411 'FIELDS' => !empty($data[
'FIELDS']) ? $data[
'FIELDS'] :
null,
412 'USER_ID' => !empty($data[
'USER_ID']) ? $data[
'USER_ID'] :
null,
423 static::actualizeHandlerForChild($activate);
425 $itemDb = MailingTriggerTable::getList(array(
426 'select' => array(
'ENDPOINT',
'MAILING_CHAIN_ID'),
428 '=MAILING_CHAIN.IS_TRIGGER' =>
'Y',
429 '=MAILING_CHAIN.MAILING.ACTIVE' =>
'Y',
432 while($item = $itemDb->fetch())
434 if(!is_array($item[
'ENDPOINT']))
448 $settings =
new Settings($item[
'ENDPOINT']);
449 if(!$settings->isClosedTrigger() && $settings->getEventModuleId() && $settings->getEventType())
451 static::actualizeHandler(
452 array(
'MODULE_ID' => $settings->getEventModuleId(),
'EVENT_TYPE' => $settings->getEventType()),
466 $moduleId = $params[
'MODULE_ID'];
467 $eventType = $params[
'EVENT_TYPE'];
468 $calledBeforeChange = $params[
'CALLED_BEFORE_CHANGE'];
470 if($params[
'IS_CLOSED_TRIGGER'])
476 if($activate ===
null)
481 if($calledBeforeChange)
486 $existsDb = MailingTriggerTable::getList(array(
487 'select' => array(
'MAILING_CHAIN_ID'),
489 '=EVENT' => $moduleId.
'/'.$eventType,
490 '=MAILING_CHAIN.IS_TRIGGER' =>
'Y',
491 '=MAILING_CHAIN.MAILING.ACTIVE' =>
'Y',
494 'group' => array(
'MAILING_CHAIN_ID'),
498 while($existsDb->fetch()) $rowsCount++;
500 if($rowsCount > $minRowsCount)
513 $moduleId, $eventType,
'sender', __CLASS__,
'handleEvent'
519 $moduleId, $eventType,
'sender', __CLASS__,
'handleEvent'
545 $endpointList =
null;
546 $fieldsTmp = array();
548 foreach($fields as $moduleId => $connectorSettings)
550 if (is_numeric($moduleId))
555 foreach($connectorSettings as $connectorCode => $connectorFields)
557 foreach($connectorFields as $k => $fields)
559 if (isset($fieldsTmp[$moduleId][$connectorCode][$k]) && is_array($fields))
560 $fieldsTmp[$moduleId][$connectorCode][$k] = array_merge($fieldsTmp[$moduleId][$connectorCode][$k], $fields);
562 $fieldsTmp[$moduleId][$connectorCode][$k] = $fields;
567 foreach($fieldsTmp as $moduleId => $connectorSettings)
569 if(is_numeric($moduleId)) $moduleId =
'';
570 foreach($connectorSettings as $connectorCode => $connectorFields)
572 foreach($connectorFields as $fields)
575 $endpoint[
'MODULE_ID'] = $moduleId;
576 $endpoint[
'CODE'] = $connectorCode;
577 $endpoint[
'FIELDS'] = $fields;
578 $endpointList[] = $endpoint;
583 return $endpointList;
635 $resultList = array();
636 $moduleIdFilter =
null;
637 $moduleConnectorFilter =
null;
641 $moduleIdFilter = array();
642 foreach($endpointList as $endpoint)
644 $moduleIdFilter[] = $endpoint[
'MODULE_ID'];
645 $moduleConnectorFilter[$endpoint[
'MODULE_ID']][] = $endpoint[
'CODE'];
650 $event =
new Event(
'sender',
'OnTriggerList', array($data), $moduleIdFilter);
653 foreach ($event->getResults() as $eventResult)
655 if ($eventResult->getType() == EventResult::ERROR)
660 $eventResultParameters = $eventResult->getParameters();
662 if($eventResultParameters && array_key_exists(
'TRIGGER', $eventResultParameters))
664 $connectorClassNameList = $eventResultParameters[
'TRIGGER'];
665 if(!is_array($connectorClassNameList))
666 $connectorClassNameList = array($connectorClassNameList);
668 foreach($connectorClassNameList as $connectorClassName)
670 if(!is_subclass_of($connectorClassName,
'\Bitrix\Sender\Trigger'))
677 if($moduleConnectorFilter && !in_array($connectorCode, $moduleConnectorFilter[$eventResult->getModuleId()]))
682 $isClosedTrigger =
false;
683 if(is_subclass_of($connectorClassName,
'\Bitrix\Sender\TriggerConnectorClosed'))
684 $isClosedTrigger =
true;
687 $connectorRequireConfigure =
$connector->requireConfigure();
688 $resultList[] = array(
689 'MODULE_ID' => $eventResult->getModuleId(),
690 'CLASS_NAME' => $connectorClassName,
691 'CODE' => $connectorCode,
692 'NAME' => $connectorName,
693 'REQUIRE_CONFIGURE' => $connectorRequireConfigure,
694 'IS_CLOSED' => $isClosedTrigger,
700 if(!empty($resultList))
701 usort($resultList, array(__CLASS__,
'sort'));
750 if(!empty($moduleId) && !empty($eventType) && !empty($chainId))
752 static::processEvent(array(
753 'MODULE_ID' => $moduleId,
754 'EVENT_TYPE' => $eventType,
755 'EVENT_DATA' => array(),
757 '=MAILING_CHAIN.ID' => $chainId
761 return static::getClosedEventAgentName($moduleId, $eventType, $chainId);
776 $eventHandlerList = array(
779 'OnAfterMailingChainSend',
782 'onAfterMailingChainSend'
786 'OnAfterPostingSendRecipient',
789 'onAfterPostingSendRecipient'
793 if($activate ===
null)
795 $existsDb = MailingChainTable::getList(array(
796 'select' => array(
'ID'),
798 '!PARENT_ID' =>
null,
799 '=IS_TRIGGER' =>
'Y',
800 '=MAILING.ACTIVE' =>
'Y',
805 if($existsDb->fetch())
815 if($activate ===
true)
818 foreach($eventHandlerList as $h)
819 $eventManager->registerEventHandler($h[0],$h[1],$h[2],$h[3],$h[4]);
821 elseif($activate ===
false)
824 foreach($eventHandlerList as $h)
825 $eventManager->unRegisterEventHandler($h[0],$h[1],$h[2],$h[3],$h[4]);
898 if(!$data || !$data[
'SEND_RESULT'] || empty($data[
'POSTING'][
'MAILING_CHAIN_ID']))
901 $chainId = $data[
'POSTING'][
'MAILING_CHAIN_ID'];
902 $dataRecipient = $data[
'RECIPIENT'];
904 static $mailingParams = array();
905 if(!isset($mailingParams[$chainId]))
907 $mailingParams[$chainId] = array();
909 $childChainDb = MailingChainTable::getList(array(
911 'ID',
'MAILING_ID',
'PARENT_ID',
'POSTING_ID'
914 '=MAILING.ACTIVE' =>
'Y',
915 '=IS_TRIGGER' =>
'Y',
917 '=PARENT_ID' => $chainId
920 while($childChain = $childChainDb->fetch())
923 $postingAddDb = PostingTable::add(array(
924 'MAILING_ID' => $childChain[
'MAILING_ID'],
925 'MAILING_CHAIN_ID' => $childChain[
'ID'],
927 if(!$postingAddDb->isSuccess())
932 $mailingParams[$chainId][] = array(
933 'POSTING_ID' => $postingAddDb->getId(),
934 'CHAIN' => $childChain,
939 if(empty($mailingParams[$chainId]))
944 foreach($mailingParams[$chainId] as $chainKey => $mailingParamsItem)
946 $postingId = $mailingParamsItem[
'POSTING_ID'];
947 $childChain = $mailingParamsItem[
'CHAIN'];
954 $recipient = array(
'POSTING_ID' =>
$postingId);
957 $recipient[
'CONTACT_ID'] = $dataRecipient[
'CONTACT_ID'];
958 if(!empty($dataRecipient[
'FIELDS']))
960 $recipient[
'FIELDS'] = $dataRecipient[
'FIELDS'];
963 if(!empty($dataRecipient[
'ROOT_ID']))
965 $recipient[
'ROOT_ID'] = $dataRecipient[
'ROOT_ID'];
969 $recipient[
'ROOT_ID'] = $dataRecipient[
'ID'];
972 if(!empty($dataRecipient[
'USER_ID']))
974 $recipient[
'USER_ID'] = $dataRecipient[
'USER_ID'];
979 if(empty($childChain[
'POSTING_ID']))
981 $chainUpdateDb = Model\LetterTable::update($childChain[
'ID'], array(
'POSTING_ID' =>
$postingId));
982 if($chainUpdateDb->isSuccess())
984 $mailingParams[$chainId][$chainKey][
'CHAIN'][
'POSTING_ID'] =
$postingId;