2012-02-09 04:02:47 +01:00
|
|
|
<?php
|
2012-03-06 03:01:17 +01:00
|
|
|
/**
|
|
|
|
* Adding this class to a {@link GridFieldConfig} of a {@link GridField} adds a header title to that field.
|
|
|
|
* The header serves double duty of displaying the name of the data the GridField is showing and
|
|
|
|
* providing an "add new" button to create new object instances.
|
|
|
|
*
|
|
|
|
* The reason for making "add new" part of the title component is to make it easier for the user to tell
|
|
|
|
* which "add new" button belongs to which datagrid, in the case where multiple datagrids are on a single
|
|
|
|
* page. It is also a more efficient use of screen space.
|
|
|
|
*
|
|
|
|
* The default DataGrid includes the add button. You can hide the button by setting a boolean using the
|
|
|
|
* setNewEnabled() method
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage gridfield
|
|
|
|
*/
|
2012-03-09 00:54:02 +01:00
|
|
|
class GridFieldToolbarHeader implements GridField_HTMLProvider {
|
2012-03-05 23:19:46 +01:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2012-03-05 23:19:46 +01:00
|
|
|
protected $newEnabled = true;
|
2012-03-08 01:58:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var type
|
|
|
|
*/
|
|
|
|
protected $gridField = null;
|
2012-03-05 23:19:46 +01:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function getHTMLFragments( $gridField) {
|
|
|
|
$this->gridField = $gridField;
|
2012-02-09 04:02:47 +01:00
|
|
|
return array(
|
2012-02-20 02:50:53 +01:00
|
|
|
'header' => $gridField->customise(array(
|
2012-03-05 23:19:46 +01:00
|
|
|
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
|
|
|
|
'NewEnabled' => $this->getNewEnabled()
|
2012-03-09 00:54:02 +01:00
|
|
|
))->renderWith('GridFieldToolbarHeader')
|
2012-02-09 04:02:47 +01:00
|
|
|
);
|
|
|
|
}
|
2012-03-05 23:19:46 +01:00
|
|
|
|
2012-03-06 03:01:17 +01:00
|
|
|
/**
|
|
|
|
* Returns whether or not the "add new" button will appear when rendering this DataGrid title
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-03-08 01:58:53 +01:00
|
|
|
public function getNewEnabled() {
|
|
|
|
if($this->gridField) {
|
|
|
|
$model = singleton($this->gridField->getModelClass());
|
|
|
|
if(!$model->canCreate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-05 23:19:46 +01:00
|
|
|
return $this->newEnabled;
|
|
|
|
}
|
|
|
|
|
2012-03-06 03:01:17 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable the "add new" button to add new DataGrid object instances
|
|
|
|
* @param $enabled
|
|
|
|
*/
|
2012-03-08 01:58:53 +01:00
|
|
|
public function setNewEnabled($enabled) {
|
2012-03-05 23:19:46 +01:00
|
|
|
$this->newEnabled = $enabled;
|
|
|
|
}
|
2012-02-09 04:02:47 +01:00
|
|
|
}
|