PHP | Solution - A better DataObjectSet that allows better overriding through extension.
class BetterDataObjectSet extends DataObjectSet {
private $PageURL = null;
public function setPageURL($link) {
$this->PageURL = $link;
}
public function Pages($maxPages = 0){
$ret = new $this->class();
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++){
$link = HTTP::setGetVar($this->paginationGetVar, $i*$this->pageLength, $this->PageURL);
$thePage = new ArrayData(array(
"PageNum" => $i+1,
"Link" => $link,
"CurrentBool" => ($this->CurrentPage() == $i+1)?true:false,
)
);
$ret->push($thePage);
}
return $ret;
}
public function PaginationSummary($context = 4) {
$ret = new $this->class();
// convert number of pages to even number for offset calculation
if($context % 2) $context--;
// find out the offset
$current = $this->CurrentPage();
$totalPages = $this->TotalPages();
// if the first or last page is shown, use all content on one side (either left or right of current page)
// otherwise half the number for usage "around" the current page
$offset = ($current == 1 || $current == $totalPages) ? $context : floor($context/2);
$leftOffset = $current - ($offset);
if($leftOffset < 1) $leftOffset = 1;
if($leftOffset + $context > $totalPages) $leftOffset = $totalPages - $context;
for($i=0; $i < $totalPages; $i++) {
$link = HTTP::setGetVar($this->paginationGetVar, $i*$this->pageLength, $this->PageURL);
$num = $i+1;
$currentBool = ($current == $i+1) ? true:false;
if(
($num == $leftOffset-1 && $num != 1 && $num != $totalPages)
|| ($num == $leftOffset+$context+1 && $num != 1 && $num != $totalPages)
) {
$ret->push(new ArrayData(array(
"PageNum" => null,
"Link" => null,
"CurrentBool" => $currentBool,
)
));
} else if($num == 1 || $num == $totalPages || in_array($num, range($current-$offset,$current+$offset))) {
$ret->push(new ArrayData(array(
"PageNum" => $num,
"Link" => $link,
"CurrentBool" => $currentBool,
)
));
}
}
return $ret;
}
public function PrevLink() {
if($this->pageStart - $this->pageLength >= 0) {
return HTTP::setGetVar($this->paginationGetVar, $this->pageStart - $this->pageLength, $this->PageURL);
}
}
public function NextLink() {
if($this->pageStart + $this->pageLength < $this->totalSize) {
return HTTP::setGetVar($this->paginationGetVar, $this->pageStart + $this->pageLength, $this->PageURL);
}
}
public function getRange($offset, $length) {
$set = parent::getRange($offset, $length);
return new $this->class($set->toArray());
}
public function groupBy($index) {
$set = parent::groupBy($index);
return new $this->class($set->toArray());
}
public function GroupedBy($index, $childControl = "Children") {
$set = parent::GroupedBy($index, $childControl);
return new $this->class($set->toArray());
}
public function groupWithParents($groupField, $groupClassName, $sortParents = null, $parentField = 'ID', $collapse = false, $requiredParents = null) {
$set = parent::groupWithParents($groupField, $groupClassName, $sortParents, $parentField, $collapse, $requiredParents);
return new $this->class($set->toArray());
}
}