1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Models\Enums\SpecialArticle; |
6
|
|
|
use App\Models\Presenters\ArticlePresenter; |
7
|
|
|
use Exception; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Spatie\Blender\Model\Model; |
12
|
|
|
use Spatie\Blender\Model\Traits\HasSlug; |
13
|
|
|
use Spatie\EloquentSortable\Sortable; |
14
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property \App\Models\Article $parent |
18
|
|
|
* @property \Illuminate\Support\Collection $children |
19
|
|
|
* @property \App\Models\Article $firstChild |
20
|
|
|
* @property \Illuminate\Support\Collection $siblings |
21
|
|
|
*/ |
22
|
|
|
class Article extends Model implements Sortable |
23
|
|
|
{ |
24
|
|
|
use ArticlePresenter, HasSlug, SortableTrait; |
25
|
|
|
|
26
|
|
|
protected $with = ['media']; |
27
|
|
|
|
28
|
|
|
public $mediaLibraryCollections = ['images', 'downloads']; |
29
|
|
|
public $translatable = ['name', 'text', 'slug', 'seo_values']; |
30
|
|
|
|
31
|
|
|
public function registerMediaConversions() |
32
|
|
|
{ |
33
|
|
|
parent::registerMediaConversions(); |
34
|
|
|
|
35
|
|
|
$this->addMediaConversion('thumb') |
36
|
|
|
->setWidth(368) |
37
|
|
|
->setHeight(232) |
38
|
|
|
->performOnCollections('images'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isSpecialArticle(): bool |
42
|
|
|
{ |
43
|
|
|
return ! empty($this->technical_name); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function children(): HasMany |
47
|
|
|
{ |
48
|
|
|
return $this->hasMany(self::class, 'parent_id')->orderBy('order_column'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function hasChildren(): bool |
52
|
|
|
{ |
53
|
|
|
return count($this->children); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getFirstChildAttribute(): Article |
57
|
|
|
{ |
58
|
|
|
if (! $this->hasChildren()) { |
59
|
|
|
throw new Exception("Article `{$this->id}` doesn't have any children."); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->children->sortBy('order_column')->first(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getSiblingsAttribute(): Collection |
66
|
|
|
{ |
67
|
|
|
return self::where('parent_id', $this->parent_id) |
68
|
|
|
->orderBy('order_column') |
69
|
|
|
->get(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function parent(): BelongsTo |
73
|
|
|
{ |
74
|
|
|
return $this->belongsTo(self::class, 'parent_id'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function hasParent(): bool |
78
|
|
|
{ |
79
|
|
|
return ! is_null($this->parent); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getUrlAttribute(): string |
83
|
|
|
{ |
84
|
|
|
$localeSegment = ''; |
85
|
|
|
|
86
|
|
|
if (locales()->count() > 1) { |
87
|
|
|
$localeSegment = '/'.locale(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this->technical_name === SpecialArticle::HOME) { |
91
|
|
|
return $localeSegment; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$parentUrl = $this->hasParent() ? $this->parent->url.'/' : ''; |
95
|
|
|
|
96
|
|
|
return "{$localeSegment}/{$parentUrl}{$this->url}"; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|