2012-09-25 05:55:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GridFieldPage displays a simple current page count summary.
|
|
|
|
* E.g. "View 1 - 15 of 32"
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-20 12:18:07 +02:00
|
|
|
* Depends on {@link GridFieldPaginator} being added to the {@link GridField}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-09-25 05:55:59 +02:00
|
|
|
*/
|
|
|
|
class GridFieldPageCount implements GridField_HTMLProvider {
|
|
|
|
/**
|
|
|
|
* @var string placement indicator for this control
|
|
|
|
*/
|
|
|
|
protected $targetFragment;
|
|
|
|
|
|
|
|
/**
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param string $targetFragment The fragment indicating the placement of this page count
|
2012-09-25 05:55:59 +02:00
|
|
|
*/
|
|
|
|
public function __construct($targetFragment = 'before') {
|
|
|
|
$this->targetFragment = $targetFragment;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-30 23:47:35 +02:00
|
|
|
/**
|
|
|
|
* Flag indicating whether or not this control should throw an error if a
|
|
|
|
* {@link GridFieldPaginator} is not present on the same {@link GridField}
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
|
|
|
* @config
|
2012-09-30 23:47:35 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $require_paginator = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-30 23:47:35 +02:00
|
|
|
/**
|
|
|
|
* Retrieves an instance of a GridFieldPaginator attached to the same control
|
|
|
|
* @param GridField $gridField The parent gridfield
|
|
|
|
* @return GridFieldPaginator The attached GridFieldPaginator, if found.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @throws LogicException
|
2012-09-30 23:47:35 +02:00
|
|
|
*/
|
|
|
|
protected function getPaginator($gridField) {
|
|
|
|
$paginator = $gridField->getConfig()->getComponentByType('GridFieldPaginator');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!$paginator && Config::inst()->get('GridFieldPageCount', 'require_paginator')) {
|
2012-09-30 23:47:35 +02:00
|
|
|
throw new LogicException(
|
|
|
|
get_class($this) . " relies on a GridFieldPaginator to be added " .
|
|
|
|
"to the same GridField, but none are present."
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-30 23:47:35 +02:00
|
|
|
return $paginator;
|
|
|
|
}
|
2012-09-25 05:55:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
// Retrieve paging parameters from the directing paginator component
|
2012-09-30 23:47:35 +02:00
|
|
|
$paginator = $this->getPaginator($gridField);
|
2012-09-25 05:55:59 +02:00
|
|
|
if ($paginator && ($forTemplate = $paginator->getTemplateParameters($gridField))) {
|
2016-08-23 04:32:26 +02:00
|
|
|
$template = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
2012-09-25 05:55:59 +02:00
|
|
|
return array(
|
2016-08-23 04:32:26 +02:00
|
|
|
$this->targetFragment => $forTemplate->renderWith($template)
|
2012-09-25 05:55:59 +02:00
|
|
|
);
|
|
|
|
}
|
2016-08-23 04:32:26 +02:00
|
|
|
|
|
|
|
return null;
|
2012-09-25 05:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|