1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larrock\Core\Traits; |
4
|
|
|
|
5
|
|
|
use Cache; |
6
|
|
|
use Spatie\MediaLibrary\Models\Media; |
7
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
8
|
|
|
|
9
|
|
|
trait GetFilesAndImages |
10
|
|
|
{ |
11
|
|
|
use HasMediaTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param Media|null $media |
15
|
|
|
* @throws \Spatie\Image\Exceptions\InvalidManipulation |
16
|
|
|
*/ |
17
|
|
|
public function registerMediaConversions(Media $media = null) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->addMediaConversion('110x110') |
20
|
|
|
->height(110)->width(110) |
21
|
|
|
->performOnCollections('images'); |
22
|
|
|
|
23
|
|
|
$this->addMediaConversion('140x140') |
24
|
|
|
->height(140)->width(140) |
25
|
|
|
->performOnCollections('images'); |
26
|
|
|
|
27
|
|
|
if ($this->config->customMediaConversions) { |
28
|
|
|
foreach ($this->config->customMediaConversions as $conversion) { |
29
|
|
|
$explode = explode('x', $conversion); |
30
|
|
|
$this->addMediaConversion($conversion) |
31
|
|
|
->height($explode[0])->width($explode[1]) |
32
|
|
|
->performOnCollections('images'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getFiles() |
38
|
|
|
{ |
39
|
|
|
return $this->hasMany('Spatie\MediaLibrary\Models\Media', 'model_id', 'id') |
|
|
|
|
40
|
|
|
->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'files']])->orderBy('order_column', 'DESC'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getImages() |
44
|
|
|
{ |
45
|
|
|
return $this->hasMany('Spatie\MediaLibrary\Models\Media', 'model_id', 'id') |
46
|
|
|
->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'images']])->orderBy('order_column', 'DESC'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getFirstImage() |
50
|
|
|
{ |
51
|
|
|
return $this->hasOne('Spatie\MediaLibrary\Models\Media', 'model_id', 'id') |
|
|
|
|
52
|
|
|
->where([['model_type', '=', $this->config->model], ['collection_name', '=', 'images']])->orderBy('order_column', 'DESC'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getFirstImageAttribute() |
56
|
|
|
{ |
57
|
|
|
$value = Cache::rememberForever(sha1('image_f_category'.$this->id.'_'.$this->config->model), function () { |
58
|
|
|
if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) { |
59
|
|
|
return $get_image->getUrl(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) { |
63
|
|
|
return $this->getCategoryActive->first()->first_image; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return '/_assets/_front/_images/empty_big.png'; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getFirstImage110Attribute() |
73
|
|
|
{ |
74
|
|
|
$value = Cache::rememberForever(sha1('image_f110_category'.$this->id.'_'.$this->config->model), function () { |
75
|
|
|
if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) { |
76
|
|
|
return $get_image->getUrl('110x110'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) { |
80
|
|
|
return $this->getCategoryActive->first()->first_image_110; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return '/_assets/_front/_images/empty_big.png'; |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
return $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getFirstImage140Attribute() |
90
|
|
|
{ |
91
|
|
|
$value = Cache::rememberForever(sha1('image_f140_category'.$this->id.'_'.$this->config->model), function () { |
92
|
|
|
if ($get_image = $this->getMedia('images')->sortByDesc('order_column')->first()) { |
93
|
|
|
return $get_image->getUrl('140x140'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (method_exists($this, 'getCategoryActive') && $this->getCategoryActive) { |
97
|
|
|
return $this->getCategoryActive->first()->first_image_140; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return '/_assets/_front/_images/empty_big.png'; |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
return $value; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.