|
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
|
|
|
$param = Config::inst()->get('CountryPrice_Translation', 'locale_get_parameter'); |
|
29
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
|
30
|
|
|
if (isset($_GET[$param])) { |
|
31
|
|
|
$countryCode = preg_replace("/[^A-Z]+/", "", strtoupper(Convert::raw2sql($_GET[$param]))); |
|
32
|
|
|
if ($countryObject->Code != $countryCode) { |
|
33
|
|
|
return $this->owner->redirect( |
|
34
|
|
|
CountryPrices_ChangeCountryController::new_country_link($countryCode) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($countryObject) { |
|
40
|
|
|
$countryID = $countryObject->ID; |
|
41
|
|
|
$newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code); |
|
42
|
|
|
if($newURL) { |
|
|
|
|
|
|
43
|
|
|
$this->owner->redirect($newURL); |
|
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
|
|
|
$oldURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
93
|
|
|
|
|
94
|
|
|
$urlParts = parse_url($oldURL); |
|
95
|
|
|
parse_str($urlParts['query'], $params); |
|
96
|
|
|
|
|
97
|
|
|
$param = Config::inst()->get('CountryPrice_Translation', 'locale_get_parameter'); |
|
98
|
|
|
$params[$param] = $countryCode; // Overwrite if exists |
|
99
|
|
|
|
|
100
|
|
|
// Note that this will url_encode all values |
|
101
|
|
|
$urlParts['query'] = http_build_query($params); |
|
102
|
|
|
|
|
103
|
|
|
// If you have pecl_http |
|
104
|
|
|
if (function_exists('http_build_url')) { |
|
105
|
|
|
$newURL = http_build_url($urlParts); |
|
106
|
|
|
} else { |
|
107
|
|
|
$newURL = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query']; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($oldURL !== $newURL && self::$_redirection_count < 3) { |
|
111
|
|
|
self::$_redirection_count++; |
|
112
|
|
|
return $newURL; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* |
|
121
|
|
|
* |
|
122
|
|
|
* @return ArrayList |
|
123
|
|
|
*/ |
|
124
|
|
|
public function ChooseNewCountryList() |
|
125
|
|
|
{ |
|
126
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
|
127
|
|
|
$currentCode = ''; |
|
128
|
|
|
if ($obj = CountryPrice_EcommerceCountry::get_real_country()) { |
|
129
|
|
|
$currentCode = $obj->Code; |
|
130
|
|
|
} |
|
131
|
|
|
$al = ArrayList::create(); |
|
132
|
|
|
foreach ($countries as $country) { |
|
133
|
|
|
$isCurrentOne = $currentCode == $country->Code ? true : false; |
|
134
|
|
|
$currency = null; |
|
|
|
|
|
|
135
|
|
|
$currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
|
136
|
|
|
$currencyCode = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
|
|
|
|
|
|
137
|
|
|
$al->push( |
|
138
|
|
|
ArrayData::create( |
|
139
|
|
|
array( |
|
140
|
|
|
'Link' => CountryPrices_ChangeCountryController::new_country_link($country->Code), |
|
141
|
|
|
'Title' => $country->Name, |
|
142
|
|
|
'CountryCode' => $country->Code, |
|
143
|
|
|
'LinkingMode' => ($isCurrentOne ? 'current' : 'link'), |
|
144
|
|
|
'Currency' => $currency, |
|
145
|
|
|
'CurrencyCode' => $currency |
|
146
|
|
|
) |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
return $al; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* |
|
155
|
|
|
* @return DataList |
|
156
|
|
|
*/ |
|
157
|
|
|
function AlternativeHrefLangLinksCachingKey() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
return 'AlternativeHrefLangLinksCachingKey'.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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.