1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventhandler.php
См. документацию.
1<?php
8
9namespace Bitrix\Sender\Integration\Crm;
10
11use Bitrix\Crm\Activity\BindingSelector;
12use Bitrix\Crm\Integrity\ActualEntitySelector;
13use Bitrix\Main\Application;
14use Bitrix\Main\DB\SqlQueryException;
15use Bitrix\Main\Loader;
16use Bitrix\Main\Localization\Loc;
17use Bitrix\Main\Web\Json;
18use Bitrix\Sender\Entity;
19use Bitrix\Sender\Recipient;
20use Bitrix\Sender\Runtime\TimeLineJob;
21use Bitrix\Sender\TimeLineQueueTable;
22
23Loc::loadMessages(__FILE__);
24
30{
31 private const TIME_LINE_COUNT_LIMIT = 500;
38 public static function onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
39 {
40 if (!$eventData['SEND_RESULT'])
41 {
42 return;
43 }
44
45 static $isModuleIncluded = null;
46 if ($isModuleIncluded === null)
47 {
48 $isModuleIncluded = Loader::includeModule('crm');
49 }
50
51 if (!$isModuleIncluded)
52 {
53 return;
54 }
55
56 if ($letter->getMessage()->isReturnCustomer())
57 {
58 return;
59 }
60
61 $recipient = $eventData['RECIPIENT'];
62 $fields = $eventData['RECIPIENT']['FIELDS'];
63
64 $entityId = $fields['CRM_ENTITY_ID'] ?? $recipient['CONTACT_ID'];
65
66 if (!$entityId)
67 {
68 return;
69 }
70
72 'RECIPIENT_ID' => $recipient['ID'],
73 'POSTING_ID' => $letter->getId(),
74 'FIELDS' => Json::encode($fields),
75 'ENTITY_ID' => $entityId,
76 'CONTACT_TYPE_ID' => $recipient['CONTACT_TYPE_ID'],
77 'CONTACT_CODE' => $recipient['CONTACT_CODE'],
78 ]);
79 }
80
86 public static function onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)
87 {
88 static $isModuleIncluded = null;
89
90 if ($isModuleIncluded === null)
91 {
92 $isModuleIncluded = Loader::includeModule('crm');
93 }
94
95 if (!$isModuleIncluded)
96 {
97 return;
98 }
99
100 if ($letter->getMessage()->isReturnCustomer())
101 {
102 return;
103 }
104
105 $dataToInsert = [];
106 foreach($eventDataArray as $eventData)
107 {
108 $recipient = $eventData['RECIPIENT'];
109 $fields = $eventData['RECIPIENT']['FIELDS'];
110
111 $entityId = $fields['CRM_ENTITY_ID'] ?? $recipient['CONTACT_ID'];
112 $contactCode = !empty($recipient['CONTACT_CODE']) ? $recipient['CONTACT_CODE'] : $entityId;
113
114 if (empty($entityId) || empty($contactCode))
115 {
116 continue;
117 }
118
119 $dataToInsert[] = [
120 'RECIPIENT_ID' => $recipient['ID'],
121 'POSTING_ID' => $letter->getId(),
122 'FIELDS' => Json::encode($fields),
123 'ENTITY_ID' => $entityId,
124 'CONTACT_TYPE_ID' => $recipient['CONTACT_TYPE_ID'],
125 'CONTACT_CODE' => $contactCode,
126 ];
127 }
128
129 if($dataToInsert)
130 {
131 TimeLineQueueTable::addMulti($dataToInsert, true);
132 }
133 }
134
141 public static function handleTimeLineEvents($letterId)
142 {
143 static $isModuleIncluded = null;
144 if ($isModuleIncluded === null)
145 {
146 $isModuleIncluded = Loader::includeModule('crm');
147 }
148
149 if (!$isModuleIncluded)
150 {
151 return "";
152 }
153
154 $entityTypeId = $entityId = null;
155 $batchData = [];
156 $idsToDelete = [];
157 self::lockTimelineQueue($letterId);
158 $letter = Entity\Letter::createInstanceById($letterId);
159
160 try
161 {
162 $queuedRows = TimeLineQueueTable::getList([
163 'filter' => [
165 '=POSTING_ID' => $letterId
166 ],
167 'limit' => self::TIME_LINE_COUNT_LIMIT
168 ])->fetchAll();
169
170 if(empty($queuedRows))
171 {
172 return "";
173 }
174
175 foreach ($queuedRows as $row)
176 {
177 $idsToDelete[] = $row['ID'];
178 if (!$letter)
179 {
180 continue;
181 }
182
183 $fields = Json::decode($row['FIELDS']);
184 if (isset($fields['CRM_ENTITY_TYPE_ID']) && $fields['CRM_ENTITY_TYPE_ID'])
185 {
186 $entityTypeId = $fields['CRM_ENTITY_TYPE_ID'];
187 }
188 if (isset($fields['CRM_ENTITY_ID']) && $fields['CRM_ENTITY_ID'])
189 {
190 $entityId = $fields['CRM_ENTITY_ID'];
191 }
192
193 if (!$entityTypeId || !$entityId)
194 {
196 $row['CONTACT_TYPE_ID'],
197 $row['CONTACT_CODE']
198 );
199 }
200 else
201 {
202 $selector = self::getEntitySelectorById($entityTypeId, $entityId);
203 }
204
205
206 if (!$selector)
207 {
208 continue;
209 }
210
211 if (!$selector->search()->hasEntities())
212 {
213 continue;
214 }
215
216 $recipient = [
217 'ID' => $row['RECIPIENT_ID'],
218 'CONTACT_TYPE_ID' => $row['CONTACT_TYPE_ID'],
219 'CONTACT_CODE' => $row['CONTACT_CODE'],
220 ];
221
222 $batchData[] = static::buildTimeLineEvent($selector, $letter, $recipient);
223 }
224
225 if (!empty($batchData))
226 {
228 }
229
230 TimeLineQueueTable::deleteList(['=ID' => $idsToDelete]);
231 } catch (\Exception $e)
232 {
233 }
234
235 self::unlockTimelineQueue($letterId);
236
237 return TimeLineJob::getAgentName($letterId);
238 }
239
240 protected static function addTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
241 {
242 $parameters = static::buildTimeLineEvent($selector, $letter, $recipient);
243
244 if(!empty($parameters))
245 {
247 }
248 }
249
250 protected static function buildTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
251 {
252 $isAd = $letter instanceof Entity\Ad;
253 $createdBy = $letter->get('CREATED_BY');
254 if (!$createdBy)
255 {
256 return [];
257 }
258
259 // convert format to time line
260 $bindings = [];
261 $activityBindings = BindingSelector::findBindings($selector);
262 foreach ($activityBindings as $binding)
263 {
264 $binding['ENTITY_ID'] = $binding['OWNER_ID'];
265 $binding['ENTITY_TYPE_ID'] = $binding['OWNER_TYPE_ID'];
266 $bindings[] = [
267 'ENTITY_TYPE_ID' => $binding['OWNER_TYPE_ID'],
268 'ENTITY_ID' => $binding['OWNER_ID'],
269 ];
270 }
271
272 return [
273 'ENTITY_TYPE_ID' => $selector->getPrimaryTypeId(),
274 'ENTITY_ID' => $selector->getPrimaryId(),
275 'TYPE_CATEGORY_ID' => $letter->getMessage()->getCode(),
276 'AUTHOR_ID' => $createdBy,
277 'SETTINGS' => [
278 'letterId' => $letter->getId(),
279 'isAds' => $isAd,
280 'recipient' => [
281 'id' => $recipient['ID'],
282 'typeId' => $recipient['CONTACT_TYPE_ID'],
283 'code' => $recipient['CONTACT_ID'],
284 ],
285 ],
286 'BINDINGS' => $bindings
287 ];
288 }
289
290 protected static function getEntitySelector()
291 {
293 static $selector = null;
294 if (!$selector)
295 {
296 $selector = new ActualEntitySelector();
297 }
298 else
299 {
300 $selector->clear();
301 }
302
303 return $selector;
304 }
305
306 protected static function getEntitySelectorById($entityTypeId, $entityId)
307 {
308 return self::getEntitySelector()->setEntity($entityTypeId, $entityId);
309 }
310
311 protected static function getEntitySelectorByRecipient($recipientTypeId, $recipientCode)
312 {
313 $selector = self::getEntitySelector();
314
315 switch ($recipientTypeId)
316 {
318 $selector->appendEmailCriterion($recipientCode);
319 break;
320
322 $selector->appendPhoneCriterion($recipientCode);
323 break;
324
325 default:
326 return null;
327 }
328
329 return $selector;
330 }
331
339 protected static function lockTimelineQueue($letterId): bool
340 {
341 $connection = Application::getInstance()->getConnection();
342 $lockName = self::getTimelineQueueLock((string)$letterId);
343
344 return $connection->lock($lockName);
345 }
346
354 protected static function unlockTimelineQueue($letterId): bool
355 {
356 $connection = Application::getInstance()->getConnection();
357 $lockName = self::getTimelineQueueLock((string)$letterId);
358
359 return $connection->unlock($lockName);
360 }
361
369 private static function getTimelineQueueLock(string $letterId): string
370 {
371 return "time_line_queue_$letterId";
372 }
373}
$connection
Определения actionsdefinitions.php:38
static deleteList(array $filter)
Определения datamanager.php:16
static getList(array $parameters=array())
Определения datamanager.php:431
static addMulti($rows, $ignoreEvents=false)
Определения datamanager.php:1041
static add(array $data)
Определения datamanager.php:877
Определения ad.php:19
static createInstanceById($id=null, array $messageCodes=[])
Определения letter.php:198
static buildTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
Определения eventhandler.php:250
static addTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
Определения eventhandler.php:240
static lockTimelineQueue($letterId)
Определения eventhandler.php:339
static onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
Определения eventhandler.php:38
static handleTimeLineEvents($letterId)
Определения eventhandler.php:141
static onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)
Определения eventhandler.php:86
static unlockTimelineQueue($letterId)
Определения eventhandler.php:354
static getEntitySelectorById($entityTypeId, $entityId)
Определения eventhandler.php:306
static getEntitySelectorByRecipient($recipientTypeId, $recipientCode)
Определения eventhandler.php:311
static createMulti(array $parameters)
Определения recipiententry.php:90
static create(array $params)
Определения recipiententry.php:22
const EMAIL
Определения type.php:21
const PHONE
Определения type.php:22
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
Определения ufield.php:9
$entityId
Определения payment.php:4
$fields
Определения yandex_run.php:501