1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BurnBright\ExternalURLField; |
4
|
|
|
|
5
|
|
|
use BurnBright\ExternalURLField\ExternalURLField; |
6
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
7
|
|
|
|
8
|
|
|
class ExternalURL extends DBVarchar |
9
|
|
|
{ |
10
|
|
|
private static $casting = array( |
|
|
|
|
11
|
|
|
"Domain" => "ExternalURL", |
12
|
|
|
"URL" => "ExternalURL" |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* 2083 is the lowest common denominator when it comes to url lengths. |
17
|
|
|
*/ |
18
|
4 |
|
public function __construct($name = null, $size = 2083, $options = array()) |
19
|
|
|
{ |
20
|
4 |
|
parent::__construct($name, $size, $options); |
21
|
4 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Remove ugly parts of a url to make it nice |
25
|
|
|
*/ |
26
|
1 |
|
public function Nice() |
27
|
|
|
{ |
28
|
1 |
|
if ($this->value && $parts = parse_url($this->URL())) { |
29
|
1 |
|
$remove = array('scheme', 'user', 'pass', 'port', 'query', 'fragment'); |
30
|
1 |
|
foreach ($remove as $part) { |
31
|
1 |
|
unset($parts[$part]); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
return rtrim(http_build_url($parts), "/"); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get just the domain of the url. |
40
|
|
|
*/ |
41
|
1 |
|
public function Domain() |
42
|
|
|
{ |
43
|
1 |
|
if ($this->value) { |
44
|
1 |
|
return parse_url($this->URL(), PHP_URL_HOST); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Remove the www subdomain, if present. |
50
|
|
|
*/ |
51
|
|
|
public function NoWWW() |
52
|
|
|
{ |
53
|
|
|
return ltrim($this->value, "www."); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Scaffold the ExternalURLField for this ExternalURL |
58
|
|
|
*/ |
59
|
1 |
|
public function scaffoldFormField($title = null, $params = null) |
60
|
|
|
{ |
61
|
1 |
|
$field = new ExternalURLField($this->name, $title); |
62
|
1 |
|
$field->setMaxLength($this->getSize()); |
63
|
|
|
|
64
|
1 |
|
return $field; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function forTemplate() |
68
|
|
|
{ |
69
|
1 |
|
if ($this->value) { |
70
|
1 |
|
return $this->URL(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|