Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
subscription.php
1<?php
8namespace Bitrix\Sender;
9
20
22{
23 const MODULE_ID = 'sender';
24
31 public static function getLinkUnsub(array $fields)
32 {
33 return Tracking::getLinkUnsub(static::MODULE_ID, $fields, Option::get('sender', 'unsub_link'));
34 }
35
42 public static function getLinkSub(array $fields)
43 {
44 $tag = Tracking::getSignedTag(static::MODULE_ID, $fields);
45 $urlPage = Option::get('sender', 'sub_link');
46 if($urlPage == "")
47 {
48 $bitrixDirectory = Application::getInstance()->getPersonalRoot();
49 $result = $bitrixDirectory.'/tools/sender_sub_confirm.php?sender_subscription=confirm&tag='.urlencode($tag);
50 }
51 else
52 {
53 $result = $urlPage.(mb_strpos($urlPage, "?") === false ? "?" : "&").'sender_subscription=confirm&tag='.urlencode($tag);
54 }
55
56 return $result;
57 }
58
65 public static function onMailEventSubscriptionList($data)
66 {
67 $data['LIST'] = static::getSubscriptions($data);
68
69 return $data;
70 }
71
72 protected static function getSubscriptions($data)
73 {
74 $resultMailingList = array();
75
76 $mailing = MailingTable::getRowById(array('ID' => $data['MAILING_ID']));
77 if(isset($data['TEST']) && $data['TEST'] == 'Y')
78 {
79 $resultMailingList[] = array(
80 'ID' => $mailing['ID'],
81 'NAME' => $mailing['NAME'],
82 'DESC' => $mailing['DESCRIPTION'],
83 'SELECTED' => true,
84 );
85
86 return $resultMailingList;
87 }
88 $contactData = $contactId = null;
89 if($data['RECIPIENT_ID'] && $recipient = PostingRecipientTable::getRowById(array('ID' => $data['RECIPIENT_ID'])))
90 {
91 if(isset($data['CONTACT_ID']) && $recipient['CONTACT_ID'] != $data['CONTACT_ID'])
92 {
93 return [];
94 }
95 $contactData = ContactTable::getRowById($contactId = $recipient['CONTACT_ID']);
96 }
97 elseif($data['CONTACT_ID'] && $contactData = ContactTable::getRowById($data['CONTACT_ID']))
98 {
99 $contactId = $contactData['ID'];
100 }
101 else
102 {
103 return [];
104 }
105
106 if ($contactData && $contactData['BLACKLISTED'] === 'Y')
107 {
108 return [];
109 }
110
111 $mailingUnsub = array();
113 'select' => array('MAILING_ID'),
114 'filter' => array(
115 '=CONTACT.ID' => $contactId,
116 '=MAILING.SITE_ID' => $mailing['SITE_ID']
117 )
118 ));
119 while($unSub = $unSubDb->fetch())
120 {
121 $mailingUnsub[] = $unSub['MAILING_ID'];
122 }
123
124 $mailingList = array();
125 // all receives mailings
126 $mailingDb = PostingRecipientTable::getList(array(
127 'select' => array('MAILING_ID' => 'POSTING.MAILING.ID'),
128 'filter' => array(
129 '=CONTACT_ID' => $contactId,
130 '=POSTING.MAILING.ACTIVE' => 'Y',
131 '=POSTING.MAILING.SITE_ID' => $mailing['SITE_ID']
132 ),
133 'group' => array('MAILING_ID')
134 ));
135 while ($mailing = $mailingDb->fetch())
136 {
137 $mailingList[] = $mailing['MAILING_ID'];
138 }
139
140 // all subscribed mailings
142 'select' => array('MAILING_ID'),
143 'filter' => array(
144 '=CONTACT.ID' => $contactId,
145 '=MAILING.ACTIVE' => 'Y',
146 '=MAILING.SITE_ID' => $mailing['SITE_ID']
147 )
148 ));
149 while ($mailing = $mailingDb->fetch())
150 {
151 $mailingList[] = $mailing['MAILING_ID'];
152 }
153
154 $mailingList = array_unique($mailingList);
155 foreach($mailingList as $mailingId)
156 {
157 if(!in_array($mailingId, $mailingUnsub))
158 {
159 $mailingDesc = MailingTable::getRowById($mailingId);
160 if($mailingDesc)
161 {
162 $resultMailingList[] = array(
163 'ID' => $mailingDesc['ID'],
164 'NAME' => $mailingDesc['NAME'],
165 'DESC' => $mailingDesc['DESCRIPTION'],
166 'SELECTED' => in_array($mailingDesc['ID'], array($data['MAILING_ID'])),
167 );
168 }
169 }
170 }
171
172 return $resultMailingList;
173 }
174
181 public static function onMailEventSubscriptionEnable($data)
182 {
183 $data['SUCCESS'] = static::subscribe($data);
184 if($data['SUCCESS'])
185 $result = EventResult::SUCCESS;
186 else
187 $result = EventResult::ERROR;
188
189 return new EventResult($result, $data, static::MODULE_ID);
190 }
191
198 public static function onMailEventSubscriptionDisable($data)
199 {
200 $data['SUCCESS'] = static::unsubscribeRecipient($data);
201 if($data['SUCCESS'])
202 $result = EventResult::SUCCESS;
203 else
204 $result = EventResult::ERROR;
205
206 return new EventResult($result, $data, static::MODULE_ID);
207 }
208
209 protected static function unsubscribeRecipient($data)
210 {
211 if(isset($data['TEST']) && $data['TEST'] == 'Y')
212 {
213 return true;
214 }
215
216 if(!$data['RECIPIENT_ID'])
217 {
218 return true;
219 }
220
221 $data['ABUSE'] = isset($data['ABUSE']) ? (bool) $data['ABUSE'] : false;
222 $data['ABUSE_TEXT'] = isset($data['ABUSE_TEXT']) ? $data['ABUSE_TEXT'] : null;
223 $result = false;
224 $recipient = PostingRecipientTable::getRow([
225 'select' => [
226 'ID', 'CONTACT_ID', 'CONTACT_CODE' => 'CONTACT.CODE', 'CONTACT_TYPE_ID' => 'CONTACT.TYPE_ID',
227 'POSTING_ID', 'POSTING_MAILING_ID' => 'POSTING.MAILING_ID'
228 ],
229 'filter' => ['=ID' => $data['RECIPIENT_ID']]
230 ]);
231 $recipient = ((!$recipient && $data['CONTACT_ID'])? ContactTable::getRow([
232 'select' => [
233 'CONTACT_ID' => 'ID', 'CONTACT_TYPE_ID' => 'TYPE_ID', 'CONTACT_CODE' => 'CODE'
234 ],
235 'filter' => ['=CONTACT_ID' => $data['CONTACT_ID']]
236 ]) : $recipient);
237 if
238 (
239 !$recipient ||
240 !$recipient['CONTACT_ID'] ||
241 ($data['CONTACT_ID']? $data['CONTACT_ID'] != $recipient['CONTACT_ID'] : false)
242 )
243 {
244 return true;
245 }
246 $contactId = $recipient['CONTACT_ID'];
247 $mailingDb = MailingTable::getList(array(
248 'select' => array('ID'),
249 'filter' => array(
250 '=ID' => $data['UNSUBSCRIBE_LIST'],
251 )
252 ));
253 while($mailing = $mailingDb->fetch())
254 {
255 $primary = null;
256 if(
257 isset($recipient['POSTING_MAILING_ID'],$recipient['POSTING_ID'],$recipient['ID']) &&
258 $recipient['POSTING_MAILING_ID'] == $mailing['ID']
259 )
260 {
261 $primary = array(
262 'POSTING_ID' => $recipient['POSTING_ID'],
263 'RECIPIENT_ID' => $recipient['ID'],
264 );
265 }
266 else
267 {
268 $mailingPostingDb = PostingRecipientTable::getList(array(
269 'select' => array('RECIPIENT_ID' => 'ID', 'POSTING_ID'),
270 'filter' => array(
271 '=POSTING.MAILING_ID' => $mailing['ID'],
272 '=CONTACT_ID' => $contactId
273 )
274 ));
275 if($mailingPosting = $mailingPostingDb->fetch())
276 {
277 $primary = $mailingPosting;
278 }
279 }
280
281 // add mark in statistic if there is no previous mark
282 if(!empty($primary))
283 {
284 $unsubExists = PostingUnsubTable::getRowById($primary);
285 if(!$unsubExists)
286 {
287 $unsubResult = PostingUnsubTable::add($primary);
288 if($unsubResult->isSuccess())
289 {
290 $eventData = array(
291 'ABUSE' => $data['ABUSE'],
292 'ABUSE_TEXT' => $data['ABUSE_TEXT'],
293 'MAILING_ID' => $mailing['ID'],
294 'RECIPIENT_ID' => $primary['RECIPIENT_ID'],
295 'CONTACT_ID' => $contactId,
296 'EMAIL' => $data['EMAIL'],
297 );
298 $event = new Event('sender', 'OnAfterRecipientUnsub', array($eventData));
299 $event->send();
300
301 if ($data['ABUSE'])
302 {
303 AbuseTable::add(array(
304 'TEXT' => $data['ABUSE_TEXT'],
305 'CONTACT_ID' => $contactId,
306 'CONTACT_CODE' => $recipient['CONTACT_CODE'],
307 'CONTACT_TYPE_ID' => $recipient['CONTACT_TYPE_ID'],
308 ))->isSuccess();
309 }
310
311 Integration\EventHandler::onAfterPostingRecipientUnsubscribe($eventData);
312 }
313 }
314
315 $result = true;
316 }
317
319 'MAILING_ID' => $mailing['ID'],
320 'CONTACT_ID' => $contactId
321 ));
322
323 if ($contactId && $data['ABUSE'])
324 {
325 ContactTable::update($contactId, array('BLACKLISTED' => 'Y'));
326 }
327 }
328
329 return $result;
330 }
331
340 public static function getList($data)
341 {
342 $resultMailingList = array();
343
344 $mailing = MailingTable::getRowById(array('ID' => $data['MAILING_ID']));
345 if(isset($data['TEST']) && $data['TEST'] == 'Y')
346 {
347 $resultMailingList[] = array(
348 'ID' => $mailing['ID'],
349 'NAME' => $mailing['NAME'],
350 'DESC' => $mailing['DESCRIPTION'],
351 'SELECTED' => true,
352 );
353
354 return $resultMailingList;
355 }
356
357 $mailingUnsub = array();
359 'select' => array('MAILING_ID'),
360 'filter' => array(
361 '=CONTACT.EMAIL' => trim(mb_strtolower($data['EMAIL'])),
362 '=MAILING.SITE_ID' => $mailing['SITE_ID']
363 )
364 ));
365 while($unSub = $unSubDb->fetch())
366 $mailingUnsub[] = $unSub['MAILING_ID'];
367
368 $mailingList = array();
369 // all receives mailings
370 $receiveMailingDb = PostingRecipientTable::getList(array(
371 'select' => array('MAILING_ID' => 'POSTING.MAILING.ID'),
372 'filter' => array(
373 '=EMAIL' => trim(mb_strtolower($data['EMAIL'])),
374 '=POSTING.MAILING.ACTIVE' => 'Y',
375 '=POSTING.MAILING.SITE_ID' => $mailing['SITE_ID']
376 ),
377 'group' => array('MAILING_ID')
378 ));
379 while ($receiveMailing = $receiveMailingDb->fetch())
380 {
381 $mailingList[] = $receiveMailing['MAILING_ID'];
382 }
383
384 // all subscribed mailings
385 $subscribedMailingDb = MailingSubscriptionTable::getSubscriptionList(array(
386 'select' => array('MAILING_ID'),
387 'filter' => array(
388 '=CONTACT.EMAIL' => trim(mb_strtolower($data['EMAIL'])),
389 '=MAILING.ACTIVE' => 'Y',
390 '=MAILING.SITE_ID' => $mailing['SITE_ID']
391 )
392 ));
393 while ($subscribedMailing = $subscribedMailingDb->fetch())
394 {
395 $mailingList[] = $subscribedMailing['MAILING_ID'];
396 }
397
398 $mailingList = array_unique($mailingList);
399 foreach($mailingList as $mailingId)
400 {
401 if(!in_array($mailingId, $mailingUnsub))
402 {
403 $mailingDesc = MailingTable::getRowById($mailingId);
404 if($mailingDesc)
405 {
406 $resultMailingList[] = array(
407 'ID' => $mailingDesc['ID'],
408 'NAME' => $mailingDesc['NAME'],
409 'DESC' => $mailingDesc['DESCRIPTION'],
410 'SELECTED' => in_array($mailingDesc['ID'], array($data['MAILING_ID'])),
411 );
412 }
413 }
414 }
415
416 return $resultMailingList;
417 }
418
425 public static function subscribe(array $data)
426 {
427 $id = static::add($data['EMAIL'], $data['SUBSCRIBE_LIST']);
428 if($id)
429 {
430 return true;
431 }
432
433 return false;
434 }
435
444 public static function unsubscribe($data)
445 {
446 $result = false;
447
448 if(isset($data['TEST']) && $data['TEST'] == 'Y')
449 return true;
450
451 $data['ABUSE'] = isset($data['ABUSE']) ? (bool) $data['ABUSE'] : false;
452 $data['ABUSE_TEXT'] = isset($data['ABUSE_TEXT']) ? $data['ABUSE_TEXT'] : null;
453
454 $posting = null;
455 if($data['RECIPIENT_ID'])
456 {
457 $postingDb = PostingRecipientTable::getList(array(
458 'select' => array('POSTING_ID', 'POSTING_MAILING_ID' => 'POSTING.MAILING_ID'),
459 'filter' => array(
460 '=ID' => $data['RECIPIENT_ID'],
461 '=CONTACT.CODE' => $data['EMAIL'],
462 '=CONTACT.TYPE_ID' => Recipient\Type::EMAIL,
463 )
464 ));
465 $posting = $postingDb->fetch();
466 }
467
468 $mailingDb = MailingTable::getList(array(
469 'select' => array('ID'),
470 'filter' => array(
471 '=ID' => $data['UNSUBSCRIBE_LIST'],
472 )
473 ));
474 while($mailing = $mailingDb->fetch())
475 {
476 $unsub = null;
477
478 if($posting && $posting['POSTING_MAILING_ID'] == $mailing['ID'])
479 {
480 $unsub = array(
481 'POSTING_ID' => $posting['POSTING_ID'],
482 'RECIPIENT_ID' => $data['RECIPIENT_ID'],
483 'CONTACT_ID' => isset($data['CONTACT_ID']) ? (int) $data['CONTACT_ID'] : null,
484 );
485 }
486 else
487 {
488 $mailingPostingDb = PostingRecipientTable::getList(array(
489 'select' => array('RECIPIENT_ID' => 'ID', 'CONTACT_ID', 'POSTING_ID'),
490 'filter' => array(
491 '=POSTING.MAILING_ID' => $mailing['ID'],
492 '=CONTACT.CODE' => $data['EMAIL'],
493 '=CONTACT.TYPE_ID' => Recipient\Type::EMAIL,
494 ),
495 'limit' => 1
496 ));
497 if($mailingPosting = $mailingPostingDb->fetch())
498 {
499 $unsub = $mailingPosting;
500 }
501 }
502
503 // add mark in statistic if there is no previous mark
504 if(!empty($unsub))
505 {
506 if ($unsub['CONTACT_ID'] && $data['ABUSE'])
507 {
508 ContactTable::update($unsub['CONTACT_ID'], array('BLACKLISTED' => 'Y'));
509 }
510
511 $unsubExists = PostingUnsubTable::getRowById($unsub);
512 if(!$unsubExists)
513 {
514 $unsubResult = PostingUnsubTable::add($unsub);
515 if($unsubResult->isSuccess())
516 {
517 $eventData = array(
518 'ABUSE' => $data['ABUSE'],
519 'ABUSE_TEXT' => $data['ABUSE_TEXT'],
520 'CAMPAIGN_ID' => $mailing['ID'],
521 'MAILING_ID' => $mailing['ID'],
522 'RECIPIENT_ID' => $unsub['RECIPIENT_ID'],
523 'EMAIL' => $data['EMAIL'],
524 );
525 $event = new Event('sender', 'OnAfterRecipientUnsub', array($eventData));
526 $event->send();
527
528 if ($data['ABUSE'])
529 {
530 AbuseTable::add(array(
531 'TEXT' => $data['ABUSE_TEXT'],
532 'CONTACT_ID' => $unsub['CONTACT_ID'],
533 'CONTACT_CODE' => $data['EMAIL'],
534 'CONTACT_TYPE_ID' => Recipient\Type::EMAIL,
535 ));
536 }
537
538 Integration\EventHandler::onAfterPostingRecipientUnsubscribe($eventData);
539 }
540 }
541
542 $result = true;
543 }
544
545 // add row to unsubscribe list
546 $contactId = ContactTable::addIfNotExist(array('EMAIL' => $data['EMAIL']));
547 if($contactId)
548 {
549 MailingSubscriptionTable::addUnSubscription(array('MAILING_ID' => $mailing['ID'], 'CONTACT_ID' => $contactId));
550 $result = true;
551 }
552 }
553
554 return $result;
555 }
556
564 public static function add($code, array $mailingIdList)
565 {
566 $contactId = null;
567
568 $typeId = Recipient\Type::detect($code);
569 $code = Recipient\Normalizer::normalize($code, $typeId);
570 $contact = ContactTable::getRow([
571 'select' => ['ID'],
572 'filter' => [
573 '=CODE' => $code,
574 '=TYPE_ID' => $typeId,
575 ]
576 ]);
577 if($contact)
578 {
579 $contactId = $contact['ID'];
580 }
581 else
582 {
583 $contactAddDb = ContactTable::add(['TYPE_ID' => $typeId, 'CODE' => $code]);
584 if($contactAddDb->isSuccess())
585 {
586 $contactId = $contactAddDb->getId();
587 }
588 }
589
590 if(!empty($contactId))
591 {
592 foreach ($mailingIdList as $mailingId)
593 {
595 'MAILING_ID' => $mailingId, 'CONTACT_ID' => $contactId
596 ));
597 }
598 }
599
600 return $contactId;
601 }
602
609 public static function getMailingList($params)
610 {
611 $filter = array("ACTIVE" => "Y", "IS_TRIGGER" => "N");
612 if(isset($params["SITE_ID"]))
613 $filter["SITE_ID"] = $params["SITE_ID"];
614 if(isset($params["IS_PUBLIC"]))
615 $filter["IS_PUBLIC"] = $params["IS_PUBLIC"];
616 if(isset($params["ACTIVE"]))
617 $filter["ACTIVE"] = $params["ACTIVE"];
618 if(isset($params["ID"]))
619 $filter["ID"] = $params["ID"];
620
621 $mailingList = array();
622 $mailingDb = MailingTable::getList(array(
623 'select' => array('ID', 'NAME', 'DESCRIPTION', 'IS_PUBLIC'),
624 'filter' => $filter,
625 'order' => array('SORT' => 'ASC', 'NAME' => 'ASC'),
626 ));
627 while($mailing = $mailingDb->fetch())
628 {
629 $mailingList[] = $mailing;
630 }
631
632 return $mailingList;
633 }
634
643 public static function sendEventConfirm($email, array $mailingIdList, $siteId)
644 {
645 $mailingNameList = array();
646 $mailingDb = MailingTable::getList(array('select' => array('NAME'), 'filter' => array("IS_TRIGGER" => "N", 'ID' => $mailingIdList)));
647 while($mailing = $mailingDb->fetch())
648 {
649 $mailingNameList[] = $mailing['NAME'];
650 }
651
652 $subscription = array(
653 'EMAIL' => $email,
654 'SITE_ID' => $siteId,
655 'MAILING_LIST' => $mailingIdList,
656 );
657 $confirmUrl = static::getLinkSub($subscription);
658 $date = new DateTime;
659 $eventSendFields = array(
660 "EVENT_NAME" => "SENDER_SUBSCRIBE_CONFIRM",
661 "C_FIELDS" => array(
662 "EMAIL" => $email,
663 "DATE" => $date->toString(),
664 "CONFIRM_URL" => $confirmUrl,
665 "MAILING_LIST" => implode("\r\n",$mailingNameList),
666 ),
667 "LID" => is_array($siteId)? implode(",", $siteId): $siteId,
668 );
669 \Bitrix\Main\Mail\Event::send($eventSendFields);
670 }
671
680 public static function isUnsubscibed($mailingId, $code, $typeId = Recipient\Type::EMAIL)
681 {
682 $code = Recipient\Normalizer::normalize($code, $typeId);
683 $filter = [
684 '=MAILING_ID' => $mailingId,
685 '=CONTACT.CODE' => $code,
686 '=CONTACT.TYPE_ID' => $typeId,
687 '=IS_UNSUB' => 'Y'
688 ];
689 $unSubDb = MailingSubscriptionTable::getList([
690 'select' => array('MAILING_ID'),
691 'filter' => $filter,
692 ]);
693 if($unSubDb->fetch())
694 {
695 return true;
696 }
697
698 return false;
699 }
700}
static addIfNotExist(array $ar)
Definition contact.php:332
static addUnSubscription(array $parameters=array())
Definition mailing.php:874
static getSubscriptionList(array $parameters=array())
Definition mailing.php:826
static addSubscription(array $parameters=array())
Definition mailing.php:851
static getUnSubscriptionList(array $parameters=array())
Definition mailing.php:838
static getLinkSub(array $fields)
static getLinkUnsub(array $fields)
static getMailingList($params)
static onMailEventSubscriptionEnable($data)
static onMailEventSubscriptionDisable($data)
static subscribe(array $data)
static sendEventConfirm($email, array $mailingIdList, $siteId)
static onMailEventSubscriptionList($data)
static isUnsubscibed($mailingId, $code, $typeId=Recipient\Type::EMAIL)
static add($code, array $mailingIdList)
static unsubscribeRecipient($data)