2012-03-09 04:50:43 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This component provides a button for opening the add new form provided by
|
2013-05-20 12:18:07 +02:00
|
|
|
* {@link GridFieldDetailForm}.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Only returns a button if {@link DataObject->canCreate()} for this record
|
2013-05-20 12:18:07 +02:00
|
|
|
* returns true.
|
2012-03-09 04:50:43 +01:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-03-09 04:50:43 +01:00
|
|
|
*/
|
|
|
|
class GridFieldAddNewButton implements GridField_HTMLProvider {
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
protected $targetFragment;
|
2012-05-28 01:55:43 +02:00
|
|
|
|
|
|
|
protected $buttonName;
|
|
|
|
|
|
|
|
public function setButtonName($name) {
|
|
|
|
$this->buttonName = $name;
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2012-05-28 01:55:43 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
public function __construct($targetFragment = 'before') {
|
|
|
|
$this->targetFragment = $targetFragment;
|
|
|
|
}
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
public function getHTMLFragments($gridField) {
|
2012-12-17 00:46:51 +01:00
|
|
|
$singleton = singleton($gridField->getModelClass());
|
2013-05-20 12:18:07 +02:00
|
|
|
|
|
|
|
if(!$singleton->canCreate()) {
|
|
|
|
return array();
|
|
|
|
}
|
2012-12-17 00:46:51 +01:00
|
|
|
|
2012-05-28 01:55:43 +02:00
|
|
|
if(!$this->buttonName) {
|
|
|
|
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
|
2012-12-17 00:46:51 +01:00
|
|
|
$objectName = $singleton->i18n_singular_name();
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName));
|
2012-05-28 01:55:43 +02:00
|
|
|
}
|
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
$data = new ArrayData(array(
|
|
|
|
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
|
2012-05-28 01:55:43 +02:00
|
|
|
'ButtonName' => $this->buttonName,
|
2012-03-09 04:50:43 +01:00
|
|
|
));
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
return array(
|
|
|
|
$this->targetFragment => $data->renderWith('GridFieldAddNewbutton'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|