1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Models\MediaRecords; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
6
|
|
|
use ByTIC\MediaLibrary\Media\Traits\FileMethodsTrait; |
7
|
|
|
use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperty; |
8
|
|
|
use Nip\Records\AbstractModels\Record; |
9
|
|
|
use Nip\Records\Collections\Collection; |
10
|
|
|
use Nip\Records\RecordManager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MediaRecords |
14
|
|
|
* @package ByTIC\MediaLibrary\Models\MediaRecords |
15
|
|
|
* |
16
|
|
|
* @method MediaRecord getNew |
17
|
|
|
*/ |
18
|
|
|
class MediaRecords extends RecordManager |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Record $model |
23
|
|
|
* @param $collection |
24
|
|
|
* @return MediaProperty[]|Collection |
25
|
|
|
*/ |
26
|
|
|
public function for(Record $model, $collection) |
27
|
|
|
{ |
28
|
|
|
$collection = is_object($collection) ? $collection->getName() : $collection; |
29
|
|
|
|
30
|
|
|
if ($model->hasRelation('Media')) { |
31
|
|
|
return $model->getRelation('Media')->getResults()->filter( |
|
|
|
|
32
|
|
|
function ($item) use ($collection) { |
33
|
|
|
return $item->collection_name == $collection; |
34
|
|
|
} |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->findByParams([ |
39
|
|
|
'where' => [ |
40
|
|
|
['model =?', $model->getManager()->getMorphName()], |
41
|
|
|
['model_id =?', $model->getPrimaryKey()], |
42
|
|
|
['collection_name=?', $collection] |
43
|
|
|
] |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Media|FileMethodsTrait $media |
49
|
|
|
*/ |
50
|
|
|
public function deteleMedia(Media $media) |
51
|
|
|
{ |
52
|
|
|
$model = $media->getModel(); |
53
|
|
|
$this->deleteByParams([ |
54
|
|
|
'where' => [ |
55
|
|
|
['model =?', $model->getManager()->getMorphName()], |
|
|
|
|
56
|
|
|
['model_id =?', $model->getPrimaryKey()], |
57
|
|
|
['collection_name=?', $media->getCollection()->getName()], |
58
|
|
|
['path=?', $media->getPath()] |
59
|
|
|
] |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $file |
65
|
|
|
* @param Record $model |
66
|
|
|
* @param $collection |
67
|
|
|
* @return Record |
68
|
|
|
*/ |
69
|
|
|
public function createFor($file, Record $model, $collection) |
70
|
|
|
{ |
71
|
|
|
$record = $this->getNew(); |
72
|
|
|
$record->populateFromFile($file); |
73
|
|
|
$record->populateFromModel($model); |
74
|
|
|
$record->populateFromCollection($collection); |
75
|
|
|
$record->insert(); |
76
|
|
|
return $record; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
* @noinspection PhpMissingParentCallCommonInspection |
82
|
|
|
*/ |
83
|
|
|
protected function generateTable() |
84
|
|
|
{ |
85
|
|
|
return 'media-records'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|