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 | //provided by stealth ... |
||
29 | $countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
||
30 | if ($countryObject) { |
||
31 | |||
32 | //check if a redirect is required ... |
||
33 | $this->checkForOffsiteRedirects($countryObject); |
||
34 | |||
35 | $countryID = $countryObject->ID; |
||
36 | //check that there is a translation |
||
37 | if ($this->owner->dataRecord->thisPageHasTranslation($countryID)) { |
||
38 | //if there is a translation but it is not showing in the URL then redirect |
||
39 | $newURL = $this->addCountryCodeToUrlIfRequired($countryObject->Code); |
||
40 | if ($newURL) { |
||
41 | if (Director::is_site_url($newURL)) { |
||
42 | if ($this->owner->request->getVar('nothing')) { |
||
43 | return; |
||
44 | } else { |
||
45 | if(!strpos($newURL, '?')) { |
||
46 | $newURL .= '?nothing=limitless'; |
||
47 | } else { |
||
48 | $newURL .= '¬hing=limitless'; |
||
49 | } |
||
50 | } |
||
51 | $this->owner->redirect($newURL); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | |||
57 | $this->owner->dataRecord->loadTranslatedValues($countryID, null); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * returns the best fieldname for |
||
62 | * @param string $fieldName [description] |
||
63 | */ |
||
64 | public function CountryDistributorBestContentValue($fieldName) |
||
65 | { |
||
66 | $countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
||
67 | |||
68 | //check country |
||
69 | if (!empty($countryObject->$fieldName)) { |
||
70 | return $countryObject->$fieldName; |
||
71 | } |
||
72 | |||
73 | //check distributor |
||
74 | $distributor = Distributor::get_one_for_country($countryObject->Code); |
||
75 | if (!empty($distributor->$fieldName)) { |
||
76 | return $distributor->$fieldName; |
||
77 | } |
||
78 | //check EcomConfig |
||
79 | $distributor = Distributor::get_one_for_country($countryObject->Code); |
||
80 | if (!empty($distributor->$fieldName)) { |
||
81 | return $distributor->$fieldName; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * caching variable |
||
87 | * |
||
88 | * @var integer |
||
89 | */ |
||
90 | private static $_redirection_count = 0; |
||
91 | |||
92 | /** |
||
93 | * returns a string for the new url if a locale parameter can be added |
||
94 | * |
||
95 | * @return string | null |
||
96 | */ |
||
97 | private function addCountryCodeToUrlIfRequired($countryCode = '') |
||
98 | { |
||
99 | if (isset($_POST) && count($_POST)) { |
||
100 | return null; |
||
101 | } |
||
102 | //to do: add query here! |
||
103 | |||
104 | $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode); |
||
105 | |||
106 | if ($newURL && self::$_redirection_count < 3) { |
||
107 | self::$_redirection_count++; |
||
108 | |||
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() |
||
161 | |||
162 | /** |
||
163 | * |
||
164 | * @param string $link - passed by reference |
||
165 | */ |
||
166 | public function UpdateCanonicalLink(&$link) |
||
176 | |||
177 | /** |
||
178 | * redirects visitors to another website if they are listed as such in |
||
179 | * CountryPrices_ChangeCountryController.off_site_url_redirects |
||
180 | * |
||
181 | * @param EcommerceCountry $countryObject current country of visitor |
||
182 | * |
||
183 | * @return null|SS_HTTPResponse |
||
184 | */ |
||
185 | protected function checkForOffsiteRedirects($countryObject) |
||
193 | } |
||
194 |
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.