56 $this->letter = $letter;
66 return in_array($this->
getCode(), array(self::NEWISH, self::INIT, self::READY));
76 return in_array($this->
getCode(), array(self::SENT));
86 return in_array($this->
getCode(), Semantics::getFinishStates());
96 return in_array($this->
getCode(), array(self::STOPPED));
106 return in_array($this->
getCode(), [self::WAITING]);
116 return in_array($this->
getCode(), [self::HALTED]);
126 return in_array($this->
getCode(), [self::SENDING]);
136 return in_array($this->
getCode(), array(self::PLANNED));
146 return in_array($this->
getCode(), [self::PAUSED]);
198 $message = $this->letter->getMessage();
199 return $message->getTransport()->isLimitsExceeded($message);
214 $message = $this->letter->getMessage();
215 $limiter = $message->getTransport()->getExceededLimiter($message);
222 return $limiter->getParameter(
'temporaryLimit') ===
true;
237 $message = $this->letter->getMessage();
238 return $message->getTransport()->getExceededLimiter($message) instanceof
TimeLimiter;
255 if (!$plannedDateSend)
261 return $plannedDateSend->getTimestamp() > $dateNow->getTimestamp();
271 return $this->letter->get(
'DATE_SEND');
281 return $this->letter->get(
'DATE_PAUSE');
291 return $this->letter->get(
'DATE_SENT');
301 return $this->letter->get(
'DATE_INSERT');
311 return $this->letter->get(
'AUTO_SEND_TIME');
321 return $this->letter->get(
'LAST_EXECUTED');
332 \CTimeZone::disable();
333 $result = Model\LetterTable::update($this->letter->getId(), array(
'AUTO_SEND_TIME' => $date));
334 \CTimeZone::enable();
335 if ($result->isSuccess())
337 $this->letter->set(
'AUTO_SEND_TIME', $date);
340 return $result->isSuccess();
382 if (!$this->letter->get(
'POSTING_ID'))
386 \CTimeZone::disable();
387 $result = Model\PostingTable::update(
388 $this->letter->get(
'POSTING_ID'),
393 \CTimeZone::enable();
395 return $result->isSuccess();
405 $map = self::getStatusMap();
406 $status = $this->letter->get(
'STATUS');
408 if (($status && isset($map[$status])))
410 return $map[$status];
430 return self::getStateName($this->
getCode());
446 $class = new \ReflectionClass(__CLASS__);
447 $constants = $class->getConstants();
450 foreach ($constants as $id => $value)
452 if (in_array($value, array(self::INIT)))
457 $list[$value] = self::getStateName($value);
472 return $this->changeState(self::PLANNED, $sendDate);
483 return $this->changeState(self::READY);
494 return $this->changeState(self::SENDING);
510 $postingId = $this->letter->get(
'POSTING_ID');
511 $updateSql =
'UPDATE ' . PostingRecipientTable::getTableName() .
512 " SET STATUS='" . PostingRecipientTable::SEND_RESULT_NONE .
"'" .
513 " WHERE POSTING_ID=" . intval($postingId) .
514 " AND STATUS='" . PostingRecipientTable::SEND_RESULT_ERROR .
"'";
516 Posting\Sender::updateActualStatus($this->letter->get(
'POSTING_ID'));
518 return $this->updateStatus(Model\LetterTable::STATUS_SEND, self::SENDING);
529 if ($this->changeState(self::PAUSED))
546 if ($this->changeState(self::HALTED))
563 if ($this->changeState(self::SENDING))
580 if ($this->changeState(self::STOPPED))
597 return $this->changeState(self::INIT);
621 return $this->changeState(self::WAITING, $method ? $method->getNextDate() :
null);
631 return $this->canChangeState(self::READY);
641 if ($this->
getCode() === self::PAUSED)
646 return $this->canChangeState(self::SENDING);
656 if (Integration\Bitrix24\Service::isCloud())
661 if ($this->letter->isTrigger() || $this->letter->isReiterate())
666 if (!$this->letter->isSupportHeatMap() || !$this->letter->get(
'POSTING_ID'))
676 $postingData = $this->letter->getLastPostingData();
677 return !empty($postingData[
'COUNT_SEND_ERROR']);
687 return $this->canChangeState(self::SENDING);
697 return $this->canChangeState(self::PAUSED);
707 return $this->canChangeState(self::STOPPED);
717 if ($this->
getCode() !== self::PAUSED)
722 return $this->canChangeState(self::SENDING);
732 return $this->canChangeState(self::NEWISH);
742 return $this->canChangeState(self::INIT);
752 return $this->canChangeState(self::WAITING);
762 return $this->canChangeState(self::HALTED);
839 private function canChangeState($state)
841 if (!$this->letter->getId())
846 return $this->isPossibleState($state);
857 private function changeState($state, Date $sendDate =
null)
859 if (!$this->isCampaignActive() && in_array($state, [self::SENDING, self::PLANNED]))
861 throw new InvalidOperationException(
Loc::getMessage(
'SENDER_DISPATCH_STATE_ERROR_CAMPAIGN_INACTIVE'));
864 if (!$this->canChangeState($state))
866 $messageText =
Loc::getMessage(
'SENDER_DISPATCH_STATE_ERROR_CHANGE', array(
867 '%old%' => $this->getName(),
868 '%new%' => self::getStateName($state)
871 throw new InvalidOperationException($messageText);
874 $map = self::getStateMap();
877 return $this->updateStatus($map[$state], $state, $sendDate);
889 private function isPossibleState($state)
894 if (!$this->letter->isSupportReiterate() && !$this->letter->isTrigger())
896 $possibleStates = array_filter($possibleStates,
function ($value)
898 return !in_array($value, [self::WAITING, self::HALTED]);
902 return in_array($state, $possibleStates);
913 private function updateStatus($status, $state, Date $sendDate =
null)
915 $fields = array(
'STATUS' => $status);
916 if ($state === self::READY && $this->letter->get(
'AUTO_SEND_TIME'))
918 $fields[
'AUTO_SEND_TIME'] =
null;
920 if ($state === self::SENDING)
922 $fields[
'AUTO_SEND_TIME'] = $sendDate ?:
new DateTime();
924 if ($state === self::PLANNED)
926 $fields[
'AUTO_SEND_TIME'] = $sendDate ?:
new DateTime();
928 if ($state === self::WAITING && $sendDate)
930 $fields[
'AUTO_SEND_TIME'] = $sendDate;
932 if ($updatedBy = $this->letter->get(
'UPDATED_BY'))
934 $fields[
'UPDATED_BY'] = $updatedBy;
936 \CTimeZone::disable();
937 $result = Model\LetterTable::update($this->letter->getId(), $fields);
938 \CTimeZone::enable();
940 if ($result->isSuccess())
942 $this->letter->set(
'STATUS', $status);
943 if (isset($fields[
'AUTO_SEND_TIME']))
945 $this->letter->set(
'AUTO_SEND_TIME', $fields[
'AUTO_SEND_TIME']);
950 $this->letter->getErrorCollection()->add($result->getErrors());
953 return $result->isSuccess();
961 private static function getStateMap()
963 $map = array_flip(self::getStatusMap());
964 $map[self::INIT] = Model\LetterTable::STATUS_NEW;
974 private static function getStatusMap()
977 Model\LetterTable::STATUS_NEW => self::NEWISH,
978 Model\LetterTable::STATUS_PLAN => self::PLANNED,
979 Model\LetterTable::STATUS_READY => self::READY,
980 Model\LetterTable::STATUS_SEND => self::SENDING,
981 Model\LetterTable::STATUS_WAIT => self::WAITING,
982 Model\LetterTable::STATUS_HALT => self::HALTED,
983 Model\LetterTable::STATUS_PAUSE => self::PAUSED,
984 Model\LetterTable::STATUS_END => self::SENT,
985 Model\LetterTable::STATUS_CANCEL => self::STOPPED,
989 private function isCampaignActive()
991 return $this->letter->get(
'CAMPAIGN_ACTIVE',
'Y') ===
'Y';
static getConnection($name="")
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)