|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\ExternalURLField; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
|
6
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
class ExternalURL extends DBVarchar |
|
10
|
|
|
{ |
|
11
|
|
|
private static $casting = [ |
|
12
|
|
|
'Domain' => ExternalURL::class, |
|
13
|
|
|
'URL' => ExternalURL::class, |
|
14
|
|
|
'Path' => 'Varchar', |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* 2083 is the lowest common denominator when it comes to url lengths. |
|
19
|
|
|
* however, 768 allows searching... |
|
20
|
|
|
* |
|
21
|
|
|
* @param null|mixed $name |
|
22
|
|
|
* @param mixed $size |
|
23
|
|
|
* @param mixed $options |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($name = null, $size = 2083, $options = []) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($name, $size, $options); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Remove ugly parts of a url to make it nice. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function Nice(): string |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->value) { |
|
36
|
|
|
$parts = parse_url($this->URL()); |
|
37
|
|
|
if ($parts) { |
|
|
|
|
|
|
38
|
|
|
$remove = ['scheme', 'user', 'pass', 'port', 'query', 'fragment']; |
|
39
|
|
|
foreach ($remove as $part) { |
|
40
|
|
|
unset($parts[$part]); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return rtrim(http_build_url($parts), '/'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get just the domain of the url. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function Domain() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->value) { |
|
56
|
|
|
return parse_url($this->URL(), PHP_URL_HOST); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return ''; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Remove the www subdomain, if present. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function NoWWW() |
|
66
|
|
|
{ |
|
67
|
|
|
//https://stackoverflow.com/questions/23349257/trim-a-full-string-instead-of-using-trim-with-characters |
|
68
|
|
|
return $url = preg_replace('/^(www\.)*/', '', (string) $this->value); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function Path() |
|
72
|
|
|
{ |
|
73
|
|
|
if ($this->value) { |
|
74
|
|
|
return trim((string) parse_url((string) $this->URL(), PHP_URL_PATH), '/'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Scaffold the ExternalURLField for this ExternalURL. |
|
82
|
|
|
* |
|
83
|
|
|
* @param null|mixed $title |
|
84
|
|
|
* @param null|mixed $params |
|
85
|
|
|
*/ |
|
86
|
|
|
public function scaffoldFormField($title = null, $params = null) |
|
87
|
|
|
{ |
|
88
|
|
|
$field = new ExternalURLField($this->name, $title); |
|
89
|
|
|
$field->setMaxLength($this->getSize()); |
|
90
|
|
|
|
|
91
|
|
|
return $field; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function forTemplate() |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->value) { |
|
97
|
|
|
return (string) $this->URL(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return ''; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function saveInto($dataObject) |
|
104
|
|
|
{ |
|
105
|
|
|
$url = (string) $this->value; |
|
106
|
|
|
if ($url) { |
|
107
|
|
|
$config = Config::inst()->get(ExternalURLField::class, 'default_config'); |
|
108
|
|
|
$defaults = $config['defaultparts']; |
|
109
|
|
|
if (!preg_match('#^[a-zA-Z]+://#', $url)) { |
|
110
|
|
|
$url = $defaults['scheme'] . '://' . $url; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$parts = parse_url($url); |
|
114
|
|
|
if ($parts) { |
|
|
|
|
|
|
115
|
|
|
//can't parse url, abort |
|
116
|
|
|
|
|
117
|
|
|
foreach (array_keys($parts) as $part) { |
|
118
|
|
|
if (true === $config['removeparts'][$part]) { |
|
119
|
|
|
unset($parts[$part]); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// this causes errors! |
|
124
|
|
|
// $parts = array_filter($defaults, fn ($default) => ! isset($parts[$part])); |
|
125
|
|
|
|
|
126
|
|
|
$this->value = rtrim(http_build_url($defaults, $parts), '/'); |
|
127
|
|
|
} else { |
|
128
|
|
|
$this->value = ''; |
|
129
|
|
|
} |
|
130
|
|
|
} else { |
|
131
|
|
|
$this->value = ''; |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
parent::saveInto($dataObject); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.