1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Tabulator; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\ORM\RelationList; |
7
|
|
|
use SilverStripe\View\ArrayData; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This component provides a button for opening the add new form |
11
|
|
|
*/ |
12
|
|
|
class TabulatorAddNewButton extends AbstractTabulatorTool |
13
|
|
|
{ |
14
|
|
|
public function forTemplate() |
15
|
|
|
{ |
16
|
|
|
$grid = $this->tabulatorGrid; |
17
|
|
|
$singleton = singleton($grid->getModelClass()); |
18
|
|
|
$context = []; |
19
|
|
|
if ($grid->getList() instanceof RelationList) { |
20
|
|
|
$record = $grid->getForm()->getRecord(); |
21
|
|
|
if ($record && $record instanceof DataObject) { |
|
|
|
|
22
|
|
|
$context['Parent'] = $record; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (!$singleton->canCreate(null, $context)) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!$this->buttonName) { |
31
|
|
|
// provide a default button name, can be changed by calling {@link setButtonName()} on this component |
32
|
|
|
$objectName = $singleton->i18n_singular_name(); |
33
|
|
|
$this->buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.Add', 'Add {name}', ['name' => $objectName]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$data = new ArrayData([ |
37
|
|
|
'NewLink' => $grid->getCreateLink(), |
38
|
|
|
'ButtonName' => $this->buttonName, |
39
|
|
|
'ButtonClasses' => 'btn-primary font-icon-plus-circled new new-link', |
40
|
|
|
'Icon' => $this->isAdmini() ? 'add' : '', |
41
|
|
|
]); |
42
|
|
|
return $this->renderWith($this->getViewerTemplates(), $data); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|