1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
trait HandleSlugs |
6
|
|
|
{ |
7
|
21 |
|
/** |
8
|
|
|
* @param \A17\Twill\Models\Model $object |
9
|
21 |
|
* @param array $fields |
10
|
21 |
|
* @return void |
11
|
21 |
|
*/ |
12
|
21 |
|
public function afterSaveHandleSlugs($object, $fields) |
13
|
21 |
|
{ |
14
|
21 |
|
if (property_exists($this->model, 'slugAttributes')) { |
15
|
21 |
|
foreach (getLocales() as $locale) { |
16
|
21 |
|
if (isset($fields['slug']) && isset($fields['slug'][$locale]) && !empty($fields['slug'][$locale])) { |
17
|
21 |
|
$object->disableLocaleSlugs($locale); |
18
|
21 |
|
$currentSlug = []; |
19
|
|
|
$currentSlug['slug'] = $fields['slug'][$locale]; |
20
|
|
|
$currentSlug['locale'] = $locale; |
21
|
|
|
$currentSlug['active'] = $this->model->isTranslatable() ? $object->translate($locale)->active : 1; |
22
|
21 |
|
$currentSlug = $this->getSlugParameters($object, $fields, $currentSlug); |
23
|
|
|
$object->updateOrNewSlug($currentSlug); |
24
|
2 |
|
} |
25
|
|
|
} |
26
|
2 |
|
} |
27
|
2 |
|
} |
28
|
|
|
|
29
|
1 |
|
/** |
30
|
|
|
* @param \A17\Twill\Models\Model $object |
31
|
1 |
|
* @return void |
32
|
1 |
|
*/ |
33
|
|
|
public function afterDeleteHandleSlugs($object) |
34
|
5 |
|
{ |
35
|
|
|
$object->slugs()->delete(); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
5 |
|
/** |
39
|
5 |
|
* @param \A17\Twill\Models\Model $object |
40
|
5 |
|
* @return void |
41
|
5 |
|
*/ |
42
|
|
|
public function afterRestoreHandleSlugs($object) |
43
|
|
|
{ |
44
|
|
|
$object->slugs()->restore(); |
45
|
|
|
} |
46
|
5 |
|
|
47
|
|
|
/** |
48
|
|
|
* @param \A17\Twill\Models\Model $object |
49
|
21 |
|
* @param array $fields |
50
|
|
|
* @return array |
51
|
21 |
|
*/ |
52
|
|
|
public function getFormFieldsHandleSlugs($object, $fields) |
53
|
21 |
|
{ |
54
|
21 |
|
unset($fields['slugs']); |
55
|
|
|
|
56
|
21 |
|
if ($object->slugs != null) { |
|
|
|
|
57
|
|
|
foreach ($object->slugs as $slug) { |
58
|
|
|
if ($slug->active || $object->slugs->where('locale', $slug->locale)->where('active', true)->count() === 0) { |
59
|
|
|
$fields['translations']['slug'][$slug->locale] = $slug->slug; |
60
|
|
|
} |
61
|
21 |
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $fields; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \A17\Twill\Models\Model $object |
69
|
|
|
* @param array $fields |
70
|
|
|
* @param array $slug |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getSlugParameters($object, $fields, $slug) |
74
|
|
|
{ |
75
|
|
|
$slugParams = $object->getSlugParams($slug['locale']); |
76
|
|
|
|
77
|
|
|
foreach ($object->slugAttributes as $param) { |
|
|
|
|
78
|
|
|
if (isset($slugParams[$param]) && isset($fields[$param])) { |
79
|
|
|
$slug[$param] = $fields[$param]; |
80
|
|
|
} elseif (isset($slugParams[$param])) { |
81
|
|
|
$slug[$param] = $slugParams[$param]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $slug; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $slug |
90
|
|
|
* @param array $with |
91
|
|
|
* @param array $withCount |
92
|
|
|
* @param array $scopes |
93
|
|
|
* @return \A17\Twill\Models\Model|null |
94
|
|
|
*/ |
95
|
|
|
public function forSlug($slug, $with = [], $withCount = [], $scopes = []) |
96
|
|
|
{ |
97
|
|
|
$query = $this->model->where($scopes)->scopes(['published', 'visible']); |
98
|
|
|
|
99
|
|
|
foreach (class_uses_recursive(get_called_class()) as $trait) { |
100
|
|
|
if (method_exists(get_called_class(), $method = 'getPublishedScopes' . class_basename($trait))) { |
101
|
|
|
$query->scopes($this->$method()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$item = (clone $query)->forSlug($slug)->with($with)->withCount($withCount)->first(); |
106
|
|
|
|
107
|
|
|
if (!$item && $item = (clone $query)->forInactiveSlug($slug)->first()) { |
108
|
|
|
$item->redirect = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!$item && config('translatable.use_property_fallback', false) |
112
|
|
|
&& config('translatable.fallback_locale') != config('app.locale')) { |
113
|
|
|
$item = (clone $query)->orWhere(function ($query) { |
114
|
|
|
return $query->withActiveTranslations(config('translatable.fallback_locale')); |
115
|
|
|
})->forFallbackLocaleSlug($slug)->first(); |
116
|
|
|
|
117
|
|
|
if ($item) { |
118
|
|
|
$item->redirect = true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $item; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $slug |
127
|
|
|
* @param array $with |
128
|
|
|
* @param array $withCount |
129
|
|
|
* @return \A17\Twill\Models\Model |
130
|
|
|
*/ |
131
|
|
|
public function forSlugPreview($slug, $with = [], $withCount = []) |
132
|
|
|
{ |
133
|
|
|
return $this->model->forInactiveSlug($slug)->with($with)->withCount($withCount)->first(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.