1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote; |
4
|
|
|
|
5
|
|
|
use Coyote\Models\Asset; |
6
|
|
|
use Coyote\Services\Eloquent\HasMany; |
7
|
|
|
use Coyote\Services\Media\Factory as MediaFactory; |
8
|
|
|
use Coyote\Services\Media\Logo; |
9
|
|
|
use Coyote\Services\Media\SerializeClass; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property int $id |
15
|
|
|
* @property int $is_agency |
16
|
|
|
* @property int $user_id |
17
|
|
|
* @property bool $is_private |
18
|
|
|
* @property string $name |
19
|
|
|
* @property string $city |
20
|
|
|
* @property string $street |
21
|
|
|
* @property string $street_number |
22
|
|
|
* @property string $postcode |
23
|
|
|
* @property string $website |
24
|
|
|
* @property string $description |
25
|
|
|
* @property string $vat_id |
26
|
|
|
* @property int $country_id |
27
|
|
|
* @property \Coyote\Firm\Benefit[] $benefits |
28
|
|
|
* @property Asset[] $assets |
29
|
|
|
* @property Logo $logo |
30
|
|
|
* @property \Coyote\Country $country |
31
|
|
|
*/ |
32
|
|
|
class Firm extends Model |
33
|
|
|
{ |
34
|
|
|
use SoftDeletes, SerializeClass; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The attributes that are mass assignable. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $fillable = [ |
42
|
|
|
'name', |
43
|
|
|
'logo', |
44
|
|
|
'website', |
45
|
|
|
'headline', |
46
|
|
|
'description', |
47
|
|
|
'employees', |
48
|
|
|
'founded', |
49
|
|
|
'is_agency', |
50
|
|
|
'country_id', |
51
|
|
|
'vat_id', |
52
|
|
|
'city', |
53
|
|
|
'street', |
54
|
|
|
'street_number', |
55
|
|
|
'postcode', |
56
|
|
|
'latitude', |
57
|
|
|
'longitude', |
58
|
|
|
'is_private', |
59
|
|
|
'youtube_url', |
60
|
|
|
'benefits', |
61
|
|
|
'country' |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $dateFormat = 'Y-m-d H:i:se'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Default fields values. Important for vue.js |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $attributes = [ |
75
|
|
|
'is_agency' => false |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
protected $casts = [ |
79
|
|
|
'is_agency' => 'bool' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string[] |
84
|
|
|
*/ |
85
|
|
|
public static function getEmployeesList() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
1 => '1-5', |
89
|
|
|
2 => '6-10', |
90
|
|
|
3 => '11-20', |
91
|
|
|
4 => '21-30', |
92
|
|
|
5 => '31-50', |
93
|
|
|
6 => '51-100', |
94
|
|
|
7 => '101-200', |
95
|
|
|
8 => '201-500', |
96
|
|
|
9 => '501-1000', |
97
|
|
|
10 => '1001-5000', |
98
|
|
|
11 => '5000+' |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return HasMany |
104
|
|
|
*/ |
105
|
|
|
public function benefits() |
106
|
|
|
{ |
107
|
|
|
$instance = new Firm\Benefit(); |
108
|
|
|
|
109
|
|
|
return new HasMany($instance->newQuery(), $this, $instance->getTable() . '.' . $this->getForeignKey(), $this->getKeyName()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
114
|
|
|
*/ |
115
|
|
|
public function assets() |
116
|
|
|
{ |
117
|
|
|
return $this->morphMany(Asset::class, 'content'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
122
|
|
|
*/ |
123
|
|
|
public function country() |
124
|
|
|
{ |
125
|
|
|
return $this->belongsTo(Country::class); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $name |
130
|
|
|
*/ |
131
|
|
|
public function setNameAttribute($name) |
132
|
|
|
{ |
133
|
|
|
$name = trim($name); |
134
|
|
|
|
135
|
|
|
$this->attributes['name'] = $name; |
136
|
|
|
$this->attributes['slug'] = str_slug($name, '_'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $value |
141
|
|
|
* @return \Coyote\Services\Media\MediaInterface |
142
|
|
|
*/ |
143
|
|
|
public function getLogoAttribute($value) |
144
|
|
|
{ |
145
|
|
|
if (!($value instanceof Logo)) { |
|
|
|
|
146
|
|
|
$logo = app(MediaFactory::class)->make('logo', ['file_name' => $value]); |
147
|
|
|
$this->attributes['logo'] = $logo; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->attributes['logo']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setLogoAttribute($logo) |
154
|
|
|
{ |
155
|
|
|
$this->attributes['logo'] = null; |
156
|
|
|
|
157
|
|
|
if ($logo) { |
158
|
|
|
$this->attributes['logo'] = trim(str_replace('/uploads', '', parse_url($logo, PHP_URL_PATH)), '/'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setYoutubeUrlAttribute($value) |
163
|
|
|
{ |
164
|
|
|
$this->attributes['youtube_url'] = $this->getEmbedUrl($value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setBenefitsAttribute($benefits) |
168
|
|
|
{ |
169
|
|
|
$benefits = array_filter(array_unique(array_map('trim', $benefits))); |
170
|
|
|
|
171
|
|
|
$models = []; |
172
|
|
|
|
173
|
|
|
foreach ($benefits as $benefit) { |
174
|
|
|
$models[] = new Firm\Benefit(['name' => $benefit]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($models) { |
178
|
|
|
$this->setRelation('benefits', collect($models)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string|null $country |
184
|
|
|
*/ |
185
|
|
|
public function setCountryAttribute(?string $country) |
186
|
|
|
{ |
187
|
|
|
$this->setAttribute( |
188
|
|
|
'country_id', |
189
|
|
|
$country ? (new Country())->where('name', $country)->orWhere('code', $country)->value('id') : null |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param array $attributes |
195
|
|
|
* @return $this|Model |
196
|
|
|
*/ |
197
|
|
|
public function fill(array $attributes) |
198
|
|
|
{ |
199
|
|
|
parent::fill($attributes); |
200
|
|
|
|
201
|
|
|
if ($this->is_agency) { |
202
|
|
|
foreach (['headline', 'latitude', 'longitude', 'country_id', 'street', 'city', 'street_number', 'postcode'] as $column) { |
203
|
|
|
$this->{$column} = null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->benefits->flush(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $url |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
private function getEmbedUrl($url) |
217
|
|
|
{ |
218
|
|
|
if (empty($url)) { |
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$components = parse_url($url); |
223
|
|
|
|
224
|
|
|
if (empty($components['query'])) { |
225
|
|
|
return $url; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
parse_str($components['query'], $query); |
229
|
|
|
|
230
|
|
|
return 'https://www.youtube.com/embed/' . $query['v']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|