Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
DisappearService.php
1<?php
2
4
9use \Bitrix\Main\Result;
11
13{
14 public const TIME_WHITELIST = [
15 0, //disable
16 1, //hour
17 24, //day
18 168, //week
19 720, //month
20 ];
21 private const DISAPPEARING_TIME_UNIT = 'hours';
22 private const TIME_UNIT_HOUR = 'HOUR';
23 private const TIME_UNIT_DAY = 'DAY';
24 private const TIME_UNIT_WEEK = 'WEEK';
25 private const TIME_UNIT_MONTH = 'MONTH';
26
34 public static function checkDisappearing($messageId, $messageFields): bool
35 {
36 $message = new Message($messageId);
37 if (
38 !$message->getChat()->getDisappearingTime()
39 || $message->isSystem()
40 )
41 {
42 return false;
43 }
44
46 'MESSAGE_ID' => $message->getId(),
47 'DATE_CREATE' => new DateTime(),
48 'DATE_REMOVE' => (new DateTime())->add($message->getChat()->getDisappearingTime() . ' ' . self::DISAPPEARING_TIME_UNIT)
49 ]);
50
51 return $result->isSuccess();
52 }
53
54 public static function disappearMessage(Message $message, int $hours): Result
55 {
56 if (
57 $message->isDisappearing()
58 )
59 {
61 }
62
64 'MESSAGE_ID' => $message->getId(),
65 'DATE_CREATE' => new DateTime(),
66 'DATE_REMOVE' => (new DateTime())->add($hours . ' ' . self::DISAPPEARING_TIME_UNIT)
67 ]);
68 }
69
70 public static function disappearChat(Chat $chat, int $hours): Result
71 {
72 $prevDisappearingTime = $chat->getDisappearingTime();
73 if ((int)$prevDisappearingTime === $hours)
74 {
75 return new Result();
76 }
77
78 $chat->setDisappearingTime($hours);
79 $result = $chat->save();
80
81 if (!$result->isSuccess())
82 {
83 return $result;
84 }
85
86 if ((int)$prevDisappearingTime === 0 && $hours > 0)
87 {
88 \CIMMessage::Add([
89 'MESSAGE_TYPE' => $chat->getType(),
90 'TO_CHAT_ID' => $chat->getChatId(),
91 'MESSAGE' => self::getDisappearingMessage($hours),
92 'SYSTEM' => 'Y',
93 'PUSH' => 'N'
94 ]);
95 }
96 elseif ($prevDisappearingTime > 0 && $hours === 0)
97 {
98 \CIMMessage::Add([
99 'MESSAGE_TYPE' => $chat->getType(),
100 'TO_CHAT_ID' => $chat->getChatId(),
101 'MESSAGE' => Loc::getMessage('DISAPPEAR_MESSAGES_OFF'),
102 'SYSTEM' => 'Y',
103 'PUSH' => 'N'
104 ]);
105 }
106 elseif ($prevDisappearingTime > 0 && $hours > 0)
107 {
108 \CIMMessage::Add([
109 'MESSAGE_TYPE' => $chat->getType(),
110 'TO_CHAT_ID' => $chat->getChatId(),
111 'MESSAGE' => self::getDisappearingMessage($hours, true),
112 'SYSTEM' => 'Y',
113 'PUSH' => 'N'
114 ]);
115 }
116
117 return $result;
118 }
119
120 public static function getMessagesDisappearingTime(array $messageIds): array
121 {
123 'filter' => [
124 'MESSAGE_ID' => $messageIds
125 ]
126 ]);
127 $result = [];
128 foreach ($rows as $row)
129 {
130 $result[$row['MESSAGE_ID']] = $row;
131 }
132
133 return $result;
134 }
135
136 private static function getDisappearingMessage(int $hours, bool $change = false): string
137 {
138 switch ($hours)
139 {
140 case 720:
141 $timeUnit = self::TIME_UNIT_MONTH;
142 break;
143 case 168:
144 $timeUnit = self::TIME_UNIT_WEEK;
145 break;
146 case 24:
147 $timeUnit = self::TIME_UNIT_DAY;
148 break;
149 default:
150 $timeUnit = self::TIME_UNIT_HOUR;
151 }
152
153 $messageParts = [
154 'DISAPPEAR_MESSAGES',
155 $change ? 'CHANGE' : 'ON',
156 $timeUnit,
157 ];
158
159 return Loc::getMessage(implode('_', $messageParts));
160 }
161}
static getType($chatData)
Definition chat.php:41
static disappearChat(Chat $chat, int $hours)
static disappearMessage(Message $message, int $hours)
static getMessagesDisappearingTime(array $messageIds)
static checkDisappearing($messageId, $messageFields)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
addError(Error $error)
Definition result.php:50