1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ContainsCollaberParamFiller.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
16
18{
19 private EO_ChatParam_Collection $storage;
20 private EO_Chat_Collection $chats;
21 private array $chatsRelationMap = [];
22 private static int $lastChatId;
23
24 private const LIMIT = 50;
25
26 public function __construct()
27 {
28 $this->storage = new EO_ChatParam_Collection();
29 }
30
31 public static function execute(): string
32 {
33 if (self::isCompleted())
34 {
35 return '';
36 }
37
38 self::$lastChatId = (int)(func_get_args()[0] ?? self::getMaxChatId());
39
40 if (self::$lastChatId <= 0)
41 {
42 self::setCompletedOption();
43
44 return '';
45 }
46
47 $doNeedToContinue = (new self())->run();
48
49 if (!$doNeedToContinue)
50 {
51 self::setCompletedOption();
52
53 return '';
54 }
55
56 return self::getAgentName();
57 }
58
59 private static function isCompleted(): bool
60 {
61 return Option::get('im', 'contains_collaber_param_filler_completed', 'N') === 'Y';
62 }
63
64 private static function setCompletedOption(): void
65 {
66 Option::set('im', 'contains_collaber_param_filler_completed', 'Y');
67 }
68
69 private static function getMaxChatId(): ?int
70 {
71 $query = ChatTable::query()
72 ->setSelect(['ID'])
73 ->setOrder(['ID' => 'DESC'])
74 ->setLimit(1)
75 ;
76
77 return $query->exec()->fetchObject()?->getId();
78 }
79
80 private static function getAgentName(): string
81 {
82 return '\\' . self::class . '::execute(' . self::$lastChatId . ');';
83 }
84
85 public function run(): bool
86 {
87 $this->fetchChats();
88 if ($this->chats->isEmpty())
89 {
90 return false;
91 }
92
93 $this->fetchChatsRelationMap();
94 $this->fillContainsCollaberParam();
95
96 $this->save();
97 $this->setLastChatId();
98
99 return self::$lastChatId !== 0;
100 }
101
102 private function fetchChats(): void
103 {
104 $query = ChatTable::query()
105 ->setSelect(['ID'])
106 ->setFilter([
107 '<=ID' => self::$lastChatId,
108 ])
109 ->setOrder(['ID' => 'DESC'])
110 ->setLimit(self::LIMIT)
111 ;
112
113 $this->chats = $query->exec()->fetchCollection();
114 }
115
116 private function fillContainsCollaberParam(): void
117 {
118 foreach ($this->chats as $chat)
119 {
120 $userIds = $this->chatsRelationMap[$chat->getId()] ?? [];
121 if (empty($userIds))
122 {
123 continue;
124 }
125
126 if (!UserCollection::hasUserByType($userIds, UserType::COLLABER))
127 {
128 continue;
129 }
130
131 $param = (new EO_ChatParam(false))
132 ->setChatId($chat->getId())
133 ->setParamName(Params::CONTAINS_COLLABER)
134 ->setParamValue('Y')
135 ;
136
137 $this->store($param);
138 }
139 }
140
141 private function fetchChatsRelationMap(): void
142 {
143 $chatIds = $this->chats->getIdList();
144 if (empty($chatIds))
145 {
146 return;
147 }
148
149 $relationCollection = RelationTable::query()
150 ->setSelect(['CHAT_ID', 'USER_ID'])
151 ->whereIn('CHAT_ID', $chatIds)
152 ->exec()
153 ->fetchAll()
154 ;
155
156 foreach ($relationCollection as $relation)
157 {
158 $chatId = (int)$relation['CHAT_ID'];
159 $userId = (int)$relation['USER_ID'];
160
161 $this->chatsRelationMap[$chatId][] = $userId;
162 }
163 }
164
165 private function store(EO_ChatParam $param): void
166 {
167 $this->storage->add($param);
168 }
169
170 private function save(): void
171 {
172 $this->storage->save(true);
173 $this->cleanCacheByChatIds(...$this->storage->getChatIdList());
174 }
175
176 private function cleanCacheByChatIds(int ...$chatIdList): void
177 {
178 foreach ($chatIdList as $chatId)
179 {
181 }
182 }
183
184 private function setLastChatId(): void
185 {
186 self::$lastChatId = min($this->chats->getIdList()) - 1;
187 }
188}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static cleanCache(int $chatId)
Определения Params.php:615
const CONTAINS_COLLABER
Определения Params.php:29
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
int $chatId
Определения Param.php:36
setChatId(int $chatId)
Определения Param.php:89