1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
segment.php
См. документацию.
1<?php
8namespace Bitrix\Sender\Entity;
9
10use Bitrix\Main\Application;
11use Bitrix\Main\Error;
12use Bitrix\Main\Localization\Loc;
13use Bitrix\Main\Type\DateTime;
14use Bitrix\Sender\Connector;
15use Bitrix\Sender\ContactTable;
16use Bitrix\Sender\GroupConnectorTable;
17use Bitrix\Sender\GroupDealCategoryTable;
18use Bitrix\Sender\GroupTable;
19use Bitrix\Sender\Internals\Model\GroupCounterTable;
20use Bitrix\Sender\Internals\SqlBatch;
21use Bitrix\Sender\ListTable;
22use Bitrix\Sender\Posting\SegmentDataBuilder;
23use Bitrix\Main\Type;
24
25Loc::loadMessages(__FILE__);
26Loc::loadMessages(__DIR__ . '/letter.php');
27
28class Segment extends Base
29{
30 const CODE_ALL = 'all';
31
33 protected $isFilterOnly = false;
34
40 public static function getDefaultIds()
41 {
42 $result = array();
43 $id = self::getIdByCode(self::CODE_ALL);
44 if ($id)
45 {
46 $result[] = $id;
47 }
48
49 return $result;
50 }
51
58 public static function getIdByCode($code)
59 {
60 $row = self::getList(array(
61 'select' => array('ID'),
62 'filter' => array('=CODE' => $code),
63 'limit' => 1,
64 'cache' => array('ttl' => 36000)
65 ))->fetch();
66
67 return $row ? $row['ID'] : null;
68 }
69
76 public static function getList(array $parameters = array())
77 {
78 return GroupTable::getList($parameters);
79 }
80
86 protected function getDefaultData()
87 {
88 return array(
89 'NAME' => '',
90 'ENDPOINTS' => array(),
91 );
92 }
93
100 protected function loadData($id)
101 {
103 if (!is_array($data))
104 {
105 return null;
106 }
107
108 $data['ENDPOINTS'] = array();
109 $groupConnectorDb = GroupConnectorTable::getList(array(
110 'filter'=>array(
111 '=GROUP_ID'=> $id
112 )
113 ));
114 while($groupConnector = $groupConnectorDb->fetch())
115 {
116 if(empty($groupConnector['ENDPOINT']) || !is_array($groupConnector['ENDPOINT']))
117 {
118 continue;
119 }
120
121 $groupConnector['ENDPOINT']['FILTER_ID'] = $groupConnector['FILTER_ID'];
122 $data['ENDPOINTS'][] = $groupConnector['ENDPOINT'];
123 }
124
125 return $data;
126 }
127
135 protected function saveData($id, array $data)
136 {
137 $endpoints = $data['ENDPOINTS'];
138 unset($data['ENDPOINTS']);
139
140 if (!$id
141 && isset($data['STATUS'])
142 && $data['STATUS'] !== GroupTable::STATUS_NEW)
143 {
144 $data['STATUS'] = GroupTable::STATUS_DONE;
145 }
146
147 $id = $this->saveByEntity(GroupTable::getEntity(), $id, $data);
148 if ($this->hasErrors())
149 {
150 return $id;
151 }
152
153 $dataCounters = array();
154 GroupConnectorTable::delete(array('GROUP_ID' => $id));
155
156 if ($endpoints)
157 {
158 foreach ($endpoints as $endpoint)
159 {
161 if (!$connector)
162 {
163 continue;
164 }
165
166 if ($this->isFilterOnly() && !($connector instanceof Connector\BaseFilter))
167 {
168 continue;
169 }
170
171 $connector->setFieldValues(is_array($endpoint['FIELDS']) ? $endpoint['FIELDS'] : null);
172 $endpoint['FIELDS'] = $connector->getFieldValues();
173 $statFields = $connector->getStatFields();
174
175 foreach (array_intersect($statFields, array_keys($endpoint['FIELDS'])) as $field)
176 {
177 \Bitrix\Sender\Log::stat('segment_field', $field, $id);
178 }
179 $isIncrementally = $connector instanceof Connector\IncrementallyConnector;
180
181 $dataCounter = $isIncrementally
182 ? new Connector\DataCounter([])
183 : $connector->getDataCounter();
184
185 $groupConnector = array(
186 'GROUP_ID' => $id,
187 'NAME' => $connector->getName(),
188 'ENDPOINT' => $endpoint,
189 'ADDRESS_COUNT' => $dataCounter->getSummary()
190 );
191
192 if(isset($endpoint['FILTER_ID']))
193 {
194 $groupConnector['FILTER_ID'] = $endpoint['FILTER_ID'];
195 }
196
197 $connectorResultDb = GroupConnectorTable::add($groupConnector);
198 if($connectorResultDb->isSuccess())
199 {
200 $dataCounters[] = $dataCounter;
201 }
202
203 $this->updateDealCategory($id, $connector);
204 }
205
206 if (isset($data['STATUS']) && GroupTable::STATUS_NEW === $data['STATUS'])
207 {
208 SegmentDataBuilder::actualize($id, true);
209 }
210
211 SegmentDataBuilder::checkIsSegmentPrepared($id);
212 $this->updateAddressCounters($id, $dataCounters);
213 }
214
215 return $id;
216 }
217
218 private function updateDealCategory(int $groupId, $connector)
219 {
220 $groupDealCategory = [];
221
222 foreach ($connector->getFieldValues() as $fieldKey => $fieldValue)
223 {
224 if($fieldKey != 'DEAL_CATEGORY_ID')
225 {
226 continue;
227 }
228 GroupDealCategoryTable::deleteList(array('GROUP_ID' => $groupId));
229
230 foreach ($fieldValue as $dealCategory)
231 {
232 $groupDealCategory[] = [
233 'GROUP_ID' => $groupId,
234 'DEAL_CATEGORY_ID' => $dealCategory
235 ];
236 }
237 }
238
239 if(!empty($groupDealCategory))
240 {
241 GroupDealCategoryTable::addMulti($groupDealCategory);
242 }
243 }
244
250 public function isHidden()
251 {
252 return $this->get('HIDDEN') === 'Y';
253 }
254
260 public function isSystem()
261 {
262 return $this->get('IS_SYSTEM') === 'Y';
263 }
264
270 public function isFilterOnly()
271 {
272 return $this->isFilterOnly;
273 }
274
281 public function setFilterOnlyMode($mode = true)
282 {
283 $this->isFilterOnly = $mode;
284 return $this;
285 }
286
293 public function appendContactSetConnector($contactSetId = null)
294 {
295 if ($this->getFirstContactSetId())
296 {
297 return $this;
298 }
299
300 if (!$contactSetId)
301 {
302 $contactSetId = ListTable::add(['SORT' => 100])->getId();
303 }
304 elseif (!ListTable::getRowById($contactSetId))
305 {
306 $this->errors->setError(new Error('Wrong contact set ID.'));
307 return $this;
308 }
309
310 $this->data['ENDPOINTS'][] = [
311 'MODULE_ID' => 'sender',
312 'CODE' => 'contact_list',
313 'FIELDS' => [
314 'LIST_ID' => $contactSetId,
315 'SENDER_SELECT_ALL' => null,
316 ],
317 ];
318 return $this;
319 }
320
326 protected function getFirstContactSetId()
327 {
328 foreach ($this->data['ENDPOINTS'] as $endpoint)
329 {
330 if ($endpoint['MODULE_ID'] !== 'sender' || $endpoint['CODE'] !== 'contact_list')
331 {
332 continue;
333 }
334
335 if (empty($endpoint['FIELDS']['LIST_ID']))
336 {
337 continue;
338 }
339
340 return $endpoint['FIELDS']['LIST_ID'];
341 }
342
343 return null;
344 }
345
352 public function upload(array $list)
353 {
354 $contactSetId = $this->getFirstContactSetId();
355 if (!$contactSetId)
356 {
357 $this->appendContactSetConnector()->save();
358 }
359
360 $contactSetId = $this->getFirstContactSetId();
361 if (!$contactSetId)
362 {
363 $this->errors->setError(new Error('Contact set not found.'));
364 return $this;
365 }
366
367 ContactTable::upload($list, false, $contactSetId);
368
369 return $this;
370 }
371
379 public static function updateAddressCounters($segmentId, array $counters)
380 {
381 if (!$segmentId)
382 {
383 return false;
384 }
385
386 $count = 0;
387 $countByType = array();
388 foreach ($counters as $dataCounter)
389 {
390 $count += $dataCounter->getSummary();
391 $list = $dataCounter->getList();
392 foreach ($list as $typeId => $typeCount)
393 {
394 if (!isset($countByType[$typeId]))
395 {
396 $countByType[$typeId] = 0;
397 }
398
399 $countByType[$typeId] += $typeCount;
400 }
401 }
402
403
404 $result = GroupTable::update($segmentId, array('ADDRESS_COUNT' => $count));
405 if (!$result->isSuccess())
406 {
407 return false;
408 }
409
410 GroupCounterTable::deleteByGroupId($segmentId);
411 $insertRows = [];
412 foreach ($countByType as $typeId => $typeCount)
413 {
414 if (!$typeCount)
415 {
416 continue;
417 }
418
419 $insertRows[] = [
420 'GROUP_ID' => $segmentId,
421 'TYPE_ID' => $typeId,
422 'CNT' => $typeCount,
423 ];
424 }
425
426 SqlBatch::insert(GroupCounterTable::getTableName(), $insertRows);
427
428 return true;
429 }
430
437 public static function getAddressCounter($segmentId)
438 {
439 $data = self::getAddressCounters(array($segmentId));
440 return new Connector\DataCounter(current($data) ?: array());
441 }
442
449 public static function getAddressCounters(array $list)
450 {
451 $data = array();
452 $list = GroupCounterTable::getList(array(
453 'select' => array('GROUP_ID', 'TYPE_ID', 'CNT'),
454 'filter' => array('=GROUP_ID' => $list),
455 'order' => array('GROUP_ID' => 'ASC')
456 ));
457 foreach ($list as $row)
458 {
459 $data[$row['GROUP_ID']][$row['TYPE_ID']] = $row['CNT'];
460 }
461 return $data;
462 }
463
471 public static function updateUseCounters(array $list, $isInclude = true)
472 {
473 if (count($list) === 0)
474 {
475 return;
476 }
477
478 $tableName = GroupTable::getTableName();
479 $now = Application::getConnection()->getSqlHelper()->convertToDbDateTime(new DateTime());
480 $ids = array();
481 foreach ($list as $element)
482 {
483 $id = $element['ID'];
484 if (!$id || !is_numeric($id))
485 {
486 continue;
487 }
488
489 $id = (int) $id;
490 if (!$id)
491 {
492 continue;
493 }
494
495 $ids[] = $id;
496 }
497 $ids = implode(', ', $ids);
498 $postfix = $isInclude ? '' : '_EXCLUDE';
499
500 $sql = "UPDATE $tableName SET USE_COUNT$postfix = USE_COUNT$postfix + 1, DATE_USE$postfix = $now WHERE ID IN ($ids)";
501 Application::getConnection()->query($sql);
502 }
503
509 public function remove()
510 {
511 return $this->removeByEntity(GroupTable::getEntity(), $this->getId());
512 }
513
519 public function copy()
520 {
521 $data = $this->loadData($this->getId());
522
523 return $this->copyData(
524 $this->getId(),
525 [
526 'NAME' => Loc::getMessage('SENDER_ENTITY_LETTER_COPY_PREFIX') . ' ' .$data['NAME'],
527 'CODE' => null,
528 'STATUS' => GroupTable::STATUS_NEW,
529 'DATE_USE' => null,
530 'USE_COUNT' => 0,
531 'USE_COUNT_EXCLUDE' => 0,
532 'DATE_INSERT' => new Type\DateTime(),
533 ]
534 );
535 }
536
543 public static function removeById($id)
544 {
545 return static::create()->removeByEntity(GroupTable::getEntity(), $id);
546 }
547}
$count
Определения admin_tab.php:4
Определения error.php:15
static getEntity()
Определения datamanager.php:65
static getRowById($id, array $parameters=[])
Определения datamanager.php:380
static getList(array $parameters=array())
Определения datamanager.php:431
static addMulti($rows, $ignoreEvents=false)
Определения datamanager.php:1041
static delete($primary)
Определения datamanager.php:1644
static add(array $data)
Определения datamanager.php:877
static update($primary, array $data)
Определения datamanager.php:1256
static getConnector(array $endpoint)
Определения manager.php:191
static upload(array $list, bool $isBlacklist=false, ?int $listId=null)
Определения contact.php:549
static updateUseCounters(array $list, $isInclude=true)
Определения segment.php:471
static getList(array $parameters=array())
Определения segment.php:76
loadData($id)
Определения segment.php:100
getFirstContactSetId()
Определения segment.php:326
static getAddressCounters(array $list)
Определения segment.php:449
static getAddressCounter($segmentId)
Определения segment.php:437
const CODE_ALL
Определения segment.php:30
static getIdByCode($code)
Определения segment.php:58
setFilterOnlyMode($mode=true)
Определения segment.php:281
static updateAddressCounters($segmentId, array $counters)
Определения segment.php:379
getDefaultData()
Определения segment.php:86
static getDefaultIds()
Определения segment.php:40
upload(array $list)
Определения segment.php:352
static removeById($id)
Определения segment.php:543
appendContactSetConnector($contactSetId=null)
Определения segment.php:293
saveData($id, array $data)
Определения segment.php:135
static deleteList(array $filter)
Определения group.php:311
const STATUS_DONE
Определения group.php:26
const STATUS_NEW
Определения group.php:23
static getTableName()
Определения group.php:30
static stat($event, $tag='', $label='')
Определения log.php:14
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения collection.php:2
hasErrors()
Определения errorableimplementation.php:17
Определения base.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$counters
Определения options.php:100