8namespace Bitrix\Sender\Entity;
10use Bitrix\Main\Application;
11use Bitrix\Main\ArgumentException;
14use Bitrix\Main\InvalidOperationException;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Main\Result;
17use Bitrix\Main\Type\Date;
18use Bitrix\Main\Type\DateTime;
19use Bitrix\Sender\Dispatch;
20use Bitrix\Sender\FileTable;
21use Bitrix\Sender\Integration;
22use Bitrix\Sender\Internals\Model\LetterSegmentTable;
23use Bitrix\Sender\Internals\Model\LetterTable;
24use Bitrix\Sender\Internals\Model\MessageFieldTable;
25use Bitrix\Sender\Internals\SqlBatch;
26use Bitrix\Sender\Message as MainMessage;
27use Bitrix\Sender\Posting;
28use Bitrix\Sender\Recipient;
29use Bitrix\Sender\Security;
30use Bitrix\Sender\Templates;
31use Bitrix\Sender\TemplateTable;
33Loc::loadMessages(__FILE__);
82 return LetterTable::getEntity()->getDataClass();
93 if (!isset($parameters[
'select']))
95 $parameters[
'select'] = static::getDefaultSelectFields();
97 if (!isset($parameters[
'filter']))
99 $parameters[
'filter'] =
array();
102 foreach (static::getFilterFields() as $field)
104 if (!$field[
'FILTER'])
109 if (isset($parameters[
'filter'][$field[
'FILTER']]))
111 $current = $parameters[
'filter'][$field[
'FILTER']];
112 if (is_array($field[
'VALUE']))
114 if (!is_array($current) && in_array($current, $field[
'VALUE']))
121 $parameters[
'filter'][$field[
'FILTER']] = $field[
'VALUE'];
124 return LetterTable::getList($parameters);
131 'SITE_ID' =>
'CAMPAIGN.SITE_ID',
132 'CAMPAIGN_ACTIVE' =>
'CAMPAIGN.ACTIVE',
134 'DATE_SEND' =>
'CURRENT_POSTING.DATE_SEND',
135 'DATE_PAUSE' =>
'CURRENT_POSTING.DATE_PAUSE',
136 'DATE_SENT' =>
'CURRENT_POSTING.DATE_SENT',
138 'COUNT_SEND_ALL' =>
'CURRENT_POSTING.COUNT_SEND_ALL',
139 'COUNT_SEND_NONE' =>
'CURRENT_POSTING.COUNT_SEND_NONE',
140 'COUNT_SEND_ERROR' =>
'CURRENT_POSTING.COUNT_SEND_ERROR',
141 'COUNT_SEND_SUCCESS' =>
'CURRENT_POSTING.COUNT_SEND_SUCCESS',
142 'COUNT_SEND_DENY' =>
'CURRENT_POSTING.COUNT_SEND_DENY',
144 'USER_NAME' =>
'CREATED_BY_USER.NAME',
145 'USER_LAST_NAME' =>
'CREATED_BY_USER.LAST_NAME',
146 'USER_ID' =>
'CREATED_BY',
161 $list = static::getList($parameters);
162 while ($item = $list->fetch())
165 if ($item[
'MESSAGE_ID'])
167 $messageIds[] = $item[
'MESSAGE_ID'];
173 $rows = MessageFieldTable::getList([
'filter' => [
'=MESSAGE_ID' => $messageIds]]);
174 while ($messageField =
$rows->fetch())
203 $row = LetterTable::getRow([
204 'select' => [
'MESSAGE_CODE'],
205 'filter' => [
'=ID' => $id],
209 $code = $row[
'MESSAGE_CODE'];
259 $row = LetterTable::getList(
array(
260 'select' =>
array(
'ID',
'IS_ADS'),
261 'filter' =>
array(
'=POSTING_ID' => $postingId),
269 if ($row[
'IS_ADS'] ===
'Y')
271 return new Ad($row[
'ID']);
275 return new static($row[
'ID']);
306 if (!
$code && empty($messageCodes))
313 $code = current($messageCodes);
316 if (empty($messageCodes))
318 $messageCodes = [
$code];
321 if (!in_array(
$code, $messageCodes))
365 'SEGMENTS_INCLUDE' =>
array(),
366 'SEGMENTS_EXCLUDE' =>
array(),
381 $this->addError(Loc::getMessage(
'SENDER_ENTITY_LETTER_ERROR_NOT_AVAILABLE'));
385 $segmentsInclude =
$data[
'SEGMENTS_INCLUDE'];
386 $segmentsExclude =
$data[
'SEGMENTS_EXCLUDE'];
388 foreach (static::getFilterFields() as $field)
395 if (is_array($field[
'VALUE']))
397 if (empty(
$data[$field[
'CODE']]) || !in_array(
$data[$field[
'CODE']], $field[
'VALUE']))
399 $data[$field[
'CODE']] = current($field[
'VALUE']);
404 $data[$field[
'CODE']] = $field[
'VALUE'];
407 $this->filterDataByEntityFields(LetterTable::getEntity(),
$data);
410 $previousData = $id ? LetterTable::getRowById($id) :
null;
411 $previousData = $previousData ?:
array();
414 if(!is_array($segmentsInclude) ||
count($segmentsInclude) == 0)
418 isset(
$data[
'NOT_USE_SEGMENTS'])
419 && !
$data[
'NOT_USE_SEGMENTS']
421 &&
$data[
'IS_TRIGGER'] <>
'Y'
422 && $previousData[
'IS_TRIGGER'] <>
'Y'
425 $this->addError(Loc::getMessage(
'SENDER_ENTITY_LETTER_ERROR_NO_SEGMENTS'));
429 $segmentsExclude = is_array($segmentsExclude) ? $segmentsExclude :
array();
432 if (!isset(
$data[
'CAMPAIGN_ID']))
435 $this->
set(
'CAMPAIGN_ID', $data[
'CAMPAIGN_ID']);
439 if (!$id &&
$data[
'IS_TRIGGER'] ===
'Y')
441 if (empty(
$data[
'PARENT_ID']))
443 $previousLetter = (
new Chain)->load(
$data[
'CAMPAIGN_ID'])->getLast();
444 if ($previousLetter && $previousLetter->getId() != $this->getId())
446 $data[
'PARENT_ID'] = $previousLetter->getId();
450 if (!isset(
$data[
'TIME_SHIFT']))
452 $data[
'TIME_SHIFT'] = 1440;
456 $data[
'REITERATE'] =
'Y';
460 if ($this->filterDataByChanging(
$data, $previousData))
462 $id = $this->saveByEntity(LetterTable::getEntity(), $id,
$data);
470 $this->saveByEntity(LetterTable::getEntity(), $id,
$data);
484 $this->setId($id)->getState()->init();
492 $content = $this->getSearchBuilder()->getContent();
493 $content->addUserById($this->
get(
'CREATED_BY'));
494 $content->addText($this->
get(
'TITLE'));
507 case $option::TYPE_EMAIL:
511 case $option::TYPE_HTML:
512 case $option::TYPE_MAIL_EDITOR:
516 case $option::TYPE_TEXT:
517 case $option::TYPE_STRING:
518 case $option::TYPE_PRESET_STRING:
519 case $option::TYPE_SMS_EDITOR:
530 $segmentsExclude = array_unique($segmentsExclude);
531 $segmentsInclude = array_unique($segmentsInclude);
532 $segmentsInclude = array_diff($segmentsInclude, $segmentsExclude);
534 $segmentsList =
array(
536 'list' => $segmentsExclude,
540 'list' => $segmentsInclude,
546 $letter = LetterTable::getById($id)->fetch();
547 LetterSegmentTable::deleteList(
array(
'LETTER_ID' => $id));
551 foreach ($segmentsList as $segments)
553 if(empty($segments[
'list']))
558 $typeCode = $segments[
'include'] ?
'INCLUDE' :
'EXCLUDE';
560 foreach ($segments[
'list'] as $segment)
562 $list[] = [
'DATE_UPDATE' => $letter[
'DATE_UPDATE'],
'ID' => $segment];
565 'GROUP_ID' => $segment,
566 'INCLUDE' => $segments[
'include'],
570 $newest = self::getArrayDiffNewest($list, $oldSegments[$typeCode]);
571 $removed = self::getArrayDiffRemoved($list, $oldSegments[$typeCode]);
573 if (
count($newest) === 0 &&
count($removed) === 0)
578 if (
count($newest) > 0)
585 if (!empty($dataToInsert))
587 SqlBatch::insert(LetterSegmentTable::getTableName(), $dataToInsert);
590 if ($isChanged && $this->
getId() && $this->
get(
'POSTING_ID'))
593 ->run($this->
get(
'POSTING_ID'),
false);
597 private static function getArrayDiffNewest(
array $current,
array $old)
599 return array_udiff($current, $old,
function(
$first, $second)
601 return $first[
'DATE_UPDATE'] < $second[
'DATE_UPDATE'] ||
$first[
'ID'] != $second[
'ID'];
605 private static function getArrayDiffRemoved(
array $current,
array $old)
607 return self::getArrayDiffNewest($old, $current);
612 if (!isset(
$data[
'TEMPLATE_TYPE']) || !isset(
$data[
'TEMPLATE_ID']))
622 isset($previousData[
'TEMPLATE_ID'])
623 &&
$data[
'TEMPLATE_ID'] === $previousData[
'TEMPLATE_ID']
624 &&
$data[
'TEMPLATE_TYPE'] === $previousData[
'TEMPLATE_TYPE']
646 if (!is_array(
$data))
652 foreach ($segments as $typeCode => $list)
654 foreach($list as $item)
656 $data[
"SEGMENTS_$typeCode"][] = $item[
'ID'];
671 $this->
getState()->wasStartedSending()
686 return !$this->
getState()->wasPostingBuilt();
698 $segments = LetterSegmentTable::getList(
array(
699 'select' => [
'INCLUDE',
'LETTER_ID',
'SEGMENT_ID',
'DATE_UPDATE' =>
'SEGMENT.DATE_UPDATE'],
704 foreach($segments as $segment)
706 if ($segment[
'INCLUDE'])
709 [
'ID' => $segment[
'SEGMENT_ID'],
'DATE_UPDATE' => $segment[
'DATE_UPDATE']];
714 [
'ID' => $segment[
'SEGMENT_ID'],
'DATE_UPDATE' => $segment[
'DATE_UPDATE']];
728 if ($this->
getState()->isFinished())
746 $messageId = $this->
get(
'MESSAGE_ID') ?:
null;
749 if (isset($this->data[
'MESSAGE_FIELDS']) && $this->data[
'MESSAGE_FIELDS'])
751 foreach ($this->data[
'MESSAGE_FIELDS'] as $field)
756 if ($this->messagesCache && isset($this->messagesCache[$messageCode]))
758 $this->message = $this->messagesCache[$messageCode];
768 $this->message->getConfiguration()->set(
'LETTER_CREATED_BY_ID', $createdById);
769 $this->message->setSiteId($this->
get(
'SITE_ID'));
774 $this->message->loadConfiguration(
$messageId);
813 return $this->
get(
'CAMPAIGN_ID');
901 return $this->
get(
'REITERATE') ===
'Y';
911 return $this->
get(
'IS_TRIGGER') ===
'Y';
929 public function remove()
931 return $this->removeByEntity(LetterTable::getEntity(), $this->
getId());
942 return static::create()->removeByEntity(LetterTable::getEntity(), $id);
952 $configurationId = $this->
getMessage()->getConfiguration()->getId();
953 if (!$configurationId)
965 'CAMPAIGN_ID' => $this->
get(
'CAMPAIGN_ID'),
966 'MESSAGE_CODE' => $this->
get(
'MESSAGE_CODE'),
967 'MESSAGE_ID' =>
$result->getId(),
968 'REITERATE' => $this->get(
'REITERATE'),
969 'TEMPLATE_TYPE' => $this->get(
'TEMPLATE_TYPE'),
970 'TEMPLATE_ID' => $this->get(
'TEMPLATE_ID'),
971 'CREATED_BY' => $this->getUser()->getId(),
972 'UPDATED_BY' => $this->getUser()->getId(),
973 'IS_TRIGGER' => $this->get(
'IS_TRIGGER'),
974 'TITLE' => Loc::getMessage(
'SENDER_ENTITY_LETTER_COPY_PREFIX') .
' ' . $this->get(
'TITLE'),
975 'SEGMENTS_INCLUDE' => $this->get(
'SEGMENTS_INCLUDE'),
976 'SEGMENTS_EXCLUDE' => $this->get(
'SEGMENTS_EXCLUDE'),
980 $this->getErrorCollection()->add(
$instance->getErrors());
983 !is_null($this->
getMessage()->getConfiguration()->
get(
'MESSAGE'))
990 $this->getMessage()->getConfiguration()->get(
'MESSAGE')
1006 return $this->
getTester()->send($codes, $parameters);
1019 return $this->
getState()->plan($date);
1023 $this->errors->setError(
new Error($exception->getMessage()));
1043 $this->errors->setError(
new Error($exception->getMessage()));
1050 $this->errors->setError(
new Error(
'Entity is not reiterate.'));
1061 ->setMonthsOfYear($scheduleMonths)
1062 ->setDaysOfMonth($scheduleMonthDays)
1063 ->setDaysOfWeek($scheduleWeekDays)
1064 ->setTime($scheduleTime[0], $scheduleTime[1]);
1070 $this->errors->setError(
new Error($exception->getMessage()));
1088 $this->errors->setError(
new Error($exception->getMessage()));
1102 return $this->
getState()->sendErrors();
1106 $this->errors->setError(
new Error($exception->getMessage()));
1124 $this->errors->setError(
new Error($exception->getMessage()));
1142 $this->errors->setError(
new Error($exception->getMessage()));
1156 return $this->
getState()->resume();
1160 $this->errors->setError(
new Error($exception->getMessage()));
1178 $this->errors->setError(
new Error($exception->getMessage()));
1190 $defaults =
array();
1192 if (!$this->
getId())
1197 if ($this->postingData !==
null)
1202 $this->postingData = $defaults;
1203 $postingFilter =
array(
1204 '=ID' => $this->
getId(),
1208 $postings = static::getList(
array(
1211 'LETTER_ID' =>
'ID',
1214 'MAILING_NAME' =>
'CAMPAIGN.NAME',
1215 'DATE_SENT' =>
'POSTING.DATE_SENT',
1216 'COUNT_SEND_ERROR' =>
'POSTING.COUNT_SEND_ERROR',
1217 'CREATED_BY' =>
'CREATED_BY',
1218 'CREATED_BY_NAME' =>
'CREATED_BY_USER.NAME',
1219 'CREATED_BY_LAST_NAME' =>
'CREATED_BY_USER.LAST_NAME',
1220 'CREATED_BY_SECOND_NAME' =>
'CREATED_BY_USER.SECOND_NAME',
1221 'CREATED_BY_LOGIN' =>
'CREATED_BY_USER.LOGIN',
1222 'CREATED_BY_TITLE' =>
'CREATED_BY_USER.TITLE',
1224 'filter' => $postingFilter,
1226 'order' =>
array(
'POSTING.DATE_SENT' =>
'DESC',
'POSTING.DATE_CREATE' =>
'DESC'),
if(isset( $_REQUEST["mode"]) &&$_REQUEST["mode"]=="ajax") if(isset($_REQUEST["mode"]) && $_REQUEST["mode"]=="save_lru" &&check_bitrix_sessid()) $first
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
static parseMonthsOfYear($monthsOfYear)
static parseDaysOfMonth($daysOfMonth)
static parseTimesOfDay($time)
static parseDaysOfWeek($daysOfWeek)
static getDefaultId($siteId=null)
static createInstanceByArray(array $data, array $messageCodes=[])
static getList(array $parameters=array())
test(array $codes, array $parameters=array())
static createInstanceByPostingId($postingId)
static getDefaultSelectFields()
saveDataSegments($id, array $segmentsInclude, array $segmentsExclude)
static createInstanceByContactId($contactId, array $messageCodes=[])
updateTemplateUseCount(array $data, array $previousData)
static createInstanceByCode($code=null, array $messageCodes=[])
static createInstanceById($id=null, array $messageCodes=[])
static getListWithMessageFields(array $parameters=array())
saveData($id, array $data)
static updateUseCounters(array $list, $isInclude=true)
static syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles=true, bool $onDeleteEntity=false)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"