Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notifyreadrecount.php
1<?php
2namespace Bitrix\Im\Update;
3
9
10Loc::loadMessages(__FILE__);
11
12final class NotifyReadRecount extends Stepper
13{
14 private const OPTION_NAME = 'im_notify_read_recount';
15
16 protected static $moduleId = 'im';
17
21 public function execute(array &$result): bool
22 {
23 Option::delete(self::$moduleId, ['name' => self::OPTION_NAME]);
24 return false;
25 global $DB;
26
27 if (!Loader::includeModule(self::$moduleId))
28 {
29 return false;
30 }
31
32 $return = false;
33
34 $params = Option::get(self::$moduleId, self::OPTION_NAME);
35 $params = ($params !== '' ? @unserialize($params, ['allowed_classes' => false]) : []);
36 $params = (is_array($params) ? $params : []);
37
38 if (empty($params))
39 {
40 $params = [
41 'lastId' => 0,
42 'number' => 0,
43 'count' => RelationTable::getCount([
44 '>COUNTER' => 0,
45 '=MESSAGE_TYPE' => \Bitrix\Im\Chat::TYPE_SYSTEM
46 ]),
47 ];
48 }
49
50 if ($params['count'] > 0)
51 {
52 $result['steps'] = '';
53 $result['count'] = $params['count'];
54
55 $cursor = RelationTable::getList([
56 'select' => ['ID', 'CHAT_ID'],
57 'filter' => [
58 '>ID' => $params['lastId'],
59 '>COUNTER' => 0,
60 '=MESSAGE_TYPE' => \Bitrix\Im\Chat::TYPE_SYSTEM
61 ],
62 'order' => ['ID' => 'ASC'],
63 'limit' => 1000
64 ]);
65
66 $connection = \Bitrix\Main\Application::getInstance()->getConnection();
67 $sqlDate = $connection->getSqlHelper()->addDaysToDateTime(-30);
68
69 $found = false;
70 while ($row = $cursor->fetch())
71 {
72 $DB->Query("
73 UPDATE b_im_message M
74 SET M.NOTIFY_READ = 'Y'
75 WHERE M.CHAT_ID = " . $row['CHAT_ID'] . "
76 AND M.NOTIFY_READ <> 'Y'
77 AND M.DATE_CREATE < {$sqlDate}
78 ");
79
80 $counterResult = $DB->Query("
81 SELECT COUNT(1) as CNT
82 FROM b_im_message M
83 WHERE M.CHAT_ID = " . $row['CHAT_ID'] . "
84 AND NOTIFY_READ <> 'Y'
85 ")->GetNext();
86
87 RelationTable::update($row['ID'], [
88 'COUNTER' => $counterResult['CNT']
89 ]);
90
91 $params['lastId'] = $row['ID'];
92 $params['number']++;
93 $found = true;
94 }
95
96 if ($found)
97 {
98 Option::set(self::$moduleId, self::OPTION_NAME, serialize($params));
99 $return = true;
100 }
101 else
102 {
103 \Bitrix\Im\Counter::clearCache();
104 Option::delete(self::$moduleId, ['name' => self::OPTION_NAME]);
105 }
106
107 $result['steps'] = $params['number'];
108 }
109
110 return $return;
111 }
112}
const TYPE_SYSTEM
Definition chat.php:20
static loadMessages($file)
Definition loc.php:64