1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Usage: |
5
|
|
|
* $myAnswer = |
6
|
|
|
* CountryPrice_Translation::get_country_url_provider() |
7
|
|
|
* ->getSomething(); |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class CountryURLProvider extends Object implements CountryURLProviderInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private static $locale_get_parameter = 'ecomlocale'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* returns the selected country code if there is one ... |
20
|
|
|
* as an uppercase code, e.g. NZ |
21
|
|
|
* @param string|null $url |
22
|
|
|
* |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public function hasCountrySegment($url = '') |
26
|
|
|
{ |
27
|
|
|
return $this->CurrentCountrySegment($url) ? true : false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* returns the selected country code if there is onCurrentCountrySegmente ... |
32
|
|
|
* as an uppercase code, e.g. NZ |
33
|
|
|
* @param string|null $url |
34
|
|
|
* @param bool $includeGetVariable |
35
|
|
|
* |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
|
|
public function CurrentCountrySegment($url = '', $includeGetVariable = true) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$potentialCountry = ''; |
41
|
|
|
if ($includeGetVariable) { |
42
|
|
|
$getVar = Config::inst()->get('CountryURLProvider', 'locale_get_parameter'); |
43
|
|
|
if(isset($_GET[$getVar])) { |
44
|
|
|
$potentialCountry = $_GET[$getVar]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
if (strlen($potentialCountry) !== 2) { |
48
|
|
|
$url = $this->getCurrentURL($url); |
49
|
|
|
$parts = parse_url($url); |
50
|
|
|
if (isset($parts['path'])) { |
51
|
|
|
$path = trim($parts['path'], '/'); |
52
|
|
|
$array = explode('/', $path); |
53
|
|
|
$potentialCountry = isset($array[0]) ? trim($array[0]) : ''; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
if (strlen($potentialCountry) === 2) { |
57
|
|
|
$potentialCountry = strtoupper($potentialCountry); |
58
|
|
|
$check = EcommerceCountry::get()->filter(['Code' => $potentialCountry])->count(); |
59
|
|
|
if ($check) { |
60
|
|
|
return $potentialCountry; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* replaces a country code in a URL with another one |
68
|
|
|
* |
69
|
|
|
* @param string $newCountryCode e.g. NZ / nz |
70
|
|
|
* @param string $url |
71
|
|
|
* |
72
|
|
|
* @return string|null only returns a string if it is different from the original! |
73
|
|
|
*/ |
74
|
|
|
public function replaceCountryCodeInUrl($newCountryCode, $url = '') |
75
|
|
|
{ |
76
|
|
|
$url = $this->getCurrentURL($url); |
77
|
|
|
$oldURL = $url; |
78
|
|
|
debug::log($url); |
79
|
|
|
|
80
|
|
|
$newCountryCode = strtolower($newCountryCode); |
81
|
|
|
$parsedUrl = parse_url($url); |
82
|
|
|
if (isset($parsedUrl['path']) && isset($parsedUrl['host'])) { |
83
|
|
|
$path = $parsedUrl['path']; |
84
|
|
|
$path = trim($path, '/'); |
85
|
|
|
$pathParts = explode('/', $path); |
86
|
|
|
|
87
|
|
|
$currentCountryCode = $this->CurrentCountrySegment($url, false); |
|
|
|
|
88
|
|
|
if ($currentCountryCode) { |
|
|
|
|
89
|
|
|
$pathParts[0] = $newCountryCode; |
90
|
|
|
} else { |
91
|
|
|
array_unshift($pathParts, $newCountryCode); |
92
|
|
|
} |
93
|
|
|
$parsedUrl['path'] = implode('/', $pathParts); |
94
|
|
|
$newURL = |
95
|
|
|
$parsedUrl['scheme'] . |
96
|
|
|
'://' . |
97
|
|
|
Controller::join_links( |
98
|
|
|
$parsedUrl['host'], |
99
|
|
|
$parsedUrl['path'] |
100
|
|
|
); |
101
|
|
|
if (isset($parsedUrl['query'])) { |
102
|
|
|
$newURL = $newURL . '?' . $parsedUrl['query']; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
if (trim($oldURL, '/') !== trim($newURL, '/')) { |
106
|
|
|
return $newURL; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* @param string|null $url can be a relative one or nothing at all ... |
115
|
|
|
* |
116
|
|
|
* @return string full URL currently being called. |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
public function getCurrentURL($url = '') |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if ($url) { |
|
|
|
|
121
|
|
|
$url = Director::absoluteURL($url); |
122
|
|
|
} else { |
123
|
|
|
$protocol = Director::is_https() ? 'https://' : 'http://'; |
124
|
|
|
|
125
|
|
|
$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
126
|
|
|
} |
127
|
|
|
if (Director::is_site_url($url)) { |
|
|
|
|
128
|
|
|
return $url; |
129
|
|
|
} else { |
130
|
|
|
return Director::absoluteURL('/'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.