Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
postingmanager.php
1<?php
9namespace Bitrix\Sender;
10
19use function MongoDB\BSON\fromPHP;
20
21Loc::loadMessages(__FILE__);
22
28{
29 const SEND_RESULT_ERROR = false;
30 const SEND_RESULT_SENT = true;
31 const SEND_RESULT_CONTINUE = 'CONTINUE';
32 const SEND_RESULT_WAIT = 'WAIT';
33 const SEND_RESULT_WAITING_RECIPIENT = 'WAITING_RECIPIENT';
34 public static $threadId;
35
37 protected static $checkStatusStep = 20;
38
40 protected static $emailSentPerIteration = 0;
41
49 public static function onMailEventMailRead(array $data)
50 {
51 $id = intval($data['RECIPIENT_ID']);
52 if ($id > 0)
53 {
54 static::read($id);
55 }
56
57 return $data;
58 }
59
67 public static function read($recipientId)
68 {
69 $postingContactPrimary = ['ID' => $recipientId];
70
71 $row = PostingRecipientTable::getRowById($postingContactPrimary);
72 if (!$row)
73 {
74 return;
75 }
76
77 if ($row['ID'])
78 {
79 PostingReadTable::add(
80 [
81 'POSTING_ID' => $row['POSTING_ID'],
82 'RECIPIENT_ID' => $row['ID'],
83 ]
84 );
85 }
86
87 if ($row['CONTACT_ID'])
88 {
89 ContactTable::update(
90 $row['CONTACT_ID'],
91 [
92 'IS_READ' => 'Y',
93 ]
94 );
95 }
96 }
97
105 public static function onMailEventMailClick(array $data)
106 {
107 $id = intval($data['RECIPIENT_ID']);
108 $url = $data['URL'];
109 if ($id > 0 && $url <> '')
110 {
111 static::click($id, $url);
112 }
113
114 return $data;
115 }
116
126 public static function click($recipientId, $url)
127 {
128 $postingContactPrimary = ['ID' => $recipientId];
129 $row = PostingRecipientTable::getRowById($postingContactPrimary);
130 if (!$row)
131 {
132 return;
133 }
134
135 if ($row['ID'])
136 {
137 $read = PostingReadTable::getRowById(
138 [
139 'POSTING_ID' => $row['POSTING_ID'],
140 'RECIPIENT_ID' => $row['ID']
141 ]
142 );
143 if ($read === null)
144 {
145 static::read($recipientId);
146 }
147
148 $postingDb = PostingTable::getList(
149 [
150 'select' => ['ID'],
151 'filter' => ['=ID' => $row['POSTING_ID']],
152 ]
153 );
154 if ($postingDb->fetch())
155 {
156 $deleteParameters = ['bx_sender_conversion_id'];
157 $letter = Entity\Letter::createInstanceByPostingId($row['POSTING_ID']);
158 $linkParams = $letter->getMessage()
159 ->getConfiguration()
160 ->get('LINK_PARAMS');
161 if ($linkParams)
162 {
163 $parametersTmp = [];
164 parse_str($linkParams, $parametersTmp);
165 if (is_array($parametersTmp))
166 {
167 $parametersTmp = array_keys($parametersTmp);
168 $deleteParameters = array_merge($deleteParameters, $parametersTmp);
169 }
170 }
171
172 $uri = new \Bitrix\Main\Web\Uri($url);
173 $fixedUrl = $uri->deleteParams($deleteParameters, false)
174 ->getUri();
175
176 $fixedUrl = urldecode($fixedUrl);
177
178 if(mb_strpos($fixedUrl, 'pub/mail/unsubscribe.php') === false)
179 {
180 $addClickDb = PostingClickTable::add(
181 [
182 'POSTING_ID' => $row['POSTING_ID'],
183 'RECIPIENT_ID' => $row['ID'],
184 'URL' => $fixedUrl
185 ]
186 );
187 }
188
189 if ($addClickDb && $addClickDb->isSuccess())
190 {
191 // send event
192 $eventData = [
193 'URL' => $url,
194 'URL_FIXED' => $fixedUrl,
195 'CLICK_ID' => $addClickDb->getId(),
196 'RECIPIENT' => $row
197 ];
198 $event = new Event('sender', 'OnAfterRecipientClick', [$eventData]);
199 $event->send();
200 }
201 }
202 }
203
204 if ($row['CONTACT_ID'])
205 {
206 ContactTable::update(
207 $row['CONTACT_ID'],
208 [
209 'IS_CLICK' => 'Y',
210 ]
211 );
212 }
213 }
214
223 public static function getChainReSend($mailingId)
224 {
225 $result = [];
226 $mailChainDb = MailingChainTable::getList(
227 [
228 'select' => ['ID'],
229 'filter' => [
230 '=MAILING.ID' => $mailingId,
231 '=MAILING.ACTIVE' => 'Y',
232 '=REITERATE' => 'N',
233 '=MAILING_CHAIN.STATUS' => MailingChainTable::STATUS_END,
234 ]
235 ]
236 );
237 while ($mailChain = $mailChainDb->fetch())
238 {
239 $result[] = $mailChain['ID'];
240 }
241
242 return (empty($result) ? null : $result);
243 }
244
254 public static function sendToAddress($mailingChainId, $address)
255 {
256 $recipientEmail = $address;
257 $emailParts = explode('@', $recipientEmail);
258 $recipientName = $emailParts[0];
259
260 global $USER;
261
262 $mailingChain = MailingChainTable::getRowById(['ID' => $mailingChainId]);
263 $fields = [
264 'NAME' => $recipientName,
265 'EMAIL_TO' => $address,
266 'USER_ID' => $USER->GetID(),
267 'SENDER_CHAIN_ID' => $mailingChain["ID"],
268 'SENDER_CHAIN_CODE' => 'sender_chain_item_'.$mailingChain["ID"]
269 ];
270
271 $letter = new Entity\Letter($mailingChainId);
272 $message = $letter->getMessage();
273
274 $siteId = MailingTable::getMailingSiteId($mailingChain['MAILING_ID']);
275
276 $message->getReadTracker()
277 ->setModuleId('sender')
278 ->setFields(['RECIPIENT_ID' => 0])
279 ->setSiteId($siteId);
280
281 $message->getClickTracker()
282 ->setModuleId('sender')
283 ->setFields(['RECIPIENT_ID' => 0])
284 ->setUriParameters(['bx_sender_conversion_id' => 0])
285 ->setSiteId($siteId);
286
287 $message->getUnsubTracker()
288 ->setModuleId('sender')
289 ->setFields(
290 [
291 'MAILING_ID' => !empty($mailingChain) ? $mailingChain['MAILING_ID'] : 0,
292 'EMAIL' => $address,
293 'TEST' => 'Y'
294 ]
295 )
296 ->setSiteId($siteId);
297
298 $message->getUnsubTracker()
299 ->setHandlerUri(Option::get('sender', 'unsub_link'));
300
301 $message->setFields($fields);
302 $result = $message->send();
303
304 return $result ? static::SEND_RESULT_SENT : static::SEND_RESULT_ERROR;
305 }
306
318 public static function send($id, $timeout = 0, $maxMailCount = 0)
319 {
320 $letter = Entity\Letter::createInstanceByPostingId($id);
321 $sender = new Posting\Sender($letter);
322
323 $sender->setThreadStrategy(
324 MessageMarketingFb::checkSelf($sender->getMessage()->getCode()) ?
325 ThreadStrategyContext::buildStrategy(
326 IThreadStrategy::SINGLE
327 ): Runtime\Env::getThreadContext()
328 )
329 ->setLimit($maxMailCount)
330 ->setTimeout($timeout)
331 ->send();
332
333 static::$threadId = $sender->getThreadStrategy()->getThreadId();
334
335 switch ($sender->getResultCode())
336 {
337 case Posting\Sender::RESULT_CONTINUE:
338 $result = static::SEND_RESULT_CONTINUE;
339 break;
340 case Posting\Sender::RESULT_ERROR:
341 $result = static::SEND_RESULT_ERROR;
342 break;
343 case Posting\Sender::RESULT_WAITING_RECIPIENT:
344 $result = static::SEND_RESULT_WAITING_RECIPIENT;
345 break;
346 case Posting\Sender::RESULT_WAIT:
347 $result = static::SEND_RESULT_WAIT;
348 break;
349 case Posting\Sender::RESULT_SENT:
350 default:
351 $result = static::SEND_RESULT_SENT;
352 break;
353 }
354
355 $limiter = $sender->getExceededLimiter();
356 if ($result === static::SEND_RESULT_CONTINUE && $limiter)
357 {
358 // update planned date only with timed limit;
359
360 if ($limiter->getParameter('sendingStart'))
361 {
362 $currentTime = $limiter->getParameter('currentTime');
363 $sendingStart = $limiter->getParameter('sendingStart');
364 $sendingStartDate = (new \DateTime())->setTimestamp($sendingStart);
365
366 $sendingStartDate = $currentTime > $sendingStart
367 ? $sendingStartDate->add(\DateInterval::createFromDateString('1 day'))
368 : $sendingStartDate;
369
370 $date = Type\DateTime::createFromPhp($sendingStartDate);
371 }
372 elseif ($limiter->getUnit())
373 {
374 $date = new Type\Date();
375 $date->add('1 day');
376 }
377 else
378 {
379 $date = new Type\DateTime();
380 $date->add('2 minute');
381 }
382
383 $letter->getState()->updatePlannedDateSend($date);
384 }
385
386 return $result;
387 }
388
401 public static function lockPosting($id, $threadId)
402 {
403 return Posting\Sender::lock($id, $threadId);
404 }
405
418 public static function unlockPosting($id, $threadId)
419 {
420 return Posting\Sender::unlock($id, $threadId);
421 }
422}
static loadMessages($file)
Definition loc.php:64
static getMailingSiteId($mailingId)
Definition mailing.php:659
static send($id, $timeout=0, $maxMailCount=0)
static click($recipientId, $url)
static unlockPosting($id, $threadId)
static read($recipientId)
static lockPosting($id, $threadId)
static sendToAddress($mailingChainId, $address)
static onMailEventMailRead(array $data)
static onMailEventMailClick(array $data)
static getChainReSend($mailingId)