@@ -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 | |
@@ -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 | return $fields;  | 
                                                        
@@ -198,9 +198,9 @@ discard block  | 
                                                    ||
| 198 | 198 | )  | 
                                                        
| 199 | 199 | );  | 
                                                        
| 200 | 200 |          $this->extend('updateFieldsToReplace', $al); | 
                                                        
| 201 | -        foreach($al as $fieldToReplace) { | 
                                                        |
| 202 | - $ignoreField = 'UseOriginal' . $fieldToReplace->PageField;  | 
                                                        |
| 203 | -            if(!empty($this->owner->$ignoreField)) { | 
                                                        |
| 201 | +        foreach ($al as $fieldToReplace) { | 
                                                        |
| 202 | + $ignoreField = 'UseOriginal'.$fieldToReplace->PageField;  | 
                                                        |
| 203 | +            if (!empty($this->owner->$ignoreField)) { | 
                                                        |
| 204 | 204 | $al->remove($fieldToReplace);  | 
                                                        
| 205 | 205 | }  | 
                                                        
| 206 | 206 | }  | 
                                                        
@@ -223,7 +223,7 @@ discard block  | 
                                                    ||
| 223 | 223 | $link = $this->Parent()->Link();  | 
                                                        
| 224 | 224 |          if ($this->EcommerceCountryID) { | 
                                                        
| 225 | 225 | $hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($link);  | 
                                                        
| 226 | -            if($hasCountrySegment){ | 
                                                        |
| 226 | +            if ($hasCountrySegment) { | 
                                                        |
| 227 | 227 | $link = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($this->EcommerceCountry()->Code, $link);  | 
                                                        
| 228 | 228 | }  | 
                                                        
| 229 | 229 |              else { | 
                                                        
@@ -236,20 +236,20 @@ discard block  | 
                                                    ||
| 236 | 236 | public function requireDefaultRecords()  | 
                                                        
| 237 | 237 |      { | 
                                                        
| 238 | 238 | parent::requireDefaultRecords();  | 
                                                        
| 239 | -        if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) { | 
                                                        |
| 239 | +        if (Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) { | 
                                                        |
| 240 | 240 | $prices = CountryPrice::get();  | 
                                                        
| 241 | 241 | $ecommerceCountries = array();  | 
                                                        
| 242 | -            foreach($prices as $price) { | 
                                                        |
| 243 | -                if($countryObject = $price->CountryObject()) { | 
                                                        |
| 244 | -                    if($buyable = $price->Buyable()) { | 
                                                        |
| 245 | -                        if($buyable instanceof Product) { | 
                                                        |
| 246 | -                            if($buyable->ID && $countryObject->ID) { | 
                                                        |
| 242 | +            foreach ($prices as $price) { | 
                                                        |
| 243 | +                if ($countryObject = $price->CountryObject()) { | 
                                                        |
| 244 | +                    if ($buyable = $price->Buyable()) { | 
                                                        |
| 245 | +                        if ($buyable instanceof Product) { | 
                                                        |
| 246 | +                            if ($buyable->ID && $countryObject->ID) { | 
                                                        |
| 247 | 247 | $filter = array(  | 
                                                        
| 248 | 248 | 'EcommerceCountryID' => $countryObject->ID,  | 
                                                        
| 249 | 249 | 'ParentID' => $buyable->ID  | 
                                                        
| 250 | 250 | );  | 
                                                        
| 251 | 251 | $ecommerceCountries[$countryObject->ID] = $countryObject;  | 
                                                        
| 252 | -                                if(! CountryPrice_Translation::get()->filter($filter)->first()) { | 
                                                        |
| 252 | +                                if (!CountryPrice_Translation::get()->filter($filter)->first()) { | 
                                                        |
| 253 | 253 | DB::alteration_message(  | 
                                                        
| 254 | 254 | 'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code,  | 
                                                        
| 255 | 255 | 'created'  | 
                                                        
@@ -263,14 +263,14 @@ discard block  | 
                                                    ||
| 263 | 263 | }  | 
                                                        
| 264 | 264 | }  | 
                                                        
| 265 | 265 | }  | 
                                                        
| 266 | -            if(count($ecommerceCountries)) { | 
                                                        |
| 267 | -                foreach(ProductGroup::get() as $productGroup) { | 
                                                        |
| 268 | -                    foreach($ecommerceCountries as $countryID => $countryObject) { | 
                                                        |
| 266 | +            if (count($ecommerceCountries)) { | 
                                                        |
| 267 | +                foreach (ProductGroup::get() as $productGroup) { | 
                                                        |
| 268 | +                    foreach ($ecommerceCountries as $countryID => $countryObject) { | 
                                                        |
| 269 | 269 | $filter = array(  | 
                                                        
| 270 | 270 | 'EcommerceCountryID' => $countryObject->ID,  | 
                                                        
| 271 | 271 | 'ParentID' => $productGroup->ID  | 
                                                        
| 272 | 272 | );  | 
                                                        
| 273 | -                        if(! CountryPrice_Translation::get()->filter($filter)->first()) { | 
                                                        |
| 273 | +                        if (!CountryPrice_Translation::get()->filter($filter)->first()) { | 
                                                        |
| 274 | 274 | DB::alteration_message(  | 
                                                        
| 275 | 275 | 'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code,  | 
                                                        
| 276 | 276 | 'created'  | 
                                                        
@@ -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 | }  | 
                                                        
@@ -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', 'us', 'row'); | 
                                                        |
| 13 | +    private static $country_segments = array('nz', 'au', 'gb', 'eu', 'us', 'row'); | 
                                                        |
| 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 | }  | 
                                                        
@@ -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 | |
@@ -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;  |