|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
|
6
|
|
|
|
|
7
|
|
|
class UrlSlugField extends InputField |
|
8
|
|
|
{ |
|
9
|
|
|
private $urlRecord; |
|
10
|
|
|
|
|
11
|
|
|
private $baseUrlSegment; |
|
12
|
|
|
|
|
13
|
|
|
private $fullUrl; |
|
14
|
|
|
|
|
15
|
21 |
|
public function setUrlRecord(UrlRecord $urlRecord) |
|
16
|
|
|
{ |
|
17
|
21 |
|
$this->urlRecord = $urlRecord; |
|
18
|
|
|
|
|
19
|
21 |
|
return $this; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getUrlRecordId(): int |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->urlRecord->id; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
74 |
|
public function setBaseUrlSegment($baseUrlSegment = null) |
|
28
|
|
|
{ |
|
29
|
74 |
|
$this->baseUrlSegment = $baseUrlSegment; |
|
30
|
|
|
|
|
31
|
74 |
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
public function value() |
|
35
|
|
|
{ |
|
36
|
4 |
|
return old($this->key, $this->rawSlugValue()); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function fullUrl(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->fullUrl |
|
42
|
|
|
? $this->fullUrl |
|
43
|
|
|
: $this->prepend.$this->value(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setFullUrl(string $fullUrl) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->fullUrl = $fullUrl; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
private function rawSlugValue(): string |
|
54
|
|
|
{ |
|
55
|
4 |
|
if (!$this->urlRecord) { |
|
56
|
|
|
return ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
4 |
|
$slug = $this->urlRecord->slug; |
|
60
|
|
|
|
|
61
|
|
|
// If this is a '/' slug, it indicates the homepage for this locale. In this case, |
|
62
|
|
|
// we wont be trimming the slash |
|
63
|
4 |
|
if ($slug === '/') { |
|
64
|
|
|
return $slug; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
if ($this->startsWithBaseUrlSegment($slug)) { |
|
68
|
2 |
|
$slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
return $slug; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $value |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
4 |
|
private function startsWithBaseUrlSegment($value): bool |
|
79
|
|
|
{ |
|
80
|
4 |
|
return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function toArray(): array |
|
84
|
|
|
{ |
|
85
|
1 |
|
return array_merge($this->values, [ |
|
86
|
1 |
|
'key' => $this->key, |
|
|
|
|
|
|
87
|
1 |
|
'prepend' => $this->prepend, |
|
|
|
|
|
|
88
|
1 |
|
'label' => $this->label, |
|
|
|
|
|
|
89
|
1 |
|
'placeholder' => $this->placeholder, |
|
|
|
|
|
|
90
|
1 |
|
'description' => $this->description, |
|
|
|
|
|
|
91
|
1 |
|
'value' => $this->value(), |
|
92
|
1 |
|
'baseUrlSegment' => $this->baseUrlSegment, |
|
93
|
|
|
'hint' => null, // Hint placeholder to show url hint when it already exists |
|
94
|
1 |
|
'is_homepage' => ($this->value() === '/'), |
|
95
|
1 |
|
'show' => !!$this->value(),// show input field or not |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|