8namespace Bitrix\Sender;
11use Bitrix\Main\DB\Exception;
13use Bitrix\Main\EventResult;
15use Bitrix\Sender\Dispatch\MethodSchedule;
16use Bitrix\Sender\Entity;
17use Bitrix\Sender\Internals\Model;
29 return static::$error;
57 public static function actualizeAgent($mailingId =
null, $mailingChainId =
null)
60 ->withCampaignId($mailingId)
61 ->withLetterId($mailingChainId)
69 $event =
new Main\Event(
'sender',
'onBeforeChainSend', [
'LETTER_ID' => $letterId]);
71 foreach (
$event->getResults() as $eventResult)
74 $eventResult->getType() ===
Main\EventResult::ERROR
75 || $eventResult->getParameters()
76 && isset($eventResult->getParameters()[
'ALLOW_SEND'])
77 && $eventResult->getParameters()[
'ALLOW_SEND'] ===
false
96 public static function chainSend($letterId)
98 static::$error =
null;
100 $letter = Model\LetterTable::getRowById($letterId);
101 if($letter && $letter[
'STATUS'] ===
Model\LetterTable::STATUS_PLAN)
103 $updateResult = Model\LetterTable::update($letterId,
array(
'STATUS' =>
Model\LetterTable::STATUS_SEND));
104 if ($updateResult->isSuccess())
106 $letter = Model\LetterTable::getRowById($letterId);
109 if(!$letter || !in_array($letter[
'STATUS'], [
110 Model\LetterTable::STATUS_SEND
116 if(!static::checkOnBeforeChainSend($letterId))
118 return Runtime\SenderJob::getAgentName($letterId);
121 $postingSendStatus =
'';
122 if(!empty($letter[
'POSTING_ID']))
127 $letter[
'POSTING_ID'],
128 Runtime\Env::getJobExecutionTimeout(),
129 Runtime\Env::getJobExecutionItemLimit()
132 catch (\Throwable $e)
144 return Runtime\SenderJob::getAgentName($letterId);
150 Model\LetterTable::update($letterId,
array(
'STATUS' => Model\LetterTable::STATUS_WAIT));
160 return Runtime\SenderJob::getAgentName($letterId);
163 if ($letter[
'REITERATE'] !==
'Y')
165 Model\LetterTable::update($letterId,
array(
'STATUS' => Model\LetterTable::STATUS_END));
169 $isNeedUpdate =
true;
170 if($letter[
'IS_TRIGGER'] ==
'Y')
173 'select' =>
array(
'ID',
'DATE_CREATE'),
176 'MAILING_CHAIN_ID' => $letter[
'ID']
178 'order' =>
array(
'DATE_CREATE' =>
'ASC'),
181 if($posting = $postingDb->fetch())
183 $dateCreate = $posting[
'DATE_CREATE'];
186 'STATUS' => Model\LetterTable::STATUS_SEND,
187 'AUTO_SEND_TIME' => $dateCreate ? $dateCreate->add($letter[
'TIME_SHIFT'].
' minutes') :
null,
188 'POSTING_ID' => $posting[
'ID']
190 Model\LetterTable::update($letterId, $updateFields);
191 $isNeedUpdate =
false;
197 $letterInstance =
new Entity\Letter();
198 $letterInstance->loadByArray($letter);
199 $letterInstance->wait();
203 'MAILING_CHAIN' => $letter
205 $event = new \Bitrix\Main\Event(
'sender',
'OnAfterMailingChainSend',
array($eventData));
219 if(\COption::GetOptionString(
"sender",
"auto_method") !==
'cron')
223 'select' =>
array(
'ID'),
229 '=MAILING.ACTIVE' =>
'Y',
234 while ($mailingChain = $mailingChainDb->fetch())
236 $event =
new Event(
'sender',
'onBeforeMailingChainSend', [
237 'chain' => $mailingChain,
240 foreach (
$event->getResults() as $eventResult)
242 if ($eventResult->getType() === EventResult::ERROR)
248 static::chainSend($mailingChain[
'ID']);
262 if(($isAgentExec && !$isAgentExecInSetting) || (!$isAgentExec && $isAgentExecInSetting))
267 $dateTodayPhp = new \DateTime();
269 $dateToday = clone $dateTodayPhp;
271 $dateTomorrow = clone $dateTodayPhp;
273 $arDateFilter =
array($dateToday, $dateTomorrow);
277 'ID',
'LAST_EXECUTED',
'POSTING_ID',
278 'MONTHS_OF_YEAR',
'DAYS_OF_MONTH',
'DAYS_OF_WEEK',
'TIMES_OF_DAY'
282 '=MAILING.ACTIVE' =>
'Y',
283 '=IS_TRIGGER' =>
'N',
288 while($arMailingChain = $chainDb->fetch())
290 $lastExecuted = $arMailingChain[
'LAST_EXECUTED'];
292 if($lastExecuted && $lastExecuted->getTimestamp() >= $dateToday->getTimestamp())
297 $event =
new Event(
'sender',
'onBeforeMailingPeriodChainSend', [
298 'chain' => $arMailingChain,
301 foreach (
$event->getResults() as $eventResult)
303 if ($eventResult->getType() === EventResult::ERROR)
309 $timeOfExecute = static::getDateExecute(
311 $arMailingChain[
"DAYS_OF_MONTH"],
312 $arMailingChain[
"DAYS_OF_WEEK"],
313 $arMailingChain[
"TIMES_OF_DAY"],
314 $arMailingChain[
"MONTHS_OF_YEAR"]
319 $arUpdateMailChain =
array(
'LAST_EXECUTED' => $datetimeToday);
322 'select' =>
array(
'ID'),
324 '=MAILING_CHAIN_ID' => $arMailingChain[
'ID'],
325 '><DATE_CREATE' => $arDateFilter
328 $arPosting = $postingDb->fetch();
335 $postingId = $arPosting[
'ID'];
336 $arUpdateMailChain[
'POSTING_ID'] = $postingId;
346 $result = Model\LetterTable::update($arMailingChain[
'ID'], $arUpdateMailChain);
368 ?
string $daysOfMonth =
'',
369 ?
string $dayOfWeek =
'',
370 ?
string $timesOfDay =
'',
371 ?
string $monthsOfYear =
''
374 $timeOfExecute =
null;
382 $arTime =
array(0,0);
384 $day = $date->
format(
'j');
385 $week = $date->
format(
'N');
386 $month = $date->
format(
'n');
388 if( (!$arDay || in_array($day, $arDay)) && (!$arWeek || in_array($week, $arWeek)) && (!$months || in_array($month, $months)) )
389 $timeOfExecute = $date->
setTime($arTime[0], $arTime[1]);
391 return $timeOfExecute;
static getList(array $parameters=array())
static createFromPhp(\DateTime $datetime)
setTime($hour, $minute, $second=0, $microseconds=0)
static createFromPhp(\DateTime $datetime)
static parseMonthsOfYear($monthsOfYear)
static parseDaysOfMonth($daysOfMonth)
static parseTimesOfDay($time)
static parseDaysOfWeek($daysOfWeek)
static initPosting(?int $mailingChainId)
static getDateExecute(\DateTime $date, ?string $daysOfMonth='', ?string $dayOfWeek='', ?string $timesOfDay='', ?string $monthsOfYear='')
static checkOnBeforeChainSend($letterId)
static checkPeriod($isAgentExec=true)
static getAgentName($mailingChainId)
static actualizeAgent($mailingId=null, $mailingChainId=null)
static getAgentNamePeriod()
static send($id, $timeout=0, $maxMailCount=0)
const SEND_RESULT_CONTINUE
const SEND_RESULT_WAITING_RECIPIENT
static hasUnprocessed($postingId, $threadId=null)
static initGroupRecipients($postingId, $checkDuplicate=true)
static isReiteratedJobCron()
static getAgentName($letterId, $threadId=false)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)