Hi Ingo,
Thanks for your input. On the same topic, I've created the functionality to reference the page number rather than the starting point but have an SEO issue else where.
When browsing down the relavent page numbers and to head back to the first page, on the list of page number links that I have at the bottom of the list, the link to page 1 (the first page of the list), instead of simply linking to domain.com/products/ it has the url domain.com/products/?page=1. I believe this is duplicating the same page unnessarily and could effect the page within search engine results.
I've made the following updates, but have been unable to figure out how to return the page url without the parameters? (In DataObjectSet.php - the comment "// How is this done?" is where I think this functionality needs to be added, but have been thinking it could be made in the ProductHolder template - all code below).
Any ideas?
In Page.php, i've added:
class Page_Controller extends ContentController {
...
function ProductList(){
$source = 'ProductPage';
$filter = 'StartDate < NOW() AND StartDate IS NOT NULL AND (EndDate > NOW() OR EndDate IS NULL)';
$sort= 'StartDate DESC';
$start;
$page; // Page Number From URL Parameter
$num=5;
if(!isset($_GET['page']) || !is_numeric($_GET['page']) || (int)$_GET['page'] < 1) {
$start = $_GET['page'] = 0;
} else {
$page = $_GET['page'];
$start = ($page - 1) * $num;
}
$latestProducts = DataObject::get($source,$filter,$sort,'',$start.','.$num);
return $latestProducts ? $latestProducts : false;
}
...
}
I've also had to update the DataObjectSet.php (tho ideally i need to bring this functionality out of here to avoid issues when upgrading silverstripe)
// Custom Pagination
protected $pageNumGetVar = "page";
public function PageNums($maxPages = 0){
$ret = new DataObjectSet();
if($maxPages) {
$startPage = ($this->CurrentPage() - floor($maxPages / 2)) - 1;
$endPage = $this->CurrentPage() + floor($maxPages / 2);
if($startPage < 0) {
$startPage = 0;
$endPage = $maxPages;
}
if($endPage > $this->TotalPages()) {
$endPage = $this->TotalPages();
$startPage = max(0, $endPage - $maxPages);
}
} else {
$startPage = 0;
$endPage = $this->TotalPages();
}
for($i=$startPage; $i < $endPage; $i++){
if($i == 0) {
// $link = Should be just the url without parameters. If page is equal to 1 then $link should be with parameters
// How is this done?
$link = HTTP::setGetVar($this->pageNumGetVar, $i + 1);
} else {
$link = HTTP::setGetVar($this->pageNumGetVar, $i + 1);
}
$thePage = new ArrayData(array(
"PageNum" => $i+1,
"Link" => $link,
"CurrentBool" => ($this->CurrentPage() == $i+1)?true:false,
)
);
$ret->push($thePage);
}
return $ret;
}
public function PageNextLink() {
if($this->pageStart + $this->pageLength < $this->totalSize) {
return HTTP::setGetVar($this->pageNumGetVar, $this->CurrentPage() + 1);
}
}
public function PagePrevLink() {
if($this->pageStart - $this->pageLength >= 0) {
return HTTP::setGetVar($this->pageNumGetVar, $this->CurrentPage() - 1);
}
}
....
}
And the template ProductHolder.ss contains the following
<% if ProductList.MoreThanOnePage %>
<div class="pagination">
<ul>
<% if ProductList.PrevLink %>
<li class="prev"><a href="$ProductList.PagePrevLink">Prev</a></li>
<% end_if %>
<% control ProductList.PageNums %>
<% if CurrentBool %>
<li class="current">$PageNum</li>
<% else %>
<li><a href="$Link">$PageNum</a></li>
<% end_if %>
<% end_control %>
<% if ProductList.NextLink %>
<li class="next"><a href="$ProductList.PageNextLink">Next</a></li>
<% end_if %>
</ul>
<p class="pages">Page $ProductList.CurrentPage of $ProductList.TotalPages Pages</p>
</div>
<% end_if %>
...