Skip to content

Commit

Permalink
ENHANCEMENT check for link mappings that match url get vars, in any o…
Browse files Browse the repository at this point in the history
…rder. Also check if Translatable class exists for 3.1 compatibility
  • Loading branch information
sheadawson committed Jul 31, 2013
1 parent 1c14bcc commit 7923607
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
17 changes: 12 additions & 5 deletions code/controllers/LinkMappingFrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down
29 changes: 25 additions & 4 deletions code/dataobjects/LinkMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

}
}

/**
Expand All @@ -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() {
Expand Down

0 comments on commit 7923607

Please sign in to comment.