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