Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
letter.php
1<?php
9
30
31Loc::loadMessages(__FILE__);
32
33class Letter extends Base
34{
36 protected $postingData = null;
37
39 protected $message;
40
43 protected $messagesCache = [];
44
46 protected $duration;
47
49 protected $method;
50
52 protected $state;
53
55 protected $counter;
56
62 protected static function getFilterFields()
63 {
64 return array(
65 array(
66 'CODE' => 'IS_ADS',
67 'VALUE' => 'N',
68 'FILTER' => '=IS_ADS'
69 ),
70 );
71 }
72
78 public static function getDataClass()
79 {
80 return LetterTable::getEntity()->getDataClass();
81 }
82
89 public static function getList(array $parameters = array())
90 {
91 if (!isset($parameters['select']))
92 {
93 $parameters['select'] = static::getDefaultSelectFields();
94 }
95 if (!isset($parameters['filter']))
96 {
97 $parameters['filter'] = array();
98 }
99
100 foreach (static::getFilterFields() as $field)
101 {
102 if (!$field['FILTER'])
103 {
104 continue;
105 }
106
107 if (isset($parameters['filter'][$field['FILTER']]))
108 {
109 $current = $parameters['filter'][$field['FILTER']];
110 if (is_array($field['VALUE']))
111 {
112 if (!is_array($current) && in_array($current, $field['VALUE']))
113 {
114 continue;
115 }
116 }
117 }
118
119 $parameters['filter'][$field['FILTER']] = $field['VALUE'];
120 }
121
122 return LetterTable::getList($parameters);
123 }
124
125 public static function getDefaultSelectFields()
126 {
127 return array(
128 '*',
129 'SITE_ID' => 'CAMPAIGN.SITE_ID',
130 'CAMPAIGN_ACTIVE' => 'CAMPAIGN.ACTIVE',
131
132 'DATE_SEND' => 'CURRENT_POSTING.DATE_SEND',
133 'DATE_PAUSE' => 'CURRENT_POSTING.DATE_PAUSE',
134 'DATE_SENT' => 'CURRENT_POSTING.DATE_SENT',
135
136 'COUNT_SEND_ALL' => 'CURRENT_POSTING.COUNT_SEND_ALL',
137 'COUNT_SEND_NONE' => 'CURRENT_POSTING.COUNT_SEND_NONE',
138 'COUNT_SEND_ERROR' => 'CURRENT_POSTING.COUNT_SEND_ERROR',
139 'COUNT_SEND_SUCCESS' => 'CURRENT_POSTING.COUNT_SEND_SUCCESS',
140 'COUNT_SEND_DENY' => 'CURRENT_POSTING.COUNT_SEND_DENY',
141
142 'USER_NAME' => 'CREATED_BY_USER.NAME',
143 'USER_LAST_NAME' => 'CREATED_BY_USER.LAST_NAME',
144 'USER_ID' => 'CREATED_BY',
145 );
146 }
155 public static function getListWithMessageFields(array $parameters = array())
156 {
157 $result = [];
158 $messageIds = [];
159 $list = static::getList($parameters);
160 while ($item = $list->fetch())
161 {
162 $result[] = $item;
163 if ($item['MESSAGE_ID'])
164 {
165 $messageIds[] = $item['MESSAGE_ID'];
166 }
167 }
168 if ($messageIds)
169 {
170 $messageFields = [];
171 $rows = MessageFieldTable::getList(['filter' => ['=MESSAGE_ID' => $messageIds]]);
172 while ($messageField = $rows->fetch())
173 {
174 $messageFields[$messageField['MESSAGE_ID']][] = $messageField;
175 }
176 foreach ($result as $key => $item)
177 {
178 if ($messageFields[$item['MESSAGE_ID']])
179 {
180 $result[$key]['MESSAGE_FIELDS'] = $messageFields[$item['MESSAGE_ID']];
181 }
182 }
183 }
184 $dbResult = new \Bitrix\Main\DB\ArrayResult($result);
185 $dbResult->setCount($list->getCount());
186 return $dbResult;
187 }
188
196 public static function createInstanceById($id = null, array $messageCodes = [])
197 {
198 $code = null;
199 if ($id)
200 {
201 $row = LetterTable::getRow([
202 'select' => ['MESSAGE_CODE'],
203 'filter' => ['=ID' => $id],
204 ]);
205 if ($row)
206 {
207 $code = $row['MESSAGE_CODE'];
208 }
209 else
210 {
211 $id = null;
212 }
213 }
214
215 $instance = self::createInstanceByCode($code, $messageCodes);
216 if (!$instance)
217 {
218 return null;
219 }
220
221 if ($id)
222 {
223 $instance->load($id);
224 }
225 elseif ($instance)
226 {
227 $instance->set('MESSAGE_CODE', $code);
228 }
229
230 return $instance;
231 }
232
240 public static function createInstanceByArray(array $data, array $messageCodes = [])
241 {
242 $code = empty($data['MESSAGE_CODE']) ? null : $data['MESSAGE_CODE'];
243 $instance = self::createInstanceByCode($code, $messageCodes);
244 $instance->loadByArray($data);
245
246 return $instance;
247 }
248
255 public static function createInstanceByPostingId($postingId)
256 {
257 $row = LetterTable::getList(array(
258 'select' => array('ID', 'IS_ADS'),
259 'filter' => array('=POSTING_ID' => $postingId),
260 'limit' => 1
261 ))->fetch();
262 if (!$row)
263 {
264 return new static();
265 }
266
267 if ($row['IS_ADS'] === 'Y')
268 {
269 return new Ad($row['ID']);
270 }
271 else
272 {
273 return new static($row['ID']);
274 }
275 }
276
284 public static function createInstanceByContactId($contactId, array $messageCodes = [])
285 {
286 $typeId = Contact::create($contactId)->get('TYPE_ID') ?: Recipient\Type::EMAIL;
287 switch ($typeId)
288 {
289 case Recipient\Type::EMAIL:
290 $code = MainMessage\iBase::CODE_MAIL;
291 break;
292 case Recipient\Type::PHONE:
293 $code = MainMessage\iBase::CODE_SMS;
294 break;
295 default:
296 return null;
297 }
298
299 return self::createInstanceByCode($code, $messageCodes);
300 }
301
302 protected static function createInstanceByCode($code = null, array $messageCodes = [])
303 {
304 if (!$code && empty($messageCodes))
305 {
306 return null;
307 }
308
309 if (!$code)
310 {
311 $code = current($messageCodes);
312 }
313
314 if (empty($messageCodes))
315 {
316 $messageCodes = [$code];
317 }
318
319 if (!in_array($code, $messageCodes))
320 {
321 return null;
322 }
323
324 try
325 {
326 $message = MainMessage\Adapter::create($code);
327 } catch (ArgumentException $e)
328 {
329 return null;
330 }
331
332 if ($message->isAds() || $message->isMarketing())
333 {
334 $instance = new Ad();
335 }
336 elseif ($message->isReturnCustomer())
337 {
338 $instance = new Rc();
339 }
340 elseif ($message->isMailing())
341 {
342 $instance = new Letter();
343 }
344 else
345 {
346 $instance = new Toloka();
347 }
348
349 return $instance;
350 }
351
357 protected function getDefaultData()
358 {
359 return array(
360 'TITLE' => '',
361 'MESSAGE_ID' => '',
362 'MESSAGE_CODE' => MainMessage\Adapter::CODE_MAIL,
363 'SEGMENTS_INCLUDE' => array(),
364 'SEGMENTS_EXCLUDE' => array(),
365 );
366 }
367
375 protected function saveData($id, array $data)
376 {
377 if(!$this->getMessage()->isAvailable())
378 {
379 $this->addError(Loc::getMessage('SENDER_ENTITY_LETTER_ERROR_NOT_AVAILABLE'));
380 return $id;
381 }
382
383 $segmentsInclude = $data['SEGMENTS_INCLUDE'];
384 $segmentsExclude = $data['SEGMENTS_EXCLUDE'];
385
386 foreach (static::getFilterFields() as $field)
387 {
388 if (!$field['CODE'])
389 {
390 continue;
391 }
392
393 if (is_array($field['VALUE']))
394 {
395 if (empty($data[$field['CODE']]) || !in_array($data[$field['CODE']], $field['VALUE']))
396 {
397 $data[$field['CODE']] = current($field['VALUE']);
398 }
399 }
400 else
401 {
402 $data[$field['CODE']] = $field['VALUE'];
403 }
404 }
405 $this->filterDataByEntityFields(LetterTable::getEntity(), $data);
406
407 $initialId = $id;
408 $previousData = $id ? LetterTable::getRowById($id) : null;
409 $previousData = $previousData ?: array();
410
411 // segment check
412 if(!is_array($segmentsInclude) || count($segmentsInclude) == 0)
413 {
414 if (
415 (
416 isset($data['NOT_USE_SEGMENTS'])
417 && !$data['NOT_USE_SEGMENTS']
418 )
419 && $data['IS_TRIGGER'] <> 'Y'
420 && $previousData['IS_TRIGGER'] <> 'Y'
421 )
422 {
423 $this->addError(Loc::getMessage('SENDER_ENTITY_LETTER_ERROR_NO_SEGMENTS'));
424 return $id;
425 }
426 }
427 $segmentsExclude = is_array($segmentsExclude) ? $segmentsExclude : array();
428
429 // campaign setting
430 if (!isset($data['CAMPAIGN_ID']))
431 {
432 $data['CAMPAIGN_ID'] = Campaign::getDefaultId(SITE_ID);
433 $this->set('CAMPAIGN_ID', $data['CAMPAIGN_ID']);
434 }
435
436 // parent letter setting for triggers
437 if (!$id && $data['IS_TRIGGER'] === 'Y')
438 {
439 if (empty($data['PARENT_ID']))
440 {
441 $previousLetter = (new Chain)->load($data['CAMPAIGN_ID'])->getLast();
442 if ($previousLetter && $previousLetter->getId() != $this->getId())
443 {
444 $data['PARENT_ID'] = $previousLetter->getId();
445 }
446 }
447
448 if (!isset($data['TIME_SHIFT']))
449 {
450 $data['TIME_SHIFT'] = 1440;
451 }
452
453 $data['STATUS'] = Dispatch\State::WAITING;
454 $data['REITERATE'] = 'Y';
455 }
456
457
458 if ($this->filterDataByChanging($data, $previousData))
459 {
460 $id = $this->saveByEntity(LetterTable::getEntity(), $id, $data);
461 }
462
463 if ($this->canChangeSegments())
464 {
465 $this->saveDataSegments($id, $segmentsInclude, $segmentsExclude);
466
467 $data['DATE_UPDATE'] = new DateTime();
468 $this->saveByEntity(LetterTable::getEntity(), $id, $data);
469 }
470
471 if ($this->hasErrors())
472 {
473 return $id;
474 }
475
476 // update template use count
477 $this->updateTemplateUseCount($data, $previousData);
478
479 // change status for init recipients
480 if (!$initialId && !$this->isTrigger())
481 {
482 $this->setId($id)->getState()->init();
483 }
484
485 return $id;
486 }
487
488 protected function prepareSearchContent()
489 {
490 $content = $this->getSearchBuilder()->getContent();
491 $content->addUserById($this->get('CREATED_BY'));
492 $content->addText($this->get('TITLE'));
493 $config = $this->getMessage()->getConfiguration();
494
495 foreach ($config->getOptions() as $option)
496 {
497 $value = $option->getValue();
498 if (!$value)
499 {
500 continue;
501 }
502
503 switch ($option->getType())
504 {
505 case $option::TYPE_EMAIL:
506 $content->addEmail($value);
507 break;
508
509 case $option::TYPE_HTML:
510 case $option::TYPE_MAIL_EDITOR:
511 $content->addHtmlLayout($value);
512 break;
513
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);
519 break;
520 }
521 }
522
523 return $this;
524 }
525
526 protected function saveDataSegments($id, array $segmentsInclude, array $segmentsExclude)
527 {
528 $segmentsExclude = array_unique($segmentsExclude);
529 $segmentsInclude = array_unique($segmentsInclude);
530 $segmentsInclude = array_diff($segmentsInclude, $segmentsExclude);
531
532 $segmentsList = array(
533 array(
534 'list' => $segmentsExclude,
535 'include' => false
536 ),
537 array(
538 'list' => $segmentsInclude,
539 'include' => true
540 ),
541 );
542
543 $oldSegments = $this->loadDataSegments($id);
544 $letter = LetterTable::getById($id)->fetch();
545 LetterSegmentTable::deleteList(array('LETTER_ID' => $id));
546
547 $isChanged = false;
548 $dataToInsert = [];
549 foreach ($segmentsList as $segments)
550 {
551 if(empty($segments['list']))
552 {
553 continue;
554 }
555
556 $typeCode = $segments['include'] ? 'INCLUDE' : 'EXCLUDE';
557 $list = [];
558 foreach ($segments['list'] as $segment)
559 {
560 $list[] = ['DATE_UPDATE' => $letter['DATE_UPDATE'], 'ID' => $segment];
561 $dataToInsert[] = array(
562 'LETTER_ID' => $id,
563 'SEGMENT_ID' => $segment,
564 'INCLUDE' => $segments['include'],
565 );
566 }
567
568 $newest = self::getArrayDiffNewest($list, $oldSegments[$typeCode]);
569 $removed = self::getArrayDiffRemoved($list, $oldSegments[$typeCode]);
570
571 if (count($newest) === 0 && count($removed) === 0)
572 {
573 continue;
574 }
575
576 if (count($newest) > 0)
577 {
578 Segment::updateUseCounters($newest, $segments['include']);
579 }
580
581 $isChanged = true;
582 }
583 if(!empty($dataToInsert))
584 {
585 LetterSegmentTable::addMulti($dataToInsert);
586 }
587
588 if ($isChanged && $this->getId() && $this->get('POSTING_ID'))
589 {
590 Posting\Builder::create()
591 ->run($this->get('POSTING_ID'), false);
592 }
593 }
594
595 private static function getArrayDiffNewest(array $current, array $old)
596 {
597 return array_udiff($current, $old, function($first, $second)
598 {
599 return $first['DATE_UPDATE'] < $second['DATE_UPDATE'] || $first['ID'] != $second['ID'];
600 });
601 }
602
603 private static function getArrayDiffRemoved(array $current, array $old)
604 {
605 return self::getArrayDiffNewest($old, $current);
606 }
607
608 protected function updateTemplateUseCount(array $data, array $previousData)
609 {
610 if (!isset($data['TEMPLATE_TYPE']) || !isset($data['TEMPLATE_ID']))
611 {
612 return false;
613 }
614
615 if (Templates\Type::getCode(Templates\Type::USER) !== $data['TEMPLATE_TYPE'])
616 {
617 return false;
618 }
619 if (
620 isset($previousData['TEMPLATE_ID'])
621 && $data['TEMPLATE_ID'] === $previousData['TEMPLATE_ID']
622 && $data['TEMPLATE_TYPE'] === $previousData['TEMPLATE_TYPE']
623 )
624 {
625 return false;
626 }
627
628 return TemplateTable::incUseCount($data['TEMPLATE_ID']);
629 }
630
637 public function loadData($id)
638 {
639 $data = static::getList(array(
640 'filter' => array(
641 '=ID' => $id
642 )
643 ))->fetch();
644 if (!is_array($data))
645 {
646 return null;
647 }
648
649 $segments = $this->loadDataSegments($id);
650 foreach ($segments as $typeCode => $list)
651 {
652 foreach($list as $item)
653 {
654 $data["SEGMENTS_$typeCode"][] = $item['ID'];
655 }
656 }
657
658 return $data;
659 }
660
666 public function hasStatistics()
667 {
668 return (
669 $this->getState()->wasStartedSending()
670 &&
671 !$this->getState()->isPlanned()
672 &&
673 $this->getMessage()->hasStatistics()
674 );
675 }
676
682 public function canChangeSegments()
683 {
684 return !$this->getState()->wasPostingBuilt();
685 }
686
693 public function loadDataSegments($id)
694 {
695 $data = array('INCLUDE' => array(), 'EXCLUDE' => array());
696 $segments = LetterSegmentTable::getList(array(
697 'select' => ['INCLUDE', 'LETTER_ID', 'SEGMENT_ID', 'DATE_UPDATE' => 'SEGMENT.DATE_UPDATE'],
698 'filter'=>array(
699 '=LETTER_ID'=> $id
700 )
701 ));
702 foreach($segments as $segment)
703 {
704 if ($segment['INCLUDE'])
705 {
706 $data['INCLUDE'][] =
707 [ 'ID' => $segment['SEGMENT_ID'], 'DATE_UPDATE' => $segment['DATE_UPDATE']];
708 }
709 else
710 {
711 $data['EXCLUDE'][] =
712 [ 'ID' => $segment['SEGMENT_ID'], 'DATE_UPDATE' => $segment['DATE_UPDATE']];
713 }
714 }
715
716 return $data;
717 }
718
724 public function canChangeTemplate()
725 {
726 if ($this->getState()->isFinished())
727 {
728 return false;
729 }
730
731 //return $this->getMessage()->getCode() !== MainMessage\Adapter::CODE_MAIL;
732 return true;
733 }
734
741 public function getMessage()
742 {
743 $messageCode = $this->get('MESSAGE_CODE') ?: MainMessage\Adapter::CODE_MAIL;
744 $messageId = $this->get('MESSAGE_ID') ?: null;
745
746 $messageFields = [];
747 if (isset($this->data['MESSAGE_FIELDS']) && $this->data['MESSAGE_FIELDS'])
748 {
749 foreach ($this->data['MESSAGE_FIELDS'] as $field)
750 {
751 $messageFields[$field['CODE']] = $field['VALUE'];
752 }
753 }
754 if ($this->messagesCache && isset($this->messagesCache[$messageCode]))
755 {
756 $this->message = $this->messagesCache[$messageCode];
757 if ($messageFields)
758 {
759 $this->message->setConfigurationData($messageFields);
760 }
761 return $this->message;
762 }
763
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'));
768 if ($messageFields)
769 {
770 $this->message->setConfigurationData($messageFields);
771 }
772 $this->message->loadConfiguration($messageId);
773
774 $this->messagesCache[$messageCode] = $this->message;
775
776 return $this->message;
777 }
778
784 public function isSupportHeatMap()
785 {
786 return $this->getMessage()->getCode() == MainMessage\Adapter::CODE_MAIL;
787 }
788
794 public function isSupportReiterate()
795 {
796 if (in_array($this->getMessage()->getCode(), ['rc_lead', 'rc_deal']))
797 {
798 return true;
799 }
800
801 return !Integration\Bitrix24\Service::isPortal();
802 }
803
809 public function getCampaignId()
810 {
811 return $this->get('CAMPAIGN_ID');
812 }
813
819 public function getDuration()
820 {
821 if ($this->duration)
822 {
823 return $this->duration;
824 }
825
826 $this->duration = new Dispatch\Duration($this);
827
828 return $this->duration;
829 }
830
836 public function getState()
837 {
838 if ($this->state)
839 {
840 return $this->state;
841 }
842
843 $this->state = new Dispatch\State($this);
844
845 return $this->state;
846 }
847
853 public function getMethod()
854 {
855 if ($this->method)
856 {
857 return $this->method;
858 }
859
860 $this->method = new Dispatch\Method($this);
861
862 return $this->method;
863 }
864
870 public function getCounter()
871 {
872 if ($this->counter)
873 {
874 return $this->counter;
875 }
876
877 $this->counter = new Posting\Counter($this);
878
879 return $this->counter;
880 }
881
887 protected function getTester()
888 {
889 return $this->getMessage()->getTester();
890 }
891
897 public function isReiterate()
898 {
899 return $this->get('REITERATE') === 'Y';
900 }
901
907 public function isTrigger()
908 {
909 return $this->get('IS_TRIGGER') === 'Y';
910 }
911
917 public function isSupportTesting()
918 {
919 return $this->getTester()->isSupport();
920 }
921
927 public function remove()
928 {
929 return $this->removeByEntity(LetterTable::getEntity(), $this->getId());
930 }
931
938 public static function removeById($id)
939 {
940 return static::create()->removeByEntity(LetterTable::getEntity(), $id);
941 }
942
948 public function copy()
949 {
950 $configurationId = $this->getMessage()->getConfiguration()->getId();
951 if (!$configurationId)
952 {
953 return null;
954 }
955
956 $result = $this->getMessage()->copyConfiguration($configurationId);
957 if (!$result->isSuccess() || !$result->getId())
958 {
959 return null;
960 }
961
962 $data = array(
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'),
975 );
976 $instance = static::create()->mergeData($data);
977 $instance->save();
978 $this->getErrorCollection()->add($instance->getErrors());
979
980 if (!is_null($this->getMessage()->getConfiguration()->get('MESSAGE')))
981 {
983 $instance->getId(),
984 0,
985 $this->getMessage()->getConfiguration()->get('MESSAGE')
986 );
987 }
988
989 return $instance->getId();
990 }
991
999 public function test(array $codes, array $parameters = array())
1000 {
1001 return $this->getTester()->send($codes, $parameters);
1002 }
1003
1010 public function plan(Date $date)
1011 {
1012 try
1013 {
1014 return $this->getState()->plan($date);
1015 }
1016 catch (InvalidOperationException $exception)
1017 {
1018 $this->errors->setError(new Error($exception->getMessage()));
1019 return false;
1020 }
1021 }
1022
1028 public function wait()
1029 {
1030 if ($this->isTrigger())
1031 {
1032 try
1033 {
1034 return $this->getState()->wait();
1035 }
1036 catch (InvalidOperationException $exception)
1037 {
1038 $this->errors->setError(new Error($exception->getMessage()));
1039 return false;
1040 }
1041 }
1042
1043 if (!$this->isReiterate())
1044 {
1045 $this->errors->setError(new Error('Entity is not reiterate.'));
1046 return false;
1047 }
1048
1049 try
1050 {
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'));
1055 $method = (new Dispatch\MethodSchedule($this))
1056 ->setMonthsOfYear($scheduleMonths)
1057 ->setDaysOfMonth($scheduleMonthDays)
1058 ->setDaysOfWeek($scheduleWeekDays)
1059 ->setTime($scheduleTime[0], $scheduleTime[1]);
1060 $this->getState()->wait($method);
1061 return true;
1062 }
1063 catch (InvalidOperationException $exception)
1064 {
1065 $this->errors->setError(new Error($exception->getMessage()));
1066 return false;
1067 }
1068 }
1069
1075 public function send()
1076 {
1077 try
1078 {
1079 return $this->getState()->send();
1080 }
1081 catch (InvalidOperationException $exception)
1082 {
1083 $this->errors->setError(new Error($exception->getMessage()));
1084 return false;
1085 }
1086 }
1087
1093 public function sendErrors()
1094 {
1095 try
1096 {
1097 return $this->getState()->sendErrors();
1098 }
1099 catch (InvalidOperationException $exception)
1100 {
1101 $this->errors->setError(new Error($exception->getMessage()));
1102 return false;
1103 }
1104 }
1105
1111 public function stop()
1112 {
1113 try
1114 {
1115 return $this->getState()->stop();
1116 }
1117 catch (InvalidOperationException $exception)
1118 {
1119 $this->errors->setError(new Error($exception->getMessage()));
1120 return false;
1121 }
1122 }
1123
1129 public function halt()
1130 {
1131 try
1132 {
1133 return $this->getState()->halt();
1134 }
1135 catch (InvalidOperationException $exception)
1136 {
1137 $this->errors->setError(new Error($exception->getMessage()));
1138 return false;
1139 }
1140 }
1141
1147 public function resume()
1148 {
1149 try
1150 {
1151 return $this->getState()->resume();
1152 }
1153 catch (InvalidOperationException $exception)
1154 {
1155 $this->errors->setError(new Error($exception->getMessage()));
1156 return false;
1157 }
1158 }
1159
1165 public function pause()
1166 {
1167 try
1168 {
1169 return $this->getState()->pause();
1170 }
1171 catch (InvalidOperationException $exception)
1172 {
1173 $this->errors->setError(new Error($exception->getMessage()));
1174 return false;
1175 }
1176 }
1177
1183 public function getLastPostingData()
1184 {
1185 $defaults = array();
1186
1187 if (!$this->getId())
1188 {
1189 return $defaults;
1190 }
1191
1192 if ($this->postingData !== null)
1193 {
1194 return $this->postingData;
1195 }
1196
1197 $this->postingData = $defaults;
1198 $postingFilter = array(
1199 '=ID' => $this->getId(),
1200 //'!POSTING.DATE_SENT' => null
1201 );
1202
1203 $postings = static::getList(array(
1204 'select' => array(
1205 'POSTING_ID',
1206 'LETTER_ID' => 'ID',
1207 'CAMPAIGN_ID',
1208 'TITLE' => 'TITLE',
1209 'MAILING_NAME' => 'CAMPAIGN.NAME',
1210 'DATE_SENT' => 'POSTING.DATE_SENT',
1211 'COUNT_SEND_ERROR' => 'POSTING.COUNT_SEND_ERROR',
1212 'CREATED_BY' => 'CREATED_BY',
1213 'CREATED_BY_NAME' => 'CREATED_BY_USER.NAME',
1214 'CREATED_BY_LAST_NAME' => 'CREATED_BY_USER.LAST_NAME',
1215 'CREATED_BY_SECOND_NAME' => 'CREATED_BY_USER.SECOND_NAME',
1216 'CREATED_BY_LOGIN' => 'CREATED_BY_USER.LOGIN',
1217 'CREATED_BY_TITLE' => 'CREATED_BY_USER.TITLE',
1218 ),
1219 'filter' => $postingFilter,
1220 'limit' => 1,
1221 'order' => array('POSTING.DATE_SENT' => 'DESC', 'POSTING.DATE_CREATE' => 'DESC'),
1222 ));
1223 if ($postingData = $postings->fetch())
1224 {
1225 $this->postingData = $postingData + $this->postingData;
1226 }
1227
1228 return $this->postingData;
1229 }
1230}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getDefaultId($siteId=null)
Definition campaign.php:63
static createInstanceByArray(array $data, array $messageCodes=[])
Definition letter.php:240
static getList(array $parameters=array())
Definition letter.php:89
test(array $codes, array $parameters=array())
Definition letter.php:999
static createInstanceByPostingId($postingId)
Definition letter.php:255
saveDataSegments($id, array $segmentsInclude, array $segmentsExclude)
Definition letter.php:526
static createInstanceByContactId($contactId, array $messageCodes=[])
Definition letter.php:284
updateTemplateUseCount(array $data, array $previousData)
Definition letter.php:608
static createInstanceByCode($code=null, array $messageCodes=[])
Definition letter.php:302
static createInstanceById($id=null, array $messageCodes=[])
Definition letter.php:196
static getListWithMessageFields(array $parameters=array())
Definition letter.php:155
saveData($id, array $data)
Definition letter.php:375
static updateUseCounters(array $list, $isInclude=true)
Definition segment.php:467
static syncFiles(int $entityId, int $entityType, string $template, bool $deleteFiles=true, bool $onDeleteEntity=false)
Definition filetable.php:72