|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Spatie\MediaLibrary\HasMedia; |
|
8
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
|
9
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media; |
|
10
|
|
|
use Spatie\ModelStatus\HasStatuses; |
|
11
|
|
|
use Spatie\Searchable\Searchable; |
|
12
|
|
|
use Spatie\Searchable\SearchResult; |
|
13
|
|
|
use Spatie\Sluggable\HasSlug; |
|
14
|
|
|
use Spatie\Sluggable\SlugOptions; |
|
15
|
|
|
use Spatie\Translatable\HasTranslations; |
|
16
|
|
|
|
|
17
|
|
|
class Glossary extends Model implements HasMedia, Searchable |
|
18
|
|
|
{ |
|
19
|
|
|
use HasFactory; |
|
20
|
|
|
use HasSlug; |
|
|
|
|
|
|
21
|
|
|
use HasTranslations; |
|
|
|
|
|
|
22
|
|
|
use InteractsWithMedia; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The attributes that aren't mass assignable. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $guarded = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The attributes that are translatable. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
public $translatable = ['term','definition', 'body']; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The parameters used in the index view search filters. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
public const SEARCH_PARAMETERS = [ |
|
44
|
|
|
'term', |
|
45
|
|
|
'is_published' |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The possible values the publishing status can be. |
|
50
|
|
|
*/ |
|
51
|
|
|
public const PUBLISHING_STATUS = [ |
|
52
|
|
|
0 => 'unpublished', |
|
53
|
|
|
1 => 'published', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Generates a unique slug. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSlugOptions(): SlugOptions |
|
60
|
10 |
|
{ |
|
61
|
|
|
return SlugOptions::create() |
|
62
|
10 |
|
->generateSlugsFrom('term') |
|
63
|
10 |
|
->saveSlugsTo('slug'); |
|
64
|
10 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the variants of the glossary term. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function variants() |
|
70
|
11 |
|
{ |
|
71
|
|
|
return $this->hasMany(GlossaryVariant::class, 'glossary_id'); |
|
72
|
11 |
|
} |
|
73
|
|
|
|
|
74
|
11 |
|
/** |
|
75
|
10 |
|
* Add Image gallery support using: |
|
76
|
11 |
|
* https://spatie.be/docs/laravel-medialibrary/v8/introduction |
|
77
|
11 |
|
* https://github.com/ebess/advanced-nova-media-library |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Spatie\MediaLibrary\MediaCollections\Models\Media|null $media |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Spatie\Image\Exceptions\InvalidManipulation |
|
82
|
|
|
*/ |
|
83
|
|
|
public function registerMediaConversions(Media $media = null): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->addMediaConversion('thumb') |
|
86
|
|
|
->width(300) |
|
87
|
|
|
->height(300); |
|
88
|
|
|
|
|
89
|
|
|
$this->addMediaConversion('facebook') |
|
90
|
|
|
->width(1200) |
|
91
|
|
|
->height(630); |
|
92
|
|
|
|
|
93
|
|
|
$this->addMediaConversion('twitter') |
|
94
|
|
|
->width(1024) |
|
95
|
|
|
->height(512); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function registerMediaCollections(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->addMediaCollection('introimage')->singleFile(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return true if the glossary term is published |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function isPublished(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->is_published; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return the glossary term publishing status |
|
115
|
1 |
|
* |
|
116
|
|
|
* @return string |
|
117
|
1 |
|
*/ |
|
118
|
|
|
public function publishingStatus(): string |
|
119
|
|
|
{ |
|
120
|
|
|
return self::PUBLISHING_STATUS[$this->is_published]; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Method required by Spatie Laravel Searchable |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getSearchResult(): SearchResult |
|
127
|
|
|
{ |
|
128
|
|
|
$url = route('glossaries.edit', $this->id); |
|
129
|
|
|
|
|
130
|
|
|
return new SearchResult( |
|
131
|
|
|
$this, |
|
|
|
|
|
|
132
|
|
|
$this->term, |
|
133
|
|
|
$url |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|