1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
class UrlRecord extends Model |
8
|
|
|
{ |
9
|
|
|
public $table = 'chief_urls'; |
10
|
|
|
|
11
|
|
|
public $guarded = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Find matching url record for passed slug and locale. The locale parameter will try |
15
|
|
|
* to match specific given locales first and records without locale as fallback. |
16
|
|
|
* |
17
|
|
|
* @param string $slug |
18
|
|
|
* @param string $locale |
19
|
|
|
* @return UrlRecord |
20
|
|
|
* @throws UrlRecordNotFound |
21
|
|
|
*/ |
22
|
30 |
|
public static function findBySlug(string $slug, string $locale): UrlRecord |
23
|
|
|
{ |
24
|
|
|
// Clear the input from any trailing slashes. |
25
|
30 |
|
if ($slug != '/') { |
26
|
28 |
|
$slug = trim($slug, '/'); |
27
|
|
|
} |
28
|
|
|
|
29
|
30 |
|
$record = static::where('slug', $slug) |
30
|
30 |
|
->where('locale', $locale) |
31
|
30 |
|
->orderBy('redirect_id', 'ASC') |
32
|
30 |
|
->first(); |
33
|
|
|
|
34
|
30 |
|
if (!$record) { |
35
|
4 |
|
throw new UrlRecordNotFound('No url record found by slug ['.$slug.'] for locale ['.$locale.'].'); |
36
|
|
|
} |
37
|
|
|
|
38
|
26 |
|
return $record; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Find matching url record for passed slug and locale. The locale parameter will try |
43
|
|
|
* to match specific given locales first and records without locale as fallback. |
44
|
|
|
* |
45
|
|
|
* @param Model $model |
46
|
|
|
* @param string $locale |
47
|
|
|
* @return UrlRecord |
48
|
|
|
* @throws UrlRecordNotFound |
49
|
|
|
*/ |
50
|
52 |
|
public static function findByModel(Model $model, string $locale): UrlRecord |
51
|
|
|
{ |
52
|
52 |
|
$record = static::where('model_type', $model->getMorphClass()) |
53
|
52 |
|
->where('model_id', $model->id) |
54
|
52 |
|
->where('locale', $locale) |
55
|
52 |
|
->orderBy('redirect_id', 'ASC') |
56
|
52 |
|
->first(); |
57
|
|
|
|
58
|
52 |
|
if (!$record) { |
59
|
18 |
|
throw new UrlRecordNotFound('No url record found for model ['.$model->getMorphClass().'@'.$model->id.'] for locale ['.$locale.'].'); |
60
|
|
|
} |
61
|
|
|
|
62
|
36 |
|
return $record; |
63
|
|
|
} |
64
|
|
|
|
65
|
71 |
|
public static function getByModel(Model $model) |
66
|
|
|
{ |
67
|
71 |
|
return static::where('model_type', $model->getMorphClass()) |
68
|
71 |
|
->where('model_id', $model->id) |
69
|
71 |
|
->get(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public static function findRecentRedirect(Model $model, string $locale): ?self |
73
|
|
|
{ |
74
|
1 |
|
return static::where('model_type', $model->getMorphClass()) |
75
|
1 |
|
->where('model_id', $model->id) |
76
|
1 |
|
->where('locale', $locale) |
77
|
1 |
|
->where('redirect_id','<>', null) |
78
|
1 |
|
->orderBy('redirect_id', 'ASC') |
79
|
1 |
|
->first(); |
80
|
|
|
} |
81
|
|
|
|
82
|
17 |
|
public function replaceAndRedirect(array $values): UrlRecord |
83
|
|
|
{ |
84
|
17 |
|
$newRecord = static::create(array_merge([ |
85
|
17 |
|
'locale' => $this->locale, |
86
|
17 |
|
'model_type' => $this->model_type, |
87
|
17 |
|
'model_id' => $this->model_id, |
88
|
17 |
|
], $values)); |
89
|
|
|
|
90
|
17 |
|
$this->redirectTo($newRecord); |
91
|
|
|
|
92
|
17 |
|
return $newRecord; |
93
|
|
|
} |
94
|
|
|
|
95
|
18 |
|
public function redirectTo(self $record = null): ?UrlRecord |
96
|
|
|
{ |
97
|
18 |
|
if (!$record) { |
98
|
3 |
|
return $this->isRedirect() ? static::find($this->redirect_id) : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
18 |
|
$this->redirect_id = $record->id; |
102
|
18 |
|
$this->save(); |
103
|
|
|
|
104
|
18 |
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
72 |
|
public function isRedirect(): bool |
108
|
|
|
{ |
109
|
72 |
|
return !!($this->redirect_id); |
110
|
|
|
} |
111
|
|
|
|
112
|
62 |
|
public function isHomepage(): bool |
113
|
|
|
{ |
114
|
62 |
|
return $this->slug === '/'; |
115
|
|
|
} |
116
|
|
|
|
117
|
62 |
|
public static function existsIgnoringRedirects($slug, string $locale = null, Model $ignoredModel = null): bool |
118
|
|
|
{ |
119
|
62 |
|
return static::exists($slug, $locale, $ignoredModel, false); |
120
|
|
|
} |
121
|
|
|
|
122
|
62 |
|
public static function exists($slug, string $locale = null, Model $ignoredModel = null, bool $includeRedirects = true): bool |
123
|
|
|
{ |
124
|
62 |
|
$builder = static::where('slug', $slug); |
125
|
|
|
|
126
|
62 |
|
if ($locale) { |
127
|
62 |
|
$builder->where('locale', $locale); |
128
|
|
|
} |
129
|
|
|
|
130
|
62 |
|
if (! $includeRedirects) { |
131
|
62 |
|
$builder->whereNull('redirect_id'); |
132
|
|
|
} |
133
|
|
|
|
134
|
62 |
|
if ($ignoredModel) { |
135
|
|
|
$builder->whereNotIn('id', function ($query) use ($ignoredModel) { |
136
|
44 |
|
$query->select('id') |
137
|
44 |
|
->from('chief_urls') |
138
|
44 |
|
->where('model_type', '=', $ignoredModel->getMorphClass()) |
139
|
44 |
|
->where('model_id', '=', $ignoredModel->id); |
140
|
44 |
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
62 |
|
return ($builder->count() > 0); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|