1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* www.mysite.com/mypage/?ecomlocale=AU |
6
|
|
|
* if there is a tranlsation page redirects to |
7
|
|
|
* URL with ?ecomlocale=AU |
8
|
|
|
* |
9
|
|
|
* if you go to a URL with ?ecomlocale=AU and the shop country does not match |
10
|
|
|
* the get param then you get redirected to that shop country. |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class CountryPrice_Page_Controller_Extension extends Extension |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* replaces `Title` and `Content` with translated content |
20
|
|
|
* where available. |
21
|
|
|
* |
22
|
|
|
* If the country code in the get parameter is not correct then |
23
|
|
|
* @return [type] [description] |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function onAfterInit() |
26
|
|
|
{ |
27
|
|
|
$countryID = 0; |
28
|
|
|
//provided by stealth ... |
29
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
30
|
|
|
|
31
|
|
|
if ($countryObject) { |
32
|
|
|
|
33
|
|
|
//check if a redirect is required ... |
34
|
|
|
$this->checkForOffsiteRedirects($countryObject); |
35
|
|
|
|
36
|
|
|
$countryID = $countryObject->ID; |
37
|
|
|
//check that there is a translation |
38
|
|
|
if ($this->owner->dataRecord->thisPageHasTranslation($countryID)) { |
39
|
|
|
//if there is a translation but it is not showing in the URL then redirect |
40
|
|
|
$newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code); |
41
|
|
|
if ($newURL) { |
42
|
|
|
$this->owner->redirect($newURL); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->owner->dataRecord->loadTranslatedValues($countryID, null); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* returns the best fieldname for |
52
|
|
|
* @param string $fieldName [description] |
53
|
|
|
*/ |
54
|
|
|
public function CountryDistributorBestContentValue($fieldName) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
57
|
|
|
|
58
|
|
|
//check country |
59
|
|
|
if (!empty($countryObject->$fieldName)) { |
60
|
|
|
return $countryObject->$fieldName; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
//check distributor |
64
|
|
|
$distributor = Distributor::get_one_for_country($countryObject->Code); |
65
|
|
|
if (!empty($distributor->$fieldName)) { |
66
|
|
|
return $distributor->$fieldName; |
67
|
|
|
} |
68
|
|
|
//check EcomConfig |
69
|
|
|
$distributor = Distributor::get_one_for_country($countryObject->Code); |
70
|
|
|
if (!empty($distributor->$fieldName)) { |
71
|
|
|
return $distributor->$fieldName; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* caching variable |
77
|
|
|
* |
78
|
|
|
* @var integer |
79
|
|
|
*/ |
80
|
|
|
private static $_redirection_count = 0; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* returns a string for the new url if a locale parameter can be added |
84
|
|
|
* |
85
|
|
|
* @return string | null |
86
|
|
|
*/ |
87
|
|
|
private function addCountryCodeToUrlIfRequired($countryCode = '') |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
if (isset($_POST) && count($_POST)) { |
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
//to do: add query here! |
93
|
|
|
$protocol = Director::is_https() ? 'https://' : 'http://'; |
94
|
|
|
$oldURL = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
95
|
|
|
|
96
|
|
|
$hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
97
|
|
|
if ($hasCountrySegment) { |
98
|
|
|
$newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
99
|
|
|
} else { |
100
|
|
|
$newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL); |
101
|
|
|
} |
102
|
|
|
if ($oldURL !== $newURL && self::$_redirection_count < 3) { |
103
|
|
|
self::$_redirection_count++; |
104
|
|
|
return $newURL; |
105
|
|
|
} |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* |
112
|
|
|
* |
113
|
|
|
* @return ArrayList |
114
|
|
|
*/ |
115
|
|
|
public function ChooseNewCountryList() |
116
|
|
|
{ |
117
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
118
|
|
|
$currentCode = ''; |
119
|
|
|
if ($obj = CountryPrice_EcommerceCountry::get_real_country()) { |
120
|
|
|
$currentCode = $obj->Code; |
121
|
|
|
} |
122
|
|
|
$al = ArrayList::create(); |
123
|
|
|
foreach ($countries as $country) { |
124
|
|
|
$isCurrentOne = $currentCode == $country->Code ? true : false; |
125
|
|
|
$currency = null; |
|
|
|
|
126
|
|
|
$currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
127
|
|
|
$currencyCode = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
|
|
|
|
128
|
|
|
$al->push( |
129
|
|
|
ArrayData::create( |
130
|
|
|
array( |
131
|
|
|
'Link' => CountryPrices_ChangeCountryController::new_country_link($country->Code), |
132
|
|
|
'Title' => $country->Name, |
133
|
|
|
'CountryCode' => $country->Code, |
134
|
|
|
'LinkingMode' => ($isCurrentOne ? 'current' : 'link'), |
135
|
|
|
'Currency' => $currency, |
136
|
|
|
'CurrencyCode' => $currency |
137
|
|
|
) |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
return $al; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @return DataList |
|
|
|
|
147
|
|
|
*/ |
148
|
|
|
public function AlternativeHrefLangLinksCachingKey() |
149
|
|
|
{ |
150
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
151
|
|
|
if ($countryObject && $countryObject->Code) { |
152
|
|
|
return 'AlternativeHrefLangLinksCachingKey-'.$countryObject->Code.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
153
|
|
|
} |
154
|
|
|
return 'AlternativeHrefLangLinksCachingKey'.$this->owner->dataRecord->ID.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* |
159
|
|
|
* @param string $link - passed by reference |
160
|
|
|
*/ |
161
|
|
|
public function UpdateCanonicalLink(&$link) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$obj = $this->owner->dataRecord->CanonicalObject(); |
164
|
|
|
if ($obj) { |
165
|
|
|
$link = $obj->Link(); |
166
|
|
|
} else { |
167
|
|
|
$link = $this->owner->dataRecord->AbsoluteLink(); |
168
|
|
|
} |
169
|
|
|
return $link; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* redirects visitors to another website if they are listed as such in |
174
|
|
|
* CountryPrices_ChangeCountryController.off_site_url_redirects |
175
|
|
|
* |
176
|
|
|
* @param EcommerceCountry $countryObject current country of visitor |
177
|
|
|
* |
178
|
|
|
* @return null|SS_HTTPResponse |
179
|
|
|
*/ |
180
|
|
|
protected function checkForOffsiteRedirects($countryObject) |
181
|
|
|
{ |
182
|
|
|
$redirectsArray = Config::inst()->get('CountryPrices_ChangeCountryController', 'off_site_url_redirects'); |
183
|
|
|
$myCountryCode = strtoupper($countryObject->Code); |
184
|
|
|
if (isset($redirectsArray[$myCountryCode])) { |
185
|
|
|
return $this->redirect($redirectsArray[$myCountryCode]); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.