Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
RecentlyUsedTable.php
1<?php
3
10use Bitrix\UI\Avatar;
11
13{
14 public static function getTableName(): string
15 {
16 return 'b_ui_avatar_mask_recently_used';
17 }
18
19 public static function getMap(): array
20 {
21 return array(
22 (new IntegerField('ID'))
23 ->configurePrimary()
24 ->configureAutocomplete(),
25
26 (new IntegerField('ITEM_ID'))->configureRequired(),
27 (new IntegerField('USER_ID', []))->configureRequired(),
28
29 (new DatetimeField('TIMESTAMP_X'))
30 ->configureDefaultValue(function() {
31 return new DateTime();
32 }),
33
34 (new Reference(
35 'MASK',
36 ItemTable::class,
37 Join::on('this.ITEM_ID', 'ref.ID')
38 ))->configureJoinType(Join::TYPE_INNER),
39 );
40 }
41
42 public static function addFromUser($itemId, $userId)
43 {
44 $entity = static::getEntity();
45 $sqlTableName = static::getTableName();
46 $sqlHelper = $entity->getConnection()->getSqlHelper();
47 $filter = ['ITEM_ID' => $itemId, 'USER_ID' => $userId];
48
49 $where = Main\ORM\Query\Query::buildFilterSql($entity, $filter);
50 if ($where !== '')
51 {
52 $sql = "DELETE FROM {$sqlHelper->quote($sqlTableName)} WHERE " . $where;
53 $entity->getConnection()->queryExecute($sql);
54 }
55 static::add($filter);
56 //Delete excessive data
57 $counter = 10;
58 $filter = ['=USER_ID' => $userId];
59 $records = static::getList([
60 'select' => ['ID'],
61 'filter' => $filter,
62 'limit' => $counter + 1,
63 'order' => [
64 'ID' => 'ASC'
65 ]
66 ])->fetchAll();
67 if (count($records) > $counter)
68 {
69 $lastRecord = end($records);
70 $filter['<ID'] = $lastRecord['ID'];
71 $where = Main\ORM\Query\Query::buildFilterSql($entity, $filter);
72 if ($where !== '')
73 {
74 $sql = "DELETE FROM {$sqlHelper->quote($sqlTableName)} WHERE " . $where;
75 $entity->getConnection()->queryExecute($sql);
76 }
77 }
78 }
79}