public static function actualizeHandlers($chainId, array $fieldsNew = null, array $fieldsOld = null) { $settingsNew = null; $settingsOld = null; if ($fieldsNew) { $settingsNew = new TriggerSettings($fieldsNew); } if ($fieldsOld) { $settingsOld = new TriggerSettings($fieldsOld); } // if old item was closed trigger if ($settingsOld && $settingsOld->isClosedTrigger()) { // delete agent $agentName = TriggerManager::getClosedEventAgentName($settingsOld->getEventModuleId(), $settingsOld->getEventType(), $chainId); $agent = new \CAgent(); $agentListDb = $agent->GetList(array(), array('MODULE_ID' => 'sender', 'NAME' => $agentName)); while ($agentItem = $agentListDb->Fetch()) { $agent->Delete($agentItem['ID']); } } // if new item is closed trigger if ($settingsNew && $settingsNew->isClosedTrigger()) { // check active state of mailing $chainDb = MailingChainTable::getList(array('select' => array('ID'), 'filter' => array('=ID' => $chainId, '=MAILING.ACTIVE' => 'Y'))); if (!$chainDb->fetch()) { return; } // add new agent $agentName = TriggerManager::getClosedEventAgentName($settingsNew->getEventModuleId(), $settingsNew->getEventType(), $chainId); // set date of next exec $agentTime = $settingsNew->getClosedTriggerTime(); $agentInterval = $settingsNew->getClosedTriggerInterval(); if ($agentInterval <= 0) { $agentInterval = 1440; } $agentTimeArray = explode(":", $agentTime); $agentDate = new \Bitrix\Main\Type\DateTime(); $agentDate->setTime((int) $agentTimeArray[0], (int) $agentTimeArray[1]); // set next exec on next day if exec was today if ($agentDate->getTimestamp() < time()) { $agentDate->add("1 days"); } // add agent $agent = new \CAgent(); $agent->AddAgent($agentName, 'sender', 'N', $agentInterval * 60, '', 'Y', $agentDate->toString()); return; } // actualize deleted/changed event if ($settingsOld && !$settingsOld->isClosedTrigger() && $settingsOld->getFullEventType()) { // if delete operation(no the NEW) // or change operation(the NEW is not equal to the OLD) if (!$settingsNew || $settingsOld->getFullEventType() != $settingsNew->getFullEventType()) { TriggerManager::actualizeHandler(array('MODULE_ID' => $settingsOld->getEventModuleId(), 'EVENT_TYPE' => $settingsOld->getEventType(), 'CALLED_BEFORE_CHANGE' => true)); } } // actualize new event if ($settingsNew && $settingsNew->getFullEventType()) { $calledBeforeChange = $fieldsOld ? false : true; TriggerManager::actualizeHandler(array('MODULE_ID' => $settingsNew->getEventModuleId(), 'EVENT_TYPE' => $settingsNew->getEventType(), 'CALLED_BEFORE_CHANGE' => $calledBeforeChange)); } }
if ($trigger) { $triggerListExists[$type] = $mailing['TRIGGER_FIELDS'][$type] + $triggerListForJS[$type][$trigger->getId()]; $trigger->setFieldFormName('post_form'); $trigger->setFieldPrefix('ENDPOINT[' . $type . '][FIELDS]'); $trigger->setFields($triggerListExists[$type]['FIELDS']); $triggerListExists[$type]['FORM'] = $trigger->getForm(); } } } if ($REQUEST_METHOD == "POST" && !$isPostedFormProcessed && check_bitrix_sessid()) { $arError = array(); $triggerListExists = array('START' => null, 'END' => null); foreach ($triggerListExists as $type => $value) { $trigger = \Bitrix\Sender\TriggerManager::getOnce($ENDPOINT[$type]); if ($trigger) { $triggerListExists[$type] = $ENDPOINT[$type] + \Bitrix\Sender\TriggerSettings::getArrayFromTrigger($trigger); } } $updateDb = \Bitrix\Sender\MailingTable::update($MAILING_ID, array('TRIGGER_FIELDS' => $triggerListExists)); if (!$updateDb->isSuccess()) { $arError = array_merge($arError, $updateDb->getErrorMessages()); } if (empty($arError)) { $isPostedFormProcessed = true; LocalRedirect('/bitrix/admin/sender_mailing_trig_edit.php?ID=' . $MAILING_ID . "&lang=" . LANGUAGE_ID); } } } if (!empty($arError)) { $message = new CAdminMessage(implode("<br>", $arError)); }
/** * @param \Bitrix\Main\Event $event * @return void */ public static function onAfterMailingChainSend(\Bitrix\Main\Event $event) { $data = $event->getParameter(0); if (!$data || empty($data['MAILING_CHAIN']['ID'])) { return; } $childChainDb = MailingChainTable::getList(array('select' => array('ID', 'MAILING_ID', 'PARENT_ID', 'POSTING_ID', 'STATUS', 'TIME_SHIFT'), 'filter' => array('=MAILING.ACTIVE' => 'Y', '=IS_TRIGGER' => 'Y', '=STATUS' => MailingChainTable::STATUS_WAIT, '=PARENT_ID' => $data['MAILING_CHAIN']['ID']))); while ($childChain = $childChainDb->fetch()) { $isSend = false; $settings = new TriggerSettings(); if (strlen($settings->getEndpoint('CODE')) <= 0) { // send certainly $isSend = true; } elseif ($settings->isEventOccur()) { // send if event occur } else { // send if event not occur } if (empty($childChain['POSTING_ID']) || $childChain['STATUS'] != MailingChainTable::STATUS_WAIT) { $isSend = false; } if ($isSend) { static::send($childChain); } } }