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($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() |
||
149 | |||
150 | /** |
||
151 | * |
||
152 | * @return DataList |
||
153 | */ |
||
154 | public function AlternativeHrefLangLinksCachingKey() |
||
162 | |||
163 | /** |
||
164 | * |
||
165 | * @param string $link - passed by reference |
||
166 | */ |
||
167 | public function UpdateCanonicalLink(&$link) |
||
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.