18 private array $sonetLogGroups = [];
19 private array|
null $userAccessCodes =
null;
21 private static array $instances = [];
25 if (!array_key_exists($userId, self::$instances))
27 self::$instances[$userId] =
new self($userId);
30 return self::$instances[$userId];
33 private function __construct(
int $userId)
35 $this->userId = $userId;
38 public function recount(
string $counter, array $sonetLogIds = []): array
45 if (empty($sonetLogIds))
50 $sonetLogIds = array_unique($sonetLogIds);
58 $counters = $this->recountPosts($sonetLogIds);
61 $counters = $this->recountComments($sonetLogIds);
70 private function recountPosts(array $sonetLogIds): array
74 foreach ($sonetLogIds as $logId)
76 $logItem = Log::getById($logId);
82 $logItemFields = $logItem->getFields();
84 if ((
int)$logItemFields[
'USER_ID'] === $this->userId)
89 if (!in_array($logItemFields[
'ENTITY_TYPE'], \CSocNetAllowed::GetAllowedEntityTypes(),
true))
94 if (!$this->isItemForEveryOne($logId) && !$this->userHasAccess($logId))
100 'RATING_ENTITY_ID' => $logItemFields[
'RATING_ENTITY_ID'],
101 'RATING_TYPE_ID' => $logItemFields[
'RATING_TYPE_ID'],
103 if (!$this->isItemSeenByUser($params))
105 foreach ($this->findGroupsByLogIdAndUser($logId) as $groupId)
108 'USER_ID' => $this->userId,
109 'SONET_LOG_ID' => $logId,
110 'GROUP_ID' => $groupId,
121 private function recountComments(array $sonetLogIds): array
125 foreach ($sonetLogIds as $logId)
127 $logItem = Log::getById($logId);
133 $logItemFields = $logItem->getFields();
134 if (!in_array($logItemFields[
'ENTITY_TYPE'], \CSocNetAllowed::GetAllowedEntityTypes(),
true))
139 if (!$this->isItemForEveryOne($logId) && !$this->userHasAccess($logId))
145 'RATING_ENTITY_ID' => $logItemFields[
'RATING_ENTITY_ID'],
146 'RATING_TYPE_ID' => $logItemFields[
'RATING_TYPE_ID'],
148 $commentsCount = $this->isItemSeenByUser($params)
149 ? $this->getCountCommentsByLogItemAndLastDateSeen($logId, $params)
150 : $this->getCountCommentsByLogItem($logId);
152 if ($commentsCount > 0)
154 foreach ($this->findGroupsByLogIdAndUser($logId) as $groupId)
157 'USER_ID' => $this->userId,
158 'SONET_LOG_ID' => $logId,
159 'GROUP_ID' => $groupId,
161 'VALUE' => $commentsCount
170 private function getCountCommentsByLogItemAndLastDateSeen(
int $logId, array $params): int
172 $lastTimeSeen = $this->getContentViewByItem($params);
173 if (!isset($lastTimeSeen[
'DATE_VIEW']))
182 '!USER_ID' => $this->userId,
183 '>LOG_DATE' => $lastTimeSeen[
'DATE_VIEW']
186 new ExpressionField(
'CNT',
'COUNT(*)'),
190 return $res[
'CNT'] ?? 0;
193 private function getCountCommentsByLogItem(
int $logId): int
199 '!USER_ID' => $this->userId,
202 new ExpressionField(
'CNT',
'COUNT(*)'),
206 return $res[
'CNT'] ?? 0;
209 private function isItemSeenByUser(array $params): bool
211 return (
bool)$this->getContentViewByItem($params);
214 private function getContentViewByItem(array $params): array|false
216 return UserContentViewTable::getList([
217 'select' => [
'DATE_VIEW'],
219 '=USER_ID' => $this->userId,
220 '=RATING_ENTITY_ID' => $params[
'RATING_ENTITY_ID'],
221 '=RATING_TYPE_ID' => $params[
'RATING_TYPE_ID']
226 private function isItemForEveryOne(
int $logItemId): bool
230 if (in_array($logAccessRight, self::PUBLIC_ACCESS_CODES,
true))
239 private function userHasAccess(
int $logItemId): bool
243 if ($this->userAccessCodes ===
null && $rights)
245 $this->userAccessCodes = [];
246 $res = UserAccessTable::getList([
247 'select' => [
'ACCESS_CODE'],
249 '=USER_ID' => $this->userId,
250 '=ACCESS_CODE' => $rights,
254 foreach ($res as $access)
256 if (isset($access[
'ACCESS_CODE']))
258 $this->userAccessCodes[] = $access[
'ACCESS_CODE'];
263 foreach ($rights as $logRight)
265 if (in_array($logRight, $this->userAccessCodes,
true))
274 private function findGroupsByLogIdAndUser(
int $sonetLogId): array
276 if (!empty($this->sonetLogGroups[$sonetLogId]))
278 return $this->sonetLogGroups[$sonetLogId];
281 $this->sonetLogGroups[$sonetLogId] = [];
283 $userAccessCodes = array_merge(self::PUBLIC_ACCESS_CODES, [
'U' . $this->userId]);
284 foreach ($sonetLogRights as $logRight)
286 if (in_array($logRight, $userAccessCodes))
290 $this->sonetLogGroups[$sonetLogId][$commonGroupId] = $commonGroupId;
295 $query = UserAccessTable::query()
300 ->where(
'USER_ID',
'=', $this->userId)
301 ->where(
'PROVIDER_ID',
'=',
'socnetgroup')
302 ->whereIn(
'ACCESS_CODE', $sonetLogRights)
305 foreach ($query->fetchAll() as $group)
308 preg_match(
'/SG([0-9]+)/m', $group[
'ACCESS_CODE'], $matches);
309 if (isset($matches[1]))
311 $groupId = (int)$matches[1];
312 $this->sonetLogGroups[$sonetLogId][$groupId] = $groupId;
316 return $this->sonetLogGroups[$sonetLogId];
static getList(array $parameters=array())