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