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($countryObject && $countryObject->Code) { |
31
|
|
|
if (isset($_GET[$param])) { |
32
|
|
|
$countryCode = preg_replace("/[^A-Z]+/", "", strtoupper(Convert::raw2sql($_GET[$param]))); |
33
|
|
|
if($countryCode) { |
34
|
|
|
if (strtoupper($countryObject->Code) != $countryCode) { |
35
|
|
|
return $this->owner->redirect( |
36
|
|
|
CountryPrices_ChangeCountryController::new_country_link($countryCode) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$countryID = $countryObject->ID; |
43
|
|
|
//check that there is a translation |
44
|
|
|
if ($this->owner->dataRecord->hasCountryLocalInURL($countryID)) { |
45
|
|
|
$newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code); |
46
|
|
|
if ($newURL) { |
47
|
|
|
$this->owner->redirect($newURL); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->owner->dataRecord->loadTranslatedValues($countryID, null); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* returns the best fieldname for |
57
|
|
|
* @param string $fieldName [description] |
58
|
|
|
*/ |
59
|
|
|
public function CountryDistributorBestContentValue($fieldName) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
62
|
|
|
|
63
|
|
|
//check country |
64
|
|
|
if (!empty($countryObject->$fieldName)) { |
65
|
|
|
return $countryObject->$fieldName; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//check distributor |
69
|
|
|
$distributor = Distributor::get_one_for_country($countryObject->Code); |
70
|
|
|
if (!empty($distributor->$fieldName)) { |
71
|
|
|
return $distributor->$fieldName; |
72
|
|
|
} |
73
|
|
|
//check EcomConfig |
74
|
|
|
$distributor = Distributor::get_one_for_country($countryObject->Code); |
75
|
|
|
if (!empty($distributor->$fieldName)) { |
76
|
|
|
return $distributor->$fieldName; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* caching variable |
82
|
|
|
* |
83
|
|
|
* @var integer |
84
|
|
|
*/ |
85
|
|
|
private static $_redirection_count = 0; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* returns a string for the new url if a locale parameter can be added |
89
|
|
|
* |
90
|
|
|
* @return string | null |
91
|
|
|
*/ |
92
|
|
|
private function addCountryCodeToUrlIfRequired($countryCode = '') |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if (isset($_POST) && count($_POST)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
//to do: add query here! |
98
|
|
|
$protocol = Director::is_https() ? 'https://' : 'http://'; |
99
|
|
|
$oldURL = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
100
|
|
|
|
101
|
|
|
$hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
102
|
|
|
if($hasCountrySegment){ |
103
|
|
|
$newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
104
|
|
|
} |
105
|
|
|
else { |
106
|
|
|
$newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL); |
107
|
|
|
} |
108
|
|
|
if ($oldURL !== $newURL && self::$_redirection_count < 3) { |
109
|
|
|
self::$_redirection_count++; |
110
|
|
|
return $newURL; |
111
|
|
|
} |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* |
119
|
|
|
* @return ArrayList |
120
|
|
|
*/ |
121
|
|
|
public function ChooseNewCountryList() |
122
|
|
|
{ |
123
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
124
|
|
|
$currentCode = ''; |
125
|
|
|
if ($obj = CountryPrice_EcommerceCountry::get_real_country()) { |
126
|
|
|
$currentCode = $obj->Code; |
127
|
|
|
} |
128
|
|
|
$al = ArrayList::create(); |
129
|
|
|
foreach ($countries as $country) { |
130
|
|
|
$isCurrentOne = $currentCode == $country->Code ? true : false; |
131
|
|
|
$currency = null; |
|
|
|
|
132
|
|
|
$currency = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
133
|
|
|
$currencyCode = CountryPrice_EcommerceCurrency::get_currency_for_country($country->Code); |
|
|
|
|
134
|
|
|
$al->push( |
135
|
|
|
ArrayData::create( |
136
|
|
|
array( |
137
|
|
|
'Link' => CountryPrices_ChangeCountryController::new_country_link($country->Code), |
138
|
|
|
'Title' => $country->Name, |
139
|
|
|
'CountryCode' => $country->Code, |
140
|
|
|
'LinkingMode' => ($isCurrentOne ? 'current' : 'link'), |
141
|
|
|
'Currency' => $currency, |
142
|
|
|
'CurrencyCode' => $currency |
143
|
|
|
) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
return $al; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* |
152
|
|
|
* @return DataList |
|
|
|
|
153
|
|
|
*/ |
154
|
|
|
public function AlternativeHrefLangLinksCachingKey() |
155
|
|
|
{ |
156
|
|
|
$countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
157
|
|
|
if($countryObject && $countryObject->Code) { |
158
|
|
|
return 'AlternativeHrefLangLinksCachingKey-'.$countryObject->Code.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
159
|
|
|
} |
160
|
|
|
return 'AlternativeHrefLangLinksCachingKey'.$this->owner->dataRecord->ID.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* @param string $link - passed by reference |
166
|
|
|
*/ |
167
|
|
|
public function UpdateCanonicalLink(&$link) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$obj = $this->owner->dataRecord->CanonicalObject(); |
170
|
|
|
if($obj) { |
171
|
|
|
$link = $obj->Link(); |
172
|
|
|
} else { |
173
|
|
|
$link = $this->owner->dataRecord->AbsoluteLink(); |
174
|
|
|
} |
175
|
|
|
return $link; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
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.