27 return 'b_sender_mailing';
37 'data_type' =>
'integer',
39 'autocomplete' =>
true,
42 'data_type' =>
'string',
46 'DESCRIPTION' => array(
47 'data_type' =>
'string',
48 'title' =>
Loc::getMessage(
'SENDER_ENTITY_MAILING_FIELD_TITLE_DESCRIPTION'),
49 'validation' => array(__CLASS__,
'validateDescription'),
51 'DATE_INSERT' => array(
52 'data_type' =>
'datetime',
57 'data_type' =>
'string',
58 'default_value' =>
'Y'
60 'TRACK_CLICK' => array(
61 'data_type' =>
'string',
62 'default_value' =>
'N',
65 'data_type' =>
'string',
66 'default_value' =>
'Y',
68 'IS_TRIGGER' => array(
69 'data_type' =>
'string',
71 'default_value' =>
'N',
74 'data_type' =>
'integer',
76 'default_value' => 100,
80 'data_type' =>
'string',
82 'default_value' => SITE_ID
84 'TRIGGER_FIELDS' => array(
85 'data_type' =>
'text',
88 'EMAIL_FROM' => array(
89 'data_type' =>
'string',
91 'title' =>
Loc::getMessage(
'SENDER_ENTITY_MAILING_FIELD_TITLE_EMAIL_FROM'),
92 'validation' => array(
'Bitrix\Sender\MailingChainTable',
'validateEmailForm'),
95 'data_type' =>
'Bitrix\Sender\MailingChainTable',
96 'reference' => array(
'=this.ID' =>
'ref.MAILING_ID'),
99 'data_type' =>
'Bitrix\Sender\PostingTable',
100 'reference' => array(
'=this.ID' =>
'ref.MAILING_ID'),
102 'MAILING_GROUP' => array(
103 'data_type' =>
'Bitrix\Sender\MailingGroupTable',
104 'reference' => array(
'=this.ID' =>
'ref.MAILING_ID'),
106 'MAILING_SUBSCRIPTION' => array(
107 'data_type' =>
'Bitrix\Sender\MailingSubscriptionTable',
108 'reference' => array(
'=this.ID' =>
'ref.MAILING_ID'),
110 'SUBSCRIBER' => array(
111 'data_type' =>
'Bitrix\Sender\MailingSubscriptionTable',
112 'reference' => array(
'=this.ID' =>
'ref.MAILING_ID',
'ref.IS_UNSUB' =>
new SqlExpression(
'?',
'N')),
115 'data_type' =>
'Bitrix\Main\SiteTable',
116 'reference' => array(
'=this.SITE_ID' =>
'ref.LID'),
143 if(array_key_exists(
'ACTIVE', $data[
'fields']))
145 if ($data[
'fields'][
'ACTIVE'] ===
'Y')
147 $chain = (new \Bitrix\Sender\Entity\Chain())->load($data[
'primary'][
'ID']);
148 foreach ($chain->getList() as $letter)
150 if (!$letter->getState()->wasStartedSending())
157 Runtime\Job::actualizeByCampaignId($data[
'primary'][
'ID']);
160 if (array_key_exists(
'ACTIVE', $data[
'fields']) || array_key_exists(
'TRIGGER_FIELDS', $data[
'fields']))
162 static::updateChainTrigger($data[
'primary'][
'ID']);
177 $primary = array(
'MAILING_ID' => $data[
'primary'][
'ID']);
192 $resultList = array();
193 $event = new \Bitrix\Main\Event(
'sender',
'OnPresetMailingList');
196 foreach ($event->getResults() as $eventResult)
198 if ($eventResult->getModuleId() ===
'sale')
203 if ($eventResult->getType() == \
Bitrix\Main\EventResult::ERROR)
208 $eventResultParameters = $eventResult->getParameters();
210 if (!empty($eventResultParameters))
212 if(!empty($params[
'CODE']))
214 $eventResultParametersTmp = array();
215 foreach($eventResultParameters as $preset)
217 if($params[
'CODE'] == $preset[
'CODE'])
219 $eventResultParametersTmp[] = $preset;
224 $eventResultParameters = $eventResultParametersTmp;
227 $resultList = array_merge($resultList, $eventResultParameters);
231 $resultListTmp = Integration\EventHandler::onSenderTriggerCampaignPreset();
232 foreach($resultList as $result)
234 if(empty($result[
'TRIGGER'][
'START'][
'ENDPOINT'][
'CODE']))
237 $trigger = Trigger\Manager::getOnce($result[
'TRIGGER'][
'START'][
'ENDPOINT']);
241 $result[
'TRIGGER'][
'START'][
'ENDPOINT'][
'NAME'] = $trigger->getName();
242 if(!empty($result[
'TRIGGER'][
'START'][
'ENDPOINT'][
'CODE']))
244 $trigger = Trigger\Manager::getOnce($result[
'TRIGGER'][
'END'][
'ENDPOINT']);
246 $result[
'TRIGGER'][
'END'][
'ENDPOINT'][
'NAME'] = $trigger->getName();
250 $resultListTmp[] = $result;
253 return $resultListTmp;
259 $errorList = array();
260 $errorCurrentNumber = 0;
262 foreach($fields as $item)
264 $errorCurrentNumber++;
266 $chainFields = array(
267 'MAILING_ID' => ($id ? $id : 1),
271 'EMAIL_FROM' => $item[
'EMAIL_FROM'],
272 'SUBJECT' => $item[
'SUBJECT'],
273 'MESSAGE' => $item[
'MESSAGE'],
274 'TEMPLATE_TYPE' => $item[
'TEMPLATE_TYPE'],
275 'TEMPLATE_ID' => $item[
'TEMPLATE_ID'],
276 'TIME_SHIFT' => intval($item[
'TIME_SHIFT']),
280 if(!empty($item[
'ID']))
281 $chainId = $item[
'ID'];
285 $chain = MailingChainTable::getRowById(array(
'ID' => $chainId));
288 $chainFields[
'STATUS'] = $chain[
'STATUS'];
292 if(empty($chainFields[
'STATUS']))
295 $chainFields[
'ID'] = $chainId;
298 MailingChainTable::checkFields($resultItem,
null, $chainFields);
299 if($resultItem->isSuccess())
305 $errorList[$errorCurrentNumber] = $resultItem->getErrors();
310 foreach($errorList as $number => $errors)
313 foreach($errors as $error)
317 $delimiter .
Loc::getMessage(
'SENDER_ENTITY_MAILING_CHAIN_ITEM_NUMBER') . $number .
': ' . $error->getMessage(),
336 static::checkFieldsChain($result, $id, $fields);
337 if(!$result->isSuccess(
true))
340 $parentChainId =
null;
341 $existChildIdList = array();
342 foreach($fields as $chainFields)
344 $chainId = $chainFields[
'ID'];
345 unset($chainFields[
'ID']);
347 $chainFields[
'MAILING_ID'] = $id;
348 $chainFields[
'IS_TRIGGER'] =
'Y';
349 $chainFields[
'REITERATE'] =
'Y';
350 $chainFields[
'PARENT_ID'] = $parentChainId;
355 $chain = MailingChainTable::getRowById(array(
'ID' => $chainId));
358 $chainFields[
'STATUS'] = $chain[
'STATUS'];
359 unset($chainFields[
'CREATED_BY']);
362 if(empty($chainFields[
'STATUS']))
369 $existChildIdList[] = $chainId;
371 $chainUpdateDb = Model\LetterTable::update($chainId, $chainFields);
372 if($chainUpdateDb->isSuccess())
378 $result->addErrors($chainUpdateDb->getErrors());
383 $chainAddDb = MailingChainTable::add($chainFields);
384 if($chainAddDb->isSuccess())
386 $chainId = $chainAddDb->getId();
387 $existChildIdList[] = $chainId;
391 $result->addErrors($chainAddDb->getErrors());
395 if(!empty($errorList))
break;
397 $parentChainId =
null;
398 if($chainId !==
null)
399 $parentChainId = $chainId;
402 $deleteChainDb = MailingChainTable::getList(array(
403 'select' => array(
'ID'),
404 'filter' => array(
'MAILING_ID' => $id,
'!ID' => $existChildIdList),
406 while($deleteChain = $deleteChainDb->fetch())
408 Model\LetterTable::delete($deleteChain[
'ID']);
411 static::updateChainTrigger($id);
423 $chainDb = MailingChainTable::getList(array(
425 'ID',
'SUBJECT',
'EMAIL_FROM',
'MESSAGE',
'TIME_SHIFT',
'PARENT_ID',
426 'DATE_INSERT',
'PRIORITY',
'LINK_PARAMS',
'TEMPLATE_TYPE',
'TEMPLATE_ID',
427 'CREATED_BY',
'CREATED_BY_NAME' =>
'CREATED_BY_USER.NAME',
'CREATED_BY_LAST_NAME' =>
'CREATED_BY_USER.LAST_NAME'
429 'filter' => array(
'=MAILING_ID' => $id,
'=PARENT_ID' => $parentId),
433 while($chain = $chainDb->fetch())
437 $parentId = $chain[
'ID'];
441 }
while($parentId !==
null);
447 public static function updateChainTrigger($id)
450 $chainDb = MailingChainTable::getList(array(
451 'select' => array(
'ID',
'TRIGGER_FIELDS' =>
'MAILING.TRIGGER_FIELDS'),
452 'filter' => array(
'=MAILING_ID' => $id,
'=IS_TRIGGER' =>
'Y',
'=PARENT_ID' =>
null),
455 $chain = $chainDb->fetch();
457 $chainId = $chain[
'ID'];
460 $triggerFields = $chain[
'TRIGGER_FIELDS'];
461 if(!is_array($triggerFields))
462 $triggerFields = array();
465 $settingsList = array();
466 foreach($triggerFields as $key => $point)
468 if(empty($point[
'CODE']))
continue;
470 $point[
'IS_EVENT_OCCUR'] =
true;
471 $point[
'IS_PREVENT_EMAIL'] =
false;
472 $point[
'SEND_INTERVAL_UNIT'] =
'M';
473 $point[
'IS_CLOSED_TRIGGER'] = ($point[
'IS_CLOSED_TRIGGER'] ==
'Y' ? true :
false);
478 $point[
'IS_TYPE_START'] =
false;
483 $point[
'IS_TYPE_START'] =
true;
486 $settingsList[] =
new Trigger\Settings($point);
491 $mailingTriggerList = array();
492 foreach($settingsList as $settings)
495 $trigger = Trigger\Manager::getOnce($settings->getEndpoint());
498 $triggerFindId = $trigger->getFullEventType() .
"/" .((int) $settings->isTypeStart());
499 $mailingTriggerList[$triggerFindId] = array(
500 'IS_TYPE_START' => $settings->isTypeStart(),
501 'NAME' => $trigger->getName(),
502 'EVENT' => $trigger->getFullEventType(),
503 'ENDPOINT' => $settings->getArray(),
510 $triggerDb = MailingTriggerTable::getList(array(
511 'select' => array(
'EVENT',
'MAILING_CHAIN_ID',
'IS_TYPE_START'),
512 'filter' => array(
'=MAILING_CHAIN_ID' => $chainId)
514 while($trigger = $triggerDb->fetch())
516 $triggerFindId = $trigger[
'EVENT'] .
"/" . ((int) $trigger[
'IS_TYPE_START']);
517 if(!isset($mailingTriggerList[$triggerFindId]))
519 MailingTriggerTable::delete($trigger);
523 MailingTriggerTable::update($trigger, $mailingTriggerList[$triggerFindId]);
524 unset($mailingTriggerList[$triggerFindId]);
528 foreach($mailingTriggerList as $triggerFindId => $settings)
531 $settings[
'MAILING_CHAIN_ID'] = $chainId;
532 MailingTriggerTable::add($settings);
535 Trigger\Manager::actualizeHandlerForChild();
540 $state = (bool) $state ==
true ?
'Y' :
'N';
541 $mailing = static::getRowById($id);
547 $triggerFields = $mailing[
'TRIGGER_FIELDS'];
548 if(!is_array($triggerFields))
553 if(!isset($triggerFields[
'START']))
558 $triggerFields[
'START'][
'WAS_RUN_FOR_OLD_DATA'] = $state;
559 $updateDb = static::update($id, array(
'TRIGGER_FIELDS' => $triggerFields));
560 if($updateDb->isSuccess())
562 static::updateChainTrigger($id);
571 $groupConnectorDb = MailingGroupTable::getList(array(
573 'CONNECTOR_ENDPOINT' =>
'GROUP.GROUP_CONNECTOR.ENDPOINT',
580 'order' => array(
'GROUP_ID' =>
'ASC')
582 while($groupConnector = $groupConnectorDb->fetch())
585 if(is_array($groupConnector[
'CONNECTOR_ENDPOINT']))
587 $connector = Connector\Manager::getConnector($groupConnector[
'CONNECTOR_ENDPOINT']);
595 $result = array_merge($result,
$connector->getPersonalizeList());
605 $mailingDb = MailingTable::getList(array(
606 'select' => array(
'ID',
'TRIGGER_FIELDS'),
609 '=IS_TRIGGER' =>
'Y',
613 if(!$mailing = $mailingDb->fetch())
616 $triggerFields = $mailing[
'TRIGGER_FIELDS'];
617 if(!is_array($triggerFields))
618 $triggerFields = array();
620 $settingsList = array();
621 foreach($triggerFields as $key => $point)
623 if(empty($point[
'CODE']))
continue;
625 $point[
'IS_EVENT_OCCUR'] =
true;
626 $point[
'IS_PREVENT_EMAIL'] =
false;
627 $point[
'SEND_INTERVAL_UNIT'] =
'M';
632 $point[
'IS_TYPE_START'] =
false;
637 $point[
'IS_TYPE_START'] =
true;
643 foreach($settingsList as $settings)
646 if(!$settings->isTypeStart())
649 $trigger = Trigger\Manager::getOnce($settings->getEndpoint());
652 $result = array_merge($result, $trigger->getPersonalizeList());
662 if (!$cache || !($cache[$mailingId] ??
false))
664 $mailing = self::getById($mailingId)->fetch();
665 $cache[$mailingId] = $mailing[
'SITE_ID'];
668 return $cache[$mailingId];
680 $entity = static::getEntity();
681 $connection = $entity->getConnection();
683 \CTimeZone::disable();
685 'DELETE FROM %s WHERE %s',
686 $connection->getSqlHelper()->quote($entity->getDbTableName()),
687 Query::buildFilterSql($entity, $filter)
689 $res = $connection->query($sql);
690 \CTimeZone::enable();
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static deleteList(array $filter)