Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filepreviewtable.php
1<?
2
4
11
28final class FilePreviewTable extends DataManager
29{
31 protected static $alreadyDeleted = [];
32
38 public static function getTableName()
39 {
40 return 'b_file_preview';
41 }
42
49 public static function getMap()
50 {
51 return [
52 new Entity\IntegerField('ID', [
53 'primary' => true,
54 'autocomplete' => true,
55 ]),
56 new Entity\IntegerField('FILE_ID', [
57 'required' => true,
58 ]),
59 new Entity\IntegerField('PREVIEW_ID'),
60 new Entity\IntegerField('PREVIEW_IMAGE_ID'),
61 new Entity\DatetimeField('CREATED_AT', [
62 'default_value' => function () {
63 return new DateTime();
64 },
65 ]),
66 new Entity\DatetimeField('TOUCHED_AT', [
67 'default_value' => function () {
68 return new DateTime();
69 },
70 ]),
71 new Entity\ReferenceField('FILE',FileTable::class,
72 ['=this.FILE_ID' => 'ref.ID'],
73 ['join_type' => 'INNER']
74 ),
75 new Entity\ReferenceField('PREVIEW',FileTable::class,
76 ['=this.PREVIEW_ID' => 'ref.ID'],
77 ['join_type' => 'LEFT']
78 ),
79 new Entity\ReferenceField('PREVIEW_IMAGE',FileTable::class,
80 ['=this.PREVIEW_IMAGE_ID' => 'ref.ID'],
81 ['join_type' => 'LEFT']
82 ),
83 ];
84 }
85
86 public static function deleteOld($dayToDeath = 22, $portion = 20)
87 {
88 $deathTime = new Date();
89 $deathTime->add("-{$dayToDeath} day");
90
91 $query = self::query();
92 $filter = $query::filter()
93 ->logic('or')
94 ->whereNull('TOUCHED_AT')
95 ->where('TOUCHED_AT', '<', $deathTime)
96 ;
97
98 $files = self::getList([
99 'select' => ['ID', 'PREVIEW_IMAGE_ID', 'PREVIEW_ID'],
100 'filter' => $filter,
101 'limit' => $portion,
102 ]);
103
104 foreach ($files as $file)
105 {
106 $keepImage = isset($file['PREVIEW_IMAGE_ID']);
107
108 self::deleteContent($file, $keepImage);
109 if (!$keepImage)
110 {
111 self::delete($file['ID']);
112 }
113 }
114 }
115
116 public static function deleteOldAgent($dayToDeath = 22, $portion = 20)
117 {
118 self::deleteOld($dayToDeath, $portion);
119
120 return "\\Bitrix\\Main\\UI\\Viewer\\FilePreviewTable::deleteOldAgent({$dayToDeath}, {$portion});";
121 }
122
123 public static function onDelete(Event $event)
124 {
125 $id = $event->getParameter('primary')['ID'];
126 if (isset(self::$alreadyDeleted[$id]))
127 {
128 return;
129 }
130
131 $file = self::getRowById($id);
132 if (!$file)
133 {
134 return;
135 }
136
137 self::deleteContent($file);
138 }
139
140 protected static function deleteContent(array $file, $keepImage = false)
141 {
142 if (isset(self::$alreadyDeleted[$file['ID']]))
143 {
144 return;
145 }
146
147 self::$alreadyDeleted[$file['ID']] = true;
148
149 \CFile::delete($file['PREVIEW_ID']);
150 \CFile::delete($file['PREVIEW_IMAGE_ID']);
151 }
152
161 public static function onFileDelete($bfile)
162 {
163 if (empty($bfile['ID']))
164 {
165 return;
166 }
167
168 $file = self::getRow([
169 'filter' => [
170 '=FILE_ID' => $bfile['ID'],
171 ]
172 ]);
173
174 if (!$file)
175 {
176 return;
177 }
178
179 self::deleteContent($file);
180 self::delete($file['ID']);
181 }
182}
getParameter($key)
Definition event.php:80
static getRow(array $parameters)
static getList(array $parameters=array())
static deleteOld($dayToDeath=22, $portion=20)
static deleteOldAgent($dayToDeath=22, $portion=20)
static deleteContent(array $file, $keepImage=false)