1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\ExcelImportExport\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Core\Extension; |
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
8
|
|
|
use LeKoala\ExcelImportExport\ExcelBulkLoader; |
9
|
|
|
use LeKoala\ExcelImportExport\ExcelImportExport; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldImportButton; |
12
|
|
|
use LeKoala\ExcelImportExport\ExcelGridFieldExportButton; |
13
|
|
|
use LeKoala\ExcelImportExport\ExcelGridFieldImportButton; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Extends {@link ModelAdmin}. to bind new forms and features |
17
|
|
|
* |
18
|
|
|
* @author Koala |
19
|
|
|
*/ |
20
|
|
|
class ModelAdminExcelExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
private static $allowed_actions = array( |
|
|
|
|
23
|
|
|
'downloadsample' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
public function onAfterInit() |
27
|
|
|
{ |
28
|
|
|
$this->updateModelImporters(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Replace default model import with a predefined value |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected function updateModelImporters() |
37
|
|
|
{ |
38
|
|
|
/* @var $owner ModelAdmin */ |
39
|
|
|
$owner = $this->owner; |
40
|
|
|
$config = $this->owner->config(); |
41
|
|
|
|
42
|
|
|
// Overwrite model imports |
43
|
|
|
$importerClasses = $config->get('model_importers'); |
44
|
|
|
|
45
|
|
|
if (is_null($importerClasses)) { |
46
|
|
|
$models = $owner->getManagedModels(); |
47
|
|
|
foreach (array_keys($models) as $modelName) { |
48
|
|
|
$importerClasses[$modelName] = ExcelBulkLoader::class; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$config->set('model_importers', $importerClasses); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function downloadsample() |
56
|
|
|
{ |
57
|
|
|
ExcelImportExport::sampleFileForClass($this->owner->modelClass); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function updateGridFieldConfig( GridFieldConfig $config ) |
61
|
|
|
{ |
62
|
|
|
/* @var $owner ModelAdmin */ |
63
|
|
|
$owner = $this->owner; |
64
|
|
|
$class = $owner->modelClass; |
65
|
|
|
// $sanitisedClass = str_replace('\\', '-', $class); |
66
|
|
|
$classConfig = $owner->config(); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
// Handle export buttons |
70
|
|
|
if ($classConfig->export_csv) { |
71
|
|
|
$GridFieldExportButton = $this->getGridFieldExportButton($config); |
72
|
|
|
$GridFieldExportButton->setExportColumns(ExcelImportExport::exportFieldsForClass($class)); |
73
|
|
|
} else { |
74
|
|
|
$config->removeComponentsByType(GridFieldExportButton::class); |
75
|
|
|
} |
76
|
|
|
if ($classConfig->export_excel) { |
77
|
|
|
$ExcelGridFieldExportButton = new ExcelGridFieldExportButton('buttons-before-left'); |
78
|
|
|
$config->addComponent($ExcelGridFieldExportButton); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Rename import button |
82
|
|
|
$config->removeComponentsByType(GridFieldImportButton::class); |
83
|
|
|
if ($this->owner->showImportForm) { |
84
|
|
|
$ExcelGridFieldImportButton = new ExcelGridFieldImportButton('buttons-before-left'); |
85
|
|
|
$ExcelGridFieldImportButton->setImportForm($this->owner->ImportForm()); |
86
|
|
|
$ExcelGridFieldImportButton->setModalTitle(_t('ExcelImportExport.IMPORTFROMFILE', 'Import from a file')); |
87
|
|
|
$config->addComponent($ExcelGridFieldImportButton); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param GridFieldConfig $config |
93
|
|
|
* @return GridFieldExportButton |
94
|
|
|
*/ |
95
|
|
|
protected function getGridFieldExportButton($config) |
96
|
|
|
{ |
97
|
|
|
return $config->getComponentByType(GridFieldExportButton::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function updateImportForm(Form $form) |
101
|
|
|
{ |
102
|
|
|
/* @var $owner ModelAdmin */ |
103
|
|
|
$owner = $this->owner; |
104
|
|
|
$class = $owner->modelClass; |
105
|
|
|
$modelSNG = singleton($class); |
106
|
|
|
$modelName = $modelSNG->i18n_singular_name(); |
107
|
|
|
|
108
|
|
|
$fields = $form->Fields(); |
109
|
|
|
|
110
|
|
|
$downloadSampleLink = $owner->Link(str_replace('\\', '-', $class) . '/downloadsample'); |
111
|
|
|
$downloadSample = '<a href="' . $downloadSampleLink . '" class="no-ajax" target="_blank">' . _t( |
112
|
|
|
'ExcelImportExport.DownloadSample', |
113
|
|
|
'Download sample file' |
114
|
|
|
) . '</a>'; |
115
|
|
|
|
116
|
|
|
$file = $fields->dataFieldByName('_CsvFile'); |
117
|
|
|
if ($file) { |
118
|
|
|
$csvDescription = ExcelImportExport::getValidExtensionsText(); |
119
|
|
|
$csvDescription .= '. ' . $downloadSample; |
120
|
|
|
$file->setDescription($csvDescription); |
121
|
|
|
$file->getValidator()->setAllowedExtensions(ExcelImportExport::getValidExtensions()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// We moved the specs into a nice to use download sample button |
125
|
|
|
$fields->removeByName("SpecFor{$modelName}"); |
126
|
|
|
|
127
|
|
|
// If you cannot delete, you cannot empty |
128
|
|
|
if (!$modelSNG->canDelete()) { |
129
|
|
|
$fields->removeByName('EmptyBeforeImport'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$actions = $form->Actions(); |
133
|
|
|
|
134
|
|
|
// Update import button |
135
|
|
|
$import = $actions->dataFieldByName('action_import'); |
136
|
|
|
if ($import) { |
137
|
|
|
$import->setTitle(_t( |
138
|
|
|
'ExcelImportExport.ImportExcel', |
139
|
|
|
"Import from Excel" |
140
|
|
|
)); |
141
|
|
|
$import->removeExtraClass('btn-outline-secondary'); |
142
|
|
|
$import->addExtraClass('btn-primary'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|