|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
|
6
|
|
|
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl; |
|
7
|
|
|
|
|
8
|
|
|
class UrlSlugFields extends Fields |
|
9
|
|
|
{ |
|
10
|
74 |
|
public static function fromModel(ProvidesUrl $model) |
|
11
|
|
|
{ |
|
12
|
74 |
|
$fields = self::initEmptyFields($model->availableLocales(), $model); |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
74 |
|
self::fillWithExistingValues($model, $fields); |
|
15
|
|
|
|
|
16
|
74 |
|
return $fields; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
public static function redirectsFromModel(ProvidesUrl $model) |
|
20
|
|
|
{ |
|
21
|
|
|
$records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) { |
|
22
|
1 |
|
return !$record->isRedirect(); |
|
23
|
1 |
|
})->sortByDesc('created_at'); |
|
24
|
|
|
|
|
25
|
1 |
|
$fields = new static([]); |
|
26
|
|
|
|
|
27
|
1 |
|
foreach ($records as $record) { |
|
28
|
|
|
$key = 'redirects-'.$record->locale.'-'.$record->slug; |
|
29
|
|
|
$fields[$key] = UrlSlugField::make($key) |
|
30
|
|
|
->setUrlRecord($record) |
|
31
|
|
|
->setFullUrl($model->resolveUrl($record->locale, $record->slug)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
return $fields; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function toJson(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return json_encode($this->toArray()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function toArray(): array |
|
43
|
|
|
{ |
|
44
|
1 |
|
$array = []; |
|
45
|
|
|
|
|
46
|
1 |
|
foreach ($this->all() as $field) { |
|
47
|
1 |
|
$array[] = $field->toArray(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return $array; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $locales |
|
55
|
|
|
* @param ProvidesUrl $model |
|
56
|
|
|
* @return UrlSlugFields |
|
57
|
|
|
*/ |
|
58
|
74 |
|
private static function initEmptyFields(array $locales, ProvidesUrl $model): self |
|
59
|
|
|
{ |
|
60
|
74 |
|
$fields = new static([]); |
|
61
|
|
|
|
|
62
|
74 |
|
foreach ($locales as $locale) { |
|
63
|
74 |
|
$fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale) |
|
64
|
74 |
|
->setBaseUrlSegment($model->baseUrlSegment($locale)) |
|
65
|
74 |
|
->prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/') |
|
|
|
|
|
|
66
|
74 |
|
->name('url-slugs[' . $locale . ']') |
|
67
|
74 |
|
->label($locale); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
74 |
|
return $fields; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ProvidesUrl $model |
|
75
|
|
|
* @param $fields |
|
76
|
|
|
*/ |
|
77
|
74 |
|
private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void |
|
78
|
|
|
{ |
|
79
|
|
|
$records = UrlRecord::getByModel($model)->reject(function ($record) { |
|
80
|
21 |
|
return $record->isRedirect(); |
|
81
|
74 |
|
})->sortBy('locale'); |
|
82
|
|
|
|
|
83
|
74 |
|
foreach ($records as $record) { |
|
84
|
21 |
|
if (!isset($fields['url-slugs.'.$record->locale])) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
21 |
|
$fields['url-slugs.'.$record->locale] |
|
89
|
21 |
|
->setUrlRecord($record) |
|
|
|
|
|
|
90
|
21 |
|
->setBaseUrlSegment($model->baseUrlSegment($record->locale)) |
|
91
|
21 |
|
->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/'); |
|
92
|
|
|
} |
|
93
|
74 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|