2012-03-09 04:50:43 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
2017-05-08 23:15:09 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\RelationList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
|
2012-03-09 04:50:43 +01:00
|
|
|
/**
|
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
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class GridFieldAddNewButton implements GridField_HTMLProvider
|
|
|
|
{
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected $targetFragment;
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected $buttonName;
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function setButtonName($name)
|
|
|
|
{
|
|
|
|
$this->buttonName = $name;
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function __construct($targetFragment = 'before')
|
|
|
|
{
|
|
|
|
$this->targetFragment = $targetFragment;
|
|
|
|
}
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
{
|
|
|
|
$singleton = singleton($gridField->getModelClass());
|
2017-05-08 23:15:09 +02:00
|
|
|
$context = [];
|
|
|
|
if ($gridField->getList() instanceof RelationList) {
|
|
|
|
$record = $gridField->getForm()->getRecord();
|
|
|
|
if ($record && $record instanceof DataObject) {
|
|
|
|
$context['Parent'] = $record;
|
|
|
|
}
|
|
|
|
}
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2017-05-08 23:15:09 +02:00
|
|
|
if (!$singleton->canCreate(null, $context)) {
|
|
|
|
return [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2012-12-17 00:46:51 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$this->buttonName) {
|
|
|
|
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
|
|
|
|
$objectName = $singleton->i18n_singular_name();
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.Add', 'Add {name}', ['name' => $objectName]);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2012-05-28 01:55:43 +02:00
|
|
|
|
2017-05-08 23:15:09 +02:00
|
|
|
$data = new ArrayData([
|
2016-11-29 00:31:16 +01:00
|
|
|
'NewLink' => Controller::join_links($gridField->Link('item'), 'new'),
|
|
|
|
'ButtonName' => $this->buttonName,
|
2017-05-08 23:15:09 +02:00
|
|
|
]);
|
2012-03-09 04:50:43 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$templates = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
2017-05-08 23:15:09 +02:00
|
|
|
return [
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->targetFragment => $data->renderWith($templates),
|
2017-05-08 23:15:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|