Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
message.php
1<?php
2namespace Bitrix\Im\Model;
3
8
9
49class MessageTable extends Main\Entity\DataManager
50{
56 public static function getTableName()
57 {
58 return 'b_im_message';
59 }
60
66 public static function getMap()
67 {
68 return array(
69 'ID' => array(
70 'data_type' => 'integer',
71 'primary' => true,
72 'autocomplete' => true,
73 //'title' => Loc::getMessage('MESSAGE_ENTITY_ID_FIELD'),
74 ),
75 'CHAT_ID' => array(
76 'data_type' => 'integer',
77 'required' => true,
78 //'title' => Loc::getMessage('MESSAGE_ENTITY_CHAT_ID_FIELD'),
79 ),
80 'AUTHOR_ID' => array(
81 'data_type' => 'integer',
82 'required' => true,
83 //'title' => Loc::getMessage('MESSAGE_ENTITY_AUTHOR_ID_FIELD'),
84 ),
85 'MESSAGE' => array(
86 'data_type' => 'text',
87 //'title' => Loc::getMessage('MESSAGE_ENTITY_MESSAGE_FIELD'),
88 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
89 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
90 ),
91 'MESSAGE_OUT' => array(
92 'data_type' => 'text',
93 //'title' => Loc::getMessage('MESSAGE_ENTITY_MESSAGE_OUT_FIELD'),
94 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
95 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
96 ),
97 'DATE_CREATE' => array(
98 'data_type' => 'datetime',
99 'required' => true,
100 //'title' => Loc::getMessage('MESSAGE_ENTITY_DATE_CREATE_FIELD'),
101 'default_value' => array(__CLASS__, 'getCurrentDate'),
102 ),
103 'EMAIL_TEMPLATE' => array(
104 'data_type' => 'string',
105 'validation' => array(__CLASS__, 'validateEmailTemplate'),
106 //'title' => Loc::getMessage('MESSAGE_ENTITY_EMAIL_TEMPLATE_FIELD'),
107 ),
108 'NOTIFY_TYPE' => array(
109 'data_type' => 'integer',
110 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_TYPE_FIELD'),
111 ),
112 'NOTIFY_MODULE' => array(
113 'data_type' => 'string',
114 'validation' => array(__CLASS__, 'validateNotifyModule'),
115 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_MODULE_FIELD'),
116 ),
117 'NOTIFY_EVENT' => array(
118 'data_type' => 'string',
119 'validation' => array(__CLASS__, 'validateNotifyEvent'),
120 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_EVENT_FIELD'),
121 ),
122 'NOTIFY_TAG' => array(
123 'data_type' => 'string',
124 'validation' => array(__CLASS__, 'validateNotifyTag'),
125 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_TAG_FIELD'),
126 ),
127 'NOTIFY_SUB_TAG' => array(
128 'data_type' => 'string',
129 'validation' => array(__CLASS__, 'validateNotifySubTag'),
130 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_SUB_TAG_FIELD'),
131 ),
132 'NOTIFY_TITLE' => array(
133 'data_type' => 'string',
134 'validation' => array(__CLASS__, 'validateNotifyTitle'),
135 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_TITLE_FIELD'),
136 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
137 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
138 ),
139 'NOTIFY_BUTTONS' => array(
140 'data_type' => 'text',
141 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_BUTTONS_FIELD'),
142 ),
143 'NOTIFY_READ' => array(
144 'data_type' => 'boolean',
145 'values' => array('N', 'Y'),
146 //'title' => Loc::getMessage('MESSAGE_ENTITY_NOTIFY_READ_FIELD'),
147 ),
148 'IMPORT_ID' => array(
149 'data_type' => 'integer',
150 //'title' => Loc::getMessage('MESSAGE_ENTITY_IMPORT_ID_FIELD'),
151 ),
152 'CHAT' => array(
153 'data_type' => 'Bitrix\Im\Model\ChatTable',
154 'reference' => array('=this.CHAT_ID' => 'ref.ID'),
155 ),
156 'AUTHOR' => array(
157 'data_type' => 'Bitrix\Main\User',
158 'reference' => array('=this.AUTHOR_ID' => 'ref.ID'),
159 ),
160 'STATUS' => array(
161 'data_type' => 'Bitrix\Im\Model\StatusTable',
162 'reference' => array('=this.AUTHOR_ID' => 'ref.USER_ID'),
163 ),
164 'RELATION' => array(
165 'data_type' => 'Bitrix\Im\Model\RelationTable',
166 'reference' => array('=this.CHAT_ID' => 'ref.CHAT_ID'),
167 'join_type' => 'INNER',
168 ),
169 'INDEX' => array(
170 'data_type' => 'Bitrix\Im\Model\MessageIndex',
171 'reference' => array('=this.ID' => 'ref.MESSAGE_ID'),
172 'join_type' => 'INNER',
173 ),
174 'UUID' => array(
175 'data_type' => \Bitrix\Im\Model\MessageUuidTable::class,
176 'reference' => array('=this.ID' => 'ref.MESSAGE_ID'),
177 'join_type' => 'LEFT',
178 ),
179 );
180 }
186 public static function validateEmailTemplate()
187 {
188 return array(
189 new Main\Entity\Validator\Length(null, 255),
190 );
191 }
197 public static function validateNotifyModule()
198 {
199 return array(
200 new Main\Entity\Validator\Length(null, 255),
201 );
202 }
208 public static function validateNotifyEvent()
209 {
210 return array(
211 new Main\Entity\Validator\Length(null, 255),
212 );
213 }
219 public static function validateNotifyTag()
220 {
221 return array(
222 new Main\Entity\Validator\Length(null, 255),
223 );
224 }
230 public static function validateNotifySubTag()
231 {
232 return array(
233 new Main\Entity\Validator\Length(null, 255),
234 );
235 }
241 public static function validateNotifyTitle()
242 {
243 return array(
244 new Main\Entity\Validator\Length(null, 255),
245 );
246 }
247
253 public static function getCurrentDate()
254 {
255 return new \Bitrix\Main\Type\DateTime();
256 }
257
267 public static function indexRecord($id)
268 {
269 $indexEnabled = \Bitrix\Main\Config\Option::get('im', 'message_history_index');
270
271 if (!$indexEnabled)
272 {
273 return;
274 }
275
276 $message = parent::getByPrimary($id)->fetch();
277 if (!is_array($message))
278 {
279 return;
280 }
281
283 [
284 'MESSAGE_ID' => $id,
285 'SEARCH_CONTENT' => self::generateSearchContent($message)
286 ]
287 );
288
289 return true;
290 }
291
299 private static function generateSearchContent(array $message) : string
300 {
301 $builder = MapBuilder::create();
302
303 if($message['AUTHOR_ID'] > 0)
304 {
305 $authorName = \Bitrix\Im\User::getInstance($message['AUTHOR_ID'])->getFullName();
306 $builder->addText($authorName);
307 }
308
309 $messageText = $message['MESSAGE'];
310 if($message['NOTIFY_TYPE'] === IM_NOTIFY_FROM)
311 {
312 $messageText = strip_tags($messageText);
313 }
314 else if($message['NOTIFY_TYPE'] === IM_NOTIFY_MESSAGE)
315 {
316 $messageText = Text::removeBbCodes($messageText);
317 }
318 $builder->addText($messageText);
319
320 $params = \CIMMessageParam::Get($message['ID']);
321 // Add text from attaches to builder
322 if(isset($params['ATTACH']))
323 {
324 $textNodes = \CIMMessageParamAttach::GetTextForIndex($params['ATTACH'][0]);
325 foreach($textNodes as $text)
326 {
327 $builder->addText($text);
328 }
329 }
330
331 // Add file names to builder
332 if(isset($params['FILE_ID']))
333 {
334 foreach($params['FILE_ID'] as $fileId)
335 {
336 $file = \Bitrix\Disk\File::getById($fileId);
337 if (!$file)
338 {
339 continue;
340 }
341 $builder->addText($file->getName());
342 }
343 }
344
345 return $builder->build();
346 }
347
353 public static function deleteBatch(array $filter)
354 {
355 $whereSql = \Bitrix\Main\Entity\Query::buildFilterSql(static::getEntity(), $filter);
356
357 if ($whereSql <> '')
358 {
359 $tableName = static::getTableName();
360 $connection = Main\Application::getConnection();
361 $connection->queryExecute("DELETE FROM {$tableName} WHERE {$whereSql}");
362 }
363 }
364
365 public static function withUnreadOnly(Query $query): void
366 {
367 $unreadSubQuery = MessageUnreadTable::query()
368 ->setSelect(['ID'])
369 ->where('MESSAGE_ID', new \Bitrix\Main\DB\SqlExpression('%s'))
370 ;
371 $query->whereExpr("EXISTS ({$unreadSubQuery->getQuery()})", ['ID']);
372 }
373
374 public static function withViewedOnly(Query $query): void
375 {
376 $viewedSubQuery = MessageViewedTable::query()
377 ->setSelect(['ID'])
378 ->where('MESSAGE_ID', new \Bitrix\Main\DB\SqlExpression('%s'))
379 ;
380 $query->whereExpr("EXISTS ({$viewedSubQuery->getQuery()})", ['ID']);
381 }
382
383 public static function withReadOnly(Query $query): void
384 {
385 $unreadSubQuery = MessageUnreadTable::query()
386 ->setSelect(['ID'])
387 ->where('MESSAGE_ID', new \Bitrix\Main\DB\SqlExpression('%s'))
388 ;
389 $query->whereExpr("NOT EXISTS ({$unreadSubQuery->getQuery()})", ['ID']);
390 }
391}
static withReadOnly(Query $query)
Definition message.php:383
static withUnreadOnly(Query $query)
Definition message.php:365
static withViewedOnly(Query $query)
Definition message.php:374
static deleteBatch(array $filter)
Definition message.php:353
static removeBbCodes($text, $withFile=false, $attachValue=false)
Definition text.php:295