Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
segment.php
1<?php
9
23
24Loc::loadMessages(__FILE__);
25Loc::loadMessages(__DIR__ . '/letter.php');
26
27class Segment extends Base
28{
29 const CODE_ALL = 'all';
30
32 protected $isFilterOnly = false;
33
39 public static function getDefaultIds()
40 {
41 $result = array();
42 $id = self::getIdByCode(self::CODE_ALL);
43 if ($id)
44 {
45 $result[] = $id;
46 }
47
48 return $result;
49 }
50
57 public static function getIdByCode($code)
58 {
59 $row = self::getList(array(
60 'select' => array('ID'),
61 'filter' => array('=CODE' => $code),
62 'limit' => 1,
63 'cache' => array('ttl' => 36000)
64 ))->fetch();
65
66 return $row ? $row['ID'] : null;
67 }
68
75 public static function getList(array $parameters = array())
76 {
77 return GroupTable::getList($parameters);
78 }
79
85 protected function getDefaultData()
86 {
87 return array(
88 'NAME' => '',
89 'ENDPOINTS' => array(),
90 );
91 }
92
99 protected function loadData($id)
100 {
101 $data = GroupTable::getRowById($id);
102 if (!is_array($data))
103 {
104 return null;
105 }
106
107 $data['ENDPOINTS'] = array();
108 $groupConnectorDb = GroupConnectorTable::getList(array(
109 'filter'=>array(
110 '=GROUP_ID'=> $id
111 )
112 ));
113 while($groupConnector = $groupConnectorDb->fetch())
114 {
115 if(empty($groupConnector['ENDPOINT']) || !is_array($groupConnector['ENDPOINT']))
116 {
117 continue;
118 }
119
120 $groupConnector['ENDPOINT']['FILTER_ID'] = $groupConnector['FILTER_ID'];
121 $data['ENDPOINTS'][] = $groupConnector['ENDPOINT'];
122 }
123
124 return $data;
125 }
126
134 protected function saveData($id, array $data)
135 {
136 $endpoints = $data['ENDPOINTS'];
137 unset($data['ENDPOINTS']);
138
139 if (!$id
140 && isset($data['STATUS'])
141 && $data['STATUS'] !== GroupTable::STATUS_NEW)
142 {
143 $data['STATUS'] = GroupTable::STATUS_DONE;
144 }
145
146 $id = $this->saveByEntity(GroupTable::getEntity(), $id, $data);
147 if ($this->hasErrors())
148 {
149 return $id;
150 }
151
152 $dataCounters = array();
153 GroupConnectorTable::delete(array('GROUP_ID' => $id));
154
155 if ($endpoints)
156 {
157 foreach ($endpoints as $endpoint)
158 {
159 $connector = Connector\Manager::getConnector($endpoint);
160 if (!$connector)
161 {
162 continue;
163 }
164
165 if ($this->isFilterOnly() && !($connector instanceof Connector\BaseFilter))
166 {
167 continue;
168 }
169
170 $connector->setFieldValues(is_array($endpoint['FIELDS']) ? $endpoint['FIELDS'] : null);
171 $endpoint['FIELDS'] = $connector->getFieldValues();
172 $statFields = $connector->getStatFields();
173
174 foreach (array_intersect($statFields, array_keys($endpoint['FIELDS'])) as $field)
175 {
176 \Bitrix\Sender\Log::stat('segment_field', $field, $id);
177 }
178 $isIncrementally = $connector instanceof Connector\IncrementallyConnector;
179
180 $dataCounter = $isIncrementally
181 ? new Connector\DataCounter([])
182 : $connector->getDataCounter();
183
184 $groupConnector = array(
185 'GROUP_ID' => $id,
186 'NAME' => $connector->getName(),
187 'ENDPOINT' => $endpoint,
188 'ADDRESS_COUNT' => $dataCounter->getSummary()
189 );
190
191 if(isset($endpoint['FILTER_ID']))
192 {
193 $groupConnector['FILTER_ID'] = $endpoint['FILTER_ID'];
194 }
195
196 $connectorResultDb = GroupConnectorTable::add($groupConnector);
197 if($connectorResultDb->isSuccess())
198 {
199 $dataCounters[] = $dataCounter;
200 }
201
202 $this->updateDealCategory($id, $connector);
203 }
204
205 if (isset($data['STATUS']) && GroupTable::STATUS_NEW === $data['STATUS'])
206 {
207 SegmentDataBuilder::actualize($id, true);
208 }
209
210 SegmentDataBuilder::checkIsSegmentPrepared($id);
211 $this->updateAddressCounters($id, $dataCounters);
212 }
213
214 return $id;
215 }
216
217 private function updateDealCategory(int $groupId, $connector)
218 {
219 $groupDealCategory = [];
220
221 foreach ($connector->getFieldValues() as $fieldKey => $fieldValue)
222 {
223 if($fieldKey != 'DEAL_CATEGORY_ID')
224 {
225 continue;
226 }
227 GroupDealCategoryTable::deleteList(array('GROUP_ID' => $groupId));
228
229 foreach ($fieldValue as $dealCategory)
230 {
231 $groupDealCategory[] = [
232 'GROUP_ID' => $groupId,
233 'DEAL_CATEGORY_ID' => $dealCategory
234 ];
235 }
236 }
237
238 if(!empty($groupDealCategory))
239 {
240 GroupDealCategoryTable::addMulti($groupDealCategory);
241 }
242 }
243
249 public function isHidden()
250 {
251 return $this->get('HIDDEN') === 'Y';
252 }
253
259 public function isSystem()
260 {
261 return $this->get('IS_SYSTEM') === 'Y';
262 }
263
269 public function isFilterOnly()
270 {
271 return $this->isFilterOnly;
272 }
273
280 public function setFilterOnlyMode($mode = true)
281 {
282 $this->isFilterOnly = $mode;
283 return $this;
284 }
285
292 public function appendContactSetConnector($contactSetId = null)
293 {
294 if ($this->getFirstContactSetId())
295 {
296 return $this;
297 }
298
299 if (!$contactSetId)
300 {
301 $contactSetId = ListTable::add(['SORT' => 100])->getId();
302 }
303 elseif (!ListTable::getRowById($contactSetId))
304 {
305 $this->errors->setError(new Error('Wrong contact set ID.'));
306 return $this;
307 }
308
309 $this->data['ENDPOINTS'][] = [
310 'MODULE_ID' => 'sender',
311 'CODE' => 'contact_list',
312 'FIELDS' => [
313 'LIST_ID' => $contactSetId,
314 'SENDER_SELECT_ALL' => null,
315 ],
316 ];
317 return $this;
318 }
319
325 protected function getFirstContactSetId()
326 {
327 foreach ($this->data['ENDPOINTS'] as $endpoint)
328 {
329 if ($endpoint['MODULE_ID'] !== 'sender' || $endpoint['CODE'] !== 'contact_list')
330 {
331 continue;
332 }
333
334 if (empty($endpoint['FIELDS']['LIST_ID']))
335 {
336 continue;
337 }
338
339 return $endpoint['FIELDS']['LIST_ID'];
340 }
341
342 return null;
343 }
344
351 public function upload(array $list)
352 {
353 $contactSetId = $this->getFirstContactSetId();
354 if (!$contactSetId)
355 {
356 $this->appendContactSetConnector()->save();
357 }
358
359 $contactSetId = $this->getFirstContactSetId();
360 if (!$contactSetId)
361 {
362 $this->errors->setError(new Error('Contact set not found.'));
363 return $this;
364 }
365
366 ContactTable::upload($list, false, $contactSetId);
367
368 return $this;
369 }
370
378 public static function updateAddressCounters($segmentId, array $counters)
379 {
380 if (!$segmentId)
381 {
382 return false;
383 }
384
385 $count = 0;
386 $countByType = array();
387 foreach ($counters as $dataCounter)
388 {
389 $count += $dataCounter->getSummary();
390 $list = $dataCounter->getList();
391 foreach ($list as $typeId => $typeCount)
392 {
393 if (!isset($countByType[$typeId]))
394 {
395 $countByType[$typeId] = 0;
396 }
397
398 $countByType[$typeId] += $typeCount;
399 }
400 }
401
402
403 $result = GroupTable::update($segmentId, array('ADDRESS_COUNT' => $count));
404 if (!$result->isSuccess())
405 {
406 return false;
407 }
408
409 GroupCounterTable::deleteByGroupId($segmentId);
410 foreach ($countByType as $typeId => $typeCount)
411 {
412 if (!$typeCount)
413 {
414 continue;
415 }
416
417 GroupCounterTable::add(array(
418 'GROUP_ID' => $segmentId,
419 'TYPE_ID' => $typeId,
420 'CNT' => $typeCount,
421 ));
422 }
423
424 return true;
425 }
426
433 public static function getAddressCounter($segmentId)
434 {
435 $data = self::getAddressCounters(array($segmentId));
436 return new Connector\DataCounter(current($data) ?: array());
437 }
438
445 public static function getAddressCounters(array $list)
446 {
447 $data = array();
448 $list = GroupCounterTable::getList(array(
449 'select' => array('GROUP_ID', 'TYPE_ID', 'CNT'),
450 'filter' => array('=GROUP_ID' => $list),
451 'order' => array('GROUP_ID' => 'ASC')
452 ));
453 foreach ($list as $row)
454 {
455 $data[$row['GROUP_ID']][$row['TYPE_ID']] = $row['CNT'];
456 }
457 return $data;
458 }
459
467 public static function updateUseCounters(array $list, $isInclude = true)
468 {
469 if (count($list) === 0)
470 {
471 return;
472 }
473
474 $tableName = GroupTable::getTableName();
475 $now = Application::getConnection()->getSqlHelper()->convertToDbDateTime(new DateTime());
476 $ids = array();
477 foreach ($list as $element)
478 {
479 $id = $element['ID'];
480 if (!$id || !is_numeric($id))
481 {
482 continue;
483 }
484
485 $id = (int) $id;
486 if (!$id)
487 {
488 continue;
489 }
490
491 $ids[] = $id;
492 }
493 $ids = implode(', ', $ids);
494 $postfix = $isInclude ? '' : '_EXCLUDE';
495
496 $sql = "UPDATE $tableName SET USE_COUNT$postfix = USE_COUNT$postfix + 1, DATE_USE$postfix = $now WHERE ID IN ($ids)";
497 Application::getConnection()->query($sql);
498 }
499
505 public function remove()
506 {
507 return $this->removeByEntity(GroupTable::getEntity(), $this->getId());
508 }
509
515 public function copy()
516 {
517 $data = $this->loadData($this->getId());
518
519 return $this->copyData(
520 $this->getId(),
521 [
522 'NAME' => Loc::getMessage('SENDER_ENTITY_LETTER_COPY_PREFIX') . ' ' .$data['NAME'],
523 'CODE' => null,
524 'STATUS' => GroupTable::STATUS_NEW,
525 'DATE_USE' => null,
526 'USE_COUNT' => 0,
527 'USE_COUNT_EXCLUDE' => 0,
528 'DATE_INSERT' => new Type\DateTime(),
529 ]
530 );
531 }
532
539 public static function removeById($id)
540 {
541 return static::create()->removeByEntity(GroupTable::getEntity(), $id);
542 }
543}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static upload(array $list, bool $isBlacklist=false, ?int $listId=null)
Definition contact.php:549
static updateUseCounters(array $list, $isInclude=true)
Definition segment.php:467
static getList(array $parameters=array())
Definition segment.php:75
static getAddressCounters(array $list)
Definition segment.php:445
static getAddressCounter($segmentId)
Definition segment.php:433
static getIdByCode($code)
Definition segment.php:57
static updateAddressCounters($segmentId, array $counters)
Definition segment.php:378
appendContactSetConnector($contactSetId=null)
Definition segment.php:292
saveData($id, array $data)
Definition segment.php:134
static deleteList(array $filter)
Definition group.php:311