-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSlide.php
176 lines (154 loc) · 4.36 KB
/
Slide.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace App\Models;
use Eloquent as Model;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
/**
* Class Slide
* @package App\Models
* @version September 1, 2020, 7:27 pm UTC
*
* @property \App\Models\Food food
* @property \App\Models\Restaurant restaurant
* @property integer order
* @property string text
* @property string button
* @property string text_position
* @property string text_color
* @property string button_color
* @property string background_color
* @property string indicator_color
* @property string image_fit
* @property integer food_id
* @property integer restaurant_id
* @property boolean enabled
*/
class Slide extends Model implements HasMedia
{
use HasMediaTrait {
getFirstMediaUrl as protected getFirstMediaUrlTrait;
}
public $table = 'slides';
public $fillable = [
'order',
'text',
'button',
'text_position',
'text_color',
'button_color',
'background_color',
'indicator_color',
'image_fit',
'food_id',
'restaurant_id',
'enabled'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'order' => 'integer',
'text' => 'string',
'button' => 'string',
'text_position' => 'string',
'text_color' => 'string',
'button_color' => 'string',
'background_color' => 'string',
'indicator_color' => 'string',
'image' => 'string',
'image_fit' => 'string',
'food_id' => 'integer',
'restaurant_id' => 'integer',
'enabled' => 'boolean'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'text_position' => 'required',
'image_fit' => 'required'
];
/**
* New Attributes
*
* @var array
*/
protected $appends = [
'custom_fields',
'has_media',
];
/**
* @param Media|null $media
* @throws \Spatie\Image\Exceptions\InvalidManipulation
*/
public function registerMediaConversions(Media $media = null)
{
$this->addMediaConversion('thumb')
->fit(Manipulations::FIT_CROP, 200, 200)
->sharpen(10);
$this->addMediaConversion('icon')
->fit(Manipulations::FIT_CROP, 100, 100)
->sharpen(10);
}
public function customFieldsValues()
{
return $this->morphMany('App\Models\CustomFieldValue', 'customizable');
}
/**
* Add Media to api results
* @return bool
*/
public function getHasMediaAttribute()
{
return $this->hasMedia('image') ? true : false;
}
/**
* to generate media url in case of fallback will
* return the file type icon
* @param string $conversion
* @return string url
*/
public function getFirstMediaUrl($collectionName = 'default',$conversion = '')
{
$url = $this->getFirstMediaUrlTrait($collectionName);
$array = explode('.', $url);
$extension = strtolower(end($array));
if (in_array($extension,config('medialibrary.extensions_has_thumb'))) {
return asset($this->getFirstMediaUrlTrait($collectionName,$conversion));
}else{
return asset(config('medialibrary.icons_folder').'/'.$extension.'.png');
}
}
public function getCustomFieldsAttribute()
{
$hasCustomField = in_array(static::class,setting('custom_field_models',[]));
if (!$hasCustomField){
return [];
}
$array = $this->customFieldsValues()
->join('custom_fields','custom_fields.id','=','custom_field_values.custom_field_id')
->where('custom_fields.in_table','=',true)
->get()->toArray();
return convertToAssoc($array,'name');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
**/
public function food()
{
return $this->belongsTo(\App\Models\Food::class, 'food_id', 'id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
**/
public function restaurant()
{
return $this->belongsTo(\App\Models\Restaurant::class, 'restaurant_id', 'id');
}
}