Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
logindex.php
1<?php
2
10
11use Bitrix\Main;
16use Bitrix\Disk\Uf\FileUserType;
17use Bitrix\Disk\AttachedObject;
19
21{
22 public static function getUserName($userId = 0)
23 {
24 $result = '';
25 $userId = (int)$userId;
26
27 if ($userId <= 0)
28 {
29 return $result;
30 }
31
32 $code = 'U'.$userId;
33 $data = self::getEntitiesName([ $code ]);
34 if (!empty($data[$code]))
35 {
36 $result = $data[$code];
37 }
38
39 return $result;
40 }
41
42 public static function getEntitiesName($entityCodesList = []): array
43 {
44 static $renderPartsUser = false;
45 static $renderPartsSonetGroup = false;
46 static $renderPartsDepartment = false;
47
48 $result = [];
49 if (
50 !is_array($entityCodesList)
51 || empty($entityCodesList)
52 )
53 {
54 return $result;
55 }
56
57 $renderOptions = [
58 'skipLink' => true,
59 ];
60
61 if ($renderPartsUser === false)
62 {
63 $renderPartsUser = new RenderParts\User($renderOptions);
64 }
65 if ($renderPartsSonetGroup === false)
66 {
67 $renderPartsSonetGroup = new RenderParts\SonetGroup($renderOptions);
68 }
69 if ($renderPartsDepartment === false)
70 {
71 $renderPartsDepartment = new RenderParts\Department($renderOptions);
72 }
73
74 foreach ($entityCodesList as $code)
75 {
76 $renderData = false;
77 if (preg_match('/^U(\d+)$/i', $code, $matches))
78 {
79 $renderData = $renderPartsUser->getData($matches[1]);
80 }
81 elseif (preg_match('/^SG(\d+)$/i', $code, $matches))
82 {
83 $renderData = $renderPartsSonetGroup->getData($matches[1]);
84 }
85 elseif (
86 preg_match('/^D(\d+)$/i', $code, $matches)
87 || preg_match('/^DR(\d+)$/i', $code, $matches)
88 )
89 {
90 $renderData = $renderPartsDepartment->getData($matches[1]);
91 }
92
93 if (
94 $renderData
95 && $renderData['name']
96 )
97 {
98 $result[$code] = $renderData['name'];
99 }
100 }
101
102 return $result;
103 }
104
105 public static function getDiskUFFileNameList($valueList = []): array
106 {
107 $result = [];
108
109 if (
110 !empty($valueList)
111 && is_array($valueList)
112 && Loader::includeModule('disk')
113 )
114 {
115 $attachedIdList = [];
116 foreach ($valueList as $value)
117 {
118 list($type, $realValue) = FileUserType::detectType($value);
119 if ($type == FileUserType::TYPE_NEW_OBJECT)
120 {
121 $file = \Bitrix\Disk\File::loadById($realValue, [ 'STORAGE' ]);
122 $result[] = strip_tags($file->getName());
123 }
124 else
125 {
126 $attachedIdList[] = $realValue;
127 }
128 }
129
130 if (!empty($attachedIdList))
131 {
132 $attachedObjects = AttachedObject::getModelList([
133 'with' => [ 'OBJECT' ],
134 'filter' => [
135 'ID' => $attachedIdList,
136 ],
137 ]);
138 foreach ($attachedObjects as $attachedObject)
139 {
140 $file = $attachedObject->getFile();
141 $result[] = strip_tags($file->getName());
142 }
143 }
144 }
145
146 return $result;
147 }
148
149 public static function setIndex($params = []): void
150 {
151 if (!is_array($params))
152 {
153 return;
154 }
155
156 $fields = ($params['fields'] ?? []);
157 $itemType = trim($params['itemType'] ?? '');
158 $itemId = (int)($params['itemId'] ?? 0);
159
160 if (
161 !is_array($fields)
162 || empty($fields)
163 || empty($itemType)
164 || !in_array($itemType, LogIndexTable::getItemTypes())
165 || $itemId <= 0
166 )
167 {
168 return;
169 }
170
171 $eventId = trim($fields['EVENT_ID'] ?? '');
172 $sourceId = (int)($fields['SOURCE_ID'] ?? 0);
173 $logId = (int)($fields['LOG_ID'] ?? 0);
174 $dateCreate = false;
175 $logDateUpdate = false;
176
177 if (
178 empty($eventId)
179 || $sourceId <= 0
180 )
181 {
182 if ($itemType === LogIndexTable::ITEM_TYPE_LOG)
183 {
184 $logId = $itemId;
185 $res = LogTable::getList([
186 'filter' => [
187 '=ID' => $itemId,
188 ],
189 'select' => [ 'ID', 'EVENT_ID', 'SOURCE_ID', 'LOG_UPDATE' ],
190 ]);
191 if ($logEntry = $res->fetch())
192 {
193 $eventId = trim($logEntry['EVENT_ID'] ?? '');
194 $sourceId = (int)($logEntry['SOURCE_ID'] ?? 0);
195 $logDateUpdate = $logEntry['LOG_UPDATE'];
196 $dateCreate = $logEntry['LOG_DATE'] ?? null;
197 }
198 }
199 elseif ($itemType === LogIndexTable::ITEM_TYPE_COMMENT)
200 {
202 'filter' => [
203 '=ID' => $itemId,
204 ],
205 'select' => [
206 'ID',
207 'LOG_ID',
208 'EVENT_ID',
209 'SOURCE_ID',
210 'LOG_UPDATE' => 'LOG.LOG_UPDATE',
211 'LOG_DATE',
212 ],
213 ]);
214 if ($comment = $res->fetch())
215 {
216 $eventId = trim($comment['EVENT_ID'] ?? '');
217 $sourceId = (int)($comment['SOURCE_ID'] ?? 0);
218 $logId = (int)($comment['LOG_ID'] ?? 0);
219 $logDateUpdate = $comment['LOG_UPDATE'];
220 $dateCreate = $comment['LOG_DATE'];
221 }
222 }
223 }
224
225 if (empty($eventId))
226 {
227 return;
228 }
229
230 $content = '';
231 $event = new Main\Event(
232 'socialnetwork',
233 ($itemType === LogIndexTable::ITEM_TYPE_COMMENT ? 'onLogCommentIndexGetContent' : 'onLogIndexGetContent'),
234 [
235 'eventId' => $eventId,
236 'sourceId' => $sourceId,
237 'itemId' => $itemId,
238 ]
239 );
240 $event->send();
241
242 foreach ($event->getResults() as $eventResult)
243 {
244 if ($eventResult->getType() == \Bitrix\Main\EventResult::SUCCESS)
245 {
246 $eventParams = $eventResult->getParameters();
247
248 if (
249 is_array($eventParams)
250 && isset($eventParams['content'])
251 )
252 {
253 $content = $eventParams['content'];
254 if (Main\Loader::includeModule('search'))
255 {
256 $content = \CSearch::killTags($content);
257 }
258 $content = trim(str_replace(
259 [ "\r", "\n", "\t" ],
260 ' ',
261 $content
262 ));
263
264 $content = self::prepareToken($content);
265 }
266 break;
267 }
268 }
269
270 if (empty($content))
271 {
272 return;
273 }
274
275 if ($logId <= 0)
276 {
277 if ($itemType === LogIndexTable::ITEM_TYPE_LOG)
278 {
279 $logId = $itemId;
280 }
281 elseif ($itemType === LogIndexTable::ITEM_TYPE_COMMENT)
282 {
284 'filter' => [
285 '=ID' => $itemId,
286 ],
287 'select' => [
288 'ID',
289 'LOG_ID',
290 'LOG.LOG_UPDATE',
291 'LOG_UPDATE' => 'LOG.LOG_UPDATE',
292 'LOG_DATE',
293 ],
294 ]);
295 if ($comment = $res->fetch())
296 {
297 $logId = (int)$comment['LOG_ID'];
298 $logDateUpdate = $comment['LOG_UPDATE'];
299 $dateCreate = $comment['LOG_DATE'];
300 }
301 }
302 }
303
304 if ($logId <= 0)
305 {
306 return;
307 }
308
309 if (
310 !$logDateUpdate
311 || (
312 !$dateCreate
313 && $itemType === LogIndexTable::ITEM_TYPE_LOG
314 )
315 )
316 {
317 $res = LogTable::getList([
318 'filter' => [
319 '=ID' => $logId,
320 ],
321 'select' => [ 'ID', 'LOG_UPDATE', 'LOG_DATE' ],
322 ]);
323 if ($logEntry = $res->fetch())
324 {
325 $logDateUpdate = $logEntry['LOG_UPDATE'];
326 if ($itemType === LogIndexTable::ITEM_TYPE_LOG)
327 {
328 $dateCreate = $logEntry['LOG_DATE'];
329 }
330 }
331 }
332
333 if (
334 !$dateCreate
335 && $itemType === LogIndexTable::ITEM_TYPE_COMMENT
336 )
337 {
339 'filter' => [
340 '=ID' => $itemId,
341 ],
342 'select' => [ 'ID', 'LOG_DATE' ],
343 ]);
344 if ($logComment = $res->fetch())
345 {
346 $dateCreate = $logComment['LOG_DATE'];
347 }
348 }
349
350 $indexFields = [
351 'itemType' => $itemType,
352 'itemId' => $itemId,
353 'logId' => $logId,
354 'content' => $content,
355 ];
356
357 if ($logDateUpdate)
358 {
359 $indexFields['logDateUpdate'] = $logDateUpdate;
360 }
361
362 if ($dateCreate)
363 {
364 $indexFields['dateCreate'] = $dateCreate;
365 }
366
367 LogIndexTable::set($indexFields);
368 }
369
370 public static function deleteIndex($params = []): void
371 {
372 if (!is_array($params))
373 {
374 return;
375 }
376
377 $itemType = trim($params['itemType'] ?? '');
378 $itemId = (int)($params['itemId'] ?? 0);
379
380 if (
381 empty($itemType)
382 || !in_array($itemType, LogIndexTable::getItemTypes())
383 || $itemId <= 0
384 )
385 {
386 return;
387 }
388
389 if ($itemType === LogIndexTable::ITEM_TYPE_LOG) // delete all comments
390 {
391 $connection = Main\Application::getConnection();
392 $query = "DELETE FROM ".LogIndexTable::getTableName()." WHERE LOG_ID = ".$itemId;
393 $connection->queryExecute($query);
394 }
395
396 LogIndexTable::delete([
397 'ITEM_TYPE' => $itemType,
398 'ITEM_ID' => $itemId,
399 ]);
400 }
401
402 public static function prepareToken($str): string
403 {
404 return str_rot13($str);
405 }
406
407 public static function OnAfterLogUpdate(\Bitrix\Main\Entity\Event $event)
408 {
409 $primary = $event->getParameter('primary');
410 $logId = (int)(!empty($primary['ID']) ? $primary['ID'] : 0);
411 $fields = $event->getParameter('fields');
412
413 if (
414 $logId > 0
415 && !empty($fields)
416 && !empty($fields['LOG_UPDATE'])
417 )
418 {
420 'logId' => $logId,
421 'value' => $fields['LOG_UPDATE'],
422 ]);
423 }
424 }
425}
static getList(array $parameters=array())
static getEntitiesName($entityCodesList=[])
Definition logindex.php:42
static OnAfterLogUpdate(\Bitrix\Main\Entity\Event $event)
Definition logindex.php:407
static getDiskUFFileNameList($valueList=[])
Definition logindex.php:105
static setLogUpdate($params=[])
Definition logindex.php:143