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 | $oldURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
||
98 | |||
99 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
||
100 | if($hasCountrySegment){ |
||
101 | $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
||
102 | } |
||
103 | else { |
||
104 | $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL); |
||
105 | } |
||
106 | |||
107 | if ($oldURL !== $newURL && self::$_redirection_count < 3) { |
||
108 | self::$_redirection_count++; |
||
109 | return $newURL; |
||
110 | } |
||
111 | return null; |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * |
||
117 | * |
||
118 | * @return ArrayList |
||
119 | */ |
||
120 | public function ChooseNewCountryList() |
||
148 | |||
149 | /** |
||
150 | * |
||
151 | * @return DataList |
||
152 | */ |
||
153 | public function AlternativeHrefLangLinksCachingKey() |
||
157 | } |
||
158 |
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.