2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Limitable;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\ORM\UnsavedRelationList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
use LogicException;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* GridFieldPaginator paginates the {@link GridField} list and adds controls
|
2013-05-20 12:18:07 +02:00
|
|
|
* to the bottom of the {@link GridField}.
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
class GridFieldPaginator implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
|
2016-08-19 00:51:35 +02:00
|
|
|
use Configurable;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-11 03:40:23 +02:00
|
|
|
/**
|
|
|
|
* Specifies default items per page
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-04-11 03:40:23 +02:00
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $default_items_per_page = 15;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2014-04-11 03:40:23 +02:00
|
|
|
protected $itemsPerPage;
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-03-09 02:32:16 +01:00
|
|
|
/**
|
2012-03-10 02:44:12 +01:00
|
|
|
* See {@link setThrowExceptionOnBadDataType()}
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
|
|
|
protected $throwExceptionOnBadDataType = true;
|
|
|
|
|
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) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->itemsPerPage = $itemsPerPage ?: GridFieldPaginator::config()->get('default_items_per_page');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 02:32:16 +01:00
|
|
|
/**
|
|
|
|
* Determine what happens when this component is used with a list that isn't {@link SS_Filterable}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-09 02:32:16 +01:00
|
|
|
* - true: An exception is thrown
|
|
|
|
* - false: This component will be ignored - it won't make any changes to the GridField.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-09 02:32:16 +01:00
|
|
|
* By default, this is set to true so that it's clearer what's happening, but the predefined
|
|
|
|
* {@link GridFieldConfig} subclasses set this to false for flexibility.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param bool $throwExceptionOnBadDataType
|
|
|
|
* @return $this
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
2012-03-10 02:44:12 +01:00
|
|
|
public function setThrowExceptionOnBadDataType($throwExceptionOnBadDataType) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->throwExceptionOnBadDataType = $throwExceptionOnBadDataType;
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this;
|
2012-03-09 02:32:16 +01:00
|
|
|
}
|
2012-03-10 02:44:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link setThrowExceptionOnBadDataType()}
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2012-03-10 02:44:12 +01:00
|
|
|
*/
|
|
|
|
public function getThrowExceptionOnBadDataType() {
|
|
|
|
return $this->throwExceptionOnBadDataType;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 02:32:16 +01:00
|
|
|
/**
|
|
|
|
* Check that this dataList is of the right data type.
|
|
|
|
* Returns false if it's a bad data type, and if appropriate, throws an exception.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param SS_List $dataList
|
|
|
|
* @return bool
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
|
|
|
protected function checkDataType($dataList) {
|
2016-09-09 08:43:05 +02:00
|
|
|
if($dataList instanceof Limitable) {
|
2012-03-09 02:32:16 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if($this->throwExceptionOnBadDataType) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new LogicException(
|
|
|
|
get_class($this) . " expects an SS_Limitable list to be passed to the GridField.");
|
2012-03-09 02:32:16 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->checkDataType($gridField->getList())) {
|
|
|
|
return [];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
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) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($gridField->getList())) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
if($actionName !== 'paginate') {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-25 05:55:59 +02:00
|
|
|
$state = $this->getGridPagerState($gridField);
|
2012-03-07 04:01:19 +01:00
|
|
|
$state->currentPage = (int)$arguments;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 05:06:21 +01:00
|
|
|
protected $totalItems = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
/**
|
|
|
|
* Retrieves/Sets up the state object used to store and retrieve information
|
|
|
|
* about the current paging details of this GridField
|
|
|
|
* @param GridField $gridField
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return GridState_Data
|
2012-09-25 05:55:59 +02:00
|
|
|
*/
|
|
|
|
protected function getGridPagerState(GridField $gridField) {
|
|
|
|
$state = $gridField->State->GridFieldPaginator;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
// Force the state to the initial page if none is set
|
2013-10-10 23:08:59 +02:00
|
|
|
$state->currentPage(1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
return $state;
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_List $dataList
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return SS_List
|
2012-01-23 18:19:27 +01:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($dataList)) return $dataList;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
$state = $this->getGridPagerState($gridField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
// Update item count prior to filter. GridFieldPageCount will rely on this value
|
|
|
|
$this->totalItems = $dataList->count();
|
2012-03-07 04:01:19 +01:00
|
|
|
|
2014-10-29 13:16:47 +01:00
|
|
|
$startRow = $this->itemsPerPage * ($state->currentPage - 1);
|
|
|
|
|
|
|
|
// Prevent visiting a page with an offset higher than the total number of items
|
|
|
|
if($startRow >= $this->totalItems) {
|
|
|
|
$state->currentPage = 1;
|
|
|
|
$startRow = 0;
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
if(!($dataList instanceof Limitable) || ($dataList instanceof UnsavedRelationList)) {
|
2011-12-06 01:56:24 +01:00
|
|
|
return $dataList;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 02:02:37 +01:00
|
|
|
return $dataList->limit((int)$this->itemsPerPage, (int)$startRow);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-23 18:19:27 +01:00
|
|
|
/**
|
2012-09-25 05:55:59 +02:00
|
|
|
* Determines arguments to be passed to the template for building this field
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return ArrayData If paging is available this will be an ArrayData
|
2012-09-25 05:55:59 +02:00
|
|
|
* object of paging details with these parameters:
|
|
|
|
* <ul>
|
|
|
|
* <li>OnlyOnePage: boolean - Is there only one page?</li>
|
|
|
|
* <li>FirstShownRecord: integer - Number of the first record displayed</li>
|
|
|
|
* <li>LastShownRecord: integer - Number of the last record displayed</li>
|
|
|
|
* <li>NumRecords: integer - Total number of records</li>
|
|
|
|
* <li>NumPages: integer - The number of pages</li>
|
|
|
|
* <li>CurrentPageNum (optional): integer - If OnlyOnePage is false, the number of the current page</li>
|
|
|
|
* <li>FirstPage (optional): GridField_FormAction - Button to go to the first page</li>
|
|
|
|
* <li>PreviousPage (optional): GridField_FormAction - Button to go to the previous page</li>
|
|
|
|
* <li>NextPage (optional): GridField_FormAction - Button to go to the next page</li>
|
|
|
|
* <li>LastPage (optional): GridField_FormAction - Button to go to last page</li>
|
|
|
|
* </ul>
|
2012-01-23 18:19:27 +01:00
|
|
|
*/
|
2012-09-25 05:55:59 +02:00
|
|
|
public function getTemplateParameters(GridField $gridField) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->checkDataType($gridField->getList())) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
$state = $this->getGridPagerState($gridField);
|
2012-03-07 04:01:19 +01:00
|
|
|
|
2012-03-05 03:28:25 +01:00
|
|
|
// Figure out which page and record range we're on
|
2012-03-09 05:06:21 +01:00
|
|
|
$totalRows = $this->totalItems;
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$totalRows) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-03-08 17:51:03 +01:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
$totalPages = (int)ceil($totalRows/$this->itemsPerPage);
|
2016-08-19 00:51:35 +02:00
|
|
|
if($totalPages == 0) {
|
2012-03-07 02:47:57 +01:00
|
|
|
$totalPages = 1;
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2012-03-07 04:01:19 +01:00
|
|
|
$firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1;
|
2016-08-19 00:51:35 +02:00
|
|
|
if($firstShownRecord > $totalRows) {
|
2012-03-07 02:47:57 +01:00
|
|
|
$firstShownRecord = $totalRows;
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2012-03-07 04:01:19 +01:00
|
|
|
$lastShownRecord = $state->currentPage * $this->itemsPerPage;
|
2016-08-19 00:51:35 +02:00
|
|
|
if($lastShownRecord > $totalRows) {
|
2012-03-05 03:28:25 +01:00
|
|
|
$lastShownRecord = $totalRows;
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// If there is only 1 page for all the records in list, we don't need to go further
|
2012-05-18 01:18:55 +02:00
|
|
|
// to sort out those first page, last page, pre and next pages, etc
|
|
|
|
// we are not render those in to the paginator.
|
|
|
|
if($totalPages === 1){
|
2012-09-25 05:55:59 +02:00
|
|
|
return new ArrayData(array(
|
2012-05-18 01:18:55 +02:00
|
|
|
'OnlyOnePage' => true,
|
|
|
|
'FirstShownRecord' => $firstShownRecord,
|
|
|
|
'LastShownRecord' => $lastShownRecord,
|
2012-09-25 05:55:59 +02:00
|
|
|
'NumRecords' => $totalRows,
|
|
|
|
'NumPages' => $totalPages
|
2012-05-18 01:18:55 +02:00
|
|
|
));
|
2012-09-25 05:55:59 +02:00
|
|
|
} else {
|
2012-05-18 01:18:55 +02:00
|
|
|
// First page button
|
|
|
|
$firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1);
|
2016-07-01 03:37:29 +02:00
|
|
|
$firstPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-double-left ss-gridfield-firstpage');
|
2016-08-19 00:51:35 +02:00
|
|
|
if($state->currentPage == 1) {
|
2012-05-18 01:18:55 +02:00
|
|
|
$firstPage = $firstPage->performDisabledTransformation();
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2012-05-18 01:18:55 +02:00
|
|
|
|
|
|
|
// Previous page button
|
|
|
|
$previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1;
|
2012-09-26 23:34:00 +02:00
|
|
|
$previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous',
|
|
|
|
'paginate', $previousPageNum);
|
2016-07-01 03:37:29 +02:00
|
|
|
$previousPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-left ss-gridfield-previouspage');
|
2012-05-18 01:18:55 +02:00
|
|
|
if($state->currentPage == 1)
|
|
|
|
$previousPage = $previousPage->performDisabledTransformation();
|
|
|
|
|
|
|
|
// Next page button
|
|
|
|
$nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1;
|
2012-09-26 23:34:00 +02:00
|
|
|
$nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next',
|
|
|
|
'paginate', $nextPageNum);
|
2016-07-01 03:37:29 +02:00
|
|
|
$nextPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-right ss-gridfield-nextpage');
|
2012-05-18 01:18:55 +02:00
|
|
|
if($state->currentPage == $totalPages)
|
|
|
|
$nextPage = $nextPage->performDisabledTransformation();
|
|
|
|
|
|
|
|
// Last page button
|
|
|
|
$lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages);
|
2016-07-01 03:37:29 +02:00
|
|
|
$lastPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-double-right ss-gridfield-lastpage');
|
2016-08-19 00:51:35 +02:00
|
|
|
if($state->currentPage == $totalPages) {
|
2012-05-18 01:18:55 +02:00
|
|
|
$lastPage = $lastPage->performDisabledTransformation();
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2012-05-18 01:18:55 +02:00
|
|
|
|
|
|
|
// Render in template
|
2012-09-25 05:55:59 +02:00
|
|
|
return new ArrayData(array(
|
2012-05-18 01:18:55 +02:00
|
|
|
'OnlyOnePage' => false,
|
|
|
|
'FirstPage' => $firstPage,
|
|
|
|
'PreviousPage' => $previousPage,
|
|
|
|
'CurrentPageNum' => $state->currentPage,
|
|
|
|
'NumPages' => $totalPages,
|
|
|
|
'NextPage' => $nextPage,
|
|
|
|
'LastPage' => $lastPage,
|
|
|
|
'FirstShownRecord' => $firstShownRecord,
|
|
|
|
'LastShownRecord' => $lastShownRecord,
|
|
|
|
'NumRecords' => $totalRows
|
|
|
|
));
|
|
|
|
}
|
2012-09-25 05:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
$forTemplate = $this->getTemplateParameters($gridField);
|
2016-08-23 04:32:26 +02:00
|
|
|
if(!$forTemplate) {
|
|
|
|
return null;
|
2012-09-25 05:55:59 +02:00
|
|
|
}
|
2016-08-23 04:32:26 +02:00
|
|
|
$template = SSViewer::get_templates_by_class($this, '_Row', __CLASS__);
|
|
|
|
return array(
|
|
|
|
'footer' => $forTemplate->renderWith(
|
|
|
|
$template,
|
|
|
|
array('Colspan' => count($gridField->getColumns()))
|
|
|
|
)
|
|
|
|
);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-01-23 18:19:27 +01:00
|
|
|
|
2012-03-05 12:12:36 +01:00
|
|
|
/**
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param int $num
|
|
|
|
* @return $this
|
2012-03-05 12:12:36 +01:00
|
|
|
*/
|
|
|
|
public function setItemsPerPage($num) {
|
|
|
|
$this->itemsPerPage = $num;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Int
|
|
|
|
*/
|
|
|
|
public function getItemsPerPage() {
|
|
|
|
return $this->itemsPerPage;
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|