89 public static function getList(array $parameters = array())
91 if (!isset($parameters[
'select']))
93 $parameters[
'select'] = static::getDefaultSelectFields();
95 if (!isset($parameters[
'filter']))
97 $parameters[
'filter'] = array();
100 foreach (static::getFilterFields() as $field)
102 if (!$field[
'FILTER'])
107 if (isset($parameters[
'filter'][$field[
'FILTER']]))
109 $current = $parameters[
'filter'][$field[
'FILTER']];
110 if (is_array($field[
'VALUE']))
112 if (!is_array($current) && in_array($current, $field[
'VALUE']))
119 $parameters[
'filter'][$field[
'FILTER']] = $field[
'VALUE'];
122 return LetterTable::getList($parameters);
159 $list = static::getList($parameters);
160 while ($item = $list->fetch())
163 if ($item[
'MESSAGE_ID'])
165 $messageIds[] = $item[
'MESSAGE_ID'];
171 $rows = MessageFieldTable::getList([
'filter' => [
'=MESSAGE_ID' => $messageIds]]);
172 while ($messageField = $rows->fetch())
174 $messageFields[$messageField[
'MESSAGE_ID']][] = $messageField;
176 foreach ($result as $key => $item)
178 if ($messageFields[$item[
'MESSAGE_ID']])
180 $result[$key][
'MESSAGE_FIELDS'] = $messageFields[$item[
'MESSAGE_ID']];
184 $dbResult = new \Bitrix\Main\DB\ArrayResult($result);
185 $dbResult->setCount($list->getCount());
286 $typeId = Contact::create($contactId)->get(
'TYPE_ID') ?: Recipient\Type::EMAIL;
289 case Recipient\Type::EMAIL:
290 $code = MainMessage\iBase::CODE_MAIL;
292 case Recipient\Type::PHONE:
293 $code = MainMessage\iBase::CODE_SMS;
304 if (!$code && empty($messageCodes))
311 $code = current($messageCodes);
314 if (empty($messageCodes))
316 $messageCodes = [$code];
319 if (!in_array($code, $messageCodes))
326 $message = MainMessage\Adapter::create($code);
334 $instance =
new Ad();
336 elseif (
$message->isReturnCustomer())
338 $instance =
new Rc();
379 $this->addError(
Loc::getMessage(
'SENDER_ENTITY_LETTER_ERROR_NOT_AVAILABLE'));
383 $segmentsInclude = $data[
'SEGMENTS_INCLUDE'];
384 $segmentsExclude = $data[
'SEGMENTS_EXCLUDE'];
386 foreach (static::getFilterFields() as $field)
393 if (is_array($field[
'VALUE']))
395 if (empty($data[$field[
'CODE']]) || !in_array($data[$field[
'CODE']], $field[
'VALUE']))
397 $data[$field[
'CODE']] = current($field[
'VALUE']);
402 $data[$field[
'CODE']] = $field[
'VALUE'];
405 $this->filterDataByEntityFields(LetterTable::getEntity(), $data);
408 $previousData = $id ? LetterTable::getRowById($id) :
null;
409 $previousData = $previousData ?: array();
412 if(!is_array($segmentsInclude) || count($segmentsInclude) == 0)
416 isset($data[
'NOT_USE_SEGMENTS'])
417 && !$data[
'NOT_USE_SEGMENTS']
419 && $data[
'IS_TRIGGER'] <>
'Y'
420 && $previousData[
'IS_TRIGGER'] <>
'Y'
423 $this->addError(
Loc::getMessage(
'SENDER_ENTITY_LETTER_ERROR_NO_SEGMENTS'));
427 $segmentsExclude = is_array($segmentsExclude) ? $segmentsExclude : array();
430 if (!isset($data[
'CAMPAIGN_ID']))
433 $this->
set(
'CAMPAIGN_ID', $data[
'CAMPAIGN_ID']);
437 if (!$id && $data[
'IS_TRIGGER'] ===
'Y')
439 if (empty($data[
'PARENT_ID']))
441 $previousLetter = (
new Chain)->load($data[
'CAMPAIGN_ID'])->getLast();
442 if ($previousLetter && $previousLetter->getId() != $this->
getId())
444 $data[
'PARENT_ID'] = $previousLetter->getId();
448 if (!isset($data[
'TIME_SHIFT']))
450 $data[
'TIME_SHIFT'] = 1440;
453 $data[
'STATUS'] = Dispatch\State::WAITING;
454 $data[
'REITERATE'] =
'Y';
458 if ($this->filterDataByChanging($data, $previousData))
460 $id = $this->saveByEntity(LetterTable::getEntity(), $id, $data);
467 $data[
'DATE_UPDATE'] =
new DateTime();
468 $this->saveByEntity(LetterTable::getEntity(), $id, $data);
482 $this->setId($id)->getState()->init();
490 $content = $this->getSearchBuilder()->getContent();
491 $content->addUserById($this->
get(
'CREATED_BY'));
492 $content->addText($this->
get(
'TITLE'));
493 $config = $this->
getMessage()->getConfiguration();
495 foreach ($config->getOptions() as $option)
497 $value = $option->getValue();
503 switch ($option->getType())
505 case $option::TYPE_EMAIL:
506 $content->addEmail($value);
509 case $option::TYPE_HTML:
510 case $option::TYPE_MAIL_EDITOR:
511 $content->addHtmlLayout($value);
514 case $option::TYPE_TEXT:
515 case $option::TYPE_STRING:
516 case $option::TYPE_PRESET_STRING:
517 case $option::TYPE_SMS_EDITOR:
518 $content->addText($value);
528 $segmentsExclude = array_unique($segmentsExclude);
529 $segmentsInclude = array_unique($segmentsInclude);
530 $segmentsInclude = array_diff($segmentsInclude, $segmentsExclude);
532 $segmentsList = array(
534 'list' => $segmentsExclude,
538 'list' => $segmentsInclude,
544 $letter = LetterTable::getById($id)->fetch();
545 LetterSegmentTable::deleteList(array(
'LETTER_ID' => $id));
549 foreach ($segmentsList as $segments)
551 if(empty($segments[
'list']))
556 $typeCode = $segments[
'include'] ?
'INCLUDE' :
'EXCLUDE';
558 foreach ($segments[
'list'] as $segment)
560 $list[] = [
'DATE_UPDATE' => $letter[
'DATE_UPDATE'],
'ID' => $segment];
561 $dataToInsert[] = array(
563 'SEGMENT_ID' => $segment,
564 'INCLUDE' => $segments[
'include'],
568 $newest = self::getArrayDiffNewest($list, $oldSegments[$typeCode]);
569 $removed = self::getArrayDiffRemoved($list, $oldSegments[$typeCode]);
571 if (count($newest) === 0 && count($removed) === 0)
576 if (count($newest) > 0)
583 if(!empty($dataToInsert))
585 LetterSegmentTable::addMulti($dataToInsert);
588 if ($isChanged && $this->
getId() && $this->
get(
'POSTING_ID'))
590 Posting\Builder::create()
591 ->run($this->
get(
'POSTING_ID'),
false);
743 $messageCode = $this->
get(
'MESSAGE_CODE') ?: MainMessage\Adapter::CODE_MAIL;
744 $messageId = $this->
get(
'MESSAGE_ID') ?:
null;
747 if (isset($this->data[
'MESSAGE_FIELDS']) && $this->data[
'MESSAGE_FIELDS'])
749 foreach ($this->data[
'MESSAGE_FIELDS'] as $field)
751 $messageFields[$field[
'CODE']] = $field[
'VALUE'];
754 if ($this->messagesCache && isset($this->messagesCache[$messageCode]))
756 $this->message = $this->messagesCache[$messageCode];
759 $this->message->setConfigurationData($messageFields);
764 $this->message = MainMessage\Adapter::create($messageCode);
765 $createdById = $this->
get(
'CREATED_BY') ?: Security\User::current()->getId();
766 $this->message->getConfiguration()->set(
'LETTER_CREATED_BY_ID', $createdById);
767 $this->message->setSiteId($this->
get(
'SITE_ID'));
770 $this->message->setConfigurationData($messageFields);
772 $this->message->loadConfiguration($messageId);
950 $configurationId = $this->
getMessage()->getConfiguration()->getId();
951 if (!$configurationId)
956 $result = $this->
getMessage()->copyConfiguration($configurationId);
957 if (!$result->isSuccess() || !$result->getId())
963 'CAMPAIGN_ID' => $this->
get(
'CAMPAIGN_ID'),
964 'MESSAGE_CODE' => $this->
get(
'MESSAGE_CODE'),
965 'MESSAGE_ID' => $result->getId(),
966 'REITERATE' => $this->get(
'REITERATE'),
967 'TEMPLATE_TYPE' => $this->get(
'TEMPLATE_TYPE'),
968 'TEMPLATE_ID' => $this->get(
'TEMPLATE_ID'),
969 'CREATED_BY' => $this->getUser()->getId(),
970 'UPDATED_BY' => $this->getUser()->getId(),
971 'IS_TRIGGER' => $this->get(
'IS_TRIGGER'),
972 'TITLE' =>
Loc::getMessage(
'SENDER_ENTITY_LETTER_COPY_PREFIX') .
' ' . $this->get(
'TITLE'),
973 'SEGMENTS_INCLUDE' => $this->get(
'SEGMENTS_INCLUDE'),
974 'SEGMENTS_EXCLUDE' => $this->get(
'SEGMENTS_EXCLUDE'),
976 $instance = static::create()->mergeData($data);
978 $this->getErrorCollection()->add($instance->getErrors());
980 if (!is_null($this->
getMessage()->getConfiguration()->
get(
'MESSAGE')))
985 $this->getMessage()->getConfiguration()->get(
'MESSAGE')
989 return $instance->getId();
1038 $this->errors->setError(
new Error($exception->getMessage()));
1045 $this->errors->setError(
new Error(
'Entity is not reiterate.'));
1051 $scheduleTime = Dispatch\MethodSchedule::parseTimesOfDay($this->
get(
'TIMES_OF_DAY'));
1052 $scheduleMonths = Dispatch\MethodSchedule::parseMonthsOfYear($this->
get(
'MONTHS_OF_YEAR'));
1053 $scheduleWeekDays = Dispatch\MethodSchedule::parseDaysOfWeek($this->
get(
'DAYS_OF_WEEK'));
1054 $scheduleMonthDays = Dispatch\MethodSchedule::parseDaysOfMonth($this->
get(
'DAYS_OF_MONTH'));
1056 ->setMonthsOfYear($scheduleMonths)
1057 ->setDaysOfMonth($scheduleMonthDays)
1058 ->setDaysOfWeek($scheduleWeekDays)
1059 ->setTime($scheduleTime[0], $scheduleTime[1]);
1065 $this->errors->setError(
new Error($exception->getMessage()));