@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class EcommerceCountriesTest extends SapphireTest { |
| 3 | 3 | |
| 4 | - protected $usesDatabase = false; |
|
| 4 | + protected $usesDatabase = false; |
|
| 5 | 5 | |
| 6 | - protected $requiredExtensions = array(); |
|
| 6 | + protected $requiredExtensions = array(); |
|
| 7 | 7 | |
| 8 | - public function testMyMethod() { |
|
| 9 | - $this->assertEquals(1, 1); |
|
| 10 | - } |
|
| 8 | + public function testMyMethod() { |
|
| 9 | + $this->assertEquals(1, 1); |
|
| 10 | + } |
|
| 11 | 11 | |
| 12 | 12 | } |
| 13 | 13 | |
@@ -157,7 +157,7 @@ |
||
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | 159 | * |
| 160 | - * @return DataList |
|
| 160 | + * @return string |
|
| 161 | 161 | */ |
| 162 | 162 | public function AlternativeHrefLangLinksCachingKey() |
| 163 | 163 | { |
@@ -97,7 +97,7 @@ |
||
| 97 | 97 | $oldURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; |
| 98 | 98 | |
| 99 | 99 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
| 100 | - if($hasCountrySegment){ |
|
| 100 | + if ($hasCountrySegment) { |
|
| 101 | 101 | $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
| 102 | 102 | } |
| 103 | 103 | else { |
@@ -99,8 +99,7 @@ |
||
| 99 | 99 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($oldURL); |
| 100 | 100 | if($hasCountrySegment){ |
| 101 | 101 | $newURL = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($countryCode, $oldURL); |
| 102 | - } |
|
| 103 | - else { |
|
| 102 | + } else { |
|
| 104 | 103 | $newURL = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($countryCode, $oldURL); |
| 105 | 104 | } |
| 106 | 105 | |
@@ -44,10 +44,10 @@ discard block |
||
| 44 | 44 | $currencyCode = isset($currencyPerCountry[$countryCode]) ? $currencyPerCountry[$countryCode] : EcommerceCurrency::default_currency_code(); |
| 45 | 45 | $currencyDO = EcommerceCurrency::get_one_from_code($currencyCode); |
| 46 | 46 | } |
| 47 | - if (! $currencyDO) { |
|
| 47 | + if (!$currencyDO) { |
|
| 48 | 48 | $currencyDO = EcommerceCurrency::create_new($currencyCode); |
| 49 | 49 | } |
| 50 | - if (! $currencyDO) { |
|
| 50 | + if (!$currencyDO) { |
|
| 51 | 51 | $currencyDO = EcommerceCurrency::get_default(); |
| 52 | 52 | } |
| 53 | 53 | return $currencyDO; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | $cachekey = "EcommerceCurrencyCountryMatrix"; |
| 69 | 69 | $cache = SS_Cache::factory('CountryPrice_EcommerceCurrency'); |
| 70 | - if (! ($serializedArray = $cache->load($cachekey))) { |
|
| 70 | + if (!($serializedArray = $cache->load($cachekey))) { |
|
| 71 | 71 | $countries = CountryPrice_EcommerceCountry::get_real_countries_list(); |
| 72 | 72 | $unserializedArray = array(); |
| 73 | 73 | $defaultCurrencyCode = EcommerceCurrency::default_currency_code(); |
@@ -123,6 +123,7 @@ |
||
| 123 | 123 | /** |
| 124 | 124 | * @var int $countryID |
| 125 | 125 | * |
| 126 | + * @param integer $countryID |
|
| 126 | 127 | * @return CountryPrice_Translation | null |
| 127 | 128 | */ |
| 128 | 129 | public function getEcommerceTranslation($countryID) |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | public function loadTranslatedValues($countryID = 0, $variableOrMethod = '') |
| 57 | 57 | { |
| 58 | 58 | $translation = null; |
| 59 | - if (! $countryID) { |
|
| 59 | + if (!$countryID) { |
|
| 60 | 60 | $countryObject = CountryPrice_EcommerceCountry::get_real_country(); |
| 61 | 61 | if ($countryObject) { |
| 62 | 62 | $countryID = $countryObject->ID; |
@@ -75,9 +75,9 @@ discard block |
||
| 75 | 75 | $fieldsToReplace = $translation->FieldsToReplace(); |
| 76 | 76 | foreach ($fieldsToReplace as $replaceFields) { |
| 77 | 77 | $pageField = $replaceFields->PageField; |
| 78 | - $pageFieldTranslated = $pageField . 'Translated'; |
|
| 78 | + $pageFieldTranslated = $pageField.'Translated'; |
|
| 79 | 79 | $translationField = $replaceFields->TranslationField; |
| 80 | - if (! $variableOrMethod || $variableOrMethod === $pageField) { |
|
| 80 | + if (!$variableOrMethod || $variableOrMethod === $pageField) { |
|
| 81 | 81 | if ($translation->hasMethod($translationField)) { |
| 82 | 82 | $this->owner->$pageField = $translation->$translationField(); |
| 83 | 83 | $this->owner->$pageFieldTranslated = $translation->$translationField(); |
@@ -3,14 +3,17 @@ |
||
| 3 | 3 | interface CountryURLProviderInterface |
| 4 | 4 | { |
| 5 | 5 | /** |
| 6 | + * @return boolean |
|
| 6 | 7 | */ |
| 7 | 8 | public function hasCountrySegment($url = ''); |
| 8 | 9 | |
| 9 | 10 | /** |
| 11 | + * @return string |
|
| 10 | 12 | */ |
| 11 | 13 | public function replaceCountryCodeInUrl($countryCode, $url = ''); |
| 12 | 14 | |
| 13 | 15 | /** |
| 16 | + * @return string |
|
| 14 | 17 | */ |
| 15 | 18 | public function addCountryCodeToUrl($countryCode, $url = ''); |
| 16 | 19 | |
@@ -225,8 +225,7 @@ |
||
| 225 | 225 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($link); |
| 226 | 226 | if($hasCountrySegment){ |
| 227 | 227 | $link = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($this->EcommerceCountry()->Code, $link); |
| 228 | - } |
|
| 229 | - else { |
|
| 228 | + } else { |
|
| 230 | 229 | $link = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($this->EcommerceCountry()->Code, $link); |
| 231 | 230 | } |
| 232 | 231 | } |
@@ -99,11 +99,11 @@ discard block |
||
| 99 | 99 | $countryDropdownField = DropdownField::create( |
| 100 | 100 | 'EcommerceCountryID', |
| 101 | 101 | $fields->dataFieldByName('EcommerceCountryID')->Title(), |
| 102 | - array('' => '-- make sure to select a country --')+$countries |
|
| 102 | + array('' => '-- make sure to select a country --') + $countries |
|
| 103 | 103 | ); |
| 104 | 104 | |
| 105 | 105 | $fields->removeFieldFromTab("Root.Main", 'ParentID'); |
| 106 | - if($this->WithoutTranslation) { |
|
| 106 | + if ($this->WithoutTranslation) { |
|
| 107 | 107 | return FieldList::create( |
| 108 | 108 | array( |
| 109 | 109 | $countryDropdownField, |
@@ -123,16 +123,16 @@ discard block |
||
| 123 | 123 | ); |
| 124 | 124 | } |
| 125 | 125 | $dbFields = $this->inheritedDatabaseFields(); |
| 126 | - foreach($dbFields as $dbField => $fieldType) { |
|
| 126 | + foreach ($dbFields as $dbField => $fieldType) { |
|
| 127 | 127 | $useField = 'UseOriginal'.$dbField; |
| 128 | - if(!empty($this->$useField)) { |
|
| 128 | + if (!empty($this->$useField)) { |
|
| 129 | 129 | $fields->replaceField( |
| 130 | 130 | $dbField, |
| 131 | 131 | $fields->dataFieldByName($dbField)->performReadonlyTransformation() |
| 132 | 132 | ); |
| 133 | 133 | } |
| 134 | - if($fields->dataFieldByName($useField)){ |
|
| 135 | - $fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use original value for ') . $dbField); |
|
| 134 | + if ($fields->dataFieldByName($useField)) { |
|
| 135 | + $fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use original value for ').$dbField); |
|
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | $fields->addFieldToTab( |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | ) |
| 207 | 207 | ); |
| 208 | 208 | $this->extend('updateFieldsToReplace', $al); |
| 209 | - foreach($al as $fieldToReplace) { |
|
| 210 | - $ignoreField = 'UseOriginal' . $fieldToReplace->PageField; |
|
| 211 | - if(!empty($this->owner->$ignoreField)) { |
|
| 209 | + foreach ($al as $fieldToReplace) { |
|
| 210 | + $ignoreField = 'UseOriginal'.$fieldToReplace->PageField; |
|
| 211 | + if (!empty($this->owner->$ignoreField)) { |
|
| 212 | 212 | $al->remove($fieldToReplace); |
| 213 | 213 | } |
| 214 | 214 | } |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | $link = $this->Parent()->Link(); |
| 232 | 232 | if ($this->EcommerceCountryID) { |
| 233 | 233 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($link); |
| 234 | - if($hasCountrySegment){ |
|
| 234 | + if ($hasCountrySegment) { |
|
| 235 | 235 | $link = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($this->EcommerceCountry()->Code, $link); |
| 236 | 236 | } |
| 237 | 237 | else { |
@@ -244,20 +244,20 @@ discard block |
||
| 244 | 244 | public function requireDefaultRecords() |
| 245 | 245 | { |
| 246 | 246 | parent::requireDefaultRecords(); |
| 247 | - if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) { |
|
| 247 | + if (Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) { |
|
| 248 | 248 | $prices = CountryPrice::get(); |
| 249 | 249 | $ecommerceCountries = array(); |
| 250 | - foreach($prices as $price) { |
|
| 251 | - if($countryObject = $price->CountryObject()) { |
|
| 252 | - if($buyable = $price->Buyable()) { |
|
| 253 | - if($buyable instanceof Product) { |
|
| 254 | - if($buyable->ID && $countryObject->ID) { |
|
| 250 | + foreach ($prices as $price) { |
|
| 251 | + if ($countryObject = $price->CountryObject()) { |
|
| 252 | + if ($buyable = $price->Buyable()) { |
|
| 253 | + if ($buyable instanceof Product) { |
|
| 254 | + if ($buyable->ID && $countryObject->ID) { |
|
| 255 | 255 | $filter = array( |
| 256 | 256 | 'EcommerceCountryID' => $countryObject->ID, |
| 257 | 257 | 'ParentID' => $buyable->ID |
| 258 | 258 | ); |
| 259 | 259 | $ecommerceCountries[$countryObject->ID] = $countryObject; |
| 260 | - if(! CountryPrice_Translation::get()->filter($filter)->first()) { |
|
| 260 | + if (!CountryPrice_Translation::get()->filter($filter)->first()) { |
|
| 261 | 261 | DB::alteration_message( |
| 262 | 262 | 'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code, |
| 263 | 263 | 'created' |
@@ -271,14 +271,14 @@ discard block |
||
| 271 | 271 | } |
| 272 | 272 | } |
| 273 | 273 | } |
| 274 | - if(count($ecommerceCountries)) { |
|
| 275 | - foreach(ProductGroup::get() as $productGroup) { |
|
| 276 | - foreach($ecommerceCountries as $countryID => $countryObject) { |
|
| 274 | + if (count($ecommerceCountries)) { |
|
| 275 | + foreach (ProductGroup::get() as $productGroup) { |
|
| 276 | + foreach ($ecommerceCountries as $countryID => $countryObject) { |
|
| 277 | 277 | $filter = array( |
| 278 | 278 | 'EcommerceCountryID' => $countryObject->ID, |
| 279 | 279 | 'ParentID' => $productGroup->ID |
| 280 | 280 | ); |
| 281 | - if(! CountryPrice_Translation::get()->filter($filter)->first()) { |
|
| 281 | + if (!CountryPrice_Translation::get()->filter($filter)->first()) { |
|
| 282 | 282 | DB::alteration_message( |
| 283 | 283 | 'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code, |
| 284 | 284 | 'created' |
@@ -114,10 +114,10 @@ |
||
| 114 | 114 | |
| 115 | 115 | $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; |
| 116 | 116 | |
| 117 | - $query = !empty($query) ? '?'. http_build_query($query) : ''; |
|
| 117 | + $query = !empty($query) ? '?'.http_build_query($query) : ''; |
|
| 118 | 118 | |
| 119 | 119 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($url); |
| 120 | - if($hasCountrySegment){ |
|
| 120 | + if ($hasCountrySegment) { |
|
| 121 | 121 | $url = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($newCountryCode, $url); |
| 122 | 122 | } |
| 123 | 123 | else { |
@@ -119,8 +119,7 @@ |
||
| 119 | 119 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($url); |
| 120 | 120 | if($hasCountrySegment){ |
| 121 | 121 | $url = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($newCountryCode, $url); |
| 122 | - } |
|
| 123 | - else { |
|
| 122 | + } else { |
|
| 124 | 123 | $url = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($newCountryCode, $url); |
| 125 | 124 | } |
| 126 | 125 | return $url; |
@@ -10,62 +10,62 @@ |
||
| 10 | 10 | |
| 11 | 11 | class CountryURLProvider extends Object implements CountryURLProviderInterface |
| 12 | 12 | { |
| 13 | - private static $country_segments = array('nz', 'au', 'gb', 'eu', 'jp', 'us', 'zz'); |
|
| 13 | + private static $country_segments = array('nz', 'au', 'gb', 'eu', 'jp', 'us', 'zz'); |
|
| 14 | 14 | |
| 15 | - public function hasCountrySegment($url = ''){ |
|
| 15 | + public function hasCountrySegment($url = '') { |
|
| 16 | 16 | $url = $this->getDefaultURL($url); |
| 17 | 17 | $parsedUrl = parse_url($url); |
| 18 | 18 | $pathSegments = explode("/", $parsedUrl['path']); |
| 19 | 19 | $firstSegment = ''; |
| 20 | - $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 21 | - foreach ($pathSegments as $position => $segment){ |
|
| 22 | - if($segment){ |
|
| 20 | + $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 21 | + foreach ($pathSegments as $position => $segment) { |
|
| 22 | + if ($segment) { |
|
| 23 | 23 | $firstSegment = $segment; |
| 24 | 24 | break; |
| 25 | 25 | } |
| 26 | 26 | } |
| 27 | - if(in_array($firstSegment, $countries)){ |
|
| 27 | + if (in_array($firstSegment, $countries)) { |
|
| 28 | 28 | return true; |
| 29 | 29 | } |
| 30 | 30 | return false; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public function replaceCountryCodeInUrl($countryCode, $url = ''){ |
|
| 33 | + public function replaceCountryCodeInUrl($countryCode, $url = '') { |
|
| 34 | 34 | $url = $this->getDefaultURL($url); |
| 35 | 35 | $parsedUrl = parse_url($url); |
| 36 | 36 | $pathParts = explode('/', $parsedUrl['path']); |
| 37 | - $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 38 | - foreach($pathParts as $pathPartsKey => $pathPart) { |
|
| 37 | + $countries = Config::inst()->get('CountryURLProvider', 'country_segments'); |
|
| 38 | + foreach ($pathParts as $pathPartsKey => $pathPart) { |
|
| 39 | 39 | //check for first match |
| 40 | - if(in_array($pathPart, $countries)) { |
|
| 40 | + if (in_array($pathPart, $countries)) { |
|
| 41 | 41 | $pathParts[$pathPartsKey] = strtolower($countryCode); |
| 42 | 42 | break; |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | $parsedUrl['path'] = implode('/', $pathParts); |
| 46 | - $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. $parsedUrl['path']; |
|
| 47 | - if(isset($parsedUrl['query'])){ |
|
| 48 | - $url = $url . $parsedUrl['query']; |
|
| 46 | + $url = $parsedUrl['scheme'].'://'.$parsedUrl['host'].$parsedUrl['path']; |
|
| 47 | + if (isset($parsedUrl['query'])) { |
|
| 48 | + $url = $url.$parsedUrl['query']; |
|
| 49 | 49 | } |
| 50 | 50 | return $url; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function addCountryCodeToUrl($countryCode, $url = ''){ |
|
| 53 | + public function addCountryCodeToUrl($countryCode, $url = '') { |
|
| 54 | 54 | $url = $this->getDefaultURL($url); |
| 55 | 55 | $parsedUrl = parse_url($url); |
| 56 | - $url = $parsedUrl['scheme']. '://'. $parsedUrl['host']. '/'. strtolower($countryCode) . $parsedUrl['path']; |
|
| 57 | - if(isset($parsedUrl['query'])){ |
|
| 58 | - $url = $url . $parsedUrl['query']; |
|
| 56 | + $url = $parsedUrl['scheme'].'://'.$parsedUrl['host'].'/'.strtolower($countryCode).$parsedUrl['path']; |
|
| 57 | + if (isset($parsedUrl['query'])) { |
|
| 58 | + $url = $url.$parsedUrl['query']; |
|
| 59 | 59 | } |
| 60 | 60 | return $url; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | private function getDefaultURL($url = '') |
| 64 | 64 | { |
| 65 | - if($url) { |
|
| 65 | + if ($url) { |
|
| 66 | 66 | return Director::absoluteURL($url); |
| 67 | 67 | } |
| 68 | - return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
| 68 | + return (isset($_SERVER['HTTPS']) ? "https" : "http")."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | } |