|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Models\MediaProperties; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Collections\Collection; |
|
6
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
|
7
|
|
|
use Nip\Records\Record; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class MediaProperty |
|
11
|
|
|
* @package ByTIC\MediaLibrary\Models\MediaRecords |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $model |
|
14
|
|
|
* @property int $model_id |
|
15
|
|
|
* @property string $collection_name |
|
16
|
|
|
* @property string $custom_properties |
|
17
|
|
|
* |
|
18
|
|
|
* @method HasMediaTrait getModel |
|
19
|
|
|
*/ |
|
20
|
|
|
class MediaProperty extends Record |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var null|array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $customPropertiesArray = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Record $model |
|
29
|
|
|
*/ |
|
30
|
|
|
public function populateFromModel(Record $model) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->model = $model->getManager()->getMorphName(); |
|
|
|
|
|
|
33
|
|
|
$this->model_id = $model->getPrimaryKey(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Collection|string $collection |
|
38
|
|
|
*/ |
|
39
|
|
|
public function populateFromCollection($collection) |
|
40
|
|
|
{ |
|
41
|
|
|
$name = is_object($collection) ? $collection->getName() : $collection; |
|
42
|
|
|
$this->collection_name = $name; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getCustomProperties() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->checkCustomProperties(); |
|
51
|
|
|
return $this->customPropertiesArray; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function checkCustomProperties() |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->customPropertiesArray == null) { |
|
57
|
|
|
$this->initCustomProperties(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function initCustomProperties() |
|
63
|
|
|
{ |
|
64
|
|
|
$properties = json_decode($this->custom_properties, true); |
|
65
|
|
|
$properties = (is_array($properties)) ? $properties : []; |
|
66
|
|
|
$this->customPropertiesArray = $properties; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $name |
|
71
|
|
|
* @param null $default |
|
|
|
|
|
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getCustomProperty($name, $default = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->checkCustomProperties(); |
|
77
|
|
|
return isset($this->customPropertiesArray[$name]) ? $this->customPropertiesArray[$name] : $default; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $name |
|
82
|
|
|
* @param $value |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setCustomPropery($name, $value) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->checkCustomProperties(); |
|
87
|
|
|
$this->customPropertiesArray[$name] = $value; |
|
88
|
|
|
$this->setCustomPropertiesAttribute($this->getCustomProperties()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param bool $value |
|
93
|
|
|
*/ |
|
94
|
|
|
public function saveDbLoaded($value) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->dbLoaded($value); |
|
97
|
|
|
$this->save(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param bool|null $value |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function dbLoaded($value = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($value == null) { |
|
107
|
|
|
return $this->getCustomProperty('dbLoaded', false); |
|
108
|
|
|
} |
|
109
|
|
|
$value = ($value == true); |
|
|
|
|
|
|
110
|
|
|
$this->setCustomPropery('dbLoaded', $value); |
|
111
|
|
|
return $value; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param null $value |
|
|
|
|
|
|
116
|
|
|
* @return mixed|null |
|
117
|
|
|
*/ |
|
118
|
|
|
public function defaultMedia($value = null) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($value == null) { |
|
|
|
|
|
|
121
|
|
|
return $this->getCustomProperty('defaultMedia', $this->getModel()->default_image); |
|
122
|
|
|
} |
|
123
|
|
|
$this->setCustomPropery('defaultMedia', $value); |
|
124
|
|
|
$this->save(); |
|
125
|
|
|
|
|
126
|
|
|
if ($this->collection_name == 'images') { |
|
127
|
|
|
$this->getModel()->default_image = $value; |
|
128
|
|
|
$this->getModel()->update(); |
|
129
|
|
|
} |
|
130
|
|
|
return $value; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $value |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setDefaultMedia($value) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->dbLoaded($value); |
|
139
|
|
|
$this->save(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param $value |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setCustomPropertiesAttribute($value) |
|
146
|
|
|
{ |
|
147
|
|
|
if (is_string($value)) { |
|
148
|
|
|
$this->customPropertiesArray = null; |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->customPropertiesArray = $value; |
|
151
|
|
|
$value = json_encode($value); |
|
152
|
|
|
} |
|
153
|
|
|
$this->setDataValue('custom_properties', $value); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..