-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandleSlugs.php
119 lines (98 loc) · 4.27 KB
/
HandleSlugs.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
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Models\Contracts\TwillModelContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
trait HandleSlugs
{
public function beforeSaveHandleSlugs(TwillModelContract $object, array $fields): void
{
if (property_exists($this->model, 'slugAttributes')) {
$object->twillSlugData = [];
foreach (getLocales() as $locale) {
if (isset($fields['slug'][$locale]) && !empty($fields['slug'][$locale])) {
$currentSlug = [];
$currentSlug['slug'] = $fields['slug'][$locale];
$currentSlug['locale'] = $locale;
$currentSlug['active'] = !$this->model->isTranslatable() || $object->translate($locale)?->active ?? false;
$currentSlug = $this->getSlugParameters($object, $fields, $currentSlug);
$object->twillSlugData[] = $currentSlug;
} else {
$slugParams = $this->model->slugAttributes;
$slugData = [];
foreach ($slugParams as $param) {
$slugData[] = $fields[$param][$locale] ?? '';
}
if (!empty(Arr::join($slugData, '-'))) {
$object->twillSlugData[] = [
'slug' => Str::slug(Arr::join($slugData, '-')),
'active' => $this->model->isTranslatable() ? $object->translate($locale)->active : 1,
'locale' => $locale
];
}
}
}
}
}
public function afterDeleteHandleSlugs(TwillModelContract $object): void
{
$object->slugs()->delete();
}
public function afterRestoreHandleSlugs(TwillModelContract $object): void
{
$object->slugs()->restore();
}
public function getFormFieldsHandleSlugs(TwillModelContract $model, array $fields): array
{
unset($fields['slugs']);
if ($model->slugs !== null) {
foreach ($model->slugs as $slug) {
if ($slug->active || $model->slugs->where('locale', $slug->locale)->where('active', true)->count() === 0) {
$fields['translations']['slug'][$slug->locale] = $slug->slug;
}
}
}
return $fields;
}
public function getSlugParameters(TwillModelContract $object, array $fields, array $slug): array
{
$slugParams = $object->getSlugParams($slug['locale']);
foreach ($object->slugAttributes as $param) {
if (isset($slugParams[$param]) && isset($fields[$param])) {
$slug[$param] = $fields[$param];
} elseif (isset($slugParams[$param])) {
$slug[$param] = $slugParams[$param];
}
}
return $slug;
}
public function forSlug(string $slug, array $with = [], array $withCount = [], array $scopes = []): ?TwillModelContract
{
$query = $this->model->where($scopes)->scopes(['published', 'visible']);
foreach (class_uses_recursive(get_called_class()) as $trait) {
if (method_exists(get_called_class(), $method = 'getPublishedScopes' . class_basename($trait))) {
$query->scopes($this->$method());
}
}
$item = (clone $query)->forSlug($slug)->with($with)->withCount($withCount)->first();
if (!$item && $item = (clone $query)->forInactiveSlug($slug)->first()) {
$item->redirect = true;
}
if (
!$item && config('translatable.use_property_fallback', false)
&& config('translatable.fallback_locale') != config('app.locale')
) {
$item = (clone $query)->orWhere(function ($query) {
return $query->withActiveTranslations(config('translatable.fallback_locale'));
})->forFallbackLocaleSlug($slug)->first();
if ($item) {
$item->redirect = true;
}
}
return $item;
}
public function forSlugPreview(string $slug, array $with = [], array $withCount = []): ?TwillModelContract
{
return $this->model->forInactiveSlug($slug)->with($with)->withCount($withCount)->first();
}
}