Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sonetrightsrecepient.php
1<?php
2
4
9
10class SonetRightsRecepient implements \Iterator
11{
12 private Query $query;
14 private array $current = [];
16 private int $key = 0;
18 private bool $valid = false;
19
20 private Result $ormQueryResult;
21
22 public function __construct(private int $sonetLogId)
23 {
24 $this->queryInit($this->sonetLogId);
25 $this->next();
26 }
27
28 public function rewind(): void
29 {
30 $this->key = 0;
31 $this->queryInit($this->sonetLogId);
32 $this->next();
33 }
34
35 public function current(): Recepient
36 {
37 return new Recepient($this->current['ID']);
38 }
39
40 public function key(): int
41 {
42 return $this->key;
43 }
44
45 public function next(): void
46 {
47 $this->key++;
48 $row = $this->ormQueryResult->fetch();
49 if ($row === false)
50 {
51 $this->valid = false;
52 unset($this->ormQueryResult);
53 return;
54 }
55
56 $this->valid = true;
57 $this->current = $row;
58 }
59
60 public function valid(): bool
61 {
62 return $this->valid;
63 }
64
65 public function getSize(): int
66 {
67 return $this->query->queryCountTotal();
68 }
69
70 private function queryInit(int $sonetLogId)
71 {
72 // G2 - all users
73 // AU - authorised users
74 $all = 'G2';
75 $allAuthorised = 'AU';
76 $sonetLogRights = \Bitrix\Socialnetwork\Item\LogRight::get($sonetLogId);
77
78 if (in_array($all, $sonetLogRights) || in_array($allAuthorised, $sonetLogRights))
79 {
80 // for all users
81 $this->query = UserTable::query()
82 ->setSelect([
83 'ID',
84 ])
85 ->where('ACTIVE', '=', 'Y');
86
87 $this->ormQueryResult = $this->query->exec();
88 return;
89 }
90
91 // filter by user access
92 $this->query = UserAccessTable::query()
93 ->setDistinct()
94 ->setSelect([
95 'ID' => 'USER_ID',
96 ])
97 ->whereIn('ACCESS_CODE', $sonetLogRights);
98
99 $this->ormQueryResult = $this->query->exec();
100 }
101}