Skip to content

Loading…

ENHANCEMENT check for link mappings that match url get vars, in any orde... #6

Merged
merged 1 commit into from

2 participants

@sheadawson

...r. Also check if Translatable class exists for 3.1 compatibility. Fixes Issue #5

@sheadawson sheadawson ENHANCEMENT check for link mappings that match url get vars, in any o…
…rder. Also check if Translatable class exists for 3.1 compatibility
7923607
@nyeholt nyeholt merged commit 9a14855 into silverstripe-australia:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2013
  1. @sheadawson

    ENHANCEMENT check for link mappings that match url get vars, in any o…

    sheadawson committed
    …rder. Also check if Translatable class exists for 3.1 compatibility
Showing with 37 additions and 9 deletions.
  1. +12 −5 code/controllers/LinkMappingFrontController.php
  2. +25 −4 code/dataobjects/LinkMapping.php
View
17 code/controllers/LinkMappingFrontController.php
@@ -18,7 +18,9 @@ public function getNestedController() {
}
// Find page by link, regardless of current locale settings
- Translatable::disable_locale_filter();
+ $translatable = class_exists('Translatable');
+ if($translatable) Translatable::disable_locale_filter();
+
$sitetree = DataObject::get_one(
'SiteTree',
sprintf(
@@ -27,12 +29,17 @@ public function getNestedController() {
(SiteTree::nested_urls() ? 'AND "ParentID" = 0' : null)
)
);
- Translatable::enable_locale_filter();
- if(!$sitetree) {
+ if($translatable) Translatable::enable_locale_filter();
+
+ if(!$sitetree) {
// first check for a link mapping to direct away to.
- $link = $request->getURL();
- $map = LinkMapping::get_by_link($link);
+ $link = $request->getURL();//
+ if(count($request->getVars()) > 1){
+ $link = $link . str_replace('url=' . $request->requestVar('url') . '&', '?', $_SERVER['QUERY_STRING']);
+ }
+
+ $map = LinkMapping::get_by_link($link);
if ($map) {
$this->response = new SS_HTTPResponse();
View
29 code/dataobjects/LinkMapping.php
@@ -36,9 +36,30 @@ class LinkMapping extends DataObject {
* @return LinkMapping
*/
public static function get_by_link($link) {
- return DataObject::get_one('LinkMapping', sprintf(
- '"MappedLink" = \'%s\'', Convert::raw2sql(self::unify_link($link))
- ));
+ $link = Convert::raw2sql(self::unify_link($link));
+
+ // check for an exact match
+ $match = LinkMapping::get()->filter('MappedLink', $link)->first();
+ if($match) return $match;
+
+ // check for a match with the same get vars in a different order
+ if(strpos($link, '?')){
+ $linkParts = explode('?', $link);
+ $url = $linkParts[0];
+ $matches = LinkMapping::get()->where("MappedLink like '$url%'");
+ parse_str($linkParts[1], $queryParams);
+
+ if($matches->count()){
+ foreach ($matches as $match) {
+ $matchQueryString = explode('?', $match->MappedLink);
+ parse_str($matchQueryString[1], $matchParams);
+ if($matchParams == $queryParams){
+ return $match;
+ }
+ }
+ }
+
+ }
}
/**
@@ -48,7 +69,7 @@ public static function get_by_link($link) {
* @return string
*/
public static function unify_link($link) {
- return strtolower(trim(Director::makeRelative(strtok($link, '?')), '/'));
+ return strtolower(trim(trim(Director::makeRelative($link), '/'), '?'));
}
public function getCMSFields() {
Something went wrong with that request. Please try again.