18 private static $optionName =
'~update_counters_16';
20 private static $startTime =
null;
21 private static $stages = array(
22 'RECIPIENT_READ' =>
'1',
23 'RECIPIENT_CLICK' =>
'2',
24 'RECIPIENT_UNSUB' =>
'3',
25 'POSTING_STATUS' =>
'4',
26 'POSTING_READ' =>
'5',
27 'POSTING_CLICK' =>
'6',
28 'POSTING_UNSUB' =>
'7',
29 'MAILING_SUBSCRIPTION' =>
'8',
30 'CONTACT_EMAIL_REGISTER' =>
'9',
36 $currentValue = Option::get(
'sender', self::$optionName,
'');
37 $stageValue = self::$stages[$stageCode];
39 if($currentValue ===
'')
41 $currentValue =
'100';
43 if($stageValue ===
'')
48 if(intval($currentValue) >= intval($stageValue))
60 Option::set(
'sender', self::$optionName, self::$stages[$stageCode]);
65 $currentValue = Option::get(
'sender', self::$optionName,
'');
66 if($currentValue ===
'')
68 $currentValue = count(self::$stages);
70 $currentValue = intval($currentValue);
72 return array(
'CURRENT' => $currentValue,
'ALL' => count(self::$stages));
75 private static function isTimeUp()
77 if(self::$startTime ===
null)
79 self::$startTime = getmicrotime();
82 if(self::$maxExecutionTime > 0 && (getmicrotime() - self::$startTime) > self::$maxExecutionTime)
100 return getmicrotime() - self::$startTime;
106 if(self::wasCompleted())
114 foreach(array(
'READ',
'CLICK',
'UNSUB') as $type)
116 if($haveData || self::wasCompleted(
'RECIPIENT_' . $type))
129 if(!$haveData && !self::wasCompleted(
'POSTING_STATUS'))
139 foreach(array(
'READ',
'CLICK',
'UNSUB') as $type)
141 if($haveData || self::wasCompleted(
'POSTING_' . $type))
154 if(!$haveData && !self::wasCompleted(
'MAILING_SUBSCRIPTION'))
164 if(!$haveData && !self::wasCompleted(
'CONTACT_EMAIL_REGISTER'))
185 'select' => array(
'RECIPIENT_ID'),
189 'Bitrix\Sender\PostingRecipientTable',
190 array(
'=this.RECIPIENT_ID' =>
'ref.ID')
194 '!UPDATE_RECIPIENT.ID' =>
null,
195 '=UPDATE_RECIPIENT.IS_' . $type =>
'N',
197 'group' => array(
'RECIPIENT_ID')
203 $dataDb = \Bitrix\Sender\PostingReadTable::getList($params);
206 $dataDb = \Bitrix\Sender\PostingClickTable::getList($params);
209 $dataDb = \Bitrix\Sender\PostingUnsubTable::getList($params);
218 while($item = $dataDb->fetch())
225 Model\Posting\RecipientTable::update($item[
'RECIPIENT_ID'], [
'IS_' . $type =>
'Y']);
233 $lastPostingId =
null;
234 $statusList = array();
236 $resultDb = \Bitrix\Sender\PostingRecipientTable::getList(array(
237 'select' => array(
'POSTING_ID',
'STATUS',
'CALC_COUNT'),
239 '!UPDATE_POSTING.ID' =>
null,
241 '=UPDATE_POSTING.COUNT_SEND_ALL' => 0,
248 'Bitrix\Sender\PostingTable',
249 array(
'=this.POSTING_ID' =>
'ref.ID'),
250 array(
'join_type' =>
'INNER')
253 'order' => array(
'CALC_COUNT' =>
'DESC',
'POSTING_ID' =>
'ASC'),
263 $data = $resultDb->fetch();
266 if(!$data || $lastPostingId != $data[
'POSTING_ID'])
269 $updateFields = self::getPostingStatusUpdateFields($lastPostingId, $statusList);
272 Model\PostingTable::update($lastPostingId, $updateFields);
276 $statusList = array();
281 $lastPostingId = $data[
'POSTING_ID'];
282 $statusList[$data[
'STATUS']] = $data[
'CALC_COUNT'];
299 $dataDb = \Bitrix\Sender\PostingRecipientTable::getList(array(
305 '=UPDATE_POSTING.COUNT_' . $type => 0,
307 '=IS_' . $type =>
'Y'
312 'Bitrix\Sender\PostingTable',
313 array(
'=this.POSTING_ID' =>
'ref.ID'),
314 array(
'join_type' =>
'INNER')
318 'order' => array(
'CNT' =>
'DESC',
'POSTING_ID' =>
'ASC'),
321 while($item = $dataDb->fetch())
328 Model\PostingTable::update($item[
'POSTING_ID'], array(
'COUNT_' . $type => $item[
'CNT']));
336 $dataDb = \Bitrix\Sender\PostingUnsubTable::getList(array(
338 'CONTACT_ID' =>
'POSTING_RECIPIENT.CONTACT_ID',
339 'MAILING_ID' =>
'POSTING.MAILING_ID',
342 'order' => array(
'ID' =>
'ASC'),
344 while($data = $dataDb->fetch())
351 $primary = array(
'MAILING_ID' => $data[
'MAILING_ID'],
'CONTACT_ID' => $data[
'CONTACT_ID']);
352 $fields = array(
'IS_UNSUB' =>
'Y');
353 $row = \Bitrix\Sender\MailingSubscriptionTable::getRowById($primary);
356 $result = \Bitrix\Sender\MailingSubscriptionTable::add($fields + $primary);
357 $result->isSuccess();
366 $connection = \Bitrix\Main\Application::getConnection();
367 switch(mb_strtoupper($connection->getType()))
370 $query =
"SELECT ID FROM b_sender_contact WHERE TYPE_ID=1 AND CODE LIKE '%[A-Z]%' COLLATE Latin1_General_BIN";
373 $query =
"SELECT ID FROM b_sender_contact WHERE TYPE_ID=1 AND CODE REGEXP BINARY '[A-Z]'";
376 $query =
"SELECT ID FROM b_sender_contact WHERE TYPE_ID=1 AND REGEXP_LIKE(CODE, '[A-Z]')";
379 $helper = $connection->getSqlHelper();
380 $query =
"SELECT ID FROM b_sender_contact WHERE TYPE_ID=1 AND {$helper->getRegexpOperator('CODE', "'[A-Z]'")}";
383 $senderContactDb = $connection->query($query);
384 while ($senderContact = $senderContactDb->fetch())
386 if (self::isTimeUp())
391 $connection->Query(
"UPDATE b_sender_contact SET CODE = LOWER(CODE) WHERE TYPE_ID=1 AND ID = " . intval($senderContact[
'ID']));
397 private static function getPostingStatusUpdateFields($postingId, $statusList)
399 if(!$postingId || count($statusList) == 0)
404 $postingUpdateFields = array(
'COUNT_SEND_ALL' => 0);
406 $map = \Bitrix\Sender\PostingTable::getRecipientStatusToPostingFieldMap();
407 foreach($map as $recipientStatus => $postingFieldName)
409 if(!array_key_exists($recipientStatus, $statusList))
415 $postingCountFieldValue = $statusList[$recipientStatus];
418 $postingUpdateFields[
'COUNT_SEND_ALL'] += $postingCountFieldValue;
419 $postingUpdateFields[$postingFieldName] = $postingCountFieldValue;
422 if($postingUpdateFields[
'COUNT_SEND_ALL'] == 0)
427 return $postingUpdateFields;
440 if ($conn->isTableExists(
'b_sender_posting_rcpnt_old'))
443 $timer =
new Runtime\Timer(Runtime\Env::getJobExecutionTimeout(), 100);
444 while (!$timer->isElapsed())
447 $sql =
"INSERT IGNORE
448 INTO b_sender_posting_recipient
490 b_sender_posting_rcpnt_old ro
498 ro2.POSTING_ID = p.ID
500 and c.CODE = ro2.EMAIL
504 $conn->query(
"delete from b_sender_posting_rcpnt_old order by ID asc limit $limit");
505 if (!$conn->query(
'select ID from b_sender_posting_rcpnt_old limit 1')->fetch())
516 "DROP TABLE IF EXISTS b_sender_posting_rcpnt_old"
523 return '\Bitrix\Sender\Internals\CounterCalculation::updateRecipientsAgent();';
static getConnection($name="")