|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
|
6
|
|
|
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl; |
|
7
|
|
|
use Thinktomorrow\Chief\Management\Assistants\UrlAssistant; |
|
8
|
|
|
|
|
9
|
|
|
class UrlSlugFields extends Fields |
|
10
|
|
|
{ |
|
11
|
70 |
|
public static function fromModel(ProvidesUrl $model) |
|
12
|
|
|
{ |
|
13
|
70 |
|
$fields = self::initEmptyFields($model->availableLocales(), $model); |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
70 |
|
self::fillWithExistingValues($model, $fields); |
|
16
|
|
|
|
|
17
|
70 |
|
return $fields; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function toJson(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return json_encode($this->toArray()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public function toArray(): array |
|
26
|
|
|
{ |
|
27
|
2 |
|
$array = []; |
|
28
|
|
|
|
|
29
|
2 |
|
foreach($this->all() as $field) { |
|
30
|
2 |
|
$array[] = $field->toArray(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
return $array; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $locales |
|
38
|
|
|
* @param ProvidesUrl $model |
|
39
|
|
|
* @return UrlSlugFields |
|
40
|
|
|
*/ |
|
41
|
70 |
|
private static function initEmptyFields(array $locales, ProvidesUrl $model): self |
|
42
|
|
|
{ |
|
43
|
|
|
// Add wildcard field as default |
|
44
|
70 |
|
$fields = new static([ $wildCardField = UrlSlugField::make('url-slugs.' . UrlAssistant::WILDCARD) |
|
45
|
70 |
|
->name('url-slugs[' . UrlAssistant::WILDCARD . ']') |
|
|
|
|
|
|
46
|
70 |
|
->label('') |
|
|
|
|
|
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
70 |
|
if (count($locales) < 2) return $fields; |
|
50
|
|
|
|
|
51
|
|
|
// Add description to wildcard field only when there are locale values. |
|
52
|
70 |
|
$wildCardField->label('Default link') |
|
53
|
70 |
|
->description('Standaard link die altijd van toepassing is indien er geen taalspecifieke link voorhanden is. Laat deze leeg indien je deze link in bepaalde talen niet wilt beschikbaar maken.'); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
70 |
|
foreach ($locales as $locale) { |
|
56
|
70 |
|
$fields['url-slugs.' . $locale] = UrlSlugField::make('url-slugs.' . $locale) |
|
57
|
70 |
|
->setBaseUrlSegment($model->baseUrlSegment($locale)) |
|
58
|
70 |
|
->prepend($model->resolveUrl($locale, $model->baseUrlSegment($locale)) .'/') |
|
|
|
|
|
|
59
|
70 |
|
->name('url-slugs[' . $locale . ']') |
|
60
|
70 |
|
->label($locale . ' link'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
70 |
|
return $fields; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ProvidesUrl $model |
|
68
|
|
|
* @param $fields |
|
69
|
|
|
*/ |
|
70
|
70 |
|
private static function fillWithExistingValues(ProvidesUrl $model, self $fields): void |
|
71
|
|
|
{ |
|
72
|
|
|
$records = MemoizedUrlRecord::getByModel($model)->reject(function ($record) { |
|
73
|
21 |
|
return $record->isRedirect(); |
|
74
|
70 |
|
})->sortBy('locale'); |
|
75
|
|
|
|
|
76
|
70 |
|
foreach ($records as $record) { |
|
77
|
|
|
|
|
78
|
21 |
|
if ($record->isManagedAsWildCard()) { |
|
79
|
4 |
|
$fields['url-slugs.' . UrlAssistant::WILDCARD] |
|
80
|
4 |
|
->setUrlRecord($record) |
|
|
|
|
|
|
81
|
4 |
|
->setBaseUrlSegment($model->baseUrlSegment($record->locale)); |
|
82
|
4 |
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
19 |
|
if(!isset($fields['url-slugs.'.$record->locale])) continue; |
|
86
|
|
|
|
|
87
|
19 |
|
$fields['url-slugs.'.$record->locale] |
|
88
|
19 |
|
->setUrlRecord($record) |
|
|
|
|
|
|
89
|
19 |
|
->setBaseUrlSegment($model->baseUrlSegment($record->locale)) |
|
90
|
19 |
|
->prepend($model->resolveUrl($record->locale, $model->baseUrlSegment($record->locale)) .'/'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |