2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-01-23 18:19:27 +01:00
|
|
|
* GridFieldPaginator paginates the gridfields list and adds controlls to the
|
|
|
|
* bottom of the gridfield.
|
2011-12-06 01:56:24 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2012-02-22 04:25:11 +01:00
|
|
|
protected $itemsPerPage = 15;
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Which template to use for rendering
|
|
|
|
*
|
2012-01-23 18:19:27 +01:00
|
|
|
* @var string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
protected $itemClass = 'GridFieldPaginator_Row';
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items should be displayed per page
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public function __construct($itemsPerPage=null) {
|
|
|
|
if($itemsPerPage) $this->itemsPerPage = $itemsPerPage;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getActions($gridField) {
|
|
|
|
return array('paginate');
|
|
|
|
}
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $actionName
|
|
|
|
* @param string $arguments
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
|
|
|
if($actionName !== 'paginate') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$state = $gridField->State->GridFieldPaginator;
|
2012-03-07 04:01:19 +01:00
|
|
|
$state->currentPage = (int)$arguments;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_List $dataList
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
2012-03-07 04:01:19 +01:00
|
|
|
$state = $gridField->State->GridFieldPaginator;
|
|
|
|
if(!is_int($state->currentPage))
|
|
|
|
$state->currentPage = 1;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
if(!$this->getListPaginatable($dataList)) {
|
|
|
|
return $dataList;
|
|
|
|
}
|
2012-03-07 04:01:19 +01:00
|
|
|
if(!$state->currentPage) {
|
2011-12-06 01:56:24 +01:00
|
|
|
return $dataList->getRange(0, (int)$this->itemsPerPage);
|
|
|
|
}
|
2012-03-07 04:01:19 +01:00
|
|
|
$startRow = $this->itemsPerPage * ($state->currentPage - 1);
|
2011-12-06 01:56:24 +01:00
|
|
|
return $dataList->getRange((int)$startRow, (int)$this->itemsPerPage);
|
|
|
|
}
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getHTMLFragments($gridField) {
|
2012-03-07 04:01:19 +01:00
|
|
|
$state = $gridField->State->GridFieldPaginator;
|
|
|
|
if(!is_int($state->currentPage))
|
|
|
|
$state->currentPage = 1;
|
|
|
|
|
2012-03-05 03:28:25 +01:00
|
|
|
// Figure out which page and record range we're on
|
2011-12-06 01:56:24 +01:00
|
|
|
$countList = clone $gridField->List;
|
|
|
|
$totalRows = $countList->limit(null)->count();
|
2012-03-08 17:51:03 +01:00
|
|
|
if(!$totalRows) return array();
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
$totalPages = ceil($totalRows/$this->itemsPerPage);
|
2012-03-07 02:47:57 +01:00
|
|
|
if($totalPages == 0)
|
|
|
|
$totalPages = 1;
|
2012-03-07 04:01:19 +01:00
|
|
|
$firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1;
|
2012-03-07 02:47:57 +01:00
|
|
|
if($firstShownRecord > $totalRows)
|
|
|
|
$firstShownRecord = $totalRows;
|
2012-03-07 04:01:19 +01:00
|
|
|
$lastShownRecord = $state->currentPage * $this->itemsPerPage;
|
2012-03-05 03:28:25 +01:00
|
|
|
if($lastShownRecord > $totalRows)
|
|
|
|
$lastShownRecord = $totalRows;
|
|
|
|
|
|
|
|
|
2012-03-07 02:45:22 +01:00
|
|
|
// First page button
|
2012-03-06 12:51:57 +01:00
|
|
|
$firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1);
|
2012-03-07 02:45:22 +01:00
|
|
|
$firstPage->addExtraClass('ss-gridfield-firstpage');
|
2012-03-07 04:01:19 +01:00
|
|
|
if($state->currentPage == 1)
|
2012-03-07 02:45:22 +01:00
|
|
|
$firstPage = $firstPage->performDisabledTransformation();
|
2012-03-05 03:28:25 +01:00
|
|
|
|
|
|
|
// Previous page button
|
2012-03-07 04:01:19 +01:00
|
|
|
$previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1;
|
2012-03-06 12:51:57 +01:00
|
|
|
$previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', 'paginate', $previousPageNum);
|
2012-03-05 03:28:25 +01:00
|
|
|
$previousPage->addExtraClass('ss-gridfield-previouspage');
|
2012-03-07 04:01:19 +01:00
|
|
|
if($state->currentPage == 1)
|
2012-03-05 03:28:25 +01:00
|
|
|
$previousPage = $previousPage->performDisabledTransformation();
|
|
|
|
|
|
|
|
// Next page button
|
2012-03-07 04:01:19 +01:00
|
|
|
$nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1;
|
2012-03-06 12:51:57 +01:00
|
|
|
$nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', 'paginate', $nextPageNum);
|
2012-03-05 03:28:25 +01:00
|
|
|
$nextPage->addExtraClass('ss-gridfield-nextpage');
|
2012-03-07 04:01:19 +01:00
|
|
|
if($state->currentPage == $totalPages)
|
2012-03-05 03:28:25 +01:00
|
|
|
$nextPage = $nextPage->performDisabledTransformation();
|
|
|
|
|
2012-03-07 04:01:19 +01:00
|
|
|
// Last page button
|
2012-03-06 12:51:57 +01:00
|
|
|
$lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages);
|
2012-03-07 02:45:22 +01:00
|
|
|
$lastPage->addExtraClass('ss-gridfield-lastpage');
|
2012-03-07 04:01:19 +01:00
|
|
|
if($state->currentPage == $totalPages)
|
2012-03-07 02:45:22 +01:00
|
|
|
$lastPage = $lastPage->performDisabledTransformation();
|
2012-03-05 03:28:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Render in template
|
|
|
|
$forTemplate = new ArrayData(array(
|
2012-03-07 02:45:22 +01:00
|
|
|
'FirstPage' => $firstPage,
|
2012-03-05 03:28:25 +01:00
|
|
|
'PreviousPage' => $previousPage,
|
2012-03-07 04:01:19 +01:00
|
|
|
'CurrentPageNum' => $state->currentPage,
|
2012-03-05 03:28:25 +01:00
|
|
|
'NumPages' => $totalPages,
|
|
|
|
'NextPage' => $nextPage,
|
2012-03-07 02:45:22 +01:00
|
|
|
'LastPage' => $lastPage,
|
2012-03-05 03:28:25 +01:00
|
|
|
'FirstShownRecord' => $firstShownRecord,
|
|
|
|
'LastShownRecord' => $lastShownRecord,
|
|
|
|
'NumRecords' => $totalRows
|
|
|
|
));
|
2011-12-06 01:56:24 +01:00
|
|
|
return array(
|
|
|
|
'footer' => $forTemplate->renderWith('GridFieldPaginator_Row', array('Colspan'=>count($gridField->getColumns()))),
|
|
|
|
);
|
|
|
|
}
|
2012-01-23 18:19:27 +01:00
|
|
|
|
2012-03-05 12:12:36 +01:00
|
|
|
/**
|
|
|
|
* @param Int
|
|
|
|
*/
|
|
|
|
public function setItemsPerPage($num) {
|
|
|
|
$this->itemsPerPage = $num;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Int
|
|
|
|
*/
|
|
|
|
public function getItemsPerPage() {
|
|
|
|
return $this->itemsPerPage;
|
|
|
|
}
|
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/** Duck check to see if list support methods we need to paginate */
|
|
|
|
protected function getListPaginatable(SS_List $list) {
|
|
|
|
// If no list yet, not paginatable
|
|
|
|
if (!$list) return false;
|
|
|
|
// Check for methods we use
|
|
|
|
if(!method_exists($list, 'getRange')) return false;
|
|
|
|
if(!method_exists($list, 'limit')) return false;
|
|
|
|
// Default it true
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|