/** * @param Entity\Event $event * @return Entity\EventResult */ public static function onAfterDelete(Entity\Event $event) { $result = new Entity\EventResult(); $data = $event->getParameters(); $primary = array('MAILING_ID' => $data['primary']['ID']); MailingGroupTable::delete($primary); MailingChainTable::delete($primary); MailingSubscriptionTable::delete($primary); PostingTable::delete($primary); return $result; }
$arPosting = $postingDb->fetch(); if ($arPosting) { $ID = intval($arPosting['MAILING_CHAIN_ID']); } } $statClickList = array(); $statResult = array('all' => 0, 'all_print' => 0, 'delivered' => 0, 'error' => 0, 'not_send' => 0, 'read' => 0, 'click' => 0, 'unsub' => 0); if ($ID > 0) { $postingDb = \Bitrix\Sender\PostingTable::getList(array('select' => array('ID', 'DATE_CREATE', 'DATE_SENT', 'STATUS', 'MAILING_CHAIN_REITERATE' => 'MAILING_CHAIN.REITERATE', 'SUBJECT' => 'MAILING_CHAIN.SUBJECT', 'COUNT_SEND_ALL', 'COUNT_SEND_NONE', 'COUNT_SEND_ERROR', 'COUNT_SEND_SUCCESS', 'COUNT_SEND_DENY', 'COUNT_READ', 'COUNT_CLICK', 'COUNT_UNSUB'), 'filter' => array('MAILING_CHAIN_ID' => $ID, '!DATE_SENT' => null), 'order' => array('DATE_SENT' => 'DESC', 'DATE_CREATE' => 'DESC'), 'limit' => 1)); $arPosting = $postingDb->fetch(); // get reiterate postings statistic $arPostingReiterateList = array(); if (!empty($arPosting) && $arPosting['MAILING_CHAIN_REITERATE'] == 'Y') { $defaultDate = new \Bitrix\Main\Type\DateTime(); $postingReiterateList = array(); $postingReiterateDb = \Bitrix\Sender\PostingTable::getList(array('select' => array('ID', 'DATE_SENT', 'COUNT_SEND_ALL', 'COUNT_READ', 'COUNT_CLICK', 'COUNT_UNSUB'), 'filter' => array('MAILING_CHAIN_ID' => $ID, '!STATUS' => \Bitrix\Sender\PostingTable::STATUS_NEW), 'order' => array('DATE_SENT' => 'DESC', 'ID' => 'DESC'), 'limit' => 50)); while ($postingReiterate = $postingReiterateDb->fetch()) { $postingReiterate['CNT'] = $postingReiterate['COUNT_SEND_ALL']; $postingReiterateList[$postingReiterate['ID']] = $postingReiterate; } foreach ($postingReiterateList as $arPostingReiterate) { if (empty($arPostingReiterate['DATE_SENT'])) { $arPostingReiterate['DATE_SENT'] = $defaultDate; } $cntDivider = $arPostingReiterate['CNT'] > 0 ? $arPostingReiterate['CNT'] : 1; $cntDivider = $cntDivider / 100; $defaultDateTimeStamp = $arPostingReiterate['DATE_SENT']->getTimestamp(); $arPostingReiterateList[$defaultDateTimeStamp] = array('date' => $arPostingReiterate['DATE_SENT']->format("d/m"), 'sent' => $arPostingReiterate['CNT'], 'read' => $arPostingReiterate['COUNT_READ'], 'click' => $arPostingReiterate['COUNT_CLICK'], 'unsub' => $arPostingReiterate['COUNT_UNSUB'], 'sent_prsnt' => '100', 'read_prsnt' => round($arPostingReiterate['COUNT_READ'] / $cntDivider, 2), 'click_prsnt' => round($arPostingReiterate['COUNT_CLICK'] / $cntDivider, 2), 'unsub_prsnt' => round($arPostingReiterate['COUNT_UNSUB'] / $cntDivider, 2)); } if (!empty($arPostingReiterateList)) { if (count($arPostingReiterateList) < 2) {
$message = $sendErrors->getMessage(); } break; case MailingChainTable::STATUS_PAUSE: $message = array("MESSAGE" => GetMessage("MAILING_ADM_SENDING_PAUSE"), "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()"))); $actionJs = 'list'; break; case MailingChainTable::STATUS_END: $message = array("MESSAGE" => GetMessage("MAILING_ADM_SENDING_RESULT_OK")); $actionJsList = true; break; default: $message = GetMessage("MAILING_ADM_POST_NOT_FOUND"); } if (is_array($message)) { $arEmailStatuses = PostingTable::getRecipientCountByStatus($arMailingChain['POSTING_ID']); $nEmailsSent = intval($arEmailStatuses[PostingRecipientTable::SEND_RESULT_SUCCESS]); $nEmailsError = intval($arEmailStatuses[PostingRecipientTable::SEND_RESULT_ERROR]); $nEmailsNone = intval($arEmailStatuses[PostingRecipientTable::SEND_RESULT_NONE]); $nEmailsTotal = $nEmailsNone + $nEmailsSent + $nEmailsError; $message = array_merge($message, array("DETAILS" => $arMailingChain['SUBJECT'] . $messageDetails . '#PROGRESS_BAR#' . '<p>' . GetMessage("MAILING_ADM_SENDING_PROCESSED") . ' <b>' . ($nEmailsSent + $nEmailsError) . '</b> ' . GetMessage("MAILING_ADM_SENDING_PROCESSED_OF") . ' <b>' . $nEmailsTotal . '</b></p>' . '<p>' . GetMessage("MAILING_ADM_WITH_ERRORS") . ': <b>' . $nEmailsError . '</b>.</p>', "HTML" => true, "TYPE" => "PROGRESS", "PROGRESS_TOTAL" => $nEmailsTotal, "PROGRESS_VALUE" => $nEmailsSent + $nEmailsError)); } } } else { $message = GetMessage("MAILING_ADM_POST_NOT_FOUND"); } $adminMessage = new CAdminMessage($message); echo $adminMessage->show(); if ($actionJsList && $actionJsMoveProgress) { ?> <script><?php
/** * @param $mailingChainId * @return int|null */ public static function initPosting($mailingChainId) { $postingId = null; $chainPrimary = array('ID' => $mailingChainId); $arMailingChain = static::getRowById($chainPrimary); if ($arMailingChain) { $needAddPosting = true; if (!empty($arMailingChain['POSTING_ID'])) { $arPosting = PostingTable::getRowById(array('ID' => $arMailingChain['POSTING_ID'])); if ($arPosting && $arPosting['STATUS'] == PostingTable::STATUS_NEW) { $postingId = $arMailingChain['POSTING_ID']; $needAddPosting = false; } } if ($needAddPosting) { $postingAddDb = PostingTable::add(array('MAILING_ID' => $arMailingChain['MAILING_ID'], 'MAILING_CHAIN_ID' => $arMailingChain['ID'])); if ($postingAddDb->isSuccess()) { $postingId = $postingAddDb->getId(); static::update($chainPrimary, array('POSTING_ID' => $postingId)); } } if ($postingId) { PostingTable::initGroupRecipients($postingId); } } return $postingId; }
/** * Handler of after add event * * @param Entity\Event $event * @return Entity\EventResult */ public static function onAfterAdd(Entity\Event $event) { $result = new Entity\EventResult(); $data = $event->getParameters(); $data = $data['fields']; // update unsub flag of recipient PostingRecipientTable::update(array('ID' => $data['RECIPIENT_ID']), array('IS_UNSUB' => 'Y')); // update unsub counter of posting $resultDb = static::getList(array('filter' => array('RECIPIENT_ID' => $data['RECIPIENT_ID']))); if ($resultDb->getSelectedRowsCount() == 1) { PostingTable::update(array('ID' => $data['POSTING_ID']), array('COUNT_UNSUB' => new \Bitrix\Main\DB\SqlExpression('?# + 1', 'COUNT_UNSUB'))); } return $result; }
/** * @param $id * @param int $timeout * @param int $maxMailCount * @return bool|string * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\DB\Exception * @throws \Bitrix\Main\Db\SqlQueryException * @throws \Exception */ public static function send($id, $timeout = 0, $maxMailCount = 0) { $start_time = getmicrotime(); @set_time_limit(0); static::$emailSentPerIteration = 0; $postingDb = PostingTable::getList(array('select' => array('ID', 'STATUS', 'MAILING_ID', 'MAILING_CHAIN_ID', 'MAILING_CHAIN_REITERATE' => 'MAILING_CHAIN.REITERATE', 'MAILING_CHAIN_IS_TRIGGER' => 'MAILING_CHAIN.IS_TRIGGER'), 'filter' => array('ID' => $id, 'MAILING.ACTIVE' => 'Y', 'MAILING_CHAIN.STATUS' => MailingChainTable::STATUS_SEND))); $postingData = $postingDb->fetch(); // posting not found if (!$postingData) { return static::SEND_RESULT_ERROR; } // if posting in new status, then import recipients from groups and set right status for sending $isInitGroupRecipients = false; $isChangeStatusToPart = false; if ($postingData["STATUS"] == PostingTable::STATUS_NEW) { $isInitGroupRecipients = true; $isChangeStatusToPart = true; } if ($postingData["STATUS"] != PostingTable::STATUS_PART && $postingData["MAILING_CHAIN_IS_TRIGGER"] == 'Y') { $isInitGroupRecipients = false; $isChangeStatusToPart = true; } if ($isInitGroupRecipients) { PostingTable::initGroupRecipients($postingData['ID']); } if ($isChangeStatusToPart) { PostingTable::update(array('ID' => $postingData['ID']), array('STATUS' => PostingTable::STATUS_PART)); $postingData["STATUS"] = PostingTable::STATUS_PART; } // posting not in right status if ($postingData["STATUS"] != PostingTable::STATUS_PART) { return static::SEND_RESULT_ERROR; } // lock posting for exclude double parallel sending if (static::lockPosting($id) === false) { throw new \Bitrix\Main\DB\Exception(Loc::getMessage('SENDER_POSTING_MANAGER_ERR_LOCK')); } // select all recipients of posting, only not processed $recipientDataDb = PostingRecipientTable::getList(array('filter' => array('POSTING_ID' => $postingData['ID'], 'STATUS' => PostingRecipientTable::SEND_RESULT_NONE), 'limit' => $maxMailCount)); while ($recipientData = $recipientDataDb->fetch()) { // create name from email $recipientEmail = $recipientData["EMAIL"]; if (empty($recipientData["NAME"])) { $recipientEmailParts = explode('@', $recipientEmail); $recipientName = $recipientEmailParts[0]; } else { $recipientName = $recipientData["NAME"]; } // prepare params for send $sendParams = array('FIELDS' => array('EMAIL_TO' => $recipientEmail, 'NAME' => $recipientName, 'USER_ID' => $recipientData["USER_ID"], 'SENDER_CHAIN_CODE' => 'sender_chain_item_' . $postingData["MAILING_CHAIN_ID"], 'UNSUBSCRIBE_LINK' => Subscription::getLinkUnsub(array('MAILING_ID' => $postingData['MAILING_ID'], 'EMAIL' => $recipientEmail, 'RECIPIENT_ID' => $recipientData["ID"]))), 'TRACK_READ' => array('MODULE_ID' => "sender", 'FIELDS' => array('RECIPIENT_ID' => $recipientData["ID"])), 'TRACK_CLICK' => array('MODULE_ID' => "sender", 'FIELDS' => array('RECIPIENT_ID' => $recipientData["ID"]), 'URL_PARAMS' => array('bx_sender_conversion_id' => $recipientData["ID"]))); if (is_array($recipientData['FIELDS']) && count($recipientData) > 0) { $sendParams['FIELDS'] = $sendParams['FIELDS'] + $recipientData['FIELDS']; } // set sending result to recipient $mailSendResult = static::sendInternal($postingData['MAILING_CHAIN_ID'], $sendParams); PostingRecipientTable::update(array('ID' => $recipientData["ID"]), array('STATUS' => $mailSendResult, 'DATE_SENT' => new Type\DateTime())); // send event $eventData = array('SEND_RESULT' => $mailSendResult == PostingRecipientTable::SEND_RESULT_SUCCESS, 'RECIPIENT' => $recipientData, 'POSTING' => $postingData); $event = new Event('sender', 'OnAfterPostingSendRecipient', array($eventData)); $event->send(); // limit executing script by time if ($timeout > 0 && getmicrotime() - $start_time >= $timeout) { break; } // increment sending statistic static::$emailSentPerIteration++; } //set status and delivered and error emails $statusList = PostingTable::getRecipientCountByStatus($id); if (!array_key_exists(PostingRecipientTable::SEND_RESULT_NONE, $statusList)) { if (array_key_exists(PostingRecipientTable::SEND_RESULT_ERROR, $statusList)) { $STATUS = PostingTable::STATUS_SENT_WITH_ERRORS; } else { $STATUS = PostingTable::STATUS_SENT; } $DATE = new Type\DateTime(); } else { $STATUS = PostingTable::STATUS_PART; $DATE = null; } // unlock posting for exclude double parallel sending static::unlockPosting($id); // update status of posting PostingTable::update(array('ID' => $id), array('STATUS' => $STATUS, 'DATE_SENT' => $DATE)); // return status to continue or end of sending if ($STATUS == PostingTable::STATUS_PART) { return static::SEND_RESULT_CONTINUE; } else { return static::SEND_RESULT_SENT; } }
/** * @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(); }
private static function getPostingStatusUpdateFields($postingId, $statusList) { if (!$postingId || count($statusList) == 0) { return null; } $postingUpdateFields = array('COUNT_SEND_ALL' => 0); $map = \Bitrix\Sender\PostingTable::getRecipientStatusToPostingFieldMap(); foreach ($map as $recipientStatus => $postingFieldName) { if (!array_key_exists($recipientStatus, $statusList)) { continue; } else { $postingCountFieldValue = $statusList[$recipientStatus]; } $postingUpdateFields['COUNT_SEND_ALL'] += $postingCountFieldValue; $postingUpdateFields[$postingFieldName] = $postingCountFieldValue; } if ($postingUpdateFields['COUNT_SEND_ALL'] == 0) { return null; } return $postingUpdateFields; }
$bVarsFromForm = true; } } //Edit/Add part ClearVars(); $str_SORT = 100; $str_ACTIVE = "Y"; $str_VISIBLE = "Y"; $arMailngChainAttachment = array(); if ($ID > 0) { $rubric = new CDBResult(\Bitrix\Sender\MailingChainTable::getList(array('select' => array('*'), 'filter' => array('ID' => $ID)))); if (!$rubric->ExtractFields("str_")) { $ID = 0; } if ($ID > 0) { $postingDb = \Bitrix\Sender\PostingTable::getList(array('select' => array('*'), 'filter' => array('ID' => $ID, '!DATE_SENT' => null), 'order' => array('DATE_SENT' => 'DESC'), 'limit' => 1)); $arPosting = $postingDb->fetch(); $str_DATE_SENT = $arPosting['DATE_SENT']; $attachmentFileDb = \Bitrix\Sender\MailingAttachmentTable::getList(array('select' => array('FILE_ID'), 'filter' => array('CHAIN_ID' => $ID))); while ($ar = $attachmentFileDb->fetch()) { if ($arFileFetch = CFile::GetFileArray($ar['FILE_ID'])) { $arMailngChainAttachment[] = $arFileFetch; } } } } if ($bVarsFromForm) { $DB->InitTableVarsForEdit("b_sender_mailing_chain", "", "str_"); } \CJSCore::Init(array("sender_admin")); $APPLICATION->SetTitle($ID > 0 ? GetMessage("sender_chain_edit_title_edit") . $ID : GetMessage("sender_chain_edit_title_add"));
$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); } $sTableID = "tbl_sender_posting_recipient"; $oSort = new CAdminSorting($sTableID, "ID", "desc"); $lAdmin = new CAdminList($sTableID, $oSort); $MAILING_ID = intval($_REQUEST['MAILING_ID']); $ID = intval($_REQUEST['ID']); if ($find_mailing_chain_id > 0) { $ID = $find_mailing_chain_id; } if ($find_click_show_url != 'Y') { $find_click_show_url = 'N'; } $showClickUrl = $find_click_show_url; if ($ID <= 0) { $postingDb = \Bitrix\Sender\PostingTable::getList(array('select' => array('MAILING_CHAIN_ID'), 'filter' => array('MAILING_ID' => $MAILING_ID), 'order' => array('DATE_SENT' => 'DESC', 'DATE_CREATE' => 'DESC'))); $arPosting = $postingDb->fetch(); if ($arPosting) { $ID = intval($arPosting['MAILING_CHAIN_ID']); } } function CheckFilter() { global $FilterArr, $lAdmin; foreach ($FilterArr as $f) { global ${$f}; } return count($lAdmin->arFilterErrors) == 0; } $FilterArr = array("find_email", "find_name", "find_mailing", "find_mailing_chain_id", "find_sent", "find_read", "find_click", "find_unsub"); $lAdmin->InitFilter($FilterArr);
/** * @param Entity\Event $event * @return Entity\EventResult */ public static function onDelete(Entity\Event $event) { $result = new Entity\EventResult(); $data = $event->getParameters(); $deleteIdList = array(); if (!empty($data['primary'])) { $itemDb = static::getList(array('select' => array('ID'), 'filter' => $data['primary'])); while ($item = $itemDb->fetch()) { $deleteIdList[] = $item['ID']; } } foreach ($deleteIdList as $chainId) { MailingAttachmentTable::delete(array('CHAIN_ID' => $chainId)); MailingTriggerTable::delete(array('MAILING_CHAIN_ID' => $chainId)); PostingTable::delete(array('MAILING_CHAIN_ID' => $chainId)); } return $result; }
echo $ID; ?> &action=send_error&lang=<?php echo LANGUAGE_ID; ?> '" title="<?php echo GetMessage("sender_chain_edit_btn_send_err_desc"); ?> " /> <?php } } if ($ID > 0) { if (in_array($str_STATUS, array(\Bitrix\Sender\MailingChainTable::STATUS_SEND, \Bitrix\Sender\MailingChainTable::STATUS_PAUSE))) { echo '<span class="sender-mailing-status-creator" style="margin-left: 30px;">(' . GetMessage("sender_chain_edit_btn_send_stat_prcnt") . ': ' . \Bitrix\Sender\PostingTable::getSendPercent($str_POSTING_ID) . '%)</span>'; } } ?> </span> </div> </td> </tr> <?php if ($ID > 0 || $ID <= 0) { ?> <tr class="adm-detail-required-field"> <td colspan="2"> <br> <?php $arEmailFromList = \Bitrix\Sender\MailingChainTable::getEmailToMeList();
$arPosting = $postingDb->fetch(); $arPostingReiterateList = array(); if (!empty($arPosting) && $arPosting['MAILING_CHAIN_REITERATE'] == 'Y') { $defaultDate = new \Bitrix\Main\Type\DateTime(); $postingReiterateDb = \Bitrix\Sender\PostingTable::getList(array( 'select' => array( 'ID', 'DATE_SENT', 'CNT', 'READ_CNT', 'CLICK_CNT', 'UNSUB_CNT' ), 'filter' => array( 'MAILING_CHAIN_ID' => $ID, '!STATUS' => \Bitrix\Sender\PostingTable::STATUS_NEW, 'POSTING_RECIPIENT.STATUS' => \Bitrix\Sender\PostingRecipientTable::SEND_RESULT_SUCCESS ), 'runtime' => array( new \Bitrix\Main\Entity\ExpressionField('CNT', 'COUNT(DISTINCT %s)', 'POSTING_RECIPIENT.ID'), new \Bitrix\Main\Entity\ExpressionField('READ_CNT', 'COUNT(DISTINCT %s)', 'POSTING_RECIPIENT.POSTING_READ.RECIPIENT_ID'), new \Bitrix\Main\Entity\ExpressionField('CLICK_CNT', 'COUNT(DISTINCT %s)', 'POSTING_RECIPIENT.POSTING_CLICK.RECIPIENT_ID'), new \Bitrix\Main\Entity\ExpressionField('UNSUB_CNT', 'COUNT(DISTINCT %s)', 'POSTING_RECIPIENT.POSTING_UNSUB.RECIPIENT_ID') ), 'order' => array('DATE_SENT' => 'DESC', 'ID' => 'DESC'), 'limit' => 50, )); while($arPostingReiterate = $postingReiterateDb->fetch()) { //echo $arPostingReiterate['POSTING_DATE_SENT'].'<br>'; if(empty($arPostingReiterate['DATE_SENT'])) $arPostingReiterate['DATE_SENT'] = $defaultDate;
} } $mailing = null; if ($MAILING_ID > 0) { $mailingDb = \Bitrix\Sender\MailingTable::getList(array('filter' => array('ID' => $MAILING_ID, 'IS_TRIGGER' => 'Y'))); if (!($mailing = $mailingDb->fetch())) { $MAILING_ID = 0; } } $statList = array(); if ($MAILING_ID > 0) { $i = 1; $chainList = \Bitrix\Sender\MailingTable::getChain($MAILING_ID); foreach ($chainList as $chain) { $stat = array('NAME' => GetMessage("sender_stat_trig_letter") . $i++, 'SUBJECT' => $chain['SUBJECT'], 'CNT' => array('SENT_SUCCESS' => 0, 'SENT_ERROR' => 0, 'READ' => 0, 'CLICK' => 0, 'UNSUB' => 0, 'GOAL' => 0, 'START' => 0)); $statRawDb = \Bitrix\Sender\PostingTable::getList(array('select' => array('CNT', 'READ_CNT', 'CLICK_CNT', 'UNSUB_CNT'), 'filter' => array('=MAILING_CHAIN_ID' => $chain['ID']), 'runtime' => array(new \Bitrix\Main\Entity\ExpressionField('CNT', 'SUM(%s)', 'COUNT_SEND_SUCCESS'), new \Bitrix\Main\Entity\ExpressionField('READ_CNT', 'SUM(%s)', 'COUNT_READ'), new \Bitrix\Main\Entity\ExpressionField('CLICK_CNT', 'SUM(%s)', 'COUNT_CLICK'), new \Bitrix\Main\Entity\ExpressionField('UNSUB_CNT', 'SUM(%s)', 'COUNT_UNSUB')))); while ($statRaw = $statRawDb->fetch()) { $stat['CNT']['SENT_SUCCESS'] += $statRaw['CNT']; $stat['CNT']['READ'] += $statRaw['READ_CNT']; $stat['CNT']['CLICK'] += $statRaw['CLICK_CNT']; $stat['CNT']['UNSUB'] += $statRaw['UNSUB_CNT']; $stat['CNT']['START'] += $statRaw['CNT']; } $statRawDb = \Bitrix\Sender\PostingRecipientTable::getList(array('select' => array('ID'), 'filter' => array('=POSTING.MAILING_CHAIN_ID' => $chain['ID'], '=STATUS' => array(\Bitrix\Sender\PostingRecipientTable::SEND_RESULT_SUCCESS, \Bitrix\Sender\PostingRecipientTable::SEND_RESULT_DENY), '!DATE_DENY' => null))); $stat['CNT']['GOAL'] = $statRawDb->getSelectedRowsCount(); $statList['CHAIN'][] = $stat; } } if (!empty($statList)) { foreach ($statList['CHAIN'] as $chain) { foreach ($chain['CNT'] as $k => $v) {
/** * @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; } } } }
/** * Change status of recipients and mailing chain for resending mails to recipients who have error sending * * @param $id * @return void */ public static function prepareReSendErrorRecipients($id) { if (!static::canReSendErrorRecipients($id)) { return; } $mailingChain = static::getRowById(array('ID' => $id)); $updateSql = 'UPDATE ' . PostingRecipientTable::getTableName() . " SET STATUS='" . PostingRecipientTable::SEND_RESULT_NONE . "'" . " WHERE POSTING_ID=" . intval($mailingChain['POSTING_ID']) . " AND STATUS='" . PostingRecipientTable::SEND_RESULT_ERROR . "'"; \Bitrix\Main\Application::getConnection()->query($updateSql); PostingTable::update(array('ID' => $mailingChain['POSTING_ID']), array('STATUS' => PostingTable::STATUS_PART)); static::update(array('ID' => $id), array('STATUS' => static::STATUS_SEND)); }
/** * @param $postingId * @param bool $checkDuplicate * @return bool * @throws \Bitrix\Main\ArgumentException */ public static function initGroupRecipients($postingId, $checkDuplicate = false) { $arPosting = \Bitrix\Sender\PostingTable::getRowById(array('ID' => $postingId)); if (!$arPosting) { return false; } $checkRecipientDuplicate = $checkDuplicate; if (!$checkDuplicate) { if ($arPosting['STATUS'] == \Bitrix\Sender\PostingTable::STATUS_NEW) { $primary = array('POSTING_ID' => $postingId); \Bitrix\Sender\PostingRecipientTable::delete($primary); $checkRecipientDuplicate = false; } } // fetch all unsubscribed emails of current mailing for excluding from recipients $arEmailNotSend = array(); $recipientUnsubDb = \Bitrix\Sender\PostingUnsubTable::getList(array('select' => array('EMAIL' => 'POSTING_RECIPIENT.EMAIL'), 'filter' => array('POSTING.MAILING_ID' => $arPosting['MAILING_ID']))); while ($recipientUnsub = $recipientUnsubDb->fetch()) { $arEmailNotSend[] = $recipientUnsub['EMAIL']; } $groupConnectorsDataCount = array(); $connection = \Bitrix\Main\Application::getConnection(); $conHelper = $connection->getSqlHelper(); $statusRecipientNone = \Bitrix\Sender\PostingRecipientTable::SEND_RESULT_NONE; // fetch all connectors for getting emails $groupDb = \Bitrix\Sender\MailingGroupTable::getList(array('select' => array('INCLUDE', 'CONNECTOR_ENDPOINT' => 'GROUP.GROUP_CONNECTOR.ENDPOINT', 'GROUP_ID'), 'filter' => array('MAILING_ID' => $arPosting['MAILING_ID']), 'order' => array('INCLUDE' => 'DESC', 'GROUP_ID' => 'ASC'))); while ($group = $groupDb->fetch()) { $connector = \Bitrix\Sender\ConnectorManager::getConnector($group['CONNECTOR_ENDPOINT']); if (!$connector) { continue; } $connectorDataCount = 0; $connector->setFieldValues($group['CONNECTOR_ENDPOINT']['FIELDS']); $connectorDataDb = $connector->getData(); while (true) { $emailList = array(); $connectorDataList = array(); $maxPart = 200; while ($arConnectorData = $connectorDataDb->Fetch()) { // collect connectors counter of addresses $connectorDataCount++; // exclude unsubscribed addresses $arConnectorData['EMAIL'] = trim(strtolower($arConnectorData['EMAIL'])); if (strlen($arConnectorData['EMAIL']) <= 0 || in_array($arConnectorData['EMAIL'], $arEmailNotSend)) { continue; } $emailList[] = $arConnectorData['EMAIL']; $connectorDataList[$arConnectorData['EMAIL']] = $arConnectorData; $maxPart--; if ($maxPart == 0) { break; } } if (empty($emailList)) { break; } foreach ($emailList as &$email) { $email = $conHelper->forSql($email); } $emailListString = "'" . implode("', '", $emailList) . "'"; if ($group['INCLUDE']) { // add address if not exists if ($checkRecipientDuplicate) { $recipientEmailDb = $connection->query("select EMAIL from b_sender_posting_recipient where EMAIL in (" . $emailListString . ") and POSTING_ID=" . intval($postingId)); while ($recipientEmail = $recipientEmailDb->fetch()) { unset($connectorDataList[$recipientEmail['EMAIL']]); } } if (!empty($connectorDataList)) { foreach ($connectorDataList as $email => $arConnectorData) { $arRecipient = array('NAME' => "'" . $conHelper->forSql($arConnectorData['NAME']) . "'", 'EMAIL' => "'" . $conHelper->forSql($arConnectorData['EMAIL']) . "'", 'STATUS' => "'" . $statusRecipientNone . "'", 'POSTING_ID' => intval($postingId)); if (array_key_exists('USER_ID', $arConnectorData) && intval($arConnectorData['USER_ID']) > 0) { $arRecipient['USER_ID'] = intval($arConnectorData['USER_ID']); } $insertColumnNamesString = implode(", ", array_keys($arRecipient)); $insertColumnValuesString = implode(", ", array_values($arRecipient)); $connection->query("insert into b_sender_posting_recipient(" . $insertColumnNamesString . ") values(" . $insertColumnValuesString . ")"); } } } else { // delete address from posting $connection->query("delete from b_sender_posting_recipient where EMAIL in (" . $emailListString . ") and POSTING_ID=" . intval($postingId)); } } //\Bitrix\Sender\GroupConnectorTable::update(array('ID' => $group['GROUP_CONNECTOR_ID']), array('ADDRESS_COUNT' => $connectorDataCount)); // collect groups counter of addresses if (array_key_exists($group['GROUP_ID'], $groupConnectorsDataCount)) { $groupConnectorsDataCount[$group['GROUP_ID']] += $connectorDataCount; } else { $groupConnectorsDataCount[$group['GROUP_ID']] = $connectorDataCount; } } // update group counter of addresses foreach ($groupConnectorsDataCount as $groupId => $groupDataCount) { \Bitrix\Sender\GroupTable::update($groupId, array('ADDRESS_COUNT' => $groupDataCount)); } return true; }
} } $groupListDb = \Bitrix\Sender\MailingChainTable::getList(array('select' => array('ID', 'MAILING_ID', 'POSTING_ID', 'CREATED_BY', 'STATUS', 'REITERATE', 'LAST_EXECUTED', 'EMAIL_FROM', 'AUTO_SEND_TIME', 'DAYS_OF_MONTH', 'DAYS_OF_WEEK', 'TIMES_OF_DAY', 'NAME' => 'SUBJECT'), 'filter' => $arFilter, 'order' => array($by => $order))); $rsData = new CAdminResult($groupListDb, $sTableID); $rsData->NavStart(); $lAdmin->NavText($rsData->GetNavPrint(GetMessage("sender_mailing_chain_adm_nav"))); $lAdmin->AddHeaders(array(array("id" => "ID", "content" => "ID", "sort" => "ID", "align" => "right", "default" => true), array("id" => "NAME", "content" => GetMessage("sender_mailing_chain_adm_field_name"), "sort" => "NAME", "default" => true), array("id" => "CREATED_BY", "content" => GetMessage("sender_mailing_chain_adm_field_created_by"), "sort" => "CREATED_BY", "default" => true), array("id" => "STATUS", "content" => GetMessage("sender_mailing_chain_adm_field_status"), "sort" => "STATUS", "default" => true), array("id" => "EMAIL_FROM", "content" => GetMessage("sender_mailing_chain_adm_field_email_from"), "sort" => "EMAIL_FROM", "default" => false), array("id" => "REITERATE", "content" => GetMessage("sender_mailing_chain_adm_field_reiterate"), "sort" => "REITERATE", "default" => false), array("id" => "AUTO_SEND_TIME", "content" => GetMessage("sender_mailing_chain_adm_field_auto_send_time"), "sort" => "AUTO_SEND_TIME", "default" => false))); while ($arRes = $rsData->NavNext(true, "f_")) { $row =& $lAdmin->AddRow($f_ID, $arRes); $row->AddViewField("NAME", '<a href="/bitrix/admin/sender_mailing_chain_edit.php?MAILING_ID=' . $MAILING_ID . '&ID=' . $f_ID . '&lang=' . LANG . '">' . $f_NAME . '</a>'); $arUser = \Bitrix\Main\UserTable::getRowById(intval($f_CREATED_BY)); $row->AddViewField("CREATED_BY", '<a href="/bitrix/admin/user_edit.php?lang=' . LANGUAGE_ID . '&ID=' . $f_CREATED_BY . '">' . htmlspecialcharsbx($arUser['NAME'] . " " . $arUser['LAST_NAME']) . "</a>"); $arStatus = MailingChainTable::getStatusList(); $statusPercent = ''; if (in_array($f_STATUS, array(MailingChainTable::STATUS_SEND, MailingChainTable::STATUS_PAUSE))) { $statusPercent = '<br>' . '<span style="font-size: 12px; color: #C2C2C2;">' . GetMessage("sender_mailing_chain_adm_action_stat") . ': ' . PostingTable::getSendPercent($f_POSTING_ID) . '%' . '</span>'; } $row->AddViewField("STATUS", $arStatus[$f_STATUS] . ' ' . $statusPercent); $row->AddViewField("REITERATE", $f_REITERATE == 'Y' ? GetMessage("MAIN_YES") : GetMessage("MAIN_NO")); $arActions = array(); $arActions[] = array("ICON" => "edit", "DEFAULT" => true, "TEXT" => GetMessage("sender_mailing_chain_adm_action_edit"), "ACTION" => $lAdmin->ActionRedirect("sender_mailing_chain_edit.php?MAILING_ID=" . $MAILING_ID . "&ID=" . $f_ID)); if ($POST_RIGHT >= "W") { $arActions[] = array("ICON" => "delete", "TEXT" => GetMessage("sender_mailing_chain_adm_action_delete"), "ACTION" => "if(confirm('" . GetMessage('sender_mailing_chain_adm_action_delete_confirm') . "')) " . $lAdmin->ActionDoGroup($f_ID, "delete", "MAILING_ID=" . $MAILING_ID)); } $arActions[] = array("SEPARATOR" => true); switch ($f_STATUS) { case MailingChainTable::STATUS_NEW: if ($POST_RIGHT >= "W") { $arActions[] = array("ICON" => "", "DEFAULT" => false, "TEXT" => GetMessage("sender_mailing_chain_adm_action_send"), "ACTION" => $lAdmin->ActionRedirect("/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=" . $MAILING_ID . "&ID=" . $f_ID . "&action=send&lang=" . LANGUAGE_ID)); } break;