1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Loaders; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Support\MediaModels; |
6
|
|
|
use Nip\Filesystem\File; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Database |
10
|
|
|
*/ |
11
|
|
|
class Database extends AbstractLoader |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return File[] |
15
|
|
|
*/ |
16
|
|
|
public function getMediaFiles() |
17
|
|
|
{ |
18
|
|
|
$files = $this->tryDatabase(); |
19
|
|
|
if (is_array($files)) { |
20
|
|
|
return $files; |
21
|
|
|
} |
22
|
|
|
$files = $this->tryFilesystem(); |
23
|
|
|
|
24
|
|
|
foreach ($files as $file) { |
25
|
|
|
MediaModels::records()->createFor( |
26
|
|
|
$file, |
27
|
|
|
$this->getCollection()->getRecord(), |
|
|
|
|
28
|
|
|
$this->getCollection()->getName() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$propertiesRecord = MediaModels::properties()->createFor( |
33
|
|
|
$this->getCollection()->getRecord(), |
|
|
|
|
34
|
|
|
$this->getCollection()->getName() |
35
|
|
|
); |
36
|
|
|
$propertiesRecord->dbLoaded(true); |
37
|
|
|
$propertiesRecord->save(); |
38
|
|
|
|
39
|
|
|
return $files; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array|false |
44
|
|
|
*/ |
45
|
|
|
protected function tryDatabase() |
46
|
|
|
{ |
47
|
|
|
$propertiesRecord = MediaModels::properties()->for( |
48
|
|
|
$this->getCollection()->getRecord(), |
|
|
|
|
49
|
|
|
$this->getCollection()->getName() |
50
|
|
|
); |
51
|
|
|
if (!is_object($propertiesRecord) || $propertiesRecord->dbLoaded() === false) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$mediaRecords = MediaModels::records()->for( |
56
|
|
|
$this->getCollection()->getRecord(), |
|
|
|
|
57
|
|
|
$this->getCollection()->getName() |
58
|
|
|
); |
59
|
|
|
$files = []; |
60
|
|
|
foreach ($mediaRecords as $mediaRecord) { |
61
|
|
|
$files[] = new File($this->getFilesystem(),$mediaRecord->path); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
return $files; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array|false |
68
|
|
|
*/ |
69
|
|
|
protected function tryFilesystem() |
70
|
|
|
{ |
71
|
|
|
$loader = $this->initFromSibling(Filesystem::class); |
72
|
|
|
return $loader->getMediaFiles(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|