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
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
protected $currentPage = 1;
|
|
|
|
|
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-02-22 04:25:11 +01:00
|
|
|
public function __construct($itemsPerPage=15) {
|
2011-12-06 01:56:24 +01:00
|
|
|
$this->itemsPerPage = $itemsPerPage;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$this->currentPage = $state->currentPage = (int)$arguments;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if(!$this->getListPaginatable($dataList)) {
|
|
|
|
return $dataList;
|
|
|
|
}
|
|
|
|
if(!$this->currentPage) {
|
|
|
|
return $dataList->getRange(0, (int)$this->itemsPerPage);
|
|
|
|
}
|
|
|
|
$startRow = $this->itemsPerPage*($this->currentPage-1);
|
|
|
|
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) {
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR.'/javascript/GridField.js');
|
|
|
|
|
|
|
|
$forTemplate = new ArrayData(array());
|
|
|
|
$forTemplate->Fields = new ArrayList;
|
|
|
|
|
|
|
|
$countList = clone $gridField->List;
|
|
|
|
$totalRows = $countList->limit(null)->count();
|
|
|
|
$totalPages = ceil($totalRows/$this->itemsPerPage);
|
|
|
|
for($idx=1; $idx<=$totalPages; $idx++) {
|
|
|
|
if($idx == $this->currentPage) {
|
|
|
|
$field = new LiteralField('pagination_'.$idx, $idx);
|
|
|
|
} else {
|
|
|
|
$field = new GridField_Action($gridField, 'pagination_'.$idx, $idx, 'paginate', $idx);
|
|
|
|
$field->addExtraClass('ss-gridfield-button');
|
|
|
|
}
|
|
|
|
|
|
|
|
$forTemplate->Fields->push($field);
|
|
|
|
}
|
|
|
|
if(!$forTemplate->Fields->Count()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'footer' => $forTemplate->renderWith('GridFieldPaginator_Row', array('Colspan'=>count($gridField->getColumns()))),
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|