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