Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
posting.php
1<?php
8namespace Bitrix\Sender;
9
16use Bitrix\Sender\Posting\Builder as PostingBuilder;
17
18Loc::loadMessages(__FILE__);
19
21{
22 const STATUS_NEW = 'N';
23 const STATUS_PART = 'P';
24 const STATUS_SENT = 'S';
26 const STATUS_ABORT = 'A';
27 const STATUS_WAIT = 'W';
28
32 public static function getTableName()
33 {
34 return 'b_sender_posting';
35 }
36
40 public static function getMap()
41 {
42 return array(
43 'ID' => array(
44 'data_type' => 'integer',
45 'primary' => true,
46 'autocomplete' => true,
47 ),
48 'MAILING_ID' => array(
49 'data_type' => 'integer',
50 'primary' => true,
51 'required' => true,
52 ),
53 'MAILING_CHAIN_ID' => array(
54 'data_type' => 'integer',
55 'primary' => true,
56 'required' => true,
57 ),
58 'DATE_CREATE' => array(
59 'data_type' => 'datetime',
60 'required' => true,
61 'default_value' => new MainType\DateTime(),
62 ),
63 'DATE_UPDATE' => array(
64 'data_type' => 'datetime',
65 'required' => true,
66 'default_value' => new MainType\DateTime(),
67 ),
68 'STATUS' => array(
69 'data_type' => 'string',
70 'required' => true,
71 'default_value' => static::STATUS_NEW,
72 ),
73 'DATE_SEND' => array(
74 'data_type' => 'datetime',
75 ),
76 'DATE_PAUSE' => array(
77 'data_type' => 'datetime',
78 ),
79 'DATE_SENT' => array(
80 'data_type' => 'datetime',
81 ),
82 'COUNT_READ' => array(
83 'data_type' => 'integer',
84 'default_value' => 0
85 ),
86 'COUNT_CLICK' => array(
87 'data_type' => 'integer',
88 'default_value' => 0
89 ),
90 'COUNT_UNSUB' => array(
91 'data_type' => 'integer',
92 'default_value' => 0
93 ),
94 'COUNT_SEND_ALL' => array(
95 'data_type' => 'integer',
96 'default_value' => 0
97 ),
98 'COUNT_SEND_NONE' => array(
99 'data_type' => 'integer',
100 'default_value' => 0
101 ),
102 'COUNT_SEND_ERROR' => array(
103 'data_type' => 'integer',
104 'default_value' => 0
105 ),
106 'COUNT_SEND_SUCCESS' => array(
107 'data_type' => 'integer',
108 'default_value' => 0
109 ),
110 'COUNT_SEND_DENY' => array(
111 'data_type' => 'integer',
112 'default_value' => 0
113 ),
114 'CONSENT_SUPPORT' => array(
115 'data_type' => 'boolean',
116 'values' => array('N', 'Y'),
117 'default_value' => 'N',
118 'required' => true,
119 ),
120 'LETTER' => array(
121 'data_type' => 'Bitrix\Sender\Internals\Model\LetterTable',
122 'reference' => array('=this.MAILING_CHAIN_ID' => 'ref.ID'),
123 ),
124 'MAILING' => array(
125 'data_type' => 'Bitrix\Sender\MailingTable',
126 'reference' => array('=this.MAILING_ID' => 'ref.ID'),
127 ),
128 'MAILING_CHAIN' => array(
129 'data_type' => 'Bitrix\Sender\MailingChainTable',
130 'reference' => array('=this.MAILING_CHAIN_ID' => 'ref.ID'),
131 ),
132 'POSTING_RECIPIENT' => array(
133 'data_type' => 'Bitrix\Sender\PostingRecipientTable',
134 'reference' => array('=this.ID' => 'ref.POSTING_ID'),
135 ),
136 'POSTING_READ' => array(
137 'data_type' => 'Bitrix\Sender\PostingReadTable',
138 'reference' => array('=this.ID' => 'ref.POSTING_ID'),
139 ),
140 'POSTING_CLICK' => array(
141 'data_type' => 'Bitrix\Sender\PostingClickTable',
142 'reference' => array('=this.ID' => 'ref.POSTING_ID'),
143 ),
144 'POSTING_UNSUB' => array(
145 'data_type' => 'Bitrix\Sender\PostingUnsubTable',
146 'reference' => array('=this.ID' => 'ref.POSTING_ID'),
147 ),
148 );
149 }
150
156 public static function onDelete(Entity\Event $event)
157 {
158 $result = new Entity\EventResult;
159 $data = $event->getParameters();
160
161
162 $listId = array();
163 if(array_key_exists('ID', $data['primary']))
164 {
165 $listId[] = $data['primary']['ID'];
166 }
167 else
168 {
169 $filter = array();
170 foreach($data['primary'] as $primKey => $primVal)
171 $filter[$primKey] = $primVal;
172
173 $tableDataList = static::getList(array(
174 'select' => array('ID'),
175 'filter' => $filter
176 ));
177 while($tableData = $tableDataList->fetch())
178 {
179 $listId[] = $tableData['ID'];
180 }
181
182 }
183
184 foreach($listId as $primaryId)
185 {
186 $primary = array('POSTING_ID' => $primaryId);
191 }
192
193
194 return $result;
195 }
196
201 public static function addRecipient($ar, $checkDuplicate = false)
202 {
203 if(!$checkDuplicate)
204 {
205 $needAdd = true;
206 }
207 else
208 {
209 $row = PostingRecipientTable::getRow(array(
210 'select' => array('ID'),
211 'filter' => array(
212 '=CONTACT_ID' => $ar['CONTACT_ID'],
213 '=POSTING_ID' => $ar['POSTING_ID']
214 )
215 ));
216 if(!$row)
217 {
218 $needAdd = true;
219 }
220 else
221 {
222 $needAdd = false;
223 }
224 }
225
226 if($needAdd)
227 {
228 PostingRecipientTable::add($ar);
229 }
230 }
231
239 public static function initGroupRecipients($postingId, $checkDuplicate = true)
240 {
241 return PostingBuilder::create()->run($postingId, $checkDuplicate);
242 }
243
253 public static function getRecipientCountByStatus($id, ?array $customFilter = null)
254 {
255 $statusList = [];
256 $select = ['CNT', 'STATUS'];
257 $filter = !$customFilter?['POSTING_ID' => $id] : ['LOGIC' => 'AND',['POSTING_ID' => $id],$customFilter];
258 $postingContactDb = PostingRecipientTable::getList([
259 'select' => $select,
260 'filter' => $filter,
261 'runtime' => [new Entity\ExpressionField('CNT', 'COUNT(*)')],
262 ]);
263 while($postingContact = $postingContactDb->fetch())
264 $statusList[$postingContact['STATUS']] = intval($postingContact['CNT']);
265
266 return $statusList;
267 }
268
274 public static function getRecipientCount($id, $status = '')
275 {
276 $count = 0;
277
278 $ar = static::getRecipientCountByStatus($id);
279 if ($status != '')
280 $count = (array_key_exists($status, $ar) ? $ar[$status] : 0);
281 else
282 foreach ($ar as $k => $v) $count += $v;
283
284 return $count;
285 }
286
293 public static function getSendPercent($id)
294 {
295 $ar = static::getRecipientCountByStatus($id);
296 $count = 0;
297 foreach ($ar as $k => $v)
298 {
299 $count += $v;
300 }
301
302 $countNew = 0;
304 {
306 }
307
308 if($count > 0 && $countNew > 0)
309 {
310 return round(($count - $countNew) / $count, 2) * 100;
311 }
312 else
313 {
314 return 100;
315 }
316 }
317
321 public static function getRecipientStatusToPostingFieldMap()
322 {
323 return array(
324 PostingRecipientTable::SEND_RESULT_NONE => 'COUNT_SEND_NONE',
325 PostingRecipientTable::SEND_RESULT_ERROR => 'COUNT_SEND_ERROR',
326 PostingRecipientTable::SEND_RESULT_SUCCESS => 'COUNT_SEND_SUCCESS',
327 PostingRecipientTable::SEND_RESULT_DENY => 'COUNT_SEND_DENY',
328 );
329 }
330
338 public static function deleteList(array $filter)
339 {
340 $entity = static::getEntity();
341 $connection = $entity->getConnection();
342
343 \CTimeZone::disable();
344 $sql = sprintf(
345 'DELETE FROM %s WHERE %s',
346 $connection->getSqlHelper()->quote($entity->getDbTableName()),
347 Query::buildFilterSql($entity, $filter)
348 );
349 $res = $connection->query($sql);
350 \CTimeZone::enable();
351
352 return $res;
353 }
354}
355
356
357
359{
363 public static function getTableName()
364 {
365 return 'b_sender_posting_read';
366 }
367
371 public static function getMap()
372 {
373 return array(
374 'ID' => array(
375 'data_type' => 'integer',
376 'primary' => true,
377 'autocomplete' => true,
378 ),
379 'POSTING_ID' => array(
380 'primary' => true,
381 'data_type' => 'integer',
382 ),
383 'RECIPIENT_ID' => array(
384 'primary' => true,
385 'data_type' => 'integer',
386 ),
387 'DATE_INSERT' => array(
388 'data_type' => 'datetime',
389 'default_value' => new MainType\DateTime(),
390 ),
391 );
392 }
393
400 public static function onAfterAdd(Entity\Event $event)
401 {
402 $result = new Entity\EventResult;
403 $data = $event->getParameters();
404 $data = $data['fields'];
405
406 // update read flag of recipient
407 Model\Posting\RecipientTable::update($data['RECIPIENT_ID'], ['IS_READ' => 'Y']);
408
409 // update read counter of posting
410 $resultDb = static::getList(array('filter' => array('RECIPIENT_ID' => $data['RECIPIENT_ID'])));
411 if($resultDb->getSelectedRowsCount() == 1)
412 {
413 Model\PostingTable::update($data['POSTING_ID'], array(
414 'COUNT_READ' => new \Bitrix\Main\DB\SqlExpression('?# + 1', 'COUNT_READ')
415 ));
416 }
417
418 return $result;
419 }
420
428 public static function deleteList(array $filter)
429 {
430 $entity = static::getEntity();
431 $connection = $entity->getConnection();
432
433 \CTimeZone::disable();
434 $sql = sprintf(
435 'DELETE FROM %s WHERE %s',
436 $connection->getSqlHelper()->quote($entity->getDbTableName()),
437 Query::buildFilterSql($entity, $filter)
438 );
439 $res = $connection->query($sql);
440 \CTimeZone::enable();
441
442 return $res;
443 }
444}
445
446
448{
452 public static function getTableName()
453 {
454 return 'b_sender_posting_click';
455 }
456
460 public static function getMap()
461 {
462 return array(
463 'ID' => array(
464 'data_type' => 'integer',
465 'primary' => true,
466 'autocomplete' => true,
467 ),
468 'POSTING_ID' => array(
469 'primary' => true,
470 'data_type' => 'integer',
471 ),
472 'RECIPIENT_ID' => array(
473 'primary' => true,
474 'data_type' => 'integer',
475 ),
476 'DATE_INSERT' => array(
477 'data_type' => 'datetime',
478 'default_value' => new MainType\DateTime(),
479 ),
480 'URL' => array(
481 'data_type' => 'string',
482 ),
483 'POSTING' => array(
484 'data_type' => 'Bitrix\Sender\PostingTable',
485 'reference' => array('=this.POSTING_ID' => 'ref.ID'),
486 ),
487 );
488 }
489
496 public static function onAfterAdd(Entity\Event $event)
497 {
498 $result = new Entity\EventResult;
499 $data = $event->getParameters();
500 $data = $data['fields'];
501
502 // update click flag of recipient
503 Model\Posting\RecipientTable::update($data['RECIPIENT_ID'], ['IS_CLICK' => 'Y']);
504
505 // update click counter of posting
506 $resultDb = static::getList(
507 array(
508 'filter' => array('RECIPIENT_ID' => $data['RECIPIENT_ID']),
509 'limit' => 2,
510 )
511 );
512
513 if($resultDb->getSelectedRowsCount() == 1)
514 {
515 Model\PostingTable::update($data['POSTING_ID'], array(
516 'COUNT_CLICK' => new \Bitrix\Main\DB\SqlExpression('?# + 1', 'COUNT_CLICK')
517 ));
518 }
519
520 return $result;
521 }
522
523
531 public static function deleteList(array $filter)
532 {
533 $entity = static::getEntity();
534 $connection = $entity->getConnection();
535
536 \CTimeZone::disable();
537 $sql = sprintf(
538 'DELETE FROM %s WHERE %s',
539 $connection->getSqlHelper()->quote($entity->getDbTableName()),
540 Query::buildFilterSql($entity, $filter)
541 );
542 $res = $connection->query($sql);
543 \CTimeZone::enable();
544
545 return $res;
546 }
547}
548
550{
554 public static function getTableName()
555 {
556 return 'b_sender_posting_unsub';
557 }
558
562 public static function getMap()
563 {
564 return array(
565 'ID' => array(
566 'data_type' => 'integer',
567 'primary' => true,
568 'autocomplete' => true,
569 ),
570 'POSTING_ID' => array(
571 'primary' => true,
572 'data_type' => 'integer',
573 ),
574 'RECIPIENT_ID' => array(
575 'primary' => true,
576 'data_type' => 'integer',
577 ),
578 'DATE_INSERT' => array(
579 'data_type' => 'datetime',
580 'default_value' => new MainType\DateTime(),
581 ),
582 'POSTING' => array(
583 'data_type' => 'Bitrix\Sender\PostingTable',
584 'reference' => array('=this.POSTING_ID' => 'ref.ID'),
585 ),
586 'POSTING_RECIPIENT' => array(
587 'data_type' => 'Bitrix\Sender\PostingRecipientTable',
588 'reference' => array('=this.RECIPIENT_ID' => 'ref.ID'),
589 ),
590 );
591 }
592
599 public static function onAfterAdd(Entity\Event $event)
600 {
601 $result = new Entity\EventResult;
602 $data = $event->getParameters();
603 $data = $data['fields'];
604
605 // update unsub flag of recipient
606 Model\Posting\RecipientTable::update($data['RECIPIENT_ID'], ['IS_UNSUB' => 'Y']);
607
608 // update unsub counter of posting
609 $resultDb = static::getList(array('filter' => array('RECIPIENT_ID' => $data['RECIPIENT_ID'])));
610 if($resultDb->getSelectedRowsCount() == 1)
611 {
612 Model\PostingTable::update($data['POSTING_ID'], array(
613 'COUNT_UNSUB' => new \Bitrix\Main\DB\SqlExpression('?# + 1', 'COUNT_UNSUB')
614 ));
615 }
616
617 return $result;
618 }
619
620
628 public static function deleteList(array $filter)
629 {
630 $entity = static::getEntity();
631 $connection = $entity->getConnection();
632
633 \CTimeZone::disable();
634 $sql = sprintf(
635 'DELETE FROM %s WHERE %s',
636 $connection->getSqlHelper()->quote($entity->getDbTableName()),
637 Query::buildFilterSql($entity, $filter)
638 );
639 $res = $connection->query($sql);
640 \CTimeZone::enable();
641
642 return $res;
643 }
644}
645
663{
664 const SEND_RESULT_NONE = 'Y';
666 const SEND_RESULT_ERROR = 'E';
667 const SEND_RESULT_WAIT = 'W';
668 const SEND_RESULT_DENY = 'D';
670
671 protected static $personalizeList = null;
675 public static function getTableName()
676 {
677 return 'b_sender_posting_recipient';
678 }
679
683 public static function getMap()
684 {
685 return array(
686 'ID' => array(
687 'data_type' => 'integer',
688 'primary' => true,
689 'autocomplete' => true,
690 ),
691 'POSTING_ID' => array(
692 'primary' => true,
693 'data_type' => 'integer',
694 ),
695 'STATUS' => array(
696 'data_type' => 'string',
697 'primary' => true,
698 'required' => true,
699 'default_value' => static::SEND_RESULT_NONE,
700 ),
701 'DATE_SENT' => array(
702 'data_type' => 'datetime',
703 ),
704 'DATE_DENY' => array(
705 'data_type' => 'datetime',
706 ),
707 'CONTACT_ID' => array(
708 'required' => true,
709 'data_type' => 'integer',
710 ),
711 'USER_ID' => array(
712 'data_type' => 'integer',
713 ),
714 'FIELDS' => array(
715 'data_type' => 'text',
716 'serialized' => true,
717 ),
718 'ROOT_ID' => array(
719 'data_type' => 'integer',
720 ),
721 'IS_READ' => array(
722 'data_type' => 'string',
723 ),
724 'IS_CLICK' => array(
725 'data_type' => 'string',
726 ),
727 'IS_UNSUB' => array(
728 'data_type' => 'string',
729 ),
730 'CONTACT' => array(
731 'data_type' => 'Bitrix\Sender\ContactTable',
732 'reference' => array('=this.CONTACT_ID' => 'ref.ID'),
733 ),
734 'POSTING' => array(
735 'data_type' => 'Bitrix\Sender\PostingTable',
736 'reference' => array('=this.POSTING_ID' => 'ref.ID'),
737 ),
738 'POSTING_READ' => array(
739 'data_type' => 'Bitrix\Sender\PostingReadTable',
740 'reference' => array('=this.ID' => 'ref.RECIPIENT_ID'),
741 ),
742 'POSTING_CLICK' => array(
743 'data_type' => 'Bitrix\Sender\PostingClickTable',
744 'reference' => array('=this.ID' => 'ref.RECIPIENT_ID'),
745 ),
746 'POSTING_UNSUB' => array(
747 'data_type' => 'Bitrix\Sender\PostingUnsubTable',
748 'reference' => array('=this.ID' => 'ref.RECIPIENT_ID'),
749 ),
750
751 );
752 }
753
754 public static function setPersonalizeList(array $personalizeList = null)
755 {
756 static::$personalizeList = $personalizeList;
757 }
758
762 public static function getPersonalizeList()
763 {
764 $list = array(
765 array(
766 'CODE' => 'NAME',
767 'NAME' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_NAME"),
768 'DESC' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_NAME_DESC"),
769 ),
770 array(
771 'CODE' => 'EMAIL_TO',
772 'NAME' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_EMAIL"),
773 'DESC' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_EMAIL_DESC"),
774 ),
775 );
776 if (!Integration\Bitrix24\Service::isCloud())
777 {
778 $list[] = array(
779 'CODE' => 'USER_ID',
780 'NAME' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_USER_ID"),
781 'DESC' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_USER_ID_DESC"),
782 );
783 $list[] = array(
784 'CODE' => 'SITE_NAME',
785 'NAME' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_SITE_NAME"),
786 'DESC' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_SITE_NAME_DESC"),
787 );
788 $list[] = array(
789 'CODE' => 'SENDER_CHAIN_CODE',
790 'NAME' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_SENDER_CHAIN_ID"),
791 'DESC' => Loc::getMessage("SENDER_POSTING_PERSONALIZE_FIELD_SENDER_CHAIN_ID_DESC"),
792 );
793 }
794
795 return array_merge(
796 $list,
797 (static::$personalizeList ? static::$personalizeList : array())
798 );
799 }
800
804 public static function getStatusList()
805 {
806 return array(
807 self::SEND_RESULT_NONE => Loc::getMessage('SENDER_POSTING_RECIPIENT_STATUS_N'),
808 self::SEND_RESULT_SUCCESS => Loc::getMessage('SENDER_POSTING_RECIPIENT_STATUS_S'),
809 self::SEND_RESULT_ERROR => Loc::getMessage('SENDER_POSTING_RECIPIENT_STATUS_E'),
810 self::SEND_RESULT_DENY => Loc::getMessage('SENDER_POSTING_RECIPIENT_STATUS_D'),
811 self::SEND_RESULT_WAIT_ACCEPT => Loc::getMessage('SENDER_POSTING_RECIPIENT_STATUS_A')
812 );
813 }
814
815 public static function hasUnprocessed($postingId, $threadId = null)
816 {
817 return (static::getCount(['=POSTING_ID' => $postingId, '=STATUS' => self::SEND_RESULT_NONE]) > 0);
818 }
819
820
828 public static function deleteList(array $filter)
829 {
830 $entity = static::getEntity();
831 $connection = $entity->getConnection();
832
833 \CTimeZone::disable();
834 $sql = sprintf(
835 'DELETE FROM %s WHERE %s',
836 $connection->getSqlHelper()->quote($entity->getDbTableName()),
837 Query::buildFilterSql($entity, $filter)
838 );
839 $res = $connection->query($sql);
840 \CTimeZone::enable();
841
842 return $res;
843 }
844
850 public static function getConflictFields(): array
851 {
852 return [
853 'POSTING_ID',
854 'CONTACT_ID',
855 ];
856 }
857}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static deleteList(array $filter)
Definition posting.php:531
static onAfterAdd(Entity\Event $event)
Definition posting.php:496
static deleteList(array $filter)
Definition posting.php:428
static onAfterAdd(Entity\Event $event)
Definition posting.php:400
static deleteList(array $filter)
Definition posting.php:828
static setPersonalizeList(array $personalizeList=null)
Definition posting.php:754
static hasUnprocessed($postingId, $threadId=null)
Definition posting.php:815
static onDelete(Entity\Event $event)
Definition posting.php:156
static deleteList(array $filter)
Definition posting.php:338
static getRecipientCountByStatus($id, ?array $customFilter=null)
Definition posting.php:253
static getRecipientCount($id, $status='')
Definition posting.php:274
static addRecipient($ar, $checkDuplicate=false)
Definition posting.php:201
static initGroupRecipients($postingId, $checkDuplicate=true)
Definition posting.php:239
static getRecipientStatusToPostingFieldMap()
Definition posting.php:321
static deleteList(array $filter)
Definition posting.php:628
static onAfterAdd(Entity\Event $event)
Definition posting.php:599