-
Notifications
You must be signed in to change notification settings - Fork 13
/
Recipe.php
372 lines (339 loc) · 10.2 KB
/
Recipe.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace App\Models\Recipes;
use App\Models\FilterScope;
use App\Models\SlugifyTrait;
use Laravel\Scout\Searchable;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\MediaLibrary\InteractsWithMedia;
use Illuminate\Database\Eloquent\SoftDeletes;
use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* App\Models\Recipes\Recipe
*
* @property int $id
* @property int $user_id
* @property int|null $cookbook_id
* @property int $category_id
* @property int $author_id
* @property string $name
* @property string $slug
* @property string|null $yield_amount
* @property string $complexity
* @property string $instructions
* @property string|null $preparation_time
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \App\Models\Users\Author $author
* @property-read \App\Models\Recipes\Category $category
* @property-read \App\Models\Recipes\Cookbook|null $cookbook
* @property-read bool $can_edit
* @property-read string $complexity_text
* @property-read Collection $photos
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ingredients\IngredientGroup[] $ingredientGroups
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ingredients\Ingredient[] $ingredients
* @property-read \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection|Media[] $media
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ratings\Rating[] $ratings
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Recipes\Tag[] $tags
* @method static Builder|Recipe filter(?array $filter, ?string $method = 'and')
* @method static Builder|Recipe isOwn()
* @method static Builder|Recipe isPublic()
* @method static Builder|Recipe newModelQuery()
* @method static Builder|Recipe newQuery()
* @method static \Illuminate\Database\Query\Builder|Recipe onlyTrashed()
* @method static Builder|Recipe query()
* @method static Builder|Recipe whereAuthorId($value)
* @method static Builder|Recipe whereCategoryId($value)
* @method static Builder|Recipe whereComplexity($value)
* @method static Builder|Recipe whereCookbookId($value)
* @method static Builder|Recipe whereCreatedAt($value)
* @method static Builder|Recipe whereDeletedAt($value)
* @method static Builder|Recipe whereId($value)
* @method static Builder|Recipe whereInstructions($value)
* @method static Builder|Recipe whereName($value)
* @method static Builder|Recipe wherePreparationTime($value)
* @method static Builder|Recipe whereSlug($value)
* @method static Builder|Recipe whereUpdatedAt($value)
* @method static Builder|Recipe whereUserId($value)
* @method static Builder|Recipe whereYieldAmount($value)
* @method static \Illuminate\Database\Query\Builder|Recipe withTrashed()
* @method static \Illuminate\Database\Query\Builder|Recipe withoutTrashed()
* @mixin \Eloquent
*/
class Recipe extends Model implements HasMedia
{
use FilterScope,
SoftDeletes,
SoftCascadeTrait,
SlugifyTrait,
HasFactory,
InteractsWithMedia,
Searchable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'cookbook_id',
'category_id',
'name',
'yield_amount',
'complexity',
'instructions',
'preparation_time',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'photos',
'can_edit',
];
/**
* The relations that should cascade on delete
*
* @var array
*/
protected $softCascade = [
'ingredients',
'ingredientGroups',
'ratings',
];
/**
* Possible values for the complexity_types enum field
*
* @var array
*/
public const COMPLEXITY_TYPES = [
'simple',
'normal',
'difficult',
];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('isAdminOrOwnOrPublic', function (Builder $query) {
if (auth()->check() && auth()->user()->admin) {
return $query;
}
return $query->where(function (Builder $q) {
/** @var Recipe $q */
return $q->isOwn();
})->orWhere(function (Builder $q) {
/** @var Recipe $q */
return $q->isPublic();
});
});
}
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
return [
'id' => $this->id,
'name' => $this->name,
'instructions' => $this->instructions,
'preparation_time' => $this->preparation_time,
];
}
/**
* Modify the query used to retrieve models when making all of the models searchable.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with([
'author',
'category',
'cookbook',
'tags',
'ingredients',
'ingredientGroups',
'ratings',
]);
}
/**
* Register new media conversions
*
* This method is used by the spatie/laravel-medialibrary package
*
* @param \Spatie\MediaLibrary\MediaCollections\Models\Media|null $media
*/
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumbnail')
->width(200)
->height(200)
->sharpen(10);
$this->addMediaConversion('webp')
->format(Manipulations::FORMAT_WEBP);
}
/**
* Get only the recipes of the logged in user
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsOwn(Builder $builder): Builder
{
return $builder->whereUserId(auth()->id());
}
/**
* Get only the "public" recipes
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsPublic(Builder $builder): Builder
{
return $builder->whereNull('cookbook_id');
}
/**
* Get translated text of complexities
*
* @return string
*/
public function getComplexityTextAttribute(): string
{
$text = '';
switch ($this->attributes['complexity']) {
case 'simple':
$text = __('Simple');
break;
case 'normal':
$text = __('Normal');
break;
case 'difficult':
$text = __('Difficult');
break;
}
/** @var string */
return $text;
}
/**
* Get the preparation time
*
* Return NULL, if the time is 00:00:00
*
* @return string|null
*/
public function getPreparationTimeAttribute(): ?string
{
if ($this->attributes['preparation_time'] == '00:00:00') {
return null;
}
return $this->attributes['preparation_time'];
}
/**
* Get the related photos
*
* @return \Illuminate\Support\Collection
*/
public function getPhotosAttribute(): Collection
{
return $this->getMedia('recipe_photos')->map(function (Media $media) {
return collect([
'id' => $media->id,
'name' => $media->name,
'url' => $media->getUrl(),
]);
});
}
/**
* Evaluate if the user can edit this recipe
*
* @return bool
*/
public function getCanEditAttribute(): bool
{
if (!auth()->check()) {
return false;
}
return auth()->user()->can('update', $this);
}
/**
* Get the recipe's author
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function author(): BelongsTo
{
return $this->belongsTo('\App\Models\Users\Author');
}
/**
* Get the recipe's category
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category(): BelongsTo
{
return $this->belongsTo('\App\Models\Recipes\Category');
}
/**
* Get the recipe's cookbook
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function cookbook(): BelongsTo
{
return $this->belongsTo('\App\Models\Recipes\Cookbook');
}
/**
* Get the recipe's tags
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany('\App\Models\Recipes\Tag');
}
/**
* Get the recipe's ingredients
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ingredients(): HasMany
{
return $this->hasMany('\App\Models\Ingredients\Ingredient');
}
/**
* Get the recipe's ingredientGroups
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ingredientGroups(): HasMany
{
return $this->hasMany('\App\Models\Ingredients\IngredientGroup');
}
/**
* Get the recipe's ratings
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ratings(): HasMany
{
return $this->hasMany('\App\Models\Ratings\Rating');
}
}