Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sonetlogcollector.php
1<?php
2
4
6use Bitrix\Main\Entity\ExpressionField;
10
12{
13 private const RECOUNT_FROM = 'lf_cnt_recount_from';
14 private int $userId;
15
20 private array $socialGroups = [];
21
22 private array $accessCodes = [];
23
24 private static array $instances = [];
25
26 public static function getInstance(int $userId)
27 {
28 if (!array_key_exists($userId, self::$instances))
29 {
30 self::$instances[$userId] = new self($userId);
31 }
32
33 return self::$instances[$userId];
34 }
35
36 private function __construct(int $userId)
37 {
38 $this->userId = $userId;
39 }
40
41 public function fetch(int $limit, int $offset): array
42 {
43 $result = [];
44
45 $query = LogRightTable::query()
46 ->setSelect([
47 'LOG_ID',
48 ])
49 ->whereIn('GROUP_CODE', $this->getAccessCodes())
50 ->where('LOG_UPDATE', '>', $this->getDateStartFrom())
51 ->setLimit($limit)
52 ->setOffset($offset)
53 ->exec();
54
55 foreach ($query->fetchAll() as $row)
56 {
57 $result[] = $row['LOG_ID'];
58 }
59
60 return $result;
61 }
62
63 public function fetchTotal(): int
64 {
65 $res = LogRightTable::getList([
66 'select' => ['CNT'],
67 'filter' => [
68 'GROUP_CODE' => $this->getAccessCodes(),
69 '>LOG_UPDATE' => $this->getDateStartFrom()
70 ],
71 'runtime' => [
72 new ExpressionField('CNT', 'COUNT(*)'),
73 ]
74 ])->fetch();
75
76 return $res['CNT'] ?? 0;
77 }
78
79
80 private function getUserAccessCodes(): array
81 {
82 return [
83 'G2',
84 'AU',
85 'U'.$this->userId,
86 ];
87 }
88
89 private function getUserAccessSocialGroups(): array
90 {
91 if (!empty($this->socialGroups))
92 {
93 return $this->socialGroups;
94 }
95
96 $query = UserAccessTable::query()
97 ->setDistinct()
98 ->setSelect([
99 'ACCESS_CODE',
100 ])
101 ->where('USER_ID', '=', $this->userId)
102 ->where('PROVIDER_ID', '=', 'socnetgroup')
103 ->exec();
104
105 foreach ($query->fetchAll() as $group)
106 {
107 $matches = [];
108 preg_match('/SG([0-9]+)/m', $group['ACCESS_CODE'], $matches);
109
110 if (isset($matches[0]))
111 {
112 $this->socialGroups[] = $matches[0];
113 }
114 }
115
116 return $this->socialGroups;
117 }
118
119 private function getDateStartFrom(): Date
120 {
121 $recountFromOption = Option::get('socialnetwork', self::RECOUNT_FROM, 'null', '-');
122
123 if ($recountFromOption !== 'null' && strtotime($recountFromOption))
124 {
125 return new Date($recountFromOption, 'Y-m-d H:i:s');
126 }
127
128 return new Date('2023-10-03 00:00:00', 'Y-m-d H:i:s');
129 }
130
131 private function getAccessCodes(): array
132 {
133 if (!empty($this->accessCodes))
134 {
135 return $this->accessCodes;
136 }
137
138 $this->accessCodes = array_merge(
139 $this->getUserAccessSocialGroups(),
140 $this->getUserAccessCodes()
141 );
142
143 return $this->accessCodes;
144 }
145}