Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2namespace Bitrix\Landing;
3
4use \Bitrix\Landing\Internals\FileTable;
5
6class File
7{
11 public const ENTITY_TYPE_SITE = 'S';
12
16 public const ENTITY_TYPE_LANDING = 'L';
17
21 public const ENTITY_TYPE_BLOCK = 'B';
22
26 public const ENTITY_TYPE_ASSET = 'A';
27
33 public static function transliterateFileName(string $fileName): string
34 {
35 $parts = pathinfo($fileName);
36 $basename = $parts['filename'];
37 $transliterateBaseName = \CUtil::translit(
38 $basename,
39 'ru',
40 [
41 'replace_space' => '_',
42 'replace_other' => '_'
43 ]
44 );
45
46 return $transliterateBaseName . '.' . $parts['extension'];
47 }
48
54 public static function sanitizeFileName(string $fileName): string
55 {
56 return preg_replace(
57 '/[\‍(\‍)\s]+/s',
58 '_',
59 $fileName
60 );
61 }
62
71 protected static function add(int $fileId, int $entityId, string $entityType, bool $temp = false): void
72 {
73 $res = FileTable::getList(array(
74 'select' => array(
75 'ID'
76 ),
77 'filter' => array(
78 'FILE_ID' => $fileId,
79 'ENTITY_ID' => $entityId,
80 '=ENTITY_TYPE' => $entityType
81 )
82 ));
83 if (!$res->fetch())
84 {
85 $res = FileTable::add(array(
86 'FILE_ID' => $fileId,
87 'ENTITY_ID' => $entityId,
88 'ENTITY_TYPE' => $entityType,
89 'TEMP' => $temp ? 'Y' : 'N'
90 ));
91 $res->isSuccess();
92 }
93 }
94
101 protected static function getFiles($entityId, $entityType)
102 {
103 $files = [];
104 $res = FileTable::getList(array(
105 'select' => array(
106 'FILE_ID'
107 ),
108 'filter' => array(
109 'ENTITY_ID' => $entityId,
110 '=ENTITY_TYPE' => $entityType
111 )
112 ));
113 while ($row = $res->fetch())
114 {
115 $files[] = $row['FILE_ID'];
116 }
117 return $files;
118 }
119
127 protected static function delete($fileId, $entityId, $entityType)
128 {
129 //@tmp log
130 Debug::log(
131 $entityId . '@' . $entityType,
132 'fileId: ' . print_r($fileId, true) . '@' . print_r(\Bitrix\Main\Diag\Helper::getBackTrace(15), true),
133 'LANDING_FILE_MARK_DELETE'
134 );
135
136 $filter = array(
137 'ENTITY_ID' => $entityId,
138 '=ENTITY_TYPE' => $entityType
139 );
140 if ($fileId)
141 {
142 $filter['FILE_ID'] = $fileId;
143 }
144 $res = FileTable::getList(array(
145 'select' => array(
146 'ID', 'FILE_ID'
147 ),
148 'filter' => $filter
149 ));
150 while ($row = $res->fetch())
151 {
152 $resUpdate = FileTable::update(
153 $row['ID'],
154 array(
155 'FILE_ID' => -1 * abs($row['FILE_ID'])
156 )
157 );
158 $resUpdate->isSuccess();
159 }
160 }
161
167 public static function deleteFinal($limit = null)
168 {
169 $deletedFiles = [];
170
171 $res = FileTable::getList([
172 'select' => [
173 'ID', 'FILE_ID'
174 ],
175 'filter' => [
176 '<FILE_ID' => 0
177 ],
178 'limit' => $limit,
179 'order' => [
180 'ID' => 'asc'
181 ]
182 ]);
183 while ($row = $res->fetch())
184 {
185 $row['FILE_ID'] *= -1;
186 FileTable::delete($row['ID']);
187 $deletedFiles[$row['FILE_ID']] = $row['FILE_ID'];
188 }
189 if (!empty($deletedFiles))
190 {
191 // don't delete still used
192 $res = FileTable::getList([
193 'select' => [
194 'FILE_ID'
195 ],
196 'filter' => [
197 'FILE_ID' => $deletedFiles
198 ]
199 ]);
200 while ($row = $res->fetch())
201 {
202 unset($deletedFiles[$row['FILE_ID']]);
203 }
204 foreach ($deletedFiles as $fid)
205 {
206 $fileData = self::getFileArray($fid);
207 if ($fileData)
208 {
209 //@tmp log
210 Debug::log(
211 $fileData['SRC'],
212 'fileId: ' . $fid,
213 'LANDING_FILE_DELETE'
214 );
215 \CFile::delete($fid);
216 }
217 }
218 }
219 }
220
228 public static function addToSite(int $id, int $fileId, bool $temp = false): void
229 {
230 if ($fileId > 0 && $id > 0)
231 {
232 self::add($fileId, $id, self::ENTITY_TYPE_SITE, $temp);
233 }
234 }
235
241 public static function getFilesFromSite($siteId)
242 {
243 return self::getFiles(
244 $siteId,
245 self::ENTITY_TYPE_SITE
246 );
247 }
248
255 public static function deleteFromSite($id, $fileId = array())
256 {
257 self::delete($fileId, $id, self::ENTITY_TYPE_SITE);
258 }
259
266 public static function addToLanding($lid, $fileId)
267 {
268 if ($fileId > 0 && $lid > 0)
269 {
270 self::add($fileId, $lid, self::ENTITY_TYPE_LANDING);
271 }
272 }
273
279 public static function getFilesFromLanding($landingId)
280 {
281 return self::getFiles(
282 $landingId,
283 self::ENTITY_TYPE_LANDING
284 );
285 }
286
293 public static function deleteFromLanding($lid, $fileId = array())
294 {
295 self::delete($fileId, $lid, self::ENTITY_TYPE_LANDING);
296 }
297
305 public static function addToBlock(int $blockId, $fileId, bool $temp = false): void
306 {
307 if ($blockId > 0)
308 {
309 if (!is_array($fileId))
310 {
311 $fileId = array($fileId);
312 }
313 foreach ($fileId as $fid)
314 {
315 if ($fid > 0)
316 {
317 self::add($fid, $blockId, self::ENTITY_TYPE_BLOCK, $temp);
318 }
319 }
320 }
321 }
322
329 public static function replaceInBlock($blockId, $fileId)
330 {
331 if ($blockId > 0)
332 {
333 if (!is_array($fileId))
334 {
335 $fileId = array($fileId);
336 }
337 $res = FileTable::getList(array(
338 'select' => array(
339 'FILE_ID'
340 ),
341 'filter' => array(
342 'ENTITY_ID' => $blockId,
343 '=ENTITY_TYPE' => self::ENTITY_TYPE_BLOCK
344 )
345 ));
346 while ($row = $res->fetch())
347 {
348 if (!in_array($row['FILE_ID'], $fileId))
349 {
350 self::delete($row['FILE_ID'], $blockId, self::ENTITY_TYPE_BLOCK);
351 }
352 }
353 self::addToBlock($blockId, $fileId);
354 }
355 }
356
363 public static function deleteFromBlock($blockId, $fileId = array())
364 {
365 self::delete($fileId, $blockId, self::ENTITY_TYPE_BLOCK);
366 }
367
374 public static function getFilesFromBlockContent($blockId, $content)
375 {
376 $fileIds = array();
377 // parse from content
378 if (preg_match_all('/data-fileid[2x]{0,2}="([\d]+)"/i', $content, $matches))
379 {
380 foreach ($matches[1] as $fid)
381 {
382 $fileIds[] = $fid;
383 }
384 }
385 // check if files ids set in blockId
386 if (!empty($fileIds))
387 {
388 $res = FileTable::getList(array(
389 'select' => array(
390 'FILE_ID'
391 ),
392 'filter' => array(
393 'FILE_ID' => $fileIds,
394 'ENTITY_ID' => $blockId,
395 '=ENTITY_TYPE' => self::ENTITY_TYPE_BLOCK
396 )
397 ));
398 $fileIds = array();
399 while ($row = $res->fetch())
400 {
401 $fileIds[] = $row['FILE_ID'];
402 }
403 }
404 return $fileIds;
405 }
406
412 public static function getFilesFromBlock($blockId)
413 {
414 return self::getFiles(
415 $blockId,
416 self::ENTITY_TYPE_BLOCK
417 );
418 }
419
426 public static function addToAsset($assetId, $fileId): void
427 {
428 if ($fileId > 0 && $assetId > 0)
429 {
430 self::add($fileId, $assetId, self::ENTITY_TYPE_ASSET);
431 self::markAssetRebuilded($assetId);
432 // todo: res from add and check error
433 }
434 }
435
441 public static function getFilesFromAsset($assetId): array
442 {
443 return self::getFiles(
444 $assetId,
445 self::ENTITY_TYPE_ASSET
446 );
447 }
448
456 public static function deleteFromAsset(int $assetId, $fileId = []): void
457 {
458 self::delete($fileId, $assetId, self::ENTITY_TYPE_ASSET);
459 }
460
466 public static function markAssetToRebuild($assetId = []): bool
467 {
468 $filter = [
469 '=ENTITY_TYPE' => self::ENTITY_TYPE_ASSET
470 ];
471 if ($assetId)
472 {
473 $filter['ENTITY_ID'] = $assetId;
474 }
475
476 $res = FileTable::getList([
477 'select' => ['ID', 'ENTITY_ID'],
478 'filter' => $filter
479 ]);
480 $files = $res->fetchAll();
481 $result = true;
482 foreach ($files as $file)
483 {
484 $resUpdate = FileTable::update(
485 $file['ID'],
486 [
487 'ENTITY_ID' => -1 * abs($file['ENTITY_ID'])
488 ]
489 );
490 $result = $result && $resUpdate->isSuccess();
491 }
492
493 return count($files) > 0 ? $result : false;
494 }
495
501 public static function markAssetRebuilded($assetId): void
502 {
503 if(!is_array($assetId))
504 {
505 $assetId = [$assetId];
506 }
507
508 foreach ($assetId as $key => $id)
509 {
510 self::deleteFromAsset(-1 * abs($id));
511 }
512 }
513
521 protected static function copyEntityFiles($from, $to, $entityType)
522 {
523 $res = FileTable::getList(array(
524 'select' => array(
525 'FILE_ID'
526 ),
527 'filter' => array(
528 'ENTITY_ID' => $from,
529 '=ENTITY_TYPE' => $entityType
530 )
531 ));
532 while ($row = $res->fetch())
533 {
534 FileTable::add(array(
535 'FILE_ID' => $row['FILE_ID'],
536 'ENTITY_ID' => $to,
537 'ENTITY_TYPE' => $entityType
538 ));
539 }
540 }
541
548 public static function copySiteFiles($from, $to)
549 {
550 self::copyEntityFiles($from, $to, self::ENTITY_TYPE_SITE);
551 }
552
559 public static function copyLandingFiles($from, $to)
560 {
561 self::copyEntityFiles($from, $to, self::ENTITY_TYPE_LANDING);
562 }
563
570 public static function copyBlockFiles($from, $to)
571 {
572 self::copyEntityFiles($from, $to, self::ENTITY_TYPE_BLOCK);
573 }
574
580 public static function getFileArray($fileId)
581 {
582 $file = \CFile::getFileArray(
583 $fileId
584 );
585 if (
586 isset($file['MODULE_ID']) &&
587 $file['MODULE_ID'] == 'landing'
588 )
589 {
590 return $file;
591 }
592 return false;
593 }
594
600 public static function getFilePath($fileId): ?string
601 {
602 $file = self::getFileArray($fileId);
603 if (isset($file['SRC']))
604 {
605 return $file['SRC'];
606 }
607 return null;
608 }
609
615 public static function releaseFile(int $fileId): void
616 {
617 $res = FileTable::getList(array(
618 'select' => [
619 'ID'
620 ],
621 'filter' => [
622 'FILE_ID' => $fileId
623 ]
624 ));
625 while ($row = $res->fetch())
626 {
627 FileTable::delete($row['ID']);
628 }
629 }
630
636 public static function deletePhysical(int $fileId): void
637 {
638 if (self::getFileArray($fileId))
639 {
640 self::releaseFile($fileId);
641 \CFile::delete($fileId);
642 }
643 }
644}
static markAssetToRebuild($assetId=[])
Definition file.php:466
static getFilesFromLanding($landingId)
Definition file.php:279
static addToLanding($lid, $fileId)
Definition file.php:266
static copyEntityFiles($from, $to, $entityType)
Definition file.php:521
static addToAsset($assetId, $fileId)
Definition file.php:426
static replaceInBlock($blockId, $fileId)
Definition file.php:329
static add(int $fileId, int $entityId, string $entityType, bool $temp=false)
Definition file.php:71
static copyLandingFiles($from, $to)
Definition file.php:559
static addToSite(int $id, int $fileId, bool $temp=false)
Definition file.php:228
static addToBlock(int $blockId, $fileId, bool $temp=false)
Definition file.php:305
static deleteFromSite($id, $fileId=array())
Definition file.php:255
static deleteFromAsset(int $assetId, $fileId=[])
Definition file.php:456
static sanitizeFileName(string $fileName)
Definition file.php:54
static getFilesFromBlock($blockId)
Definition file.php:412
static deletePhysical(int $fileId)
Definition file.php:636
static copyBlockFiles($from, $to)
Definition file.php:570
static getFilesFromSite($siteId)
Definition file.php:241
static getFiles($entityId, $entityType)
Definition file.php:101
static deleteFinal($limit=null)
Definition file.php:167
static transliterateFileName(string $fileName)
Definition file.php:33
const ENTITY_TYPE_ASSET
Definition file.php:26
const ENTITY_TYPE_BLOCK
Definition file.php:21
static deleteFromBlock($blockId, $fileId=array())
Definition file.php:363
const ENTITY_TYPE_SITE
Definition file.php:11
static copySiteFiles($from, $to)
Definition file.php:548
static getFileArray($fileId)
Definition file.php:580
static deleteFromLanding($lid, $fileId=array())
Definition file.php:293
static getFilesFromAsset($assetId)
Definition file.php:441
static getFilesFromBlockContent($blockId, $content)
Definition file.php:374
static getFilePath($fileId)
Definition file.php:600
const ENTITY_TYPE_LANDING
Definition file.php:16
static markAssetRebuilded($assetId)
Definition file.php:501
static releaseFile(int $fileId)
Definition file.php:615
static delete($primary)
Definition file.php:242
static add(array $data)
Definition file.php:213
static update($primary, array $data)
Definition file.php:228