Fix links on paginated lists when there are GET vars

Prior to this change, if there were already GET vars on a page
with a PaginatedList, the links would include a mix of '&' and '&'.
This commit is contained in:
Andrew O'Neil 2017-09-27 15:41:08 +10:00
parent f981d09080
commit c7cbbb29f4
2 changed files with 77 additions and 4 deletions

View File

@ -436,7 +436,12 @@ class PaginatedList extends ListDecorator
*/
public function FirstLink()
{
return HTTP::setGetVar($this->getPaginationGetVar(), 0);
return HTTP::setGetVar(
$this->getPaginationGetVar(),
0,
$this->request ? $this->request->getURL(true) : null,
'&'
);
}
/**
@ -446,7 +451,12 @@ class PaginatedList extends ListDecorator
*/
public function LastLink()
{
return HTTP::setGetVar($this->getPaginationGetVar(), ($this->TotalPages() - 1) * $this->getPageLength());
return HTTP::setGetVar(
$this->getPaginationGetVar(),
($this->TotalPages() - 1) * $this->getPageLength(),
$this->request ? $this->request->getURL(true) : null,
'&'
);
}
/**
@ -458,7 +468,12 @@ class PaginatedList extends ListDecorator
public function NextLink()
{
if ($this->NotLastPage()) {
return HTTP::setGetVar($this->getPaginationGetVar(), $this->getPageStart() + $this->getPageLength());
return HTTP::setGetVar(
$this->getPaginationGetVar(),
$this->getPageStart() + $this->getPageLength(),
$this->request ? $this->request->getURL(true) : null,
'&'
);
}
}
@ -471,7 +486,12 @@ class PaginatedList extends ListDecorator
public function PrevLink()
{
if ($this->NotFirstPage()) {
return HTTP::setGetVar($this->getPaginationGetVar(), $this->getPageStart() - $this->getPageLength());
return HTTP::setGetVar(
$this->getPaginationGetVar(),
$this->getPageStart() - $this->getPageLength(),
$this->request ? $this->request->getURL(true) : null,
'&'
);
}
}

View File

@ -9,6 +9,7 @@ use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Tests\DataObjectTest\Player;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\HTTPRequest;
/**
* Tests for the {@link SilverStripe\ORM\PaginatedList} class.
@ -330,6 +331,19 @@ class PaginatedListTest extends SapphireTest
$this->assertContains('start=0', $list->FirstLink());
}
public function testFirstLinkContainsCurrentGetParameters() {
$request = new HTTPRequest(
'GET',
'http://coolsite.com/my-cool-page',
['awesomeness' => 'nextLevel', 'start' => 20]
);
$list = new PaginatedList(new ArrayList(), $request);
$list->setTotalItems(50);
$list->setPageLength(10);
$this->assertEquals($list->FirstLink(), 'http://coolsite.com/my-cool-page?awesomeness=nextLevel&start=0');
}
public function testLastLink()
{
$list = new PaginatedList(new ArrayList());
@ -342,6 +356,19 @@ class PaginatedListTest extends SapphireTest
$this->assertContains('start=0', $list->LastLink());
}
public function testLastLinkContainsCurrentGetParameters() {
$request = new HTTPRequest(
'GET',
'http://coolsite.com/my-cool-page',
['awesomeness' => 'nextLevel']
);
$list = new PaginatedList(new ArrayList(), $request);
$list->setTotalItems(50);
$list->setPageLength(10);
$this->assertEquals($list->LastLink(), 'http://coolsite.com/my-cool-page?awesomeness=nextLevel&start=40');
}
public function testNextLink()
{
$list = new PaginatedList(new ArrayList());
@ -363,6 +390,19 @@ class PaginatedListTest extends SapphireTest
$this->assertNull($list->NextLink());
}
public function testNextLinkContainsCurrentGetParameters() {
$request = new HTTPRequest(
'GET',
'http://coolsite.com/my-cool-page',
['awesomeness' => 'nextLevel']
);
$list = new PaginatedList(new ArrayList(), $request);
$list->setTotalItems(50);
$list->setPageLength(10);
$this->assertEquals($list->NextLink(), 'http://coolsite.com/my-cool-page?awesomeness=nextLevel&start=10');
}
public function testPrevLink()
{
$list = new PaginatedList(new ArrayList());
@ -380,4 +420,17 @@ class PaginatedListTest extends SapphireTest
$list->setPageLength(0);
$this->assertNull($list->PrevLink());
}
public function testPrevLinkContainsCurrentGetParameters() {
$request = new HTTPRequest(
'GET',
'http://coolsite.com/my-cool-page',
['awesomeness' => 'nextLevel', 'start' => '30']
);
$list = new PaginatedList(new ArrayList(), $request);
$list->setTotalItems(50);
$list->setPageLength(10);
$this->assertEquals($list->PrevLink(), 'http://coolsite.com/my-cool-page?awesomeness=nextLevel&start=20');
}
}