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