Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailingmanager.php
1<?php
8namespace Bitrix\Sender;
9
10use Bitrix\Main;
18
20{
21 /* @var Exception $error */
22 protected static $error = null;
23
27 public static function getErrors()
28 {
29 return static::$error;
30 }
31
36 public static function getAgentNamePeriod()
37 {
38 return Runtime\ReiteratedJob::getAgentName();
39 }
40
46 public static function getAgentName($mailingChainId)
47 {
48 return Runtime\SenderJob::getAgentName($mailingChainId);
49 }
50
57 public static function actualizeAgent($mailingId = null, $mailingChainId = null)
58 {
59 (new Runtime\SenderJob())
60 ->withCampaignId($mailingId)
61 ->withLetterId($mailingChainId)
62 ->actualize();
63
64 (new Runtime\ReiteratedJob())->actualize();
65 }
66
67 protected static function checkOnBeforeChainSend($letterId)
68 {
69 $event = new Main\Event('sender', 'onBeforeChainSend', ['LETTER_ID' => $letterId]);
70 $event->send();
71 foreach ($event->getResults() as $eventResult)
72 {
73 if (
74 $eventResult->getType() === Main\EventResult::ERROR
75 || $eventResult->getParameters()
76 && isset($eventResult->getParameters()['ALLOW_SEND'])
77 && $eventResult->getParameters()['ALLOW_SEND'] === false
78 )
79 {
80 return false;
81 }
82 }
83
84 return true;
85 }
96 public static function chainSend($letterId)
97 {
98 static::$error = null;
99
100 $letter = Model\LetterTable::getRowById($letterId);
101 if($letter && $letter['STATUS'] === Model\LetterTable::STATUS_PLAN)
102 {
103 $updateResult = Model\LetterTable::update($letterId, array('STATUS' => Model\LetterTable::STATUS_SEND));
104 if ($updateResult->isSuccess())
105 {
106 $letter = Model\LetterTable::getRowById($letterId);
107 }
108 }
109 if(!$letter || !in_array($letter['STATUS'], [
110 Model\LetterTable::STATUS_SEND
111 ]))
112 {
113 return "";
114 }
115
116 if(!static::checkOnBeforeChainSend($letterId))
117 {
118 return Runtime\SenderJob::getAgentName($letterId);
119 }
120
121 $postingSendStatus = '';
122 if(!empty($letter['POSTING_ID']))
123 {
124 try
125 {
126 $postingSendStatus = PostingManager::send(
127 $letter['POSTING_ID'],
128 Runtime\Env::getJobExecutionTimeout(),
129 Runtime\Env::getJobExecutionItemLimit()
130 );
131 }
132 catch (Exception $e)
133 {
134 static::$error = $e;
135 AddMessage2Log('Exception in mailing send. PostingId: ' . $letter['POSTING_ID'],'sender');
136 $postingSendStatus = PostingManager::SEND_RESULT_ERROR;
137 }
138 }
139
140 if(
142 )
143 {
144 return Runtime\SenderJob::getAgentName($letterId);
145 }
146
147
148 if ($postingSendStatus === PostingManager::SEND_RESULT_WAIT)
149 {
150 Model\LetterTable::update($letterId, array('STATUS' => Model\LetterTable::STATUS_WAIT));
151 return "";
152 }
153
154
155 if (
157 || \Bitrix\Sender\PostingRecipientTable::hasUnprocessed($letter['POSTING_ID'])
158 )
159 {
160 return Runtime\SenderJob::getAgentName($letterId);
161 }
162
163 if ($letter['REITERATE'] !== 'Y')
164 {
165 Model\LetterTable::update($letterId, array('STATUS' => Model\LetterTable::STATUS_END));
166 return "";
167 }
168
169 $isNeedUpdate = true;
170 if($letter['IS_TRIGGER'] == 'Y')
171 {
172 $postingDb = PostingTable::getList(array(
173 'select' => array('ID', 'DATE_CREATE'),
174 'filter' => array(
175 'STATUS' => PostingTable::STATUS_NEW,
176 'MAILING_CHAIN_ID' => $letter['ID']
177 ),
178 'order' => array('DATE_CREATE' => 'ASC'),
179 'limit' => 1
180 ));
181 if($posting = $postingDb->fetch())
182 {
183 $dateCreate = $posting['DATE_CREATE'];
185 $updateFields = [
186 'STATUS' => Model\LetterTable::STATUS_SEND,
187 'AUTO_SEND_TIME' => $dateCreate ? $dateCreate->add($letter['TIME_SHIFT'].' minutes') : null,
188 'POSTING_ID' => $posting['ID']
189 ];
190 Model\LetterTable::update($letterId, $updateFields);
191 $isNeedUpdate = false;
192 }
193 }
194
195 if ($isNeedUpdate)
196 {
197 $letterInstance = new Entity\Letter();
198 $letterInstance->loadByArray($letter);
199 $letterInstance->wait();
200 }
201
202 $eventData = array(
203 'MAILING_CHAIN' => $letter
204 );
205 $event = new \Bitrix\Main\Event('sender', 'OnAfterMailingChainSend', array($eventData));
206 $event->send();
207
208 return "";
209 }
210
217 public static function checkSend()
218 {
219 if(\COption::GetOptionString("sender", "auto_method") !== 'cron')
220 return;
221
222 $mailingChainDb = MailingChainTable::getList(array(
223 'select' => array('ID'),
224 'filter' => array(
225 '=STATUS' => array(
228 ),
229 '=MAILING.ACTIVE' => 'Y',
230 '<=AUTO_SEND_TIME' => new Type\DateTime(),
231 )
232 ));
233
234 while ($mailingChain = $mailingChainDb->fetch())
235 {
236 $event = new Event('sender', 'onBeforeMailingChainSend', [
237 'chain' => $mailingChain,
238 ]);
239 $event->send();
240 foreach ($event->getResults() as $eventResult)
241 {
242 if ($eventResult->getType() === EventResult::ERROR)
243 {
244 continue 2;
245 }
246 }
247
248 static::chainSend($mailingChain['ID']);
249 }
250 }
251
259 public static function checkPeriod($isAgentExec = true)
260 {
261 $isAgentExecInSetting = !Runtime\Env::isReiteratedJobCron();
262 if(($isAgentExec && !$isAgentExecInSetting) || (!$isAgentExec && $isAgentExecInSetting))
263 {
264 return "";
265 }
266
267 $dateTodayPhp = new \DateTime();
268 $datetimeToday = Type\DateTime::createFromPhp(clone $dateTodayPhp);
269 $dateToday = clone $dateTodayPhp;
270 $dateToday = Type\Date::createFromPhp($dateToday->setTime(0,0,0));
271 $dateTomorrow = clone $dateTodayPhp;
272 $dateTomorrow = Type\Date::createFromPhp($dateTomorrow->setTime(0,0,0))->add('1 DAY');
273 $arDateFilter = array($dateToday, $dateTomorrow);
274
275 $chainDb = MailingChainTable::getList(array(
276 'select' => array(
277 'ID', 'LAST_EXECUTED', 'POSTING_ID',
278 'MONTHS_OF_YEAR', 'DAYS_OF_MONTH', 'DAYS_OF_WEEK', 'TIMES_OF_DAY'
279 ),
280 'filter' => array(
281 '=REITERATE' => 'Y',
282 '=MAILING.ACTIVE' => 'Y',
283 '=IS_TRIGGER' => 'N',
285 //'!><LAST_EXECUTED' => $arDateFilter,
286 )
287 ));
288 while($arMailingChain = $chainDb->fetch())
289 {
290 $lastExecuted = $arMailingChain['LAST_EXECUTED'];
291 /* @var \Bitrix\Main\Type\DateTime $lastExecuted*/
292 if($lastExecuted && $lastExecuted->getTimestamp() >= $dateToday->getTimestamp())
293 {
294 continue;
295 }
296
297 $event = new Event('sender', 'onBeforeMailingPeriodChainSend', [
298 'chain' => $arMailingChain,
299 ]);
300 $event->send();
301 foreach ($event->getResults() as $eventResult)
302 {
303 if ($eventResult->getType() === EventResult::ERROR)
304 {
305 continue 2;
306 }
307 }
308
309 $timeOfExecute = static::getDateExecute(
310 $dateTodayPhp,
311 $arMailingChain["DAYS_OF_MONTH"],
312 $arMailingChain["DAYS_OF_WEEK"],
313 $arMailingChain["TIMES_OF_DAY"],
314 $arMailingChain["MONTHS_OF_YEAR"]
315 );
316
317 if($timeOfExecute)
318 {
319 $arUpdateMailChain = array('LAST_EXECUTED' => $datetimeToday);
320
321 $postingDb = PostingTable::getList(array(
322 'select' => array('ID'),
323 'filter' => array(
324 '=MAILING_CHAIN_ID' => $arMailingChain['ID'],
325 '><DATE_CREATE' => $arDateFilter
326 )
327 ));
328 $arPosting = $postingDb->fetch();
329 if(!$arPosting)
330 {
331 $postingId = MailingChainTable::initPosting($arMailingChain['ID']);
332 }
333 else
334 {
335 $postingId = $arPosting['ID'];
336 $arUpdateMailChain['POSTING_ID'] = $postingId;
338 }
339
340 if ($postingId)
341 {
342 $arUpdateMailChain['STATUS'] = MailingChainTable::STATUS_SEND;
343 $arUpdateMailChain['AUTO_SEND_TIME'] = Type\DateTime::createFromPhp($timeOfExecute);
344 }
345
346 $result = Model\LetterTable::update($arMailingChain['ID'], $arUpdateMailChain);
347 if (!$result->isSuccess())
348 {
349 return "";
350 }
351 }
352 }
353
354 (new Runtime\ReiteratedJob())->actualize();
355 return '';
356 }
357
366 protected static function getDateExecute(
367 \DateTime $date,
368 ?string $daysOfMonth = '',
369 ?string $dayOfWeek = '',
370 ?string $timesOfDay = '',
371 ?string $monthsOfYear = ''
372 )
373 {
374 $timeOfExecute = null;
375
376 $months = MethodSchedule::parseMonthsOfYear($monthsOfYear);
377 $arDay = MethodSchedule::parseDaysOfMonth($daysOfMonth);
378 $arWeek = MethodSchedule::parseDaysOfWeek($dayOfWeek);
379 $arTime = MethodSchedule::parseTimesOfDay($timesOfDay);
380
381 if(!$arTime)
382 $arTime = array(0,0);
383
384 $day = $date->format('j');
385 $week = $date->format('N');
386 $month = $date->format('n');
387
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]);
390
391 return $timeOfExecute;
392 }
393}
setTime($hour, $minute, $second=0, $microseconds=0)
Definition datetime.php:144
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 send($id, $timeout=0, $maxMailCount=0)
static hasUnprocessed($postingId, $threadId=null)
Definition posting.php:815
static initGroupRecipients($postingId, $checkDuplicate=true)
Definition posting.php:239