$status = MailingChainTable::STATUS_SEND; } MailingChainTable::update($mailingChainPrimary, array('STATUS' => $status)); $message = array("MESSAGE" => GetMessage("MAILING_ADM_SENDING_PLANING"), "DETAILS" => $arMailingChain['SUBJECT'] . '#PROGRESS_BAR#', "HTML" => true, "TYPE" => "PROGRESS", "PROGRESS_TOTAL" => 1, "PROGRESS_VALUE" => 1); if (\Bitrix\Sender\MailingChainTable::isReadyToSend($ID)) { $message["BUTTONS"] = array(array("ID" => "btn_cont", "VALUE" => GetMessage("MAILING_ADM_BTN_NEXT"), "ONCLICK" => "window.location='sender_mailing_chain_admin.php?MAILNG_ID=" . $MAILING_ID . "&ID=" . $ID . "&action=send&lang=" . LANGUAGE_ID . "'")); } $actionJsList = true; } } else { switch ($arMailingChain["STATUS"]) { case MailingChainTable::STATUS_NEW: $messageDetails = '<br><p>' . GetMessage("MAILING_ADM_SENDING_NOTE_LINE1") . '<br>' . GetMessage("MAILING_ADM_SENDING_NOTE_LINE2") . '</p>'; $message = array("MESSAGE" => GetMessage("MAILING_ADM_SENDING_SEND"), "BUTTONS" => array(array("ID" => "btn_stop", "VALUE" => GetMessage("MAILING_ADM_BTN_STOP"), "ONCLICK" => "Stop()"), array("ID" => "btn_cont", "VALUE" => GetMessage("MAILING_ADM_BTN_CONT"), "ONCLICK" => "Cont()"))); MailingChainTable::initPosting($mailingChainId); MailingChainTable::update($mailingChainPrimary, array('STATUS' => MailingChainTable::STATUS_SEND)); $actionJsList = true; $actionJsMoveProgress = true; break; case MailingChainTable::STATUS_SEND: \Bitrix\Main\Application::getInstance()->getConnection()->startTransaction(); \Bitrix\Sender\MailingManager::chainSend($mailingChainId); \Bitrix\Main\Application::getInstance()->getConnection()->commitTransaction(); $sendErrors = \Bitrix\Sender\MailingManager::getErrors(); if (empty($sendErrors)) { $messageDetails = '<br><p>' . GetMessage("MAILING_ADM_SENDING_NOTE_LINE1") . '<br>' . GetMessage("MAILING_ADM_SENDING_NOTE_LINE2") . '</p>'; $message = array("MESSAGE" => GetMessage("MAILING_ADM_SENDING_SEND"), "BUTTONS" => array(array("ID" => "btn_stop", "VALUE" => GetMessage("MAILING_ADM_BTN_STOP"), "ONCLICK" => "Stop()"), array("ID" => "btn_cont", "VALUE" => GetMessage("MAILING_ADM_BTN_CONT"), "ONCLICK" => "Cont()"))); $actionJsMoveProgress = true; } else { $message = $sendErrors->getMessage(); }
$arFields["DAYS_OF_MONTH"] = $DAYS_OF_MONTH; $arFields["TIMES_OF_DAY"] = $TIMES_OF_DAY; if ($ID <= 0) { $arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_WAIT; } else { $arMailingChainOld = \Bitrix\Sender\MailingChainTable::getRowById(array('ID' => $ID)); if ($arMailingChainOld['STATUS'] == \Bitrix\Sender\MailingChainTable::STATUS_NEW) { $arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_SEND; } } break; default: $arError[] = GetMessage("sender_chain_edit_error_send_type"); } if (empty($arError)) { $mailingUpdateDb = \Bitrix\Sender\MailingChainTable::update(array('ID' => $ID), $arFields); if ($mailingUpdateDb->isSuccess()) { //\Bitrix\Sender\MailingChainTable::initPosting($ID); } else { $arError = $mailingUpdateDb->getErrorMessages(); } } if (empty($arError)) { LocalRedirect('sender_mailing_chain_edit.php?MAILING_ID=' . $MAILING_ID . '&ID=' . $ID . '&lang=' . LANGUAGE_ID); } else { $DB->InitTableVarsForEdit("b_sender_mailing_chain", "", "str_"); if (!isset($SEND_TYPE)) { if ($str_REITERATE == 'Y') { $SEND_TYPE = 'REITERATE'; } elseif (!empty($str_AUTO_SEND_TIME)) { $SEND_TYPE = 'TIME';
/** * @return string * @throws \Bitrix\Main\ArgumentException */ public static function checkPeriod($isAgentExec = true) { $isAgentExecInSetting = \COption::GetOptionString("sender", "reiterate_method") !== 'cron'; if ($isAgentExec && !$isAgentExecInSetting || !$isAgentExec && $isAgentExecInSetting) { return ""; } $dateTodayPhp = new \DateTime(); $datetimeToday = Type\DateTime::createFromPhp(clone $dateTodayPhp); $dateToday = clone $dateTodayPhp; $dateToday = Type\Date::createFromPhp($dateToday->setTime(0, 0, 0)); $dateTomorrow = clone $dateTodayPhp; $dateTomorrow = Type\Date::createFromPhp($dateTomorrow->setTime(0, 0, 0))->add('1 DAY'); $arDateFilter = array($dateToday, $dateTomorrow); $chainDb = MailingChainTable::getList(array('select' => array('ID', 'LAST_EXECUTED', 'POSTING_ID', 'DAYS_OF_MONTH', 'DAYS_OF_WEEK', 'TIMES_OF_DAY'), 'filter' => array('=REITERATE' => 'Y', '=MAILING.ACTIVE' => 'Y', 'STATUS' => MailingChainTable::STATUS_WAIT))); while ($arMailingChain = $chainDb->fetch()) { $lastExecuted = $arMailingChain['LAST_EXECUTED']; /* @var \Bitrix\Main\Type\DateTime $lastExecuted*/ if ($lastExecuted && $lastExecuted->getTimestamp() >= $dateToday->getTimestamp()) { continue; } $timeOfExecute = static::getDateExecute($dateTodayPhp, $arMailingChain["DAYS_OF_MONTH"], $arMailingChain["DAYS_OF_WEEK"], $arMailingChain["TIMES_OF_DAY"]); if ($timeOfExecute) { $arUpdateMailChain = array('LAST_EXECUTED' => $datetimeToday); $postingDb = PostingTable::getList(array('select' => array('ID'), 'filter' => array('=MAILING_CHAIN_ID' => $arMailingChain['ID'], '><DATE_CREATE' => $arDateFilter))); $arPosting = $postingDb->fetch(); if (!$arPosting) { $postingId = MailingChainTable::initPosting($arMailingChain['ID']); } else { $postingId = $arPosting['ID']; $arUpdateMailChain['POSTING_ID'] = $postingId; PostingTable::initGroupRecipients($postingId); } if ($postingId) { $arUpdateMailChain['STATUS'] = MailingChainTable::STATUS_SEND; $arUpdateMailChain['AUTO_SEND_TIME'] = Type\DateTime::createFromPhp($timeOfExecute); } MailingChainTable::update(array('ID' => $arMailingChain['ID']), $arUpdateMailChain); } } return static::getAgentNamePeriod(); }
public static function updateChain($id, array $fields) { $result = new \Bitrix\Main\Entity\Result(); static::checkFieldsChain($result, $id, $fields); if (!$result->isSuccess(true)) { return $result; } $parentChainId = null; $existChildIdList = array(); foreach ($fields as $chainFields) { $chainId = $chainFields['ID']; unset($chainFields['ID']); $chainFields['MAILING_ID'] = $id; $chainFields['IS_TRIGGER'] = 'Y'; $chainFields['REITERATE'] = 'Y'; $chainFields['PARENT_ID'] = $parentChainId; // default status if ($chainId > 0) { $chain = \Bitrix\Sender\MailingChainTable::getRowById(array('ID' => $chainId)); if ($chain && $chain['STATUS'] != \Bitrix\Sender\MailingChainTable::STATUS_WAIT) { $chainFields['STATUS'] = $chain['STATUS']; unset($chainFields['CREATED_BY']); } } if (empty($chainFields['STATUS'])) { $chainFields['STATUS'] = \Bitrix\Sender\MailingChainTable::STATUS_WAIT; } // add or update if ($chainId > 0) { $existChildIdList[] = $chainId; $chainUpdateDb = MailingChainTable::update(array('ID' => $chainId), $chainFields); if ($chainUpdateDb->isSuccess()) { } else { $result->addErrors($chainUpdateDb->getErrors()); } } else { $chainAddDb = MailingChainTable::add($chainFields); if ($chainAddDb->isSuccess()) { $chainId = $chainAddDb->getId(); $existChildIdList[] = $chainId; } else { $result->addErrors($chainAddDb->getErrors()); } } if (!empty($errorList)) { break; } $parentChainId = null; if ($chainId !== null) { $parentChainId = $chainId; } } $deleteChainDb = MailingChainTable::getList(array('select' => array('ID'), 'filter' => array('MAILING_ID' => $id, '!ID' => $existChildIdList))); while ($deleteChain = $deleteChainDb->fetch()) { MailingChainTable::delete(array('ID' => $deleteChain['ID'])); } static::updateChainTrigger($id); return $result; }
if (!empty($TEMPLATE_ACTION_SAVE_NAME) && !empty($MESSAGE)) { $CONTENT = $MESSAGE; $useBlockEditor = false; if ($TEMPLATE_TYPE && $TEMPLATE_ID) { \Bitrix\Main\Loader::includeModule('fileman'); $chainTemplate = \Bitrix\Sender\Preset\Template::getById($TEMPLATE_TYPE, $TEMPLATE_ID); if ($chainTemplate && $chainTemplate['HTML']) { $CONTENT = \Bitrix\Fileman\Block\Editor::fillTemplateBySliceContent($chainTemplate['HTML'], $CONTENT); if ($CONTENT) { $useBlockEditor = true; } } } $addResult = \Bitrix\Sender\TemplateTable::add(array('NAME' => $TEMPLATE_ACTION_SAVE_NAME, 'CONTENT' => $CONTENT)); if ($useBlockEditor && $addResult->isSuccess()) { \Bitrix\Sender\MailingChainTable::update(array('ID' => $ID), array('TEMPLATE_TYPE' => 'USER', 'TEMPLATE_ID' => $addResult->getId())); } } } if ($apply != "") { LocalRedirect("/bitrix/admin/sender_mailing_chain_edit.php?MAILING_ID=" . $MAILING_ID . "&ID=" . $ID . "&lang=" . LANG . "&" . $tabControl->ActiveTabParam()); } else { LocalRedirect("/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=" . $MAILING_ID . "&lang=" . LANG); } } else { if ($e = $APPLICATION->GetException()) { $arError[] = GetMessage("rub_save_error"); } if (!empty($arError)) { $message = new CAdminMessage(implode('<br>', $arError)); }
/** * @param \Bitrix\Main\Event $event * @return void */ public static function onAfterPostingSendRecipient(\Bitrix\Main\Event $event) { $data = $event->getParameter(0); if (!$data || !$data['SEND_RESULT'] || empty($data['POSTING']['MAILING_CHAIN_ID'])) { return; } $chainId = $data['POSTING']['MAILING_CHAIN_ID']; $dataRecipient = $data['RECIPIENT']; static $mailingParams = array(); if (!isset($mailingParams[$chainId])) { $mailingParams[$chainId] = array(); $childChainDb = MailingChainTable::getList(array('select' => array('ID', 'MAILING_ID', 'PARENT_ID', 'POSTING_ID'), 'filter' => array('=MAILING.ACTIVE' => 'Y', '=IS_TRIGGER' => 'Y', '=STATUS' => array(MailingChainTable::STATUS_WAIT, MailingChainTable::STATUS_SEND), '=PARENT_ID' => $chainId))); while ($childChain = $childChainDb->fetch()) { // add posting $postingAddDb = PostingTable::add(array('MAILING_ID' => $childChain['MAILING_ID'], 'MAILING_CHAIN_ID' => $childChain['ID'])); if (!$postingAddDb->isSuccess()) { continue; } $mailingParams[$chainId][] = array('POSTING_ID' => $postingAddDb->getId(), 'CHAIN' => $childChain); } } if (empty($mailingParams[$chainId])) { return; } foreach ($mailingParams[$chainId] as $mailingParamsItem) { $postingId = $mailingParamsItem['POSTING_ID']; $childChain = $mailingParamsItem['CHAIN']; // check email as unsubscribed if (Subscription::isUnsubscibed($childChain['MAILING_ID'], $data['RECIPIENT']['EMAIL'])) { continue; } $recipient = array('POSTING_ID' => $postingId); $recipient['STATUS'] = PostingRecipientTable::SEND_RESULT_NONE; $recipient['EMAIL'] = $dataRecipient['EMAIL']; if (!empty($dataRecipient['FIELDS'])) { $recipient['FIELDS'] = $dataRecipient['FIELDS']; } if (!empty($dataRecipient['ROOT_ID'])) { $recipient['ROOT_ID'] = $dataRecipient['ROOT_ID']; } else { $recipient['ROOT_ID'] = $dataRecipient['ID']; } if (!empty($dataRecipient['NAME'])) { $recipient['NAME'] = $dataRecipient['NAME']; } if (!empty($dataRecipient['USER_ID'])) { $recipient['USER_ID'] = $dataRecipient['USER_ID']; } // add recipient PostingTable::addRecipient($recipient, true); if (empty($mailingParams[$chainId]['CHAIN']['POSTING_ID'])) { $chainUpdateDb = MailingChainTable::update(array('ID' => $childChain['ID']), array('POSTING_ID' => $postingId)); if ($chainUpdateDb->isSuccess()) { $mailingParams[$chainId]['CHAIN']['POSTING_ID'] = $postingId; } } } }