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