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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MediaProperties |
12
|
|
|
* @package ByTIC\MediaLibrary\Models\MediaRecords |
13
|
|
|
* |
14
|
|
|
* @method MediaProperty getNew() |
15
|
|
|
*/ |
16
|
|
|
class MediaProperties extends RecordManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @deprecated use initRelationsMedia |
20
|
|
|
*/ |
21
|
|
|
protected function initRelations() |
22
|
|
|
{ |
23
|
|
|
parent::initRelations(); |
24
|
|
|
|
25
|
|
|
$this->initRelationsMediaTrait(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function initRelationsMediaTrait() |
29
|
|
|
{ |
30
|
|
|
$this->morphTo( |
31
|
|
|
'Model', |
32
|
|
|
['morphTypeField' => 'model', 'fk' => 'model_id'] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Record|HasMediaPropertiesTrait $model |
38
|
|
|
* @param Collection|string $collection |
39
|
|
|
* @return MediaProperty|Record |
40
|
|
|
*/ |
41
|
|
|
public function for(Record $model, $collection) |
42
|
|
|
{ |
43
|
|
|
$collection = is_object($collection) ? $collection->getName() : $collection; |
44
|
|
|
if ($model->hasRelation('MediaProperties')) { |
45
|
|
|
return $model->getRelation('MediaProperties')->getResults()->filter( |
|
|
|
|
46
|
|
|
function ($item) use ($collection) { |
47
|
|
|
return $item->collection_name == $collection; |
48
|
|
|
} |
49
|
|
|
)->current(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->findOneByParams([ |
|
|
|
|
53
|
|
|
'where' => [ |
54
|
|
|
['model =?', $model->getManager()->getMorphName()], |
55
|
|
|
['model_id =?', $model->getPrimaryKey()], |
56
|
|
|
['collection_name=?', $collection] |
57
|
|
|
] |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $collection |
63
|
|
|
* @return MediaProperty|Record |
64
|
|
|
*/ |
65
|
|
|
public function forCollection(Collection $collection) |
66
|
|
|
{ |
67
|
|
|
return $this->for($collection->getRecord(), $collection->getName()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Record|HasMediaPropertiesTrait $model |
72
|
|
|
* @param $collection |
73
|
|
|
* @return MediaProperty |
74
|
|
|
*/ |
75
|
|
|
public function createFor(Record $model, $collection) |
76
|
|
|
{ |
77
|
|
|
$record = $this->getNew(); |
78
|
|
|
$record->populateFromModel($model); |
79
|
|
|
$record->populateFromCollection($collection); |
80
|
|
|
$record->setCustomPropertiesAttribute('{}'); |
81
|
|
|
$record->insert(); |
82
|
|
|
return $record; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
* @noinspection PhpMissingParentCallCommonInspection |
88
|
|
|
*/ |
89
|
|
|
protected function generateTable() |
90
|
|
|
{ |
91
|
|
|
return 'media-properties'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|