1 | <?php |
||
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 | //check that there is a country! |
||
40 | if ($countryObject) { |
||
41 | $countryID = $countryObject->ID; |
||
42 | //check that there is a translation |
||
43 | if ($this->owner->dataRecord->hasCountryLocalInURL($countryID)) { |
||
44 | $newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code); |
||
45 | if ($newURL) { |
||
46 | $this->owner->redirect($newURL); |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 | |||
51 | $this->owner->dataRecord->loadTranslatedValues($countryID, null); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * returns the best fieldname for |
||
56 | * @param string $fieldName [description] |
||
57 | */ |
||
58 | public function CountryDistributorBestContentValue($fieldName) |
||
59 | { |
||
60 | $countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
||
61 | |||
62 | //check country |
||
63 | if (!empty($countryObject->$fieldName)) { |
||
64 | return $countryObject->$fieldName; |
||
65 | } |
||
66 | |||
67 | //check distributor |
||
68 | $distributor = Distributor::get_one_for_country($countryObject->Code); |
||
69 | if (!empty($distributor->$fieldName)) { |
||
70 | return $distributor->$fieldName; |
||
71 | } |
||
72 | //check EcomConfig |
||
73 | $distributor = Distributor::get_one_for_country($countryObject->Code); |
||
74 | if (!empty($distributor->$fieldName)) { |
||
75 | return $distributor->$fieldName; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * caching variable |
||
81 | * |
||
82 | * @var integer |
||
83 | */ |
||
84 | private static $_redirection_count = 0; |
||
85 | |||
86 | /** |
||
87 | * returns a string for the new url if a locale parameter can be added |
||
88 | * |
||
89 | * @return string | null |
||
90 | */ |
||
91 | private function addCountryCodeToUrlIfRequired($countryCode = '') |
||
92 | { |
||
93 | if (isset($_POST) && count($_POST)) { |
||
94 | return null; |
||
95 | } |
||
96 | //to do: add query here! |
||
97 | $protocol = Director::is_https() ? 'https://' : 'http://' |
||
98 | $oldURL = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
||
99 | |||
100 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
||
101 | if($hasCountrySegment){ |
||
102 | $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
||
103 | } |
||
104 | else { |
||
105 | $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL); |
||
106 | } |
||
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 | return 'AlternativeHrefLangLinksCachingKey'.'-'.$this->owner->dataRecord->ID.'-'.strtotime($this->owner->dataRecord->LastEdited); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * |
||
161 | * @param string $link - passed by reference |
||
162 | */ |
||
163 | public function UpdateCanonicalLink(&$link) |
||
164 | { |
||
165 | $obj = $this->owner->dataRecord->CanonicalObject(); |
||
166 | if($obj) { |
||
167 | $link = $obj->Link(); |
||
168 | } else { |
||
169 | $link = $this->owner->dataRecord->AbsoluteLink(); |
||
170 | } |
||
171 | return $link; |
||
172 | } |
||
173 | |||
174 | } |
||
175 |
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.