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