Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
10
16
29
31{
32 public static $debug = false;
33 public static $postingId = null;
34
39 public static function handleEvent()
40 {
41 $args = func_get_args();
42 if (isset($args[0]) && $args[0] instanceof Event)
43 {
44 $event = $args[0];
45 /* @var Event $event */
46 $moduleId = $event->getModuleId();
47 $eventType = $event->getEventType();
48 $eventData = $event->getParameters();
49 }
50 else
51 {
52 global $BX_MODULE_EVENT_LAST;
53 $moduleId = $BX_MODULE_EVENT_LAST['FROM_MODULE_ID'];
54 $eventType = $BX_MODULE_EVENT_LAST['MESSAGE_ID'];
55 $eventData = $args;
56 }
57
58 static::processEvent(array(
59 'MODULE_ID' => $moduleId,
60 'EVENT_TYPE' => $eventType,
61 'EVENT_DATA' => $eventData,
62 'FILTER' => array(),
63 ));
64 }
65
70 protected static function processEvent($params)
71 {
72 $moduleId = $params['MODULE_ID'];
73 $eventType = $params['EVENT_TYPE'];
74 $eventData = $params['EVENT_DATA'];
75
76 $filter = array(
77 '=MAILING_CHAIN.MAILING.ACTIVE' => 'Y',
78 '=MAILING_CHAIN.IS_TRIGGER' => 'Y',
79 '=MAILING_CHAIN.STATUS' => array(MailingChainTable::STATUS_WAIT, MailingChainTable::STATUS_SEND),
80 '=EVENT' => $moduleId.'/'.$eventType
81 );
82 if(isset($params['FILTER']) && is_array($params['FILTER']))
83 {
84 $filter = $filter + $params['FILTER'];
85 }
86
87 $chainDb = MailingTriggerTable::getList(array(
88 'select' => array(
89 'ENDPOINT',
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'
98 ),
99 'filter' => $filter,
100 'order' => array('MAILING_CHAIN_ID' => 'ASC', 'IS_TYPE_START' => 'ASC')
101 ));
102 while($chain = $chainDb->fetch())
103 {
104 $settings = new Settings($chain['ENDPOINT']);
105 $trigger = static::getOnce($settings->getEndpoint());
106 if(!$trigger) continue;
107
108 $trigger->setSiteId($chain['SITE_ID']);
109 $trigger->setFields($settings->getFields());
110 $trigger->setParams(array('CHAIN' => $chain, 'EVENT' => $eventData));
111
112 // mark trigger as first run for process old data
113 $runForOldData = ($trigger->canRunForOldData() && $settings->canRunForOldData() && !$settings->wasRunForOldData());
114 $trigger->setRunForOldData($runForOldData);
115
116 // run trigger filter
117 if(!$trigger->filter()) continue;
118
119 //add recipient to posting
120 static::$postingId = null;
121 $recipientDb = $trigger->getRecipientResult();
122 while($recipient = $recipientDb->fetch())
123 {
124 if($settings->isTypeStart())
125 {
126 static::addRecipient($chain, $settings, $recipient);
127 }
128 else
129 {
130 static::stop($chain, $recipient, true);
131 }
132 }
133
134 // mark mailing trigger fields as first run for process old data
135 if($runForOldData)
136 {
137 MailingTable::setWasRunForOldData($chain['MAILING_ID'], $runForOldData);
138 }
139
140 if($settings->isTypeStart())
141 {
142 // prevent email event
143 if($settings->isPreventEmail())
144 {
145 static::preventMailEvent($trigger->getMailEventToPrevent());
146 }
147
148 //start sending of mailing chain
149 static::send($chain);
150 }
151
152 }
153
154 //return $data;
155 }
156
163 protected static function stop($chain, $data, $setGoal)
164 {
165 if(!$data || empty($data['EMAIL']))
166 {
167 return;
168 }
169
170 $code = $data['EMAIL'];
171 $typeId = Recipient\Type::detect($data['EMAIL']);
172 $code = Recipient\Normalizer::normalize($code, $typeId);
173
174 // if mailing continue, then stop it
175 $recipientDb = PostingRecipientTable::getList(array(
176 'select' => array('ID', 'ROOT_ID', 'POSTING_ID', 'STATUS', 'POSTING_STATUS' => 'POSTING.STATUS'),
177 'filter' => array(
178 '=CONTACT.CODE' => $code,
179 '=CONTACT.TYPE_ID' => $typeId,
180 '=POSTING.MAILING_ID' => $chain['MAILING_ID'],
181 '=STATUS' => array(
184 )
185 ),
186 'limit' => 1
187 ));
188 if($recipient = $recipientDb->fetch())
189 {
190 // if mailing continue, then stop it and the next was riched
191 $updateFields = array('STATUS' => PostingRecipientTable::SEND_RESULT_DENY);
192 Model\Posting\RecipientTable::update($recipient['ID'], $updateFields);
193
194 // change status of posting if all emails sent
195 if(!in_array($recipient['POSTING_STATUS'], array(PostingTable::STATUS_NEW, PostingTable::STATUS_PART)))
196 {
197 $recipientCountDb = PostingRecipientTable::getList(array(
198 'select' => array('POSTING_ID'),
199 'filter' => array(
200 '=POSTING_ID' => $recipient['POSTING_ID'],
201 '=STATUS' => array(
204 )
205 ),
206 'limit' => 1
207 ));
208 if(!$recipientCountDb->fetch())
209 {
210 Model\PostingTable::update($recipient['POSTING_ID'], ['STATUS' => PostingTable::STATUS_SENT]);
211 }
212 }
213 }
214
215 if(!$setGoal)
216 {
217 return;
218 }
219
220 // set flag of taking the goal to last success sending
221 $recipientDb = PostingRecipientTable::getList(array(
222 'select' => array('ID', 'DATE_DENY'),
223 'filter' => array(
224 '=CONTACT.CODE' => $code,
225 '=CONTACT.TYPE_ID' => $typeId,
226 '=POSTING.MAILING_ID' => $chain['MAILING_ID'],
227 '=STATUS' => array(
229 )
230 ),
231 'order' => array('DATE_SENT' => 'DESC', 'ID' => 'DESC'),
232 'limit' => 1
233 ));
234 if($recipient = $recipientDb->fetch())
235 {
236 if(empty($recipient['DATE_DENY']))
237 {
238 Model\Posting\RecipientTable::update($recipient['ID'], ['DATE_DENY' => new DateTime]);
239 }
240 }
241 }
242
247 protected static function send($chain)
248 {
249 // set send status
250 if(empty($chain['ID']))
251 return;
252
253 if(empty($chain['POSTING_ID']))
254 {
255 if(empty(static::$postingId))
256 {
257 return;
258 }
259
260 $updateFields['POSTING_ID'] = static::$postingId;
261 }
262
263 $updateFields = array();
264 if($chain['STATUS'] == MailingChainTable::STATUS_WAIT)
265 {
266 $autoSendTime = new DateTime;
267 $autoSendTime->add($chain['TIME_SHIFT'] . ' minutes');
268 $updateFields['STATUS'] = MailingChainTable::STATUS_SEND;
269 $updateFields['AUTO_SEND_TIME'] = $autoSendTime;
270 }
271 else
272 {
273 $updateFields['AUTO_SEND_TIME'] = $chain['AUTO_SEND_TIME'];
274 $updateFields['STATUS'] = $chain['STATUS'];
275 }
276
277 if(count($updateFields) > 0)
278 {
279 Model\LetterTable::update($chain['ID'], $updateFields);
280 }
281 }
282
287 protected static function preventMailEvent(array $emailEvent)
288 {
289 if(isset($emailEvent['EVENT_NAME']) && $emailEvent['EVENT_NAME'] <> '')
290 {
291 if(!empty($emailEvent['FILTER']) && is_array($emailEvent['FILTER']))
292 {
293 MailEventHandler::prevent($emailEvent['EVENT_NAME'], $emailEvent['FILTER']);
294 }
295 }
296 }
297
304 protected static function addRecipient($chain, $settings, $data)
305 {
306 if(!$data || empty($data['EMAIL']))
307 {
308 return;
309 }
310
311 $code = $data['EMAIL'];
312 $typeId = Recipient\Type::detect($data['EMAIL']);
313 $code = Recipient\Normalizer::normalize($code, $typeId);
314
315 // check email to unsubscription
316 if(Subscription::isUnsubscibed($chain['MAILING_ID'], $code))
317 {
318 return;
319 }
320
321 // if this is event for child
322 if(!empty($chain['PARENT_ID']))
323 {
324 $recipientDb = PostingRecipientTable::getList(array(
325 'select' => array('ID', 'STATUS'),
326 'filter' => array(
327 '=CONTACT.CODE' => $code,
328 '=CONTACT.TYPE_ID' => $typeId,
329 '=POSTING.MAILING_CHAIN_ID' => $chain['ID'],
330 '=POSTING.STATUS' => array(PostingTable::STATUS_NEW, PostingTable::STATUS_PART)
331 )
332 ));
333
334 while($recipient = $recipientDb->fetch())
335 {
336 // check if event should came or didn't came
337 $statusNew = null;
338 if($settings->isEventOccur() && $recipient['STATUS'] == PostingRecipientTable::SEND_RESULT_WAIT)
339 {
341 }
342 elseif(!$settings->isEventOccur() && $recipient['STATUS'] == PostingRecipientTable::SEND_RESULT_NONE)
343 {
345 }
346
347 if($statusNew !== null)
348 {
349 Model\Posting\RecipientTable::update(
350 $recipient['ID'],
351 ['STATUS' => $statusNew]
352 )->isSuccess();
353 }
354 }
355 }
356 else
357 {
358 // check email to have not finished mailing
359 $recipientExistsDb = PostingRecipientTable::getList(array(
360 'select' => array('ID'),
361 'filter' => array(
362 '=CONTACT.CODE' => $code,
363 '=CONTACT.TYPE_ID' => $typeId,
364 '=POSTING.MAILING_ID' => $chain['MAILING_ID'],
365 '=STATUS' => array(
368 )
369 ),
370 'limit' => 1
371 ));
372 if($recipientExistsDb->fetch())
373 {
374 return;
375 }
376
377 if(static::$postingId)
378 {
379 $postingId = static::$postingId;
380 }
381 else
382 {
383 $postingAddDb = PostingTable::add(array(
384 'MAILING_ID' => $chain['MAILING_ID'],
385 'MAILING_CHAIN_ID' => $chain['ID'],
386 ));
387 if(!$postingAddDb->isSuccess()) return;
388
389 $postingId = $postingAddDb->getId();
390 static::$postingId = $postingId;
391 }
392
393 $contact = ContactTable::getRow(['filter' => [
394 '=CODE' => $code,
395 '=TYPE_ID' => $typeId,
396 ]]);
397 if (!$contact)
398 {
399 $contact = [
400 'CODE' => $code,
401 'TYPE_ID' => $typeId,
402 'NAME' => !empty($data['NAME']) ? $data['NAME'] : null,
403 'USER_ID' => !empty($data['USER_ID']) ? $data['USER_ID'] : null,
404 ];
405 $contact['ID'] = ContactTable::add($contact)->getId();
406 }
407
408 PostingRecipientTable::add([
409 'POSTING_ID' => $postingId,
410 'CONTACT_ID' => $contact['ID'],
411 'FIELDS' => !empty($data['FIELDS']) ? $data['FIELDS'] : null,
412 'USER_ID' => !empty($data['USER_ID']) ? $data['USER_ID'] : null,
413 ])->isSuccess();
414 }
415 }
416
421 public static function activateAllHandlers($activate = true)
422 {
423 static::actualizeHandlerForChild($activate);
424
425 $itemDb = MailingTriggerTable::getList(array(
426 'select' => array('ENDPOINT', 'MAILING_CHAIN_ID'),
427 'filter' => array(
428 '=MAILING_CHAIN.IS_TRIGGER' => 'Y',
429 '=MAILING_CHAIN.MAILING.ACTIVE' => 'Y',
430 )
431 ));
432 while($item = $itemDb->fetch())
433 {
434 if(!is_array($item['ENDPOINT']))
435 {
436 continue;
437 }
438
439 if($activate)
440 {
441 MailingTriggerTable::actualizeHandlers($item['MAILING_CHAIN_ID'], $item['ENDPOINT'], null);
442 }
443 else
444 {
445 MailingTriggerTable::actualizeHandlers($item['MAILING_CHAIN_ID'], null, $item['ENDPOINT']);
446 }
447
448 $settings = new Settings($item['ENDPOINT']);
449 if(!$settings->isClosedTrigger() && $settings->getEventModuleId() && $settings->getEventType())
450 {
451 static::actualizeHandler(
452 array('MODULE_ID' => $settings->getEventModuleId(), 'EVENT_TYPE' => $settings->getEventType()),
453 $activate
454 );
455 }
456 }
457 }
458
464 public static function actualizeHandler(array $params, $activate = null)
465 {
466 $moduleId = $params['MODULE_ID'];
467 $eventType = $params['EVENT_TYPE'];
468 $calledBeforeChange = $params['CALLED_BEFORE_CHANGE'];
469
470 if($params['IS_CLOSED_TRIGGER'])
471 {
472
473 return;
474 }
475
476 if($activate === null)
477 {
478 // if actualizing will be called before deleting record (or updating record with clearing field),
479 // query will select this record.
480 // In this reason, it should be considered - check if more 1 or 0 selected rows.
481 if($calledBeforeChange)
482 $minRowsCount = 1;
483 else
484 $minRowsCount = 0;
485
486 $existsDb = MailingTriggerTable::getList(array(
487 'select' => array('MAILING_CHAIN_ID'),
488 'filter' => array(
489 '=EVENT' => $moduleId.'/'.$eventType,
490 '=MAILING_CHAIN.IS_TRIGGER' => 'Y',
491 '=MAILING_CHAIN.MAILING.ACTIVE' => 'Y',
492 //'=STATUS' => array(MailingChainTable::STATUS_WAIT, MailingChainTable::STATUS_SEND)
493 ),
494 'group' => array('MAILING_CHAIN_ID'),
495 'limit' => 2
496 ));
497 $rowsCount = 0;
498 while($existsDb->fetch()) $rowsCount++;
499
500 if($rowsCount > $minRowsCount)
501 {
502 $activate = true;
503 }
504 else
505 {
506 $activate = false;
507 }
508 }
509
510 if($activate)
511 {
512 EventManager::getInstance()->registerEventHandler(
513 $moduleId, $eventType, 'sender', __CLASS__, 'handleEvent'
514 );
515 }
516 else
517 {
518 EventManager::getInstance()->unRegisterEventHandler(
519 $moduleId, $eventType, 'sender', __CLASS__, 'handleEvent'
520 );
521 }
522 }
523
528 public static function getFieldsFromEndpoint(array $endpointList)
529 {
530 $resultList = array();
531 foreach($endpointList as $endpoint)
532 {
533 $resultList[$endpoint['MODULE_ID']][$endpoint['CODE']][] = $endpoint['FIELDS'];
534 }
535
536 return $resultList;
537 }
538
543 public static function getEndpointFromFields(array $fields)
544 {
545 $endpointList = null;
546 $fieldsTmp = array();
547
548 foreach($fields as $moduleId => $connectorSettings)
549 {
550 if (is_numeric($moduleId))
551 {
552 $moduleId = '';
553 }
554
555 foreach($connectorSettings as $connectorCode => $connectorFields)
556 {
557 foreach($connectorFields as $k => $fields)
558 {
559 if (isset($fieldsTmp[$moduleId][$connectorCode][$k]) && is_array($fields))
560 $fieldsTmp[$moduleId][$connectorCode][$k] = array_merge($fieldsTmp[$moduleId][$connectorCode][$k], $fields);
561 else
562 $fieldsTmp[$moduleId][$connectorCode][$k] = $fields;
563 }
564 }
565 }
566
567 foreach($fieldsTmp as $moduleId => $connectorSettings)
568 {
569 if(is_numeric($moduleId)) $moduleId = '';
570 foreach($connectorSettings as $connectorCode => $connectorFields)
571 {
572 foreach($connectorFields as $fields)
573 {
574 $endpoint = array();
575 $endpoint['MODULE_ID'] = $moduleId;
576 $endpoint['CODE'] = $connectorCode;
577 $endpoint['FIELDS'] = $fields;
578 $endpointList[] = $endpoint;
579 }
580 }
581 }
582
583 return $endpointList;
584 }
585
592 public static function getList(array $endpointList = null)
593 {
594 $triggerList = array();
595
596 $classList = static::getClassList($endpointList);
597 foreach($classList as $classDescription)
598 {
600 $trigger = new $classDescription['CLASS_NAME'];
601 $trigger->setModuleId($classDescription['MODULE_ID']);
602 $triggerList[] = $trigger;
603 }
604
605 return $triggerList;
606 }
607
614 public static function getOnce(array $endpoint)
615 {
616 $trigger = null;
617 $triggerList = static::getList(array($endpoint));
619 foreach($triggerList as $trigger)
620 {
621 break;
622 }
623
624 return $trigger;
625 }
626
633 public static function getClassList(array $endpointList = null)
634 {
635 $resultList = array();
636 $moduleIdFilter = null;
637 $moduleConnectorFilter = null;
638
639 if($endpointList)
640 {
641 $moduleIdFilter = array();
642 foreach($endpointList as $endpoint)
643 {
644 $moduleIdFilter[] = $endpoint['MODULE_ID'];
645 $moduleConnectorFilter[$endpoint['MODULE_ID']][] = $endpoint['CODE'];
646 }
647 }
648
649 $data = array();
650 $event = new Event('sender', 'OnTriggerList', array($data), $moduleIdFilter);
651 $event->send();
652
653 foreach ($event->getResults() as $eventResult)
654 {
655 if ($eventResult->getType() == EventResult::ERROR)
656 {
657 continue;
658 }
659
660 $eventResultParameters = $eventResult->getParameters();
661
662 if($eventResultParameters && array_key_exists('TRIGGER', $eventResultParameters))
663 {
664 $connectorClassNameList = $eventResultParameters['TRIGGER'];
665 if(!is_array($connectorClassNameList))
666 $connectorClassNameList = array($connectorClassNameList);
667
668 foreach($connectorClassNameList as $connectorClassName)
669 {
670 if(!is_subclass_of($connectorClassName, '\Bitrix\Sender\Trigger'))
671 {
672 continue;
673 }
674
675 $connector = new $connectorClassName;
676 $connectorCode = $connector->getCode();
677 if($moduleConnectorFilter && !in_array($connectorCode, $moduleConnectorFilter[$eventResult->getModuleId()]))
678 {
679 continue;
680 }
681
682 $isClosedTrigger = false;
683 if(is_subclass_of($connectorClassName, '\Bitrix\Sender\TriggerConnectorClosed'))
684 $isClosedTrigger = true;
685
686 $connectorName = $connector->getName();
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,
695 );
696 }
697 }
698 }
699
700 if(!empty($resultList))
701 usort($resultList, array(__CLASS__, 'sort'));
702
703 return $resultList;
704 }
705
711 public static function sort($a, $b)
712 {
713 if ($a['NAME'] == $b['NAME'])
714 return 0;
715
716 return ($a['NAME'] < $b['NAME']) ? -1 : 1;
717 }
718
723 public static function debug($mess)
724 {
725 if(static::$debug)
726 {
727 Debug::writeToFile($mess, "", "__bx_sender_trigger.log");
728 }
729 }
730
737 public static function getClosedEventAgentName($moduleId, $eventType, $chainId)
738 {
739 return '\Bitrix\Sender\TriggerManager::fireClosedEventAgent("' . $moduleId . '","' . $eventType .'","' . $chainId .'");';
740 }
741
748 public static function fireClosedEventAgent($moduleId, $eventType, $chainId)
749 {
750 if(!empty($moduleId) && !empty($eventType) && !empty($chainId))
751 {
752 static::processEvent(array(
753 'MODULE_ID' => $moduleId,
754 'EVENT_TYPE' => $eventType,
755 'EVENT_DATA' => array(),
756 'FILTER' => array(
757 '=MAILING_CHAIN.ID' => $chainId
758 ),
759 ));
760
761 return static::getClosedEventAgentName($moduleId, $eventType, $chainId);
762 }
763 else
764 {
765 return '';
766 }
767 }
768
769
774 public static function actualizeHandlerForChild($activate = null)
775 {
776 $eventHandlerList = array(
777 array(
778 'sender',
779 'OnAfterMailingChainSend',
780 'sender',
781 __CLASS__,
782 'onAfterMailingChainSend'
783 ),
784 array(
785 'sender',
786 'OnAfterPostingSendRecipient',
787 'sender',
788 __CLASS__,
789 'onAfterPostingSendRecipient'
790 )
791 );
792
793 if($activate === null)
794 {
795 $existsDb = MailingChainTable::getList(array(
796 'select' => array('ID'),
797 'filter' => array(
798 '!PARENT_ID' => null,
799 '=IS_TRIGGER' => 'Y',
800 '=MAILING.ACTIVE' => 'Y',
801 //'=STATUS' => array(MailingChainTable::STATUS_WAIT, MailingChainTable::STATUS_SEND)
802 ),
803 'limit' => 1
804 ));
805 if($existsDb->fetch())
806 {
807 $activate = true;
808 }
809 else
810 {
811 $activate = false;
812 }
813 }
814
815 if($activate === true)
816 {
817 $eventManager = EventManager::getInstance();
818 foreach($eventHandlerList as $h)
819 $eventManager->registerEventHandler($h[0],$h[1],$h[2],$h[3],$h[4]);
820 }
821 elseif($activate === false)
822 {
823 $eventManager = EventManager::getInstance();
824 foreach($eventHandlerList as $h)
825 $eventManager->unRegisterEventHandler($h[0],$h[1],$h[2],$h[3],$h[4]);
826 }
827 }
828
833 public static function onAfterMailingChainSend(Event $event)
834 {
835 $data = $event->getParameter(0);
836
837 if(!$data || empty($data['MAILING_CHAIN']['ID']))
838 return;
839
840 $childChainDb = MailingChainTable::getList(array(
841 'select' => array(
842 'ID',
843 'MAILING_ID',
844 'PARENT_ID',
845 'POSTING_ID',
846 'STATUS',
847 'TIME_SHIFT'
848 ),
849 'filter' => array(
850 '=MAILING.ACTIVE' => 'Y',
851 '=IS_TRIGGER' => 'Y',
853 '=PARENT_ID' => $data['MAILING_CHAIN']['ID']
854 )
855 ));
856 while($childChain = $childChainDb->fetch())
857 {
858 $isSend = false;
859
860 $settings = new Settings();
861 if($settings->getEndpoint('CODE') == '')
862 {
863 // send certainly
864 $isSend = true;
865 }
866 elseif($settings->isEventOccur())
867 {
868 // send if event occur
869 }
870 else
871 {
872 // send if event not occur
873 }
874
875 if(empty($childChain['POSTING_ID']) || $childChain['STATUS'] != MailingChainTable::STATUS_WAIT)
876 {
877 $isSend = false;
878 }
879
880
881 if($isSend)
882 {
883 static::send($childChain);
884 }
885
886 }
887 }
888
889
894 public static function onAfterPostingSendRecipient(Event $event)
895 {
896 $data = $event->getParameter(0);
897
898 if(!$data || !$data['SEND_RESULT'] || empty($data['POSTING']['MAILING_CHAIN_ID']))
899 return;
900
901 $chainId = $data['POSTING']['MAILING_CHAIN_ID'];
902 $dataRecipient = $data['RECIPIENT'];
903
904 static $mailingParams = array();
905 if(!isset($mailingParams[$chainId]))
906 {
907 $mailingParams[$chainId] = array();
908
909 $childChainDb = MailingChainTable::getList(array(
910 'select' => array(
911 'ID', 'MAILING_ID', 'PARENT_ID', 'POSTING_ID'
912 ),
913 'filter' => array(
914 '=MAILING.ACTIVE' => 'Y',
915 '=IS_TRIGGER' => 'Y',
917 '=PARENT_ID' => $chainId
918 )
919 ));
920 while($childChain = $childChainDb->fetch())
921 {
922 // add posting
923 $postingAddDb = PostingTable::add(array(
924 'MAILING_ID' => $childChain['MAILING_ID'],
925 'MAILING_CHAIN_ID' => $childChain['ID'],
926 ));
927 if(!$postingAddDb->isSuccess())
928 {
929 continue;
930 }
931
932 $mailingParams[$chainId][] = array(
933 'POSTING_ID' => $postingAddDb->getId(),
934 'CHAIN' => $childChain,
935 );
936 }
937 }
938
939 if(empty($mailingParams[$chainId]))
940 {
941 return;
942 }
943
944 foreach($mailingParams[$chainId] as $chainKey => $mailingParamsItem)
945 {
946 $postingId = $mailingParamsItem['POSTING_ID'];
947 $childChain = $mailingParamsItem['CHAIN'];
948
949 // check email as unsubscribed
950 // TODO: modify to accept RID
951 if(Subscription::isUnsubscibed($childChain['MAILING_ID'], $data['RECIPIENT']['EMAIL']))
952 continue;
953
954 $recipient = array('POSTING_ID' => $postingId);
955 $recipient['STATUS'] = PostingRecipientTable::SEND_RESULT_NONE;
956
957 $recipient['CONTACT_ID'] = $dataRecipient['CONTACT_ID'];
958 if(!empty($dataRecipient['FIELDS']))
959 {
960 $recipient['FIELDS'] = $dataRecipient['FIELDS'];
961 }
962
963 if(!empty($dataRecipient['ROOT_ID']))
964 {
965 $recipient['ROOT_ID'] = $dataRecipient['ROOT_ID'];
966 }
967 else
968 {
969 $recipient['ROOT_ID'] = $dataRecipient['ID'];
970 }
971
972 if(!empty($dataRecipient['USER_ID']))
973 {
974 $recipient['USER_ID'] = $dataRecipient['USER_ID'];
975 }
976
977 // add recipient
978 PostingTable::addRecipient($recipient, true);
979 if(empty($childChain['POSTING_ID']))
980 {
981 $chainUpdateDb = Model\LetterTable::update($childChain['ID'], array('POSTING_ID' => $postingId));
982 if($chainUpdateDb->isSuccess())
983 {
984 $mailingParams[$chainId][$chainKey]['CHAIN']['POSTING_ID'] = $postingId;
985 }
986 }
987 }
988
989 }
990
995 public static function onAfterRecipientUnsub($data)
996 {
997 static::stop(
998 array('MAILING_ID' => $data['MAILING_ID']),
999 array(
1000 'RECIPIENT_ID' => $data['RECIPIENT_ID'],
1001 'CONTACT_ID' => $data['CONTACT_ID'],
1002 ),
1003 false
1004 );
1005 }
1006
1011 public static function onTriggerList($data)
1012 {
1013 return Integration\EventHandler::onTriggerList($data);
1014 }
1015}
static writeToFile($var, $varName="", $fileName="")
Definition debug.php:88
getParameter($key)
Definition event.php:80
add($interval)
Definition date.php:145
static prevent($eventName, array $filter)
static setWasRunForOldData($id, $state)
Definition mailing.php:538
static actualizeHandlers($chainId, array $fieldsNew=null, array $fieldsOld=null)
static addRecipient($ar, $checkDuplicate=false)
Definition posting.php:201
static isUnsubscibed($mailingId, $code, $typeId=Recipient\Type::EMAIL)
static fireClosedEventAgent($moduleId, $eventType, $chainId)
Definition manager.php:748
static onAfterPostingSendRecipient(Event $event)
Definition manager.php:894
static addRecipient($chain, $settings, $data)
Definition manager.php:304
static activateAllHandlers($activate=true)
Definition manager.php:421
static stop($chain, $data, $setGoal)
Definition manager.php:163
static actualizeHandlerForChild($activate=null)
Definition manager.php:774
static actualizeHandler(array $params, $activate=null)
Definition manager.php:464
static processEvent($params)
Definition manager.php:70
static getEndpointFromFields(array $fields)
Definition manager.php:543
static getFieldsFromEndpoint(array $endpointList)
Definition manager.php:528
static onAfterMailingChainSend(Event $event)
Definition manager.php:833
static onAfterRecipientUnsub($data)
Definition manager.php:995
static getClassList(array $endpointList=null)
Definition manager.php:633
static preventMailEvent(array $emailEvent)
Definition manager.php:287
static getClosedEventAgentName($moduleId, $eventType, $chainId)
Definition manager.php:737