Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventhandler.php
1<?php
10
11use Bitrix\Crm\Activity\BindingSelector;
12use Bitrix\Crm\Integrity\ActualEntitySelector;
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
71 TimeLineQueueTable::add([
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 }
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
113 if (!$entityId)
114 {
115 continue;
116 }
117
118 $dataToInsert[] = [
119 'RECIPIENT_ID' => $recipient['ID'],
120 'POSTING_ID' => $letter->getId(),
121 'FIELDS' => Json::encode($fields),
122 'ENTITY_ID' => $entityId,
123 'CONTACT_TYPE_ID' => $recipient['CONTACT_TYPE_ID'],
124 'CONTACT_CODE' => $recipient['CONTACT_CODE'],
125 ];
126 }
127
128 if($dataToInsert)
129 {
130 TimeLineQueueTable::addMulti($dataToInsert, true);
131 }
132 }
133
140 public static function handleTimeLineEvents($letterId)
141 {
142 static $isModuleIncluded = null;
143 if ($isModuleIncluded === null)
144 {
145 $isModuleIncluded = Loader::includeModule('crm');
146 }
147
148 if (!$isModuleIncluded)
149 {
150 return "";
151 }
152
153 $entityTypeId = $entityId = null;
154 $batchData = [];
155 $idsToDelete = [];
156 self::lockTimelineQueue($letterId);
157 $letter = Entity\Letter::createInstanceById($letterId);
158
159 try
160 {
161 $queuedRows = TimeLineQueueTable::getList([
162 'filter' => [
164 '=POSTING_ID' => $letterId
165 ],
166 'limit' => self::TIME_LINE_COUNT_LIMIT
167 ])->fetchAll();
168
169 if(empty($queuedRows))
170 {
171 return "";
172 }
173
174 foreach ($queuedRows as $row)
175 {
176 $idsToDelete[] = $row['ID'];
177 if (!$letter)
178 {
179 continue;
180 }
181
182 $fields = Json::decode($row['FIELDS']);
183 if (isset($fields['CRM_ENTITY_TYPE_ID']) && $fields['CRM_ENTITY_TYPE_ID'])
184 {
185 $entityTypeId = $fields['CRM_ENTITY_TYPE_ID'];
186 }
187 if (isset($fields['CRM_ENTITY_ID']) && $fields['CRM_ENTITY_ID'])
188 {
189 $entityId = $fields['CRM_ENTITY_ID'];
190 }
191
192 if (!$entityTypeId || !$entityId)
193 {
195 $row['CONTACT_TYPE_ID'],
196 $row['CONTACT_CODE']
197 );
198 }
199 else
200 {
201 $selector = self::getEntitySelectorById($entityTypeId, $entityId);
202 }
203
204
205 if (!$selector)
206 {
207 continue;
208 }
209
210 if (!$selector->search()->hasEntities())
211 {
212 continue;
213 }
214
215 $recipient = [
216 'ID' => $row['RECIPIENT_ID'],
217 'CONTACT_TYPE_ID' => $row['CONTACT_TYPE_ID'],
218 'CONTACT_CODE' => $row['CONTACT_CODE'],
219 ];
220
221 $batchData[] = static::buildTimeLineEvent($selector, $letter, $recipient);
222 }
223
224 if (!empty($batchData))
225 {
226 Timeline\RecipientEntry::createMulti($batchData);
227 }
228
229 TimeLineQueueTable::deleteList(['=ID' => $idsToDelete]);
230 } catch (\Exception $e)
231 {
232 }
233
234 self::unlockTimelineQueue($letterId);
235
236 return TimeLineJob::getAgentName($letterId);
237 }
238
239 protected static function addTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
240 {
241 $parameters = static::buildTimeLineEvent($selector, $letter, $recipient);
242
243 if(!empty($parameters))
244 {
245 Timeline\RecipientEntry::create($parameters);
246 }
247 }
248
249 protected static function buildTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
250 {
251 $isAd = $letter instanceof Entity\Ad;
252 $createdBy = $letter->get('CREATED_BY');
253 if (!$createdBy)
254 {
255 return [];
256 }
257
258 // convert format to time line
259 $bindings = [];
260 $activityBindings = BindingSelector::findBindings($selector);
261 foreach ($activityBindings as $binding)
262 {
263 $binding['ENTITY_ID'] = $binding['OWNER_ID'];
264 $binding['ENTITY_TYPE_ID'] = $binding['OWNER_TYPE_ID'];
265 $bindings[] = [
266 'ENTITY_TYPE_ID' => $binding['OWNER_TYPE_ID'],
267 'ENTITY_ID' => $binding['OWNER_ID'],
268 ];
269 }
270
271 return [
272 'ENTITY_TYPE_ID' => $selector->getPrimaryTypeId(),
273 'ENTITY_ID' => $selector->getPrimaryId(),
274 'TYPE_CATEGORY_ID' => $letter->getMessage()->getCode(),
275 'AUTHOR_ID' => $createdBy,
276 'SETTINGS' => [
277 'letterId' => $letter->getId(),
278 'isAds' => $isAd,
279 'recipient' => [
280 'id' => $recipient['ID'],
281 'typeId' => $recipient['CONTACT_TYPE_ID'],
282 'code' => $recipient['CONTACT_ID'],
283 ],
284 ],
285 'BINDINGS' => $bindings
286 ];
287 }
288
289 protected static function getEntitySelector()
290 {
292 static $selector = null;
293 if (!$selector)
294 {
295 $selector = new ActualEntitySelector();
296 }
297 else
298 {
299 $selector->clear();
300 }
301
302 return $selector;
303 }
304
305 protected static function getEntitySelectorById($entityTypeId, $entityId)
306 {
307 return self::getEntitySelector()->setEntity($entityTypeId, $entityId);
308 }
309
310 protected static function getEntitySelectorByRecipient($recipientTypeId, $recipientCode)
311 {
312 $selector = self::getEntitySelector();
313
314 switch ($recipientTypeId)
315 {
316 case Recipient\Type::EMAIL:
317 $selector->appendEmailCriterion($recipientCode);
318 break;
319
320 case Recipient\Type::PHONE:
321 $selector->appendPhoneCriterion($recipientCode);
322 break;
323
324 default:
325 return null;
326 }
327
328 return $selector;
329 }
330
338 protected static function lockTimelineQueue($letterId): bool
339 {
340 $connection = Application::getInstance()->getConnection();
341 $lockName = self::getTimelineQueueLock((string)$letterId);
342
343 return $connection->lock($lockName);
344 }
345
353 protected static function unlockTimelineQueue($letterId): bool
354 {
355 $connection = Application::getInstance()->getConnection();
356 $lockName = self::getTimelineQueueLock((string)$letterId);
357
358 return $connection->unlock($lockName);
359 }
360
368 private static function getTimelineQueueLock(string $letterId): string
369 {
370 return "time_line_queue_$letterId";
371 }
372}
static loadMessages($file)
Definition loc.php:64
static buildTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
static addTimeLineEvent(ActualEntitySelector $selector, Entity\Base $letter, $recipient)
static onAfterPostingSendRecipient(array $eventData, Entity\Letter $letter)
static onAfterPostingSendRecipientMultiple(array $eventDataArray, Entity\Letter $letter)
static getEntitySelectorById($entityTypeId, $entityId)
static getEntitySelectorByRecipient($recipientTypeId, $recipientCode)