|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\ExcelImportExport; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
|
6
|
|
|
use SilverStripe\View\SSViewer; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
8
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction; |
|
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldImportButton; |
|
10
|
|
|
use SilverStripe\Model\ArrayData; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Adds an "Import" action |
|
14
|
|
|
*/ |
|
15
|
|
|
class ExcelGridFieldImportButton extends GridFieldImportButton |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* We need to override the getHTMLFragments to allow changing the title |
|
19
|
|
|
* |
|
20
|
|
|
* @param GridField $gridField |
|
21
|
|
|
* @return array<int|string,mixed> |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getHTMLFragments($gridField) |
|
24
|
|
|
{ |
|
25
|
|
|
$modalID = $gridField->ID() . '_ImportModal'; |
|
26
|
|
|
|
|
27
|
|
|
// Check for form message prior to rendering form (which clears session messages) |
|
28
|
|
|
/** @var Form|null $form */ |
|
29
|
|
|
$form = $this->getImportForm(); |
|
30
|
|
|
$hasMessage = $form && $form->getMessage(); |
|
31
|
|
|
|
|
32
|
|
|
// Render modal |
|
33
|
|
|
$template = SSViewer::get_templates_by_class(GridFieldImportButton::class, '_Modal'); |
|
34
|
|
|
$viewer = new ArrayData([ |
|
35
|
|
|
'ImportModalTitle' => $this->getModalTitle(), |
|
36
|
|
|
'ImportModalID' => $modalID, |
|
37
|
|
|
'ImportIframe' => $this->getImportIframe(), |
|
38
|
|
|
'ImportForm' => $this->getImportForm(), |
|
39
|
|
|
]); |
|
40
|
|
|
$modal = $viewer->renderWith($template)->forTemplate(); |
|
41
|
|
|
|
|
42
|
|
|
// Build action button |
|
43
|
|
|
$button = new GridField_FormAction( |
|
44
|
|
|
$gridField, |
|
45
|
|
|
'import', |
|
46
|
|
|
_t('ExcelImportExport.XLSIMPORT', 'Import'), |
|
47
|
|
|
'import', |
|
48
|
|
|
[] |
|
49
|
|
|
); |
|
50
|
|
|
$button |
|
51
|
|
|
->addExtraClass('btn btn-secondary font-icon-upload btn--icon-large action_import') |
|
52
|
|
|
->setForm($gridField->getForm()) |
|
53
|
|
|
->setAttribute('data-toggle', 'modal') |
|
54
|
|
|
->setAttribute('aria-controls', $modalID) |
|
55
|
|
|
->setAttribute('data-target', "#{$modalID}") |
|
56
|
|
|
->setAttribute('data-modal', $modal); |
|
57
|
|
|
|
|
58
|
|
|
// If form has a message, trigger it to automatically open |
|
59
|
|
|
if ($hasMessage) { |
|
60
|
|
|
$button->setAttribute('data-state', 'open'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
|
|
$this->targetFragment => $button->Field() |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|